Canvas: reindent Kiwi code, and provide operator<<(ostream&) for several objects
This commit is contained in:
parent
ba3515e619
commit
1baa8d68c7
|
@ -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"
|
||||||
|
@ -19,101 +20,129 @@ namespace kiwi
|
||||||
|
|
||||||
enum RelationalOperator
|
enum RelationalOperator
|
||||||
{
|
{
|
||||||
OP_LE,
|
OP_LE,
|
||||||
OP_GE,
|
OP_GE,
|
||||||
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,
|
||||||
RelationalOperator op,
|
RelationalOperator op,
|
||||||
double strength = strength::required) : m_data(new ConstraintData(expr, op, strength)) {}
|
double strength = strength::required) : m_data(new ConstraintData(expr, op, strength)) {}
|
||||||
|
|
||||||
Constraint(const Constraint &other, double strength) : m_data(new ConstraintData(other, strength)) {}
|
Constraint(const Constraint &other, double strength) : m_data(new ConstraintData(other, strength)) {}
|
||||||
|
|
||||||
~Constraint() {}
|
~Constraint() {}
|
||||||
|
|
||||||
const Expression &expression() const
|
const Expression &expression() const
|
||||||
{
|
{
|
||||||
return m_data->m_expression;
|
return m_data->m_expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
RelationalOperator op() const
|
RelationalOperator op() const
|
||||||
{
|
{
|
||||||
return m_data->m_op;
|
return m_data->m_op;
|
||||||
}
|
}
|
||||||
|
|
||||||
double strength() const
|
double strength() const
|
||||||
{
|
{
|
||||||
return m_data->m_strength;
|
return m_data->m_strength;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!() const
|
bool operator!() const
|
||||||
{
|
{
|
||||||
return !m_data;
|
return !m_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
bool involves (Variable const & v) const {
|
||||||
static Expression reduce(const Expression &expr)
|
if (expression().involves (v)) {
|
||||||
{
|
return true;
|
||||||
std::map<Variable, double> vars;
|
}
|
||||||
typedef std::vector<Term>::const_iterator iter_t;
|
return false;
|
||||||
iter_t end = expr.terms().end();
|
}
|
||||||
for (iter_t it = expr.terms().begin(); it != end; ++it)
|
|
||||||
vars[it->variable()] += it->coefficient();
|
|
||||||
std::vector<Term> terms(vars.begin(), vars.end());
|
|
||||||
return Expression(terms, expr.constant());
|
|
||||||
}
|
|
||||||
|
|
||||||
class ConstraintData : public SharedData
|
private:
|
||||||
{
|
static Expression reduce(const Expression &expr)
|
||||||
|
{
|
||||||
|
std::map<Variable, double> vars;
|
||||||
|
typedef std::vector<Term>::const_iterator iter_t;
|
||||||
|
iter_t end = expr.terms().end();
|
||||||
|
for (iter_t it = expr.terms().begin(); it != end; ++it)
|
||||||
|
vars[it->variable()] += it->coefficient();
|
||||||
|
std::vector<Term> terms(vars.begin(), vars.end());
|
||||||
|
return Expression(terms, expr.constant());
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
class ConstraintData : public SharedData
|
||||||
ConstraintData(const Expression &expr,
|
{
|
||||||
RelationalOperator op,
|
|
||||||
double strength) : SharedData(),
|
|
||||||
m_expression(reduce(expr)),
|
|
||||||
m_strength(strength::clip(strength)),
|
|
||||||
m_op(op) {}
|
|
||||||
|
|
||||||
ConstraintData(const Constraint &other, double strength) : SharedData(),
|
public:
|
||||||
m_expression(other.expression()),
|
ConstraintData(const Expression &expr,
|
||||||
m_strength(strength::clip(strength)),
|
RelationalOperator op,
|
||||||
m_op(other.op()) {}
|
double strength) : SharedData(),
|
||||||
|
m_expression(reduce(expr)),
|
||||||
|
m_strength(strength::clip(strength)),
|
||||||
|
m_op(op) {}
|
||||||
|
|
||||||
~ConstraintData() {}
|
ConstraintData(const Constraint &other, double strength) : SharedData(),
|
||||||
|
m_expression(other.expression()),
|
||||||
|
m_strength(strength::clip(strength)),
|
||||||
|
m_op(other.op()) {}
|
||||||
|
|
||||||
Expression m_expression;
|
~ConstraintData() {}
|
||||||
double m_strength;
|
|
||||||
RelationalOperator m_op;
|
|
||||||
|
|
||||||
private:
|
Expression m_expression;
|
||||||
ConstraintData(const ConstraintData &other);
|
double m_strength;
|
||||||
|
RelationalOperator m_op;
|
||||||
|
|
||||||
ConstraintData &operator=(const ConstraintData &other);
|
private:
|
||||||
};
|
ConstraintData(const ConstraintData &other);
|
||||||
|
|
||||||
SharedDataPtr<ConstraintData> m_data;
|
ConstraintData &operator=(const ConstraintData &other);
|
||||||
|
};
|
||||||
|
|
||||||
friend bool operator<(const Constraint &lhs, const Constraint &rhs)
|
SharedDataPtr<ConstraintData> m_data;
|
||||||
{
|
|
||||||
return lhs.m_data < rhs.m_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
friend bool operator==(const Constraint &lhs, const Constraint &rhs)
|
friend bool operator<(const Constraint &lhs, const Constraint &rhs)
|
||||||
{
|
{
|
||||||
return lhs.m_data == rhs.m_data;
|
return lhs.m_data < rhs.m_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend bool operator!=(const Constraint &lhs, const Constraint &rhs)
|
friend bool operator==(const Constraint &lhs, const Constraint &rhs)
|
||||||
{
|
{
|
||||||
return lhs.m_data != rhs.m_data;
|
return lhs.m_data == rhs.m_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
friend bool operator!=(const Constraint &lhs, const Constraint &rhs)
|
||||||
|
{
|
||||||
|
return lhs.m_data != rhs.m_data;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static std::ostream& operator<< (std::ostream& o, kiwi::Constraint const & c)
|
||||||
|
{
|
||||||
|
return o << c.expression() << " OP " << c.op();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace kiwi
|
} // namespace kiwi
|
||||||
|
|
|
@ -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,38 +16,57 @@ 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) {}
|
||||||
|
|
||||||
Expression(const std::vector<Term> &terms, double constant = 0.0) : m_terms(terms), m_constant(constant) {}
|
Expression(const std::vector<Term> &terms, double constant = 0.0) : m_terms(terms), m_constant(constant) {}
|
||||||
|
|
||||||
~Expression() {}
|
~Expression() {}
|
||||||
|
|
||||||
const std::vector<Term> &terms() const
|
const std::vector<Term> &terms() const
|
||||||
{
|
{
|
||||||
return m_terms;
|
return m_terms;
|
||||||
}
|
}
|
||||||
|
|
||||||
double constant() const
|
double constant() const
|
||||||
{
|
{
|
||||||
return m_constant;
|
return m_constant;
|
||||||
}
|
}
|
||||||
|
|
||||||
double value() const
|
double value() const
|
||||||
{
|
{
|
||||||
typedef std::vector<Term>::const_iterator iter_t;
|
typedef std::vector<Term>::const_iterator iter_t;
|
||||||
double result = m_constant;
|
double result = m_constant;
|
||||||
iter_t end = m_terms.end();
|
iter_t end = m_terms.end();
|
||||||
for (iter_t it = m_terms.begin(); it != end; ++it)
|
for (iter_t it = m_terms.begin(); it != end; ++it)
|
||||||
result += it->value();
|
result += it->value();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
bool involves (Variable const & v) const {
|
||||||
std::vector<Term> m_terms;
|
for (std::vector<Term>::const_iterator it = m_terms.begin(); it != m_terms.end(); ++it) {
|
||||||
double m_constant;
|
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
|
} // namespace kiwi
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user