File Coverage

/usr/local/lib/perl5/site_perl/5.26.1/x86_64-linux/auto/share/dist/Alien-Kiwisolver/include/kiwi/constraint.h
Criterion Covered Total %
statement 30 32 93.7
branch 7 12 58.3
condition n/a
subroutine n/a
pod n/a
total 37 44 84.0


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 <map>
10             #include <vector>
11             #include "expression.h"
12             #include "shareddata.h"
13             #include "strength.h"
14             #include "term.h"
15             #include "variable.h"
16              
17             namespace kiwi
18             {
19              
20             enum RelationalOperator
21             {
22             OP_LE,
23             OP_GE,
24             OP_EQ
25             };
26              
27 288064           class Constraint
28             {
29              
30             public:
31 2876           Constraint() = default;
32              
33 3220           Constraint(const Expression &expr,
34             RelationalOperator op,
35 3220 50         double strength = strength::required) : m_data(new ConstraintData(expr, op, strength)) {}
36              
37 1100 50         Constraint(const Constraint &other, double strength) : m_data(new ConstraintData(other, strength)) {}
38              
39 15740           Constraint(const Constraint &) = default;
40              
41 23606           Constraint(Constraint &&) noexcept = default;
42              
43 43660           ~Constraint() = default;
44              
45 4316           const Expression &expression() const
46             {
47 4316           return m_data->m_expression;
48             }
49              
50 5075           RelationalOperator op() const
51             {
52 5075           return m_data->m_op;
53             }
54              
55 4656           double strength() const
56             {
57 4656           return m_data->m_strength;
58             }
59              
60 0           bool operator!() const
61             {
62 0           return !m_data;
63             }
64              
65             Constraint& operator=(const Constraint &) = default;
66              
67             Constraint& operator=(Constraint &&) noexcept = default;
68              
69             private:
70 3220           friend int* get_refcount( kiwi::Constraint* obj ); static Expression reduce(const Expression &expr)
71             {
72 6440           std::map<Variable, double> vars;
73 9352 100         for (const auto & term : expr.terms())
74 6132 50         vars[term.variable()] += term.coefficient();
75              
76 6440 50         std::vector<Term> terms(vars.begin(), vars.end());
77 6440           return Expression(std::move(terms), expr.constant());
78             }
79              
80             class ConstraintData : public SharedData
81             {
82              
83             public:
84 3220           ConstraintData(const Expression &expr,
85             RelationalOperator op,
86             double strength) : SharedData(),
87             m_expression(reduce(expr)),
88 3220           m_strength(strength::clip(strength)),
89 3220           m_op(op) {}
90              
91 1100           ConstraintData(const Constraint &other, double strength) : SharedData(),
92 1100           m_expression(other.expression()),
93 1100           m_strength(strength::clip(strength)),
94 3300 50         m_op(other.op()) {}
95              
96 1438           ~ConstraintData() = default;
97              
98             Expression m_expression;
99             double m_strength;
100             RelationalOperator m_op;
101              
102             private:
103             ConstraintData(const ConstraintData &other);
104              
105             ConstraintData &operator=(const ConstraintData &other);
106             };
107              
108             SharedDataPtr<ConstraintData> m_data;
109              
110 60033           friend bool operator<(const Constraint &lhs, const Constraint &rhs)
111             {
112 60033           return lhs.m_data < rhs.m_data;
113             }
114              
115             friend bool operator==(const Constraint &lhs, const Constraint &rhs)
116             {
117             return lhs.m_data == rhs.m_data;
118             }
119              
120             friend bool operator!=(const Constraint &lhs, const Constraint &rhs)
121             {
122             return lhs.m_data != rhs.m_data;
123             }
124             };
125              
126             } // namespace kiwi