SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Origin.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <carl-common/util/streamingOperators.h>
4 #include <carl-common/datastructures/Bitset.h>
5 
6 #include <algorithm>
7 #include <iostream>
8 #include <vector>
9 
10 namespace smtrat {
11 namespace cad {
12 using carl::operator<<;
13 
14 /**
15  * This class represents one or more origins of some object.
16  * An origin is a single id or a pair of ids (where a single id is represented as a pair of the same id).
17  *
18  * As an object can have multiple origins, this class stores a list of such pairs.
19  * This class makes sure that the pairs are unique.
20  */
21 class Origin {
22 public:
23  struct BaseType {
24  std::size_t level;
25  std::size_t first;
26  std::size_t second;
27  bool first_active = true;
28  bool second_active = true;
29  bool ec_active = true;
30  explicit BaseType(std::size_t level, std::size_t id): BaseType(level,id,id) {}
31  BaseType(std::size_t lvl, std::size_t id1, std::size_t id2): level(lvl),first(id1),second(id2) {
32  if (first > second) std::swap(first, second);
33  }
34  bool active() const {
36  }
37  void activate(const carl::Bitset& ids) {
38  if (!first_active && ids.test(first)) first_active = true;
39  if (!second_active && ids.test(second)) second_active = true;
40  }
41  void deactivate(const carl::Bitset& ids) {
42  if (first_active && ids.test(first)) first_active = false;
43  if (second_active && ids.test(second)) second_active = false;
44  }
45  bool operator==(const BaseType& bt) const {
46  return (level == bt.level) && (first == bt.first) && (second == bt.second);
47  }
48  bool operator<(const BaseType& bt) const {
49  if (level != bt.level) return level < bt.level;
50  if (first != bt.first) return first < bt.first;
51  return second < bt.second;
52  }
53  friend std::ostream& operator<<(std::ostream& os, const BaseType& bt) {
54  if (!bt.active()) os << "![" << bt.first_active << bt.second_active << bt.ec_active << "]";
55  return os << "(" << bt.first << "," << bt.second << ")@" << bt.level;
56  }
57  };
58 private:
59  std::vector<BaseType> mData;
60 
61  void makeUnique() {
62  std::sort(mData.begin(), mData.end());
63  mData.erase(std::unique(mData.begin(), mData.end()), mData.end());
64  }
65  template<typename F>
66  void removeFiltered(F&& f) {
67  auto it = std::remove_if(mData.begin(), mData.end(), f);
68  mData.erase(it, mData.end());
69  }
70 
71 public:
72  Origin() {}
73  Origin(const Origin& po): mData(po.mData) {}
74  Origin(Origin&& po): mData(std::move(po.mData)) {}
75 
76  explicit Origin(std::size_t level, std::size_t id): mData(1, BaseType(level, id)) {}
77  explicit Origin(const BaseType& bt): mData(1, bt) {}
78 
79  auto begin() const {
80  return mData.begin();
81  }
82  auto end() const {
83  return mData.end();
84  }
85 
86  // returns true, if Origin contains at least one active BaseType
87  bool isActive() const {
88  return std::any_of(mData.begin(), mData.end(),
89  [](const auto& o){ return o.active(); }
90  );
91  }
92 
93  // deactivates BaseTypes due to inactive polynomials
94  void deactivate(std::size_t level, const carl::Bitset& rhs) {
95  std::for_each(mData.begin(), mData.end(),
96  [level, &rhs](auto& o){
97  if (o.level == level) o.deactivate(rhs);
98  });
99  }
100 
101  // deactivates BaseTypes due to equational constraint
102  void deactivateEC(std::size_t level, const carl::Bitset& rhs) {
103  for(auto& it : mData) {
104  if((it.level == level) && !rhs.test(it.first) && !rhs.test(it.second)) {
105  it.ec_active = false;
106  }
107  }
108  }
109 
110  // activates BaseTypes due to activated polynomials
111  void activate(std::size_t level, const carl::Bitset& rhs) {
112  std::for_each(mData.begin(), mData.end(),
113  [level, &rhs](auto& o){
114  if (o.level == level) o.activate(rhs);
115  });
116  }
117 
118  // activates BaseTypes due to equational constraint that is not used for restricted projection anymore
119  void activateEC(std::size_t level, const carl::Bitset& rhs) {
120  for(auto& it : mData) {
121  if((it.level == level) && !rhs.test(it.first) && !rhs.test(it.second)) {
122  it.ec_active = true;
123  }
124  }
125  }
126 
127  Origin& operator=(const Origin& rhs) {
128  mData = rhs.mData;
129  return *this;
130  }
132  mData = std::move(rhs.mData);
133  return *this;
134  }
135 
136  bool empty() const {
137  return mData.empty();
138  }
139 
140  bool operator==(const Origin& rhs) const {
141  return mData == rhs.mData;
142  }
143  /// Adds the pair to the origins.
144  Origin& operator+=(const BaseType& rhs) {
145  mData.emplace_back(rhs);
146  makeUnique();
147  return *this;
148  }
149  /// Removes the pair from the origins.
150  Origin& operator-=(const BaseType& rhs) {
151  removeFiltered([&](const BaseType& bt){ return bt == rhs; });
152  return *this;
153  }
154  Origin& erase(std::size_t level, const carl::Bitset& rhs) {
155  removeFiltered([&](const BaseType& bt){ return (bt.level == level) && (rhs.test(bt.first) || rhs.test(bt.second)); });
156  return *this;
157  }
158 
159  Origin operator|(const Origin& rhs) const {
160  Origin res(rhs);
161  res.mData.insert(res.mData.end(), mData.begin(), mData.end());
162  res.makeUnique();
163  return res;
164  }
165 
166  friend std::ostream& operator<<(std::ostream& os, const Origin& po) {
167  return os << po.mData;
168  }
169 };
170 
171 }
172 }
This class represents one or more origins of some object.
Definition: Origin.h:21
Origin(const Origin &po)
Definition: Origin.h:73
Origin & erase(std::size_t level, const carl::Bitset &rhs)
Definition: Origin.h:154
auto begin() const
Definition: Origin.h:79
Origin & operator+=(const BaseType &rhs)
Adds the pair to the origins.
Definition: Origin.h:144
void activate(std::size_t level, const carl::Bitset &rhs)
Definition: Origin.h:111
void deactivateEC(std::size_t level, const carl::Bitset &rhs)
Definition: Origin.h:102
friend std::ostream & operator<<(std::ostream &os, const Origin &po)
Definition: Origin.h:166
Origin(std::size_t level, std::size_t id)
Definition: Origin.h:76
Origin & operator=(Origin &&rhs)
Definition: Origin.h:131
void makeUnique()
Definition: Origin.h:61
Origin operator|(const Origin &rhs) const
Definition: Origin.h:159
Origin(Origin &&po)
Definition: Origin.h:74
Origin & operator-=(const BaseType &rhs)
Removes the pair from the origins.
Definition: Origin.h:150
bool operator==(const Origin &rhs) const
Definition: Origin.h:140
bool isActive() const
Definition: Origin.h:87
auto end() const
Definition: Origin.h:82
void activateEC(std::size_t level, const carl::Bitset &rhs)
Definition: Origin.h:119
Origin & operator=(const Origin &rhs)
Definition: Origin.h:127
void deactivate(std::size_t level, const carl::Bitset &rhs)
Definition: Origin.h:94
std::vector< BaseType > mData
Definition: Origin.h:59
Origin(const BaseType &bt)
Definition: Origin.h:77
bool empty() const
Definition: Origin.h:136
void removeFiltered(F &&f)
Definition: Origin.h:66
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:67
Class to create the formulas for axioms.
friend std::ostream & operator<<(std::ostream &os, const BaseType &bt)
Definition: Origin.h:53
bool operator==(const BaseType &bt) const
Definition: Origin.h:45
void deactivate(const carl::Bitset &ids)
Definition: Origin.h:41
void activate(const carl::Bitset &ids)
Definition: Origin.h:37
bool operator<(const BaseType &bt) const
Definition: Origin.h:48
BaseType(std::size_t lvl, std::size_t id1, std::size_t id2)
Definition: Origin.h:31
BaseType(std::size_t level, std::size_t id)
Definition: Origin.h:30