carl  24.04
Computer ARithmetic Library
BVConstraint.h
Go to the documentation of this file.
1 /**
2  * @file BVConstraint.h
3  * @author Andreas Krueger <andreas.krueger@rwth-aachen.de>
4  */
5 
6 #pragma once
7 
8 #include "BVCompareRelation.h"
9 #include "BVTerm.h"
10 
11 namespace carl {
12 
13 // Forward declaration
14 class BVConstraintPool;
15 
16 class BVConstraint {
17  friend class BVConstraintPool;
18 
19 private:
20  /// The relation for comparing left- and right-hand side.
22  /// The left-hand side of the (in)equality.
24  /// The right-hand size of the (in)equality.
26  /// The hash value.
27  std::size_t mHash = 0;
28  /// The unique id.
29  std::size_t mId = 0;
30 
31  explicit BVConstraint(bool _consistent = true)
32  : mRelation(_consistent ? BVCompareRelation::EQ : BVCompareRelation::NEQ) {}
33 
34  /**
35  * Constructs the constraint: _lhs _relation _rhs
36  * @param _lhs The bit-vector term to be used as left-hand side of the constraint.
37  * @param _rhs The bit-vector term to be used as right-hand side of the constraint.
38  * @param _relation The relation symbol to be used for the constraint.
39  */
40  BVConstraint(const BVCompareRelation& _relation, const BVTerm& _lhs, const BVTerm& _rhs)
41  : mRelation(_relation), mLhs(_lhs), mRhs(_rhs),
43  assert(mLhs.width() == mRhs.width());
44  }
45 
46 public:
47  static BVConstraint create(bool _consistent = true);
48 
49  static BVConstraint create(const BVCompareRelation& _relation,
50  const BVTerm& _lhs, const BVTerm& _rhs);
51 
52  /**
53  * @return The bit-vector term being the left-hand side of this constraint.
54  */
55  const BVTerm& lhs() const {
56  return mLhs;
57  }
58 
59  /**
60  * @return The bit-vector term being the right-hand side of this constraint.
61  */
62  const BVTerm& rhs() const {
63  return mRhs;
64  }
65 
66  /**
67  * @return The relation symbol of this constraint.
68  */
70  return mRelation;
71  }
72 
73  /**
74  * @return The unique id of this constraint.
75  */
76  std::size_t id() const {
77  return mId;
78  }
79 
80  /**
81  * @return A hash value for this constraint.
82  */
83  std::size_t hash() const {
84  return mHash;
85  }
86 
87  /**
88  * @return An approximation of the complexity of this bit vector constraint.
89  */
90  std::size_t complexity() const {
91  return 1 + mLhs.complexity() + mRhs.complexity();
92  }
93 
94  void gatherBVVariables(std::set<BVVariable>& vars) const {
95  mLhs.gatherBVVariables(vars);
96  mRhs.gatherBVVariables(vars);
97  }
98 
99  void gatherVariables(carlVariables& vars) const {
100  std::set<BVVariable> bvvars;
101  gatherBVVariables(bvvars);
102  for (const auto& bvv : bvvars) {
103  vars.add(bvv.variable());
104  }
105  }
106 
107  bool is_constant() const {
108  return mLhs.isInvalid() && mRhs.isInvalid();
109  }
110 
111  bool isAlwaysConsistent() const {
113  }
114 
115  bool isAlwaysInconsistent() const {
117  }
118 };
119 
120 bool operator==(const BVConstraint& lhs, const BVConstraint& rhs);
121 bool operator<(const BVConstraint& lhs, const BVConstraint& rhs);
122 
123 std::ostream& operator<<(std::ostream& os, const BVConstraint& c);
124 } // namespace carl
125 
126 namespace std {
127 /**
128  * Implements std::hash for bit-vector constraints.
129  */
130 template<>
131 struct hash<carl::BVConstraint> {
132  /**
133  * @param _constraint The bit-vector constraint to get the hash for.
134  * @return The hash of the given constraint.
135  */
136  std::size_t operator()(const carl::BVConstraint& c) const {
137  return c.hash();
138  }
139 };
140 } // namespace std
carl is the main namespace for the library.
bool operator<(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
std::size_t hash_all(Args &&... args)
Hashes an arbitrary number of values.
Definition: hash.h:71
void add(Variable v)
Definition: Variables.h:141
void gatherVariables(carlVariables &vars) const
Definition: BVConstraint.h:99
const BVTerm & rhs() const
Definition: BVConstraint.h:62
bool isAlwaysInconsistent() const
Definition: BVConstraint.h:115
bool isAlwaysConsistent() const
Definition: BVConstraint.h:111
BVTerm mLhs
The left-hand side of the (in)equality.
Definition: BVConstraint.h:23
const BVTerm & lhs() const
Definition: BVConstraint.h:55
std::size_t mId
The unique id.
Definition: BVConstraint.h:29
std::size_t id() const
Definition: BVConstraint.h:76
bool is_constant() const
Definition: BVConstraint.h:107
BVConstraint(const BVCompareRelation &_relation, const BVTerm &_lhs, const BVTerm &_rhs)
Constructs the constraint: _lhs _relation _rhs.
Definition: BVConstraint.h:40
BVConstraint(bool _consistent=true)
Definition: BVConstraint.h:31
std::size_t complexity() const
Definition: BVConstraint.h:90
BVCompareRelation relation() const
Definition: BVConstraint.h:69
std::size_t hash() const
Definition: BVConstraint.h:83
BVTerm mRhs
The right-hand size of the (in)equality.
Definition: BVConstraint.h:25
std::size_t mHash
The hash value.
Definition: BVConstraint.h:27
static BVConstraint create(bool _consistent=true)
void gatherBVVariables(std::set< BVVariable > &vars) const
Definition: BVConstraint.h:94
BVCompareRelation mRelation
The relation for comparing left- and right-hand side.
Definition: BVConstraint.h:21
std::size_t operator()(const carl::BVConstraint &c) const
Definition: BVConstraint.h:136
std::size_t width() const
Definition: BVTerm.cpp:43
size_t complexity() const
Definition: BVTerm.cpp:95
void gatherBVVariables(std::set< BVVariable > &vars) const
Definition: BVTerm.cpp:51
bool isInvalid() const
Definition: BVTerm.cpp:55