carl  24.04
Computer ARithmetic Library
BVVariable.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include "../sort/SortManager.h"
7 
8 namespace carl {
9 
10  /**
11  * Represent a BitVector-Variable
12  */
13  class BVVariable {
14  private:
17  std::size_t mWidth = 0;
18 
19  public:
20 
21  BVVariable() = default;
22 
23  BVVariable(Variable _variable, const Sort& _sort):
24  mVar(_variable), mSort(_sort)
25  {
26  assert(SortManager::getInstance().getType(_sort) == VariableType::VT_BITVECTOR);
27 
28  const std::vector<std::size_t>* indices = SortManager::getInstance().getIndices(_sort);
29  assert(indices != nullptr && indices->size() == 1 && indices->front() > 0);
30 
31  mWidth = indices->front();
32  }
33 
34  Variable variable() const {
35  return mVar;
36  }
37  explicit operator Variable() const {
38  return mVar;
39  }
40 
41  /**
42  * @return The sort (domain) of this uninterpreted variable.
43  */
44  const Sort& sort() const {
45  return mSort;
46  }
47 
48  std::size_t width() const {
49  return mWidth;
50  }
51 
52  /**
53  * @return The string representation of this bit vector variable.
54  */
55  std::string toString(bool _friendlyNames) const {
56  return VariablePool::getInstance().get_name(mVar, _friendlyNames);
57  }
58 
59  /**
60  * Print the given bit vector variable on the given output stream.
61  * @param os The output stream to print on.
62  * @param v The bit vector variable to print.
63  * @return The output stream after printing the given bit vector variable on it.
64  */
65  friend std::ostream& operator<<(std::ostream& os, const BVVariable& v) {
66  return os << v.variable();
67  }
68  };
69 
70  inline bool operator==(const BVVariable& lhs, const BVVariable& rhs) {
71  return lhs.variable() == rhs.variable();
72  }
73  inline bool operator==(const BVVariable& lhs, const Variable& rhs) {
74  return lhs.variable() == rhs;
75  }
76  inline bool operator==(const Variable& lhs, const BVVariable& rhs) {
77  return lhs == rhs.variable();
78  }
79  inline bool operator<(const BVVariable& lhs, const BVVariable& rhs) {
80  return lhs.variable() < rhs.variable();
81  }
82  inline bool operator<(const BVVariable& lhs, const Variable& rhs) {
83  return lhs.variable() < rhs;
84  }
85  inline bool operator<(const Variable& lhs, const BVVariable& rhs) {
86  return lhs < rhs.variable();
87  }
88 } // end namespace carl
89 
90 namespace std {
91  /**
92  * Implement std::hash for bitvector variables.
93  */
94  template<>
95  struct hash<carl::BVVariable> {
96  /**
97  * @param v The bitvector variable to get the hash for.
98  * @return The hash of the given bitvector variable.
99  */
100  std::size_t operator()(const carl::BVVariable& v) const {
101  return std::hash<carl::Variable>()(v.variable());
102  }
103  };
104 }
carl is the main namespace for the library.
bool operator<(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
static const Variable NO_VARIABLE
Instance of an invalid variable.
Definition: Variable.h:203
Specialization of std::hash for Variable.
Definition: Variable.h:224
std::string get_name(Variable v, bool variableName=true) const
Get a human-readable name for the given variable.
static SortManager & getInstance()
Returns the single instance of this class by reference.
Definition: Singleton.h:45
Represent a BitVector-Variable.
Definition: BVVariable.h:13
std::size_t mWidth
Definition: BVVariable.h:17
BVVariable()=default
const Sort & sort() const
Definition: BVVariable.h:44
std::string toString(bool _friendlyNames) const
Definition: BVVariable.h:55
BVVariable(Variable _variable, const Sort &_sort)
Definition: BVVariable.h:23
Variable mVar
Definition: BVVariable.h:15
std::size_t width() const
Definition: BVVariable.h:48
Variable variable() const
Definition: BVVariable.h:34
friend std::ostream & operator<<(std::ostream &os, const BVVariable &v)
Print the given bit vector variable on the given output stream.
Definition: BVVariable.h:65
std::size_t operator()(const carl::BVVariable &v) const
Definition: BVVariable.h:100
Implements a sort (for defining types of variables and functions).
Definition: Sort.h:20
const std::vector< std::size_t > * getIndices(const Sort &sort) const
Definition: SortManager.h:205