dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
BitIncrementIterator.cpp
1
6#include "BitIncrementIterator.h"
7
8#include <iostream>
9#include <utility>
10
11namespace dlinear {
12
13std::vector<bool> &operator++(std::vector<bool> &vector) {
14 if (vector.empty()) return vector;
15 bool carry = vector[vector.size() - 1];
16 vector[vector.size() - 1] = !vector[vector.size() - 1];
17 for (int i = static_cast<int>(vector.size()) - 2; carry && i >= 0; i--) {
18 carry = vector[i];
19 vector[i] = !vector[i];
20 }
21 return vector;
22}
23
24std::vector<bool> &operator--(std::vector<bool> &vector) {
25 if (vector.empty()) return vector;
26 bool carry = !vector[vector.size() - 1];
27 vector[vector.size() - 1] = !vector[vector.size() - 1];
28 for (int i = static_cast<int>(vector.size()) - 2; carry && i >= 0; i--) {
29 carry = !vector[i];
30 vector[i] = !vector[i];
31 }
32 return vector;
33}
34
35BitIncrementIterator::BitIncrementIterator(std::vector<bool> starting_value)
36 : vector_{starting_value},
37 fixed_(starting_value.size(), false),
38 starting_vector_{starting_value},
39 ending_vector_{std::move(starting_value)} {
41}
42
43BitIncrementIterator &BitIncrementIterator::operator++() {
44 if (IsDone()) {
45 vector_.clear();
46 return *this;
47 }
48
49 bool carry = vector_[vector_.size() - 1] || fixed_[vector_.size() - 1];
50 if (!fixed_[vector_.size() - 1]) vector_[vector_.size() - 1] = !vector_[vector_.size() - 1];
51 for (int i = static_cast<int>(vector_.size()) - 2; carry && i >= 0; i--) {
52 carry = fixed_[i] ? carry : vector_[i];
53 if (!fixed_[i]) vector_[i] = !vector_[i];
54 }
55
56 return *this;
57}
58
59const BitIncrementIterator BitIncrementIterator::operator++(int) {
60 BitIncrementIterator tmp(*this);
61 operator++();
62 return tmp;
63}
64
65BitIncrementIterator &BitIncrementIterator::operator--() {
66 if (IsDone()) {
67 vector_.clear();
68 return *this;
69 }
70
71 bool carry = !vector_[vector_.size() - 1] || fixed_[vector_.size() - 1];
72 if (!fixed_[vector_.size() - 1]) vector_[vector_.size() - 1] = !vector_[vector_.size() - 1];
73 for (int i = static_cast<int>(vector_.size()) - 2; carry && i >= 0; i--) {
74 carry = fixed_[i] ? carry : !vector_[i];
75 if (!fixed_[i]) vector_[i] = !vector_[i];
76 }
77
78 return *this;
79}
80const BitIncrementIterator BitIncrementIterator::operator--(int) {
81 BitIncrementIterator tmp{*this};
82 operator--();
83 return tmp;
84}
85
86bool BitIncrementIterator::operator[](size_t i) const { return vector_[i]; }
87
88bool BitIncrementIterator::Learn(size_t i) {
89 if (fixed_[i]) return false;
90 UpdateVector(i, !vector_[i]);
91 fixed_[i] = true;
92 return true;
93}
94
95bool BitIncrementIterator::Learn(size_t i, bool value) {
96 if (fixed_[i] && vector_[i] != value) return false;
97 if (vector_[i] != value) UpdateVector(i, value);
98 fixed_[i] = true;
99 return true;
100}
101
103 // The vector is NOT done if at least one non-fixed bit is different from the corresponding ending vector value
104 for (size_t i = 0; i < vector_.size(); i++) {
105 if (!fixed_[i] && vector_[i] != ending_vector_[i]) return false;
106 }
107 return true;
108}
109
111 for (size_t i = start_pos; i < vector_.size(); i++) {
112 if (!fixed_[i]) vector_[i] = starting_vector_[i];
113 }
114}
115
117 for (int i = static_cast<int>(start_pos); i >= 0; i--) {
118 if (!fixed_[i]) vector_[i] = starting_vector_[i];
119 }
120}
121
123 for (size_t i = 0; i < vector_.size(); i++) {
124 if (!fixed_[i]) vector_[i] = starting_vector_[i];
125 }
126}
127
128void BitIncrementIterator::UpdateVector(size_t i, bool value) {
129 if (value && ending_vector_[i]) { // The bit True has been learned and the ending vector bit is True
130 while (!vector_[i]) ++(*this);
131 } else if (value && !ending_vector_[i]) {
132 ResetNonFixedRight(i + 1);
133 } else if (ending_vector_[i]) { // The bit False has been learned and the ending vector bit is True
135 } else { // The bit False has been learned and the ending vector bit is False
136 while (vector_[i]) ++(*this);
137 }
138 vector_[i] = value;
139}
140
141std::ostream &operator<<(std::ostream &os, const BitIncrementIterator &it) {
142 std::copy(it->begin(), std::prev(it->end()), std::ostream_iterator<bool>(os, ", "));
143 return os << *(it->rbegin());
144}
145
146} // namespace dlinear
BitIncrementIterator class.
std::vector< bool > starting_vector_
Vector to store the starting value of the iterator.
void ResetNonFixedLeft(std::size_t start_pos)
Reset all the non-fixed bits to the left of start_pos to their starting value.
bool IsDone() const
Check if the iterator is done.
bool Learn(std::size_t i)
Learn the value of the bit at position i by inverting the bit.
void ResetNonFixedRight(std::size_t start_pos=0)
Reset all the non-fixed bits to the right of start_pos to their starting value.
void ResetNonFixed()
Reset all the non-fixed bits in the vector to their starting value.
std::vector< bool > ending_vector_
Vector to store the ending value of the iterator.
std::vector< bool > vector_
The bit vector that will assume all the possible values.
BitIncrementIterator(std::size_t n)
Construct a new BitIncrementIterator object.
void UpdateVector(std::size_t i, bool value)
After a Learn operation to set the i 'th bit to value, update the vector.
std::vector< bool > fixed_
Vector to indicate the fixed bits.
Global namespace for the dlinear library.
STL namespace.