Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
BitVector.h
1 /*
2  * BitVector class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2005-17, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_UTIL_BIT_VECTOR_H
22 #define ELM_UTIL_BIT_VECTOR_H
23 
24 #include <elm/assert.h>
25 #include <elm/io.h>
26 #include <elm/PreIterator.h>
27 
28 namespace elm {
29 
30 // BitVector class
31 class BitVector {
32  typedef t::uint8 word_t;
33 public:
34  inline BitVector(void): bits(nullptr), _size(0) { }
35  BitVector(int size, bool set = false);
36  BitVector(const BitVector& vec);
37  BitVector(const BitVector& vec, int new_size);
38  inline ~BitVector(void) { if(bits) delete [] bits; }
39  inline int size(void) const { return _size; }
40 
41  inline bool bit(int i) const {
42  ASSERTP(i < _size, "index out of bounds");
43  return (bits[windex(i)] & (1 << bindex(i))) != 0;
44  }
45 
46  inline bool isEmpty(void) const {
47  mask();
48  for(int i = 0; i < wcount(); i++)
49  if(bits[i]) return false;
50  return true;
51  }
52 
53  bool includes(const BitVector& vec) const;
54  bool includesStrictly(const BitVector &vec) const;
55  bool equals(const BitVector& vec) const;
56  int countBits(void) const;
57  void resize(int new_size);
58  bool meets(const BitVector& bv);
59 
60  inline void set(int index) const
61  { ASSERTP(index < _size, "index out of bounds"); bits[windex(index)] |= word_t(1) << bindex(index); }
62  inline void set(int index, bool value) const
63  { ASSERTP(index < _size, "index out of bounds"); if(value) set(index); else clear(index); }
64  inline void clear(int index) const
65  { ASSERTP(index < _size, "index out of bounds"); bits[windex(index)] &= ~(word_t(1) << bindex(index)); }
66 
67  void copy(const BitVector& bits) const;
68  void clear(void);
69  void set(void);
70 
71  void applyNot(void);
72  void applyOr(const BitVector& vec);
73  void applyAnd(const BitVector& vec);
74  void applyReset(const BitVector& vec);
75 #ifdef EXPERIMENTAL
76  inline void shiftLeft(int n = 1) { doShiftLeft(n, bits); }
77  inline void shiftRight(int n = 1) { doShiftRight(n, bits); }
78  inline void rotateLeft(int n = 1) { doRotateLeft(n, bits); }
79  inline void rotateRight(int n = 1) { doRotateRight(n, bits); }
80 #endif
81 
82  BitVector makeNot(void) const;
83  BitVector makeOr(const BitVector& vec) const;
84  BitVector makeAnd(const BitVector& vec) const;
85  BitVector makeReset(const BitVector& vec) const;
86 #ifdef EXPERIMENTAL
87  inline BitVector makeShiftLeft(int n = 1) const { BitVector r(size()); doShiftLeft(n, r.bits); return r; }
88  inline BitVector makeShiftRight(int n = 1) const { BitVector r(size()); doShiftRight(n, r.bits); return r; }
89  inline BitVector makeRotateLeft(int n = 1) const { BitVector r(size()); doRotateLeft(n, r.bits); return r; }
90  inline BitVector makeRotateRight(int n = 1) const { BitVector r(size()); doRotateRight(n, r.bits); return r; }
91 #endif
92 
93  void print(io::Output& out) const;
94 
95  // useful operations
96  int countOnes(void) const;
97  inline int countZeroes(void) const { return countBits() - countOnes(); }
98 
99  class Iter {
100  public:
101  inline Iter(const BitVector& v): bvec(v), i(0) { }
102  inline bool ended() const { return i >= bvec.size(); }
103  inline bool item() const { return bvec.bit(i); }
104  inline void next() { i++; }
105  inline bool equals(const Iter& it) const { return i == it.i; }
106 
107  inline bool operator()() const { return !ended(); }
108  inline bool operator*() const { return item(); }
109  inline Iter& operator++() { next(); return *this; }
110  inline Iter operator++(int) { Iter o = *this; next(); return o; }
111  inline bool operator==(const Iter& it) const { return equals(it); }
112  inline bool operator!=(const Iter& it) const { return !equals(it); }
113 
114  protected:
115  const BitVector& bvec;
116  int i;
117  };
118 
119  // OneIterator iter
120  class OneIterator: public Iter {
121  public:
122  inline OneIterator(const BitVector& bit_vector, int ii = 0): Iter(bit_vector) { i = ii - 1; next(); }
123  inline int item() const { return i; }
124  inline void next() { do i++; while(i < bvec.size() && !bvec.bit(i)); }
125  inline int operator*() const { return item(); }
126  inline Iter& operator++() { next(); return *this; }
127  inline Iter operator++(int) { Iter o = *this; next(); return o; }
128  };
129  inline OneIterator begin() const { return OneIterator(*this); }
130  inline OneIterator end() const { return OneIterator(*this, size()); }
131 
132  // ZeroIterator iter
133  class ZeroIterator: public Iter {
134  public:
135  inline ZeroIterator(const BitVector& bit_vector): Iter(bit_vector) { i = -1; next(); }
136  inline int item(void) const { return i; }
137  inline void next(void) { do i++; while(i < bvec.size() && bvec.bit(i)); }
138  inline int operator*() const { return item(); }
139  inline Iter& operator++() { next(); return *this; }
140  inline Iter operator++(int) { Iter o = *this; next(); return o; }
141  };
142 
143  // Ref delegate
144  class Ref {
145  public:
146  inline Ref(BitVector& v, int i): _v(v), _i(i) { }
147  inline bool get(void) const { return _v.bit(_i); }
148  inline void set(void) { _v.set(_i); }
149  inline void clear(void) { _v.clear(_i); }
150  inline void set(bool b) { if(b) set(); else clear(); }
151 
152  inline operator bool(void) const { return get(); }
153  inline Ref& operator=(bool b) { set(b); return *this;}
154  private:
155  BitVector& _v;
156  int _i;
157  };
158 
159  // Operators
160  inline operator bool(void) const { return !isEmpty(); }
161  inline bool operator[](int index) const { return bit(index); }
162  inline Ref operator[](int i) { return Ref(*this, i); }
163  inline BitVector operator~(void) const { return makeNot(); }
164  inline BitVector operator|(const BitVector& vec) const { return makeOr(vec); }
165  inline BitVector operator&(const BitVector& vec) const { return makeAnd(vec); }
166  inline BitVector operator+(const BitVector& vec) const { return makeOr(vec); }
167  inline BitVector operator-(const BitVector& vec) const { return makeReset(vec); }
168 #ifdef EXPERIMENTAL
169  inline BitVector operator<<(int n) const { return makeShiftLeft(n); }
170  inline BitVector operator>>(int n) const { return makeShiftRight(n); }
171 #endif
172  BitVector& operator=(const BitVector& vec);
173  inline BitVector& operator|=(const BitVector& vec) { applyOr(vec); return *this; }
174  inline BitVector& operator&=(const BitVector& vec) { applyAnd(vec); return *this; }
175  inline BitVector& operator+=(const BitVector& vec) { applyOr(vec); return *this; }
176  inline BitVector& operator-=(const BitVector& vec) { applyReset(vec); return *this; }
177 #ifdef EXPERIMENTAL
178  inline BitVector& operator<<=(int d) { shiftLeft(d); return *this; }
179  inline BitVector& operator>>=(int d) { shiftRight(d); return *this; }
180 #endif
181  inline bool operator==(const BitVector& vec) const { return equals(vec); }
182  inline bool operator!=(const BitVector& vec) const { return !equals(vec); }
183  inline bool operator<(const BitVector& vec) const { return vec.includesStrictly(*this); }
184  inline bool operator<=(const BitVector& vec) const { return vec.includes(*this); }
185  inline bool operator>(const BitVector& vec) const { return includesStrictly(vec); }
186  inline bool operator>=(const BitVector& vec) const { return includes(vec); }
187 
188  inline t::size __size(void) const { return sizeof(*this) + wcount() * sizeof(word_t); }
189 
190 private:
191  word_t *bits;
192  int _size;
193 
194  inline int wsize(void) const { return sizeof(word_t) << 3; }
195  inline int wshift(void) const { return type_info<word_t>::shift + 3; }
196  inline int inWords(int s) const { return (s + wsize() - 1) >> wshift(); }
197  inline int wcount(void) const { return inWords(_size); }
198  inline int windex(int index) const { return index >> wshift(); }
199  inline int bindex(int index) const { return index & (wsize() - 1); }
200 
201  inline void mask(word_t *bits) const {
202  word_t mask = word_t(-1) >> (wsize() - bindex(_size));
203  if(mask) bits[wcount() - 1] &= mask;
204  }
205  inline void mask(void) const { mask(bits); }
206 #ifdef EXPERIMENTAL
207  void doShiftLeft(int n, word_t *tbits) const;
208  void doShiftRight(int n, word_t *tbits) const;
209  void doRotateLeft(int n, word_t *tbits) const;
210  void doRotateRight(int n, word_t *tbits) const;
211 #endif
212 };
213 
215  { bvec.print(out); return out; }
216 
217 } // elm
218 
219 #endif // ELM_UTIL_BIT_VECTOR_H
elm::BitVector::operator&
BitVector operator&(const BitVector &vec) const
Definition: BitVector.h:165
elm::BitVector::operator<
bool operator<(const BitVector &vec) const
Definition: BitVector.h:183
elm::BitVector::Iter::Iter
Iter(const BitVector &v)
Definition: BitVector.h:101
elm::BitVector::Iter::operator++
Iter & operator++()
Definition: BitVector.h:109
elm::BitVector::OneIterator::item
int item() const
Definition: BitVector.h:123
elm::t::out
typename type_info< T >::out_t out
Definition: type_info.h:284
elm::BitVector::clear
void clear(int index) const
Definition: BitVector.h:64
elm::BitVector::ZeroIterator::operator*
int operator*() const
Definition: BitVector.h:138
elm::BitVector::~BitVector
~BitVector(void)
Definition: BitVector.h:38
elm::BitVector::begin
OneIterator begin() const
Definition: BitVector.h:129
elm::BitVector::operator<=
bool operator<=(const BitVector &vec) const
Definition: BitVector.h:184
elm::BitVector::applyNot
void applyNot(void)
Definition: util_BitVector.cpp:229
elm::BitVector::operator|=
BitVector & operator|=(const BitVector &vec)
Definition: BitVector.h:173
elm::BitVector::isEmpty
bool isEmpty(void) const
Definition: BitVector.h:46
elm::BitVector::Iter::operator*
bool operator*() const
Definition: BitVector.h:108
elm::BitVector::Ref::operator=
Ref & operator=(bool b)
Definition: BitVector.h:153
elm::operator<<
AutoString & operator<<(CString str, const T &value)
Definition: AutoString.h:75
elm::BitVector::operator+=
BitVector & operator+=(const BitVector &vec)
Definition: BitVector.h:175
elm::BitVector::makeAnd
BitVector makeAnd(const BitVector &vec) const
Definition: util_BitVector.cpp:307
elm::BitVector::Ref
Definition: BitVector.h:144
elm::BitVector::applyAnd
void applyAnd(const BitVector &vec)
Definition: util_BitVector.cpp:253
elm::BitVector::Ref::get
bool get(void) const
Definition: BitVector.h:147
elm::BitVector::Iter::operator!=
bool operator!=(const Iter &it) const
Definition: BitVector.h:112
elm::BitVector::Iter::operator++
Iter operator++(int)
Definition: BitVector.h:110
elm::BitVector::operator-
BitVector operator-(const BitVector &vec) const
Definition: BitVector.h:167
elm::BitVector::operator=
BitVector & operator=(const BitVector &vec)
Definition: util_BitVector.cpp:88
elm::BitVector::operator!=
bool operator!=(const BitVector &vec) const
Definition: BitVector.h:182
elm::BitVector::ZeroIterator::ZeroIterator
ZeroIterator(const BitVector &bit_vector)
Definition: BitVector.h:135
elm::BitVector::Ref::set
void set(void)
Definition: BitVector.h:148
elm::BitVector::ZeroIterator::next
void next(void)
Definition: BitVector.h:137
value
elm::BitVector::Iter::ended
bool ended() const
Definition: BitVector.h:102
bool
elm::BitVector::end
OneIterator end() const
Definition: BitVector.h:130
elm::BitVector::copy
void copy(const BitVector &bits) const
Definition: util_BitVector.cpp:211
elm::BitVector::OneIterator::OneIterator
OneIterator(const BitVector &bit_vector, int ii=0)
Definition: BitVector.h:122
elm::BitVector::size
int size(void) const
Definition: BitVector.h:39
elm::BitVector::applyReset
void applyReset(const BitVector &vec)
Definition: util_BitVector.cpp:266
elm::BitVector::Iter::equals
bool equals(const Iter &it) const
Definition: BitVector.h:105
elm::BitVector::OneIterator::operator++
Iter operator++(int)
Definition: BitVector.h:127
elm::BitVector::makeNot
BitVector makeNot(void) const
Definition: util_BitVector.cpp:279
elm::BitVector::Ref::Ref
Ref(BitVector &v, int i)
Definition: BitVector.h:146
elm::BitVector::meets
bool meets(const BitVector &bv)
Definition: util_BitVector.cpp:462
elm::BitVector::applyOr
void applyOr(const BitVector &vec)
Definition: util_BitVector.cpp:241
elm::BitVector::Iter::operator==
bool operator==(const Iter &it) const
Definition: BitVector.h:111
elm::BitVector::bit
bool bit(int i) const
Definition: BitVector.h:41
elm
Definition: adapter.h:26
elm::BitVector::clear
void clear(void)
Definition: util_BitVector.cpp:220
elm::BitVector::ZeroIterator::operator++
Iter & operator++()
Definition: BitVector.h:139
elm::word_t
WAHVector::word_t word_t
Definition: util_WAHVector.cpp:29
elm::BitVector::Ref::clear
void clear(void)
Definition: BitVector.h:149
elm::BitVector::countZeroes
int countZeroes(void) const
Definition: BitVector.h:97
elm::BitVector::set
void set(void)
Definition: util_BitVector.cpp:184
elm::BitVector::operator>
bool operator>(const BitVector &vec) const
Definition: BitVector.h:185
elm::t::size
uint64 size
Definition: arch.h:35
elm::BitVector::ZeroIterator
Definition: BitVector.h:133
elm::BitVector::operator|
BitVector operator|(const BitVector &vec) const
Definition: BitVector.h:164
elm::BitVector::set
void set(int index) const
Definition: BitVector.h:60
elm::BitVector::Iter
Definition: BitVector.h:99
elm::BitVector::ZeroIterator::operator++
Iter operator++(int)
Definition: BitVector.h:140
elm::BitVector::OneIterator::next
void next()
Definition: BitVector.h:124
elm::BitVector::Iter::i
int i
Definition: BitVector.h:116
elm::BitVector::makeOr
BitVector makeOr(const BitVector &vec) const
Definition: util_BitVector.cpp:292
elm::BitVector::operator&=
BitVector & operator&=(const BitVector &vec)
Definition: BitVector.h:174
elm::t::uint8
unsigned char uint8
Definition: arch.h:27
elm::BitVector::operator~
BitVector operator~(void) const
Definition: BitVector.h:163
elm::BitVector::includesStrictly
bool includesStrictly(const BitVector &vec) const
Definition: util_BitVector.cpp:149
elm::BitVector::Iter::operator()
bool operator()() const
Definition: BitVector.h:107
elm::BitVector::operator+
BitVector operator+(const BitVector &vec) const
Definition: BitVector.h:166
elm::BitVector::ZeroIterator::item
int item(void) const
Definition: BitVector.h:136
elm::BitVector::OneIterator
Definition: BitVector.h:120
elm::BitVector::OneIterator::operator*
int operator*() const
Definition: BitVector.h:125
elm::BitVector::operator==
bool operator==(const BitVector &vec) const
Definition: BitVector.h:181
elm::BitVector::print
void print(io::Output &out) const
Definition: util_BitVector.cpp:437
elm::BitVector::operator>=
bool operator>=(const BitVector &vec) const
Definition: BitVector.h:186
elm::BitVector::BitVector
BitVector(void)
Definition: BitVector.h:34
elm::BitVector::equals
bool equals(const BitVector &vec) const
Definition: util_BitVector.cpp:168
elm::BitVector::Ref::set
void set(bool b)
Definition: BitVector.h:150
elm::BitVector::operator[]
Ref operator[](int i)
Definition: BitVector.h:162
elm::BitVector::__size
t::size __size(void) const
Definition: BitVector.h:188
elm::BitVector::includes
bool includes(const BitVector &vec) const
Definition: util_BitVector.cpp:134
elm::BitVector::countOnes
int countOnes(void) const
Definition: util_BitVector.cpp:493
elm::operator>>
io::StringInput operator>>(const string &str, T &val)
Definition: AutoString.h:82
elm::BitVector
Definition: BitVector.h:31
elm::BitVector::makeReset
BitVector makeReset(const BitVector &vec) const
Definition: util_BitVector.cpp:322
elm::mask
word_t mask(int n)
Definition: util_WAHVector.cpp:50
elm::BitVector::resize
void resize(int new_size)
Definition: util_BitVector.cpp:512
elm::BitVector::Iter::next
void next()
Definition: BitVector.h:104
elm::BitVector::Iter::item
bool item() const
Definition: BitVector.h:103
elm::BitVector::operator[]
bool operator[](int index) const
Definition: BitVector.h:161
elm::io::Output
Definition: Output.h:179
elm::BitVector::set
void set(int index, bool value) const
Definition: BitVector.h:62
elm::BitVector::operator-=
BitVector & operator-=(const BitVector &vec)
Definition: BitVector.h:176
elm::BitVector::countBits
int countBits(void) const
Definition: util_BitVector.cpp:449
elm::BitVector::Iter::bvec
const BitVector & bvec
Definition: BitVector.h:115
elm::BitVector::OneIterator::operator++
Iter & operator++()
Definition: BitVector.h:126