33 #ifndef DAL_BIT_VECTOR_H__
34 #define DAL_BIT_VECTOR_H__
59 typedef unsigned int bit_support;
60 static const bit_support WD_BIT = bit_support(CHAR_BIT*
sizeof(bit_support));
61 static const bit_support WD_MASK = WD_BIT - 1;
62 typedef dynamic_array<bit_support, 4> bit_container;
66 struct APIDECL bit_reference {
74 bit_reference(bit_support* x, bit_support m,
size_type y, bit_vector* z)
75 { p = x; ind = y; bv = z; mask = m; }
76 bit_reference(
void) {}
77 operator bool(
void)
const {
return (*p & mask) != 0; }
78 bit_reference& operator = (
bool x);
79 bit_reference& operator=(
const bit_reference& x)
80 {
return *
this = bool(x); }
82 {
return bool(*
this) == bool(x); }
83 bool operator<(
const bit_reference& x)
const
84 {
return bool(*
this) < bool(x); }
85 bool operator>(
const bit_reference& x)
const
86 {
return bool(*
this) > bool(x); }
87 bool operator>=(
const bit_reference& x)
const
88 {
return bool(*
this) >= bool(x); }
89 void flip(
void) {
if (
bool(*
this)) *
this =
false;
else *
this =
true; }
92 struct APIDECL bit_iterator {
93 typedef bool value_type;
94 typedef bit_reference reference;
95 typedef bit_reference* pointer;
97 typedef ptrdiff_t difference_type;
98 typedef std::random_access_iterator_tag iterator_category;
102 bit_container::iterator p;
105 inline void bump_up()
106 { ind++;
if (!(mask <<= 1)) { ++p; mask = 1;} }
107 inline void bump_down()
108 { ind--;
if (!(mask >>= 1)) { --p; mask = 1; mask <<= WD_MASK; } }
110 bit_iterator(
void) {}
111 bit_iterator(bit_vector &b,
size_type i);
112 reference operator*()
const
113 {
return reference(&(*p), mask, ind, bv); }
114 bit_iterator& operator++() { bump_up();
return *
this; }
115 bit_iterator operator++(
int)
116 { bit_iterator tmp=*
this; bump_up();
return tmp; }
117 bit_iterator& operator--() { bump_down();
return *
this; }
118 bit_iterator operator--(
int)
119 { bit_iterator tmp = *
this; bump_down();
return tmp; }
120 bit_iterator& operator+=(difference_type i);
121 bit_iterator& operator-=(difference_type i)
122 { *
this += -i;
return *
this; }
123 bit_iterator
operator+(difference_type i)
const
124 { bit_iterator tmp = *
this;
return tmp += i; }
125 bit_iterator
operator-(difference_type i)
const
126 { bit_iterator tmp = *
this;
return tmp -= i; }
127 difference_type
operator-(bit_iterator x)
const {
return ind - x.ind; }
128 reference operator[](difference_type i) {
return *(*
this + i); }
129 size_type index(
void)
const {
return ind; }
130 bool operator==(
const bit_iterator& x)
const {
return ind == x.ind; }
131 bool operator!=(
const bit_iterator& x)
const {
return ind != x.ind; }
132 bool operator<(bit_iterator x)
const {
return ind < x.ind; }
133 bool operator>(bit_iterator x)
const {
return ind > x.ind; }
134 bool operator>=(bit_iterator x)
const {
return ind >= x.ind; }
137 struct APIDECL bit_const_iterator {
138 typedef bool value_type;
139 typedef bool reference;
140 typedef const bool* pointer;
142 typedef ptrdiff_t difference_type;
143 typedef std::random_access_iterator_tag iterator_category;
147 bit_container::const_iterator p;
148 const bit_vector* bv;
150 inline void bump_up()
151 { ind++;
if (!(mask <<= 1)) { ++p; mask = 1;} }
152 inline void bump_down()
153 { ind--;
if (!(mask >>= 1)) { --p; mask = 1; mask <<= WD_MASK; } }
155 bit_const_iterator() {}
156 bit_const_iterator(
const bit_vector &b,
size_type i);
157 bit_const_iterator(
const bit_iterator& x)
158 : ind(x.ind), mask(x.mask), p(x.p), bv(x.bv) {}
159 reference operator*()
const {
return (*p & mask) != 0; }
160 bit_const_iterator& operator++() { bump_up();
return *
this; }
161 bit_const_iterator operator++(
int)
162 { bit_const_iterator tmp = *
this; bump_up();
return tmp; }
163 bit_const_iterator& operator--() { bump_down();
return *
this; }
164 bit_const_iterator operator--(
int)
165 { bit_const_iterator tmp = *
this; bump_down();
return tmp; }
166 bit_const_iterator& operator+=(difference_type i);
167 bit_const_iterator& operator-=(difference_type i)
168 { *
this += -i;
return *
this; }
169 bit_const_iterator
operator+(difference_type i)
const
170 { bit_const_iterator tmp = *
this;
return tmp += i; }
171 bit_const_iterator
operator-(difference_type i)
const
172 { bit_const_iterator tmp = *
this;
return tmp -= i; }
173 difference_type
operator-(bit_const_iterator x)
const {
return ind-x.ind; }
174 reference operator[](difference_type i) {
return *(*
this + i); }
175 size_type index(
void)
const {
return ind; }
176 bool operator==(
const bit_const_iterator& x)
const {
return ind == x.ind; }
177 bool operator!=(
const bit_const_iterator& x)
const {
return ind != x.ind; }
178 bool operator<(bit_const_iterator x)
const {
return ind < x.ind; }
179 bool operator>(bit_const_iterator x)
const {
return ind > x.ind; }
180 bool operator>=(bit_const_iterator x)
const {
return ind >= x.ind; }
184 class APIDECL bit_vector :
public bit_container {
187 typedef bool value_type;
189 typedef ptrdiff_t difference_type;
190 typedef bool const_reference;
191 typedef const bool* const_pointer;
192 typedef bit_reference reference;
193 typedef bit_reference* pointer;
194 typedef bit_iterator iterator;
195 typedef bit_const_iterator const_iterator;
199 mutable size_type ifirst_true, ilast_true;
200 mutable size_type ifirst_false, ilast_false;
202 mutable bool icard_valid;
209 ifirst_true = std::min(ifirst_true, i);
210 ilast_true = std::max(ilast_true, i);
214 ifirst_false = std::min(ifirst_false, i);
215 ilast_false = std::max(ilast_false, i);
219 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
220 typedef std::reverse_iterator<iterator> reverse_iterator;
221 size_type size(
void)
const {
return std::max(ilast_true, ilast_false)+1;}
223 iterator begin(
void) {
return iterator(*
this, 0); }
224 const_iterator begin(
void)
const {
return const_iterator(*
this, 0); }
225 iterator end(
void) {
return iterator(*
this, size()); }
226 const_iterator end(
void)
const {
return const_iterator(*
this, size()); }
228 reverse_iterator rbegin(
void) {
return reverse_iterator(end()); }
229 const_reverse_iterator rbegin(
void)
const
230 {
return const_reverse_iterator(end()); }
231 reverse_iterator rend(
void) {
return reverse_iterator(begin()); }
232 const_reverse_iterator rend(
void)
const
233 {
return const_reverse_iterator(begin()); }
236 {
return bit_container::capacity() * WD_BIT; }
239 reference front(
void) {
return *begin(); }
240 const_reference front(
void)
const {
return *begin(); }
241 reference back(
void) {
return *(end() - 1); }
242 const_reference back(
void)
const {
return *(end() - 1); }
245 const_reference operator [](
size_type ii)
const
246 {
return (ii >= size()) ? false : *const_iterator(*
this, ii); }
248 {
if (ii >= size()) fill_false(size(),ii);
return *iterator(*
this, ii);}
250 void swap(bit_vector &da);
253 icard = 0; icard_valid =
true;
254 ifirst_false = ilast_false = ifirst_true = ilast_true = 0;
259 reference r1 = (*this)[i1], r2 = (*this)[i2];
260 bool tmp = r1; r1 = r2; r2 = tmp;
264 return bit_container::memsize() +
sizeof(bit_vector)
265 -
sizeof(bit_container);
277 bit_vector &setminus(
const bit_vector &bv);
278 bit_vector &operator |=(
const bit_vector &bv);
279 bit_vector &operator &=(
const bit_vector &bv);
281 bit_vector operator |(
const bit_vector &bv)
const
282 { bit_vector r(*
this); r |= bv;
return r; }
283 bit_vector operator &(
const bit_vector &bv)
const
284 { bit_vector r(*
this); r &= bv;
return r; }
285 bool operator ==(
const bit_vector &bv)
const;
286 bool operator !=(
const bit_vector &bv)
const
287 {
return !((*this) == bv); }
289 bit_vector(
void) {
clear(); }
290 template <
size_t N> bit_vector(
const std::bitset<N> &bs) {
292 for (
size_type i=0; i < bs.size(); ++i) {
if (bs[i])
add(i); }
296 template <
typename ICONT> dal::bit_vector& merge_from(
const ICONT& c) {
297 for (
typename ICONT::const_iterator it = c.begin(); it != c.end(); ++it)
303 template <
typename IT> dal::bit_vector& merge_from(IT b, IT e) {
304 while (b != e) {
add(*b++); }
309 bool contains(
const dal::bit_vector &other)
const;
314 if (i < ifirst_true || i > ilast_true)
return false;
315 else return (((*(
const bit_container*)(
this))[i / WD_BIT]) &
316 (bit_support(1) << (i & WD_MASK))) ? true :
false; }
320 void sup(
size_type i) { (*this)[i] =
false; }
321 void del(
size_type i) { (*this)[i] =
false; }
325 int first(
void)
const {
return (card() == 0) ? -1 : int(first_true()); }
326 int last(
void)
const {
return (card() == 0) ? -1 : int(last_true()); }
327 inline int take_first(
void)
328 {
int res = first();
if (res >= 0) sup(res);
return res; }
329 inline int take_last(
void)
330 {
int res = last();
if (res >= 0) sup(res);
return res; }
348 class APIDECL bv_visitor {
350 bit_container::const_iterator it;
354 bv_visitor(
const dal::bit_vector& b) :
355 it(((const bit_container&)b).begin()+b.first()/WD_BIT),
356 ilast(b.last()+1), ind(b.first()), v(0) {
357 if (ind < ilast) { v = *it; v >>= (ind&WD_MASK); }
359 bool finished()
const {
return ind >= ilast; }
361 operator size_type()
const {
return ind; }
367 class APIDECL bv_visitor_c {
371 bv_visitor_c(
const dal::bit_vector& b) : bv(b), v(bv) {}
372 bool finished()
const {
return v.finished(); }
373 bool operator++() {
return ++v; }
379 inline int APIDECL &operator << (
int &i, bit_vector &s)
380 { i = s.take_first();
return i; }
381 inline const int APIDECL &operator >> (
const int &i, bit_vector &s)
382 { s.add(i);
return i; }
384 inline size_t APIDECL &operator << (
size_t &i, bit_vector &s)
385 { i = s.take_first();
return i; }
386 inline const size_t &operator >> (
const size_t &i, bit_vector &s)
387 { s.add(i);
return i; }
389 std::ostream APIDECL &operator <<(std::ostream &o,
const bit_vector &s);
392 template<
typename ITERABLE_BV>
393 class const_bv_iterator
399 typedef std::forward_iterator_tag iterator_category;
405 const_bv_iterator(
const ITERABLE_BV* p_iterable,
size_type pos)
406 : p_iterable_(const_cast<ITERABLE_BV*>(p_iterable)), pos_(pos)
409 bool operator!= (
const const_bv_iterator &other)
const{
410 return pos_ < other.pos_;
419 if (pos_ == other.pos_)
return 0;
421 auto &vector_this = p_iterable_->v_;
422 auto &vector_other = other.p_iterable_->v_;
423 bv_visitor v_this(vector_this), v_other(vector_other);
424 while (v_this < pos_) ++v_this;
425 while (v_other < other.pos_) ++v_other;
426 auto &v = (pos_ < other.pos_) ? v_this : v_other;
427 auto &v_end = (pos_ >= other.pos_) ? v_this : v_other;
430 while(v < v_end) { ++v; ++count;}
434 const const_bv_iterator &operator++(){
441 ITERABLE_BV *p_iterable_;
458 bv_iterable(
const bit_vector &v) : v_(v), visitor_(v){}
460 const_bv_iterator<bv_iterable> begin()
const;
461 const_bv_iterator<bv_iterable> end()
const;
462 inline bool operator++(){
return ++visitor_;};
466 const bit_vector& v_;
468 friend class const_bv_iterator<bv_iterable>;
475 bv_iterable_c(
const bit_vector &v) : v_(v), visitor_(v_){}
476 const_bv_iterator<bv_iterable_c> begin()
const;
477 const_bv_iterator<bv_iterable_c> end()
const;
478 inline bool operator++(){
return ++visitor_;};
484 friend class const_bv_iterator<bv_iterable_c>;
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void add(const L1 &l1, L2 &l2)
*/
rational_fraction< T > operator-(const polynomial< T > &P, const rational_fraction< T > &Q)
Subtract Q from P.
rational_fraction< T > operator+(const polynomial< T > &P, const rational_fraction< T > &Q)
Add Q to P.
bool operator==(const pconvex_structure &p1, const pconvex_structure &p2)
Stored objects must be compared by keys, because there is a possibility that they are duplicated in s...
size_t size_type
used as the common size type in the library