carl  24.04
Computer ARithmetic Library
BVTerm.cpp
Go to the documentation of this file.
1 /**
2  * @file BVTerm.cpp
3  * @author Andreas Krueger <andreas.krueger@rwth-aachen.de>
4  */
5 
6 #include "BVTerm.h"
7 #include "BVTermContent.h"
8 #include "BVTermPool.h"
9 
11 
12 #include <utility>
13 
14 namespace carl {
16  : mpContent(BVTermPool::getInstance().create()) {
17 }
18 
20  : mpContent(BVTermPool::getInstance().create(_type, std::move(_value))) {
21 }
22 
23 BVTerm::BVTerm(BVTermType _type, const BVVariable& _variable)
24  : mpContent(BVTermPool::getInstance().create(_type, _variable)) {
25 }
26 
27 BVTerm::BVTerm(BVTermType _type, const BVTerm& _operand, std::size_t _index)
28  : mpContent(BVTermPool::getInstance().create(_type, _operand, _index)) {
29 }
30 
31 BVTerm::BVTerm(BVTermType _type, const BVTerm& _first, const BVTerm& _second)
32  : mpContent(BVTermPool::getInstance().create(_type, _first, _second)) {
33 }
34 
35 BVTerm::BVTerm(BVTermType _type, const BVTerm& _operand, std::size_t _first, std::size_t _last)
36  : mpContent(BVTermPool::getInstance().create(_type, _operand, _first, _last)) {
37 }
38 
39 std::size_t BVTerm::hash() const {
40  return mpContent->hash();
41 }
42 
43 std::size_t BVTerm::width() const {
44  return mpContent->width();
45 }
46 
48  return mpContent->type();
49 }
50 
51 void BVTerm::gatherBVVariables(std::set<BVVariable>& vars) const {
53 }
54 
55 bool BVTerm::isInvalid() const {
56  return mpContent->isInvalid();
57 }
58 
59 const BVTerm& BVTerm::operand() const {
61  return mpContent->as<BVExtractContent>().mOperand;
62  } else {
63  return mpContent->as<BVUnaryContent>().mOperand;
64  }
65 }
66 
67 std::size_t BVTerm::index() const {
68  return mpContent->as<BVUnaryContent>().mIndex;
69 }
70 
71 const BVTerm& BVTerm::first() const {
72  return mpContent->as<BVBinaryContent>().mFirst;
73 }
74 
75 const BVTerm& BVTerm::second() const {
76  return mpContent->as<BVBinaryContent>().mSecond;
77 }
78 
79 std::size_t BVTerm::highest() const {
80  return mpContent->as<BVExtractContent>().mHighest;
81 }
82 
83 std::size_t BVTerm::lowest() const {
84  return mpContent->as<BVExtractContent>().mLowest;
85 }
86 
87 const BVVariable& BVTerm::variable() const {
88  return mpContent->as<BVVariable>();
89 }
90 
91 const BVValue& BVTerm::value() const {
92  return mpContent->as<BVValue>();
93 }
94 
95 std::size_t BVTerm::complexity() const {
96  return mpContent->complexity();
97 }
98 
99 BVTerm BVTerm::substitute(const std::map<BVVariable, BVTerm>& _substitutions) const {
100  BVTermType type = this->type();
101 
102  if (type == BVTermType::CONSTANT) {
103  return *this;
104  }
105  if (type == BVTermType::VARIABLE) {
106  auto iter = _substitutions.find(variable());
107  if (iter != _substitutions.end()) {
108  return iter->second;
109  }
110  return *this;
111  }
112  if (typeIsUnary(type)) {
113  BVTerm operandSubstituted = operand().substitute(_substitutions);
114  return BVTerm(type, operandSubstituted, index());
115  }
116  if (typeIsBinary(type)) {
117  BVTerm firstSubstituted = first().substitute(_substitutions);
118  BVTerm secondSubstituted = second().substitute(_substitutions);
119  return BVTerm(type, firstSubstituted, secondSubstituted);
120  }
121  if (type == BVTermType::EXTRACT) {
122  BVTerm operandSubstituted = operand().substitute(_substitutions);
123  return BVTerm(type, operandSubstituted, highest(), lowest());
124  }
125 
126  assert(false);
127  return BVTerm();
128 }
129 
130 bool operator==(const BVTerm& lhs, const BVTerm& rhs) {
131  return lhs.mpContent == rhs.mpContent;;
132 }
133 bool operator<(const BVTerm& lhs, const BVTerm& rhs) {
134  return *lhs.mpContent < *rhs.mpContent;;
135 }
136 
137 std::ostream& operator<<(std::ostream& os, const BVTerm& term) {
138  return os << *term.mpContent;
139 }
140 
141 
142 
143 } // namespace carl
carl is the main namespace for the library.
bool typeIsUnary(BVTermType type)
Definition: BVTermType.h:66
bool operator<(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
BVTermType
Definition: BVTermType.h:10
bool typeIsBinary(BVTermType type)
Definition: BVTermType.h:74
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)
const BVTerm & operand() const
Definition: BVTerm.cpp:59
std::size_t index() const
Definition: BVTerm.cpp:67
std::size_t width() const
Definition: BVTerm.cpp:43
size_t complexity() const
Definition: BVTerm.cpp:95
std::size_t highest() const
Definition: BVTerm.cpp:79
void gatherBVVariables(std::set< BVVariable > &vars) const
Definition: BVTerm.cpp:51
bool isInvalid() const
Definition: BVTerm.cpp:55
const BVVariable & variable() const
Definition: BVTerm.cpp:87
const BVTermContent * mpContent
Definition: BVTerm.h:30
const BVTerm & second() const
Definition: BVTerm.cpp:75
const BVValue & value() const
Definition: BVTerm.cpp:91
std::size_t hash() const
Definition: BVTerm.cpp:39
BVTerm substitute(const std::map< BVVariable, BVTerm > &) const
Definition: BVTerm.cpp:99
BVTermType type() const
Definition: BVTerm.cpp:47
const BVTerm & first() const
Definition: BVTerm.cpp:71
std::size_t lowest() const
Definition: BVTerm.cpp:83
std::size_t width() const
BVTermType type() const
const T & as() const
Definition: BVTermContent.h:95
bool isInvalid() const
void gatherBVVariables(std::set< BVVariable > &vars) const
std::size_t hash() const
std::size_t complexity() const
Represent a BitVector-Variable.
Definition: BVVariable.h:13