line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------- |
2
|
|
|
|
|
|
|
| Copyright (c) 2013-2017, 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
|
275128
|
|
|
|
|
|
class Constraint |
30
|
|
|
|
|
|
|
{ |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
public: |
33
|
2876
|
|
|
|
|
|
Constraint() = default; |
34
|
|
|
|
|
|
|
|
35
|
3220
|
|
|
|
|
|
Constraint(const Expression &expr, |
36
|
|
|
|
|
|
|
RelationalOperator op, |
37
|
3220
|
50
|
|
|
|
|
double strength = strength::required) : m_data(new ConstraintData(expr, op, strength)) {} |
38
|
|
|
|
|
|
|
|
39
|
1100
|
50
|
|
|
|
|
Constraint(const Constraint &other, double strength) : m_data(new ConstraintData(other, strength)) {} |
40
|
|
|
|
|
|
|
|
41
|
15740
|
|
|
|
|
|
Constraint(const Constraint &) = default; |
42
|
|
|
|
|
|
|
|
43
|
23742
|
|
|
|
|
|
Constraint(Constraint &&) noexcept = default; |
44
|
|
|
|
|
|
|
|
45
|
43796
|
|
|
|
|
|
~Constraint() = default; |
46
|
|
|
|
|
|
|
|
47
|
4316
|
|
|
|
|
|
const Expression &expression() const |
48
|
|
|
|
|
|
|
{ |
49
|
4316
|
|
|
|
|
|
return m_data->m_expression; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
5075
|
|
|
|
|
|
RelationalOperator op() const |
53
|
|
|
|
|
|
|
{ |
54
|
5075
|
|
|
|
|
|
return m_data->m_op; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
4656
|
|
|
|
|
|
double strength() const |
58
|
|
|
|
|
|
|
{ |
59
|
4656
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
bool operator!() const |
75
|
|
|
|
|
|
|
{ |
76
|
0
|
|
|
|
|
|
return !m_data; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Constraint& operator=(const Constraint &) = default; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Constraint& operator=(Constraint &&) noexcept = default; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
private: |
84
|
3220
|
|
|
|
|
|
friend int* get_refcount( kiwi::Constraint* obj ); static Expression reduce(const Expression &expr) |
85
|
|
|
|
|
|
|
{ |
86
|
6440
|
|
|
|
|
|
std::map<Variable, double> vars; |
87
|
9352
|
100
|
|
|
|
|
for (const auto & term : expr.terms()) |
88
|
6132
|
50
|
|
|
|
|
vars[term.variable()] += term.coefficient(); |
89
|
|
|
|
|
|
|
|
90
|
6440
|
50
|
|
|
|
|
std::vector<Term> terms(vars.begin(), vars.end()); |
91
|
6440
|
|
|
|
|
|
return Expression(std::move(terms), expr.constant()); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
class ConstraintData : public SharedData |
95
|
|
|
|
|
|
|
{ |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
public: |
98
|
3220
|
|
|
|
|
|
ConstraintData(const Expression &expr, |
99
|
|
|
|
|
|
|
RelationalOperator op, |
100
|
|
|
|
|
|
|
double strength) : SharedData(), |
101
|
|
|
|
|
|
|
m_expression(reduce(expr)), |
102
|
3220
|
|
|
|
|
|
m_strength(strength::clip(strength)), |
103
|
3220
|
|
|
|
|
|
m_op(op) {} |
104
|
|
|
|
|
|
|
|
105
|
1100
|
|
|
|
|
|
ConstraintData(const Constraint &other, double strength) : SharedData(), |
106
|
1100
|
|
|
|
|
|
m_expression(other.expression()), |
107
|
1100
|
|
|
|
|
|
m_strength(strength::clip(strength)), |
108
|
3300
|
50
|
|
|
|
|
m_op(other.op()) {} |
109
|
|
|
|
|
|
|
|
110
|
1438
|
|
|
|
|
|
~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
|
60115
|
|
|
|
|
|
friend bool operator<(const Constraint &lhs, const Constraint &rhs) |
125
|
|
|
|
|
|
|
{ |
126
|
60115
|
|
|
|
|
|
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 |