13
0

Canvas: reindent Kiwi code, and provide operator<<(ostream&) for several objects

This commit is contained in:
Paul Davis 2020-06-10 14:39:52 -06:00
parent ba3515e619
commit 1baa8d68c7
4 changed files with 165 additions and 109 deletions

View File

@ -6,6 +6,7 @@
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
#pragma once
#include <ostream>
#include <map>
#include <vector>
#include "expression.h"
@ -24,6 +25,22 @@ enum RelationalOperator
OP_EQ
};
static std::ostream& operator<< (std::ostream& o, RelationalOperator op)
{
switch (op) {
case OP_LE:
o << "<=";
break;
case OP_GE:
o << ">=";
break;
case OP_EQ:
o << "==";
break;
}
return o;
}
class Constraint
{
@ -58,6 +75,13 @@ public:
return !m_data;
}
bool involves (Variable const & v) const {
if (expression().involves (v)) {
return true;
}
return false;
}
private:
static Expression reduce(const Expression &expr)
{
@ -116,4 +140,9 @@ private:
}
};
static std::ostream& operator<< (std::ostream& o, kiwi::Constraint const & c)
{
return o << c.expression() << " OP " << c.op();
}
} // namespace kiwi

View File

@ -6,6 +6,7 @@
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
#pragma once
#include <ostream>
#include <vector>
#include "term.h"
@ -44,9 +45,28 @@ public:
return result;
}
bool involves (Variable const & v) const {
for (std::vector<Term>::const_iterator it = m_terms.begin(); it != m_terms.end(); ++it) {
if (it->variable().equals (v)) {
return true;
}
}
return false;
}
private:
std::vector<Term> m_terms;
double m_constant;
};
static std::ostream& operator<<(std::ostream& o, kiwi::Expression const &e)
{
o << e.constant() << " + ";
for (std::vector<kiwi::Term>::const_iterator it = e.terms().begin(); it != e.terms().end(); ++it) {
o << (*it) << ' ';
}
return o;
}
} // namespace kiwi

View File

@ -6,6 +6,7 @@
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
#pragma once
#include <ostream>
#include <utility>
#include "variable.h"
@ -48,4 +49,10 @@ private:
double m_coefficient;
};
static std::ostream& operator<< (std::ostream& o, kiwi::Term const & t)
{
return o << t.variable().name() << " * " << t.coefficient();
}
} // namespace kiwi

View File

@ -68,7 +68,7 @@ public:
}
// operator== is used for symbolics
bool equals(const Variable &other)
bool equals(const Variable &other) const
{
return m_data == other.m_data;
}