aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/kiwisolver/py3/kiwi/constraint.h
blob: 558dd924aeb0df011b7a0c52efcd07f2ee5d4c2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*-----------------------------------------------------------------------------
| Copyright (c) 2013-2017, Nucleic Development Team.
|
| Distributed under the terms of the Modified BSD License.
|
| The full license is in the file COPYING.txt, distributed with this software.
|----------------------------------------------------------------------------*/
#pragma once
#include <map>
#include <vector>
#include "expression.h"
#include "shareddata.h"
#include "strength.h"
#include "term.h"
#include "variable.h"


namespace kiwi
{

enum RelationalOperator { OP_LE, OP_GE, OP_EQ };


class Constraint
{

public:

	Constraint() : m_data( 0 ) {}

	Constraint( const Expression& expr,
				RelationalOperator op,
				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 Expression& expression() const
	{
		return m_data->m_expression;
	}

	RelationalOperator op() const
	{
		return m_data->m_op;
	}

	double strength() const
	{
		return m_data->m_strength;
	}

	bool operator!() const
	{
		return !m_data;
	}

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() );
	}

	class ConstraintData : public SharedData
	{

	public:

		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(),
			m_expression( other.expression() ),
			m_strength( strength::clip( strength ) ),
			m_op( other.op() ) {}

		~ConstraintData() {}

		Expression m_expression;
		double m_strength;
		RelationalOperator m_op;

	private:

		ConstraintData( const ConstraintData& other );

		ConstraintData& operator=( const ConstraintData& other );
	};

	SharedDataPtr<ConstraintData> m_data;

        friend bool operator<( const Constraint& lhs, const Constraint& rhs )
        {
                return lhs.m_data < rhs.m_data;
        }

        friend bool operator==( const Constraint& lhs, const Constraint& rhs )
        {
                return lhs.m_data == rhs.m_data;
        }

        friend bool operator!=( const Constraint& lhs, const Constraint& rhs )
        {
                return lhs.m_data != rhs.m_data;
        }
};

} // namespace kiwi