| 1 |
/*----------------------------------------------------------------------------- |
| 2 |
| Copyright (c) 2013-2026, Nucleic Development Team. |
| 3 |
| |
| 4 |
| Distributed under the terms of the Modified BSD License. |
| 5 |
| |
| 6 |
| The full license is in the file LICENSE, distributed with this software. |
| 7 |
|----------------------------------------------------------------------------*/ |
| 8 |
#pragma once |
| 9 |
#include <cstdlib> |
| 10 |
#include <map> |
| 11 |
#include <vector> |
| 12 |
#include "expression.h" |
| 13 |
#include "shareddata.h" |
| 14 |
#include "strength.h" |
| 15 |
#include "term.h" |
| 16 |
#include "variable.h" |
| 17 |
#include "util.h" |
| 18 |
|
| 19 |
namespace kiwi |
| 20 |
{ |
| 21 |
|
| 22 |
enum RelationalOperator |
| 23 |
{ |
| 24 |
OP_LE, |
| 25 |
OP_GE, |
| 26 |
OP_EQ |
| 27 |
}; |
| 28 |
|
| 29 |
class Constraint |
| 30 |
{ |
| 31 |
|
| 32 |
public: |
| 33 |
Constraint() = default; |
| 34 |
|
| 35 |
Constraint(const Expression &expr, |
| 36 |
RelationalOperator op, |
| 37 |
double strength = strength::required) : m_data(new ConstraintData(expr, op, strength)) {} |
| 38 |
|
| 39 |
Constraint(const Constraint &other, double strength) : m_data(new ConstraintData(other, strength)) {} |
| 40 |
|
| 41 |
Constraint(const Constraint &) = default; |
| 42 |
|
| 43 |
Constraint(Constraint &&) noexcept = default; |
| 44 |
|
| 45 |
~Constraint() = default; |
| 46 |
|
| 47 |
const Expression &expression() const |
| 48 |
{ |
| 49 |
return m_data->m_expression; |
| 50 |
} |
| 51 |
|
| 52 |
RelationalOperator op() const |
| 53 |
{ |
| 54 |
return m_data->m_op; |
| 55 |
} |
| 56 |
|
| 57 |
double strength() const |
| 58 |
{ |
| 59 |
return m_data->m_strength; |
| 60 |
} |
| 61 |
|
| 62 |
bool violated() const |
| 63 |
{ |
| 64 |
switch (m_data->m_op) |
| 65 |
{ |
| 66 |
case OP_EQ: return !impl::nearZero(m_data->m_expression.value()); |
| 67 |
case OP_GE: return m_data->m_expression.value() < 0.0; |
| 68 |
case OP_LE: return m_data->m_expression.value() > 0.0; |
| 69 |
} |
| 70 |
|
| 71 |
std::abort(); |
| 72 |
} |
| 73 |
|
| 74 |
bool operator!() const |
| 75 |
{ |
| 76 |
return !m_data; |
| 77 |
} |
| 78 |
|
| 79 |
Constraint& operator=(const Constraint &) = default; |
| 80 |
|
| 81 |
Constraint& operator=(Constraint &&) noexcept = default; |
| 82 |
|
| 83 |
private: |
| 84 |
static Expression reduce(const Expression &expr) |
| 85 |
{ |
| 86 |
std::map<Variable, double> vars; |
| 87 |
for (const auto & term : expr.terms()) |
| 88 |
vars[term.variable()] += term.coefficient(); |
| 89 |
|
| 90 |
std::vector<Term> terms(vars.begin(), vars.end()); |
| 91 |
return Expression(std::move(terms), expr.constant()); |
| 92 |
} |
| 93 |
|
| 94 |
class ConstraintData : public SharedData |
| 95 |
{ |
| 96 |
|
| 97 |
public: |
| 98 |
ConstraintData(const Expression &expr, |
| 99 |
RelationalOperator op, |
| 100 |
double strength) : SharedData(), |
| 101 |
m_expression(reduce(expr)), |
| 102 |
m_strength(strength::clip(strength)), |
| 103 |
m_op(op) {} |
| 104 |
|
| 105 |
ConstraintData(const Constraint &other, double strength) : SharedData(), |
| 106 |
m_expression(other.expression()), |
| 107 |
m_strength(strength::clip(strength)), |
| 108 |
m_op(other.op()) {} |
| 109 |
|
| 110 |
~ConstraintData() = default; |
| 111 |
|
| 112 |
Expression m_expression; |
| 113 |
double m_strength; |
| 114 |
RelationalOperator m_op; |
| 115 |
|
| 116 |
private: |
| 117 |
ConstraintData(const ConstraintData &other); |
| 118 |
|
| 119 |
ConstraintData &operator=(const ConstraintData &other); |
| 120 |
}; |
| 121 |
|
| 122 |
SharedDataPtr<ConstraintData> m_data; |
| 123 |
|
| 124 |
friend bool operator<(const Constraint &lhs, const Constraint &rhs) |
| 125 |
{ |
| 126 |
return lhs.m_data < rhs.m_data; |
| 127 |
} |
| 128 |
|
| 129 |
friend bool operator==(const Constraint &lhs, const Constraint &rhs) |
| 130 |
{ |
| 131 |
return lhs.m_data == rhs.m_data; |
| 132 |
} |
| 133 |
|
| 134 |
friend bool operator!=(const Constraint &lhs, const Constraint &rhs) |
| 135 |
{ |
| 136 |
return lhs.m_data != rhs.m_data; |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
} // namespace kiwi |