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); }
81 bool operator==(
const bit_reference& x)
const 82 {
return bool(*
this) == bool(x); }
83 bool operator<(
const bit_reference& x)
const 84 {
return bool(*
this) < bool(x); }
85 void flip(
void) {
if (
bool(*
this)) *
this =
false;
else *
this =
true; }
88 struct APIDECL bit_iterator {
89 typedef bool value_type;
90 typedef bit_reference reference;
91 typedef bit_reference* pointer;
93 typedef ptrdiff_t difference_type;
94 typedef std::random_access_iterator_tag iterator_category;
98 bit_container::iterator p;
101 inline void bump_up()
102 { ind++;
if (!(mask <<= 1)) { ++p; mask = 1;} }
103 inline void bump_down()
104 { ind--;
if (!(mask >>= 1)) { --p; mask = 1; mask <<= WD_MASK; } }
106 bit_iterator(
void) {}
107 bit_iterator(bit_vector &b, size_type i);
108 reference operator*()
const 109 {
return reference(&(*p), mask, ind, bv); }
110 bit_iterator& operator++() { bump_up();
return *
this; }
111 bit_iterator operator++(
int)
112 { bit_iterator tmp=*
this; bump_up();
return tmp; }
113 bit_iterator& operator--() { bump_down();
return *
this; }
114 bit_iterator operator--(
int)
115 { bit_iterator tmp = *
this; bump_down();
return tmp; }
116 bit_iterator& operator+=(difference_type i);
117 bit_iterator& operator-=(difference_type i)
118 { *
this += -i;
return *
this; }
119 bit_iterator
operator+(difference_type i)
const 120 { bit_iterator tmp = *
this;
return tmp += i; }
121 bit_iterator
operator-(difference_type i)
const 122 { bit_iterator tmp = *
this;
return tmp -= i; }
123 difference_type
operator-(bit_iterator x)
const {
return ind - x.ind; }
124 reference operator[](difference_type i) {
return *(*
this + i); }
125 size_type index(
void)
const {
return ind; }
126 bool operator==(
const bit_iterator& x)
const {
return ind == x.ind; }
127 bool operator!=(
const bit_iterator& x)
const {
return ind != x.ind; }
128 bool operator<(bit_iterator x)
const {
return ind < x.ind; }
131 struct APIDECL bit_const_iterator {
132 typedef bool value_type;
133 typedef bool reference;
134 typedef const bool* pointer;
136 typedef ptrdiff_t difference_type;
137 typedef std::random_access_iterator_tag iterator_category;
141 bit_container::const_iterator p;
142 const bit_vector* bv;
144 inline void bump_up()
145 { ind++;
if (!(mask <<= 1)) { ++p; mask = 1;} }
146 inline void bump_down()
147 { ind--;
if (!(mask >>= 1)) { --p; mask = 1; mask <<= WD_MASK; } }
149 bit_const_iterator() {}
150 bit_const_iterator(
const bit_vector &b, size_type i);
151 bit_const_iterator(
const bit_iterator& x)
152 : ind(x.ind), mask(x.mask), p(x.p), bv(x.bv) {}
153 reference operator*()
const {
return (*p & mask) != 0; }
154 bit_const_iterator& operator++() { bump_up();
return *
this; }
155 bit_const_iterator operator++(
int)
156 { bit_const_iterator tmp = *
this; bump_up();
return tmp; }
157 bit_const_iterator& operator--() { bump_down();
return *
this; }
158 bit_const_iterator operator--(
int)
159 { bit_const_iterator tmp = *
this; bump_down();
return tmp; }
160 bit_const_iterator& operator+=(difference_type i);
161 bit_const_iterator& operator-=(difference_type i)
162 { *
this += -i;
return *
this; }
163 bit_const_iterator
operator+(difference_type i)
const 164 { bit_const_iterator tmp = *
this;
return tmp += i; }
165 bit_const_iterator
operator-(difference_type i)
const 166 { bit_const_iterator tmp = *
this;
return tmp -= i; }
167 difference_type
operator-(bit_const_iterator x)
const {
return ind-x.ind; }
168 reference operator[](difference_type i) {
return *(*
this + i); }
169 size_type index(
void)
const {
return ind; }
170 bool operator==(
const bit_const_iterator& x)
const {
return ind == x.ind; }
171 bool operator!=(
const bit_const_iterator& x)
const {
return ind != x.ind; }
172 bool operator<(bit_const_iterator x)
const {
return ind < x.ind; }
176 class APIDECL bit_vector :
public bit_container {
179 typedef bool value_type;
181 typedef ptrdiff_t difference_type;
182 typedef bool const_reference;
183 typedef const bool* const_pointer;
184 typedef bit_reference reference;
185 typedef bit_reference* pointer;
186 typedef bit_iterator iterator;
187 typedef bit_const_iterator const_iterator;
191 mutable size_type ifirst_true, ilast_true;
192 mutable size_type ifirst_false, ilast_false;
193 mutable size_type icard;
194 mutable bool icard_valid;
196 void fill_false(size_type i1, size_type i2);
200 void change_for_true(size_type i) {
201 ifirst_true = std::min(ifirst_true, i);
202 ilast_true = std::max(ilast_true, i);
205 void change_for_false(size_type i) {
206 ifirst_false = std::min(ifirst_false, i);
207 ilast_false = std::max(ilast_false, i);
211 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
212 typedef std::reverse_iterator<iterator> reverse_iterator;
213 size_type size(
void)
const {
return std::max(ilast_true, ilast_false)+1;}
215 iterator begin(
void) {
return iterator(*
this, 0); }
216 const_iterator begin(
void)
const {
return const_iterator(*
this, 0); }
217 iterator end(
void) {
return iterator(*
this, size()); }
218 const_iterator end(
void)
const {
return const_iterator(*
this, size()); }
220 reverse_iterator rbegin(
void) {
return reverse_iterator(end()); }
221 const_reverse_iterator rbegin(
void)
const 222 {
return const_reverse_iterator(end()); }
223 reverse_iterator rend(
void) {
return reverse_iterator(begin()); }
224 const_reverse_iterator rend(
void)
const 225 {
return const_reverse_iterator(begin()); }
227 size_type capacity(
void)
const 228 {
return bit_container::capacity() * WD_BIT; }
229 size_type max_size(
void)
const {
return (
size_type(-1)); }
231 reference front(
void) {
return *begin(); }
232 const_reference front(
void)
const {
return *begin(); }
233 reference back(
void) {
return *(end() - 1); }
234 const_reference back(
void)
const {
return *(end() - 1); }
237 const_reference operator [](size_type ii)
const 238 {
return (ii >= size()) ?
false : *const_iterator(*
this, ii); }
239 reference operator [](size_type ii)
240 {
if (ii >= size()) fill_false(size(),ii);
return *iterator(*
this, ii);}
242 void swap(bit_vector &da);
245 icard = 0; icard_valid =
true;
246 ifirst_false = ilast_false = ifirst_true = ilast_true = 0;
249 void swap(size_type i1, size_type i2) {
251 reference r1 = (*this)[i1], r2 = (*this)[i2];
252 bool tmp = r1; r1 = r2; r2 = tmp;
255 size_type memsize(
void)
const {
256 return bit_container::memsize() +
sizeof(bit_vector)
257 -
sizeof(bit_container);
259 size_type card(
void)
const;
261 size_type first_true(
void)
const;
263 size_type first_false(
void)
const;
265 size_type last_true(
void)
const;
267 size_type last_false(
void)
const;
269 bit_vector &setminus(
const bit_vector &bv);
270 bit_vector &operator |=(
const bit_vector &bv);
271 bit_vector &operator &=(
const bit_vector &bv);
273 bit_vector operator |(
const bit_vector &bv)
const 274 { bit_vector r(*
this); r |= bv;
return r; }
275 bit_vector operator &(
const bit_vector &bv)
const 276 { bit_vector r(*
this); r &= bv;
return r; }
277 bool operator ==(
const bit_vector &bv)
const;
278 bool operator !=(
const bit_vector &bv)
const 279 {
return !((*this) == bv); }
281 bit_vector(
void) {
clear(); }
282 template <
size_t N> bit_vector(
const std::bitset<N> &bs) {
284 for (size_type i=0; i < bs.size(); ++i) {
if (bs[i])
add(i); }
288 template <
typename ICONT> dal::bit_vector& merge_from(
const ICONT& c) {
289 for (
typename ICONT::const_iterator it = c.begin(); it != c.end(); ++it)
295 template <
typename IT> dal::bit_vector& merge_from(IT b, IT e) {
296 while (b != e) {
add(*b++); }
301 bool contains(
const dal::bit_vector &other)
const;
305 bool is_in(size_type i)
const {
306 if (i < ifirst_true || i > ilast_true)
return false;
307 else return (((*(
const bit_container*)(
this))[i / WD_BIT]) &
308 (bit_support(1) << (i & WD_MASK))) ?
true :
false; }
309 void add(size_type i) { (*this)[i] =
true; }
311 void add(size_type i, size_type nb);
312 void sup(size_type i) { (*this)[i] =
false; }
313 void del(size_type i) { (*this)[i] =
false; }
315 void sup(size_type i, size_type nb);
316 void del(size_type i, size_type nb);
317 int first(
void)
const {
return (card() == 0) ? -1 : int(first_true()); }
318 int last(
void)
const {
return (card() == 0) ? -1 : int(last_true()); }
319 inline int take_first(
void)
320 {
int res = first();
if (res >= 0) sup(res);
return res; }
321 inline int take_last(
void)
322 {
int res = last();
if (res >= 0) sup(res);
return res; }
340 class APIDECL bv_visitor {
342 bit_container::const_iterator it;
346 bv_visitor(
const dal::bit_vector& b) :
347 it(((const bit_container&)b).begin()+b.first()/WD_BIT),
348 ilast(b.last()+1), ind(b.first()), v(0) {
349 if (ind < ilast) { v = *it; v >>= (ind&WD_MASK); }
351 bool finished()
const {
return ind >= ilast; }
353 operator size_type()
const {
return ind; }
359 class APIDECL bv_visitor_c {
363 bv_visitor_c(
const dal::bit_vector& b) : bv(b), v(bv) {}
364 bool finished()
const {
return v.finished(); }
365 bool operator++() {
return ++v; }
371 inline int APIDECL &operator << (
int &i, bit_vector &s)
372 { i = s.take_first();
return i; }
373 inline const int APIDECL &operator >> (
const int &i, bit_vector &s)
374 { s.add(i);
return i; }
376 inline size_t APIDECL &operator << (
size_t &i, bit_vector &s)
377 { i = s.take_first();
return i; }
378 inline const size_t &operator >> (
const size_t &i, bit_vector &s)
379 { s.add(i);
return i; }
381 std::ostream APIDECL &operator <<(std::ostream &o,
const bit_vector &s);
384 template<
typename ITERABLE_BV>
385 class const_bv_iterator
391 typedef std::forward_iterator_tag iterator_category;
392 typedef size_type value_type;
393 typedef size_type difference_type;
394 typedef size_type* pointer;
395 typedef size_type& reference;
397 const_bv_iterator(
const ITERABLE_BV* p_iterable, size_type pos)
398 : p_iterable_(const_cast<ITERABLE_BV*>(p_iterable)), pos_(pos)
401 bool operator!= (
const const_bv_iterator &other)
const{
402 return pos_ < other.pos_;
405 size_type operator *()
const{
410 size_type
operator-(
const const_bv_iterator &other)
const{
411 if (pos_ == other.pos_)
return 0;
413 auto &vector_this = p_iterable_->v_;
414 auto &vector_other = other.p_iterable_->v_;
415 bv_visitor v_this(vector_this), v_other(vector_other);
416 while (v_this < pos_) ++v_this;
417 while (v_other < other.pos_) ++v_other;
418 auto &v = (pos_ < other.pos_) ? v_this : v_other;
419 auto &v_end = (pos_ >= other.pos_) ? v_this : v_other;
422 while(v < v_end) { ++v; ++count;}
426 const const_bv_iterator &operator++(){
433 ITERABLE_BV *p_iterable_;
450 bv_iterable(
const bit_vector &v) : v_(v), visitor_(v){}
452 const_bv_iterator<bv_iterable> begin()
const;
453 const_bv_iterator<bv_iterable> end()
const;
454 inline bool operator++(){
return ++visitor_;};
458 const bit_vector& v_;
460 friend class const_bv_iterator<bv_iterable>;
467 bv_iterable_c(
const bit_vector &v) : v_(v), visitor_(v_){}
468 const_bv_iterator<bv_iterable_c> begin()
const;
469 const_bv_iterator<bv_iterable_c> end()
const;
470 inline bool operator++(){
return ++visitor_;};
476 friend class const_bv_iterator<bv_iterable_c>;
rational_fraction< T > operator+(const polynomial< T > &P, const rational_fraction< T > &Q)
Add Q to P.
rational_fraction< T > operator-(const polynomial< T > &P, const rational_fraction< T > &Q)
Subtract Q from P.
size_t size_type
used as the common size type in the library
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void add(const L1 &l1, L2 &l2)
*/