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

@ -1,11 +1,12 @@
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
| Copyright (c) 2013-2017, Nucleic Development Team. | Copyright (c) 2013-2017, Nucleic Development Team.
| |
| Distributed under the terms of the Modified BSD License. | Distributed under the terms of the Modified BSD License.
| |
| The full license is in the file LICENSE, distributed with this software. | The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/ |----------------------------------------------------------------------------*/
#pragma once #pragma once
#include <ostream>
#include <map> #include <map>
#include <vector> #include <vector>
#include "expression.h" #include "expression.h"
@ -24,10 +25,26 @@ enum RelationalOperator
OP_EQ 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 class Constraint
{ {
public: public:
Constraint() : m_data(0) {} Constraint() : m_data(0) {}
Constraint(const Expression &expr, Constraint(const Expression &expr,
@ -58,7 +75,14 @@ public:
return !m_data; return !m_data;
} }
private: bool involves (Variable const & v) const {
if (expression().involves (v)) {
return true;
}
return false;
}
private:
static Expression reduce(const Expression &expr) static Expression reduce(const Expression &expr)
{ {
std::map<Variable, double> vars; std::map<Variable, double> vars;
@ -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 } // namespace kiwi

View file

@ -1,11 +1,12 @@
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
| Copyright (c) 2013-2017, Nucleic Development Team. | Copyright (c) 2013-2017, Nucleic Development Team.
| |
| Distributed under the terms of the Modified BSD License. | Distributed under the terms of the Modified BSD License.
| |
| The full license is in the file LICENSE, distributed with this software. | The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/ |----------------------------------------------------------------------------*/
#pragma once #pragma once
#include <ostream>
#include <vector> #include <vector>
#include "term.h" #include "term.h"
@ -15,7 +16,7 @@ namespace kiwi
class Expression class Expression
{ {
public: public:
Expression(double constant = 0.0) : m_constant(constant) {} Expression(double constant = 0.0) : m_constant(constant) {}
Expression(const Term &term, double constant = 0.0) : m_terms(1, term), m_constant(constant) {} Expression(const Term &term, double constant = 0.0) : m_terms(1, term), m_constant(constant) {}
@ -44,9 +45,28 @@ public:
return result; return result;
} }
private: 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; std::vector<Term> m_terms;
double m_constant; 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 } // namespace kiwi

View file

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

View file

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