Click
Loading...
Searching...
No Matches
bitvector.hh
Go to the documentation of this file.
1// -*- c-basic-offset: 4; related-file-name: "../../lib/bitvector.cc" -*-
2#ifndef CLICK_BITVECTOR_HH
3#define CLICK_BITVECTOR_HH
4#include <click/string.hh>
5#include <click/glue.hh>
6CLICK_DECLS
7
21class Bitvector {
22 public:
23
24 class Bit;
25 typedef bool (Bitvector::*unspecified_bool_type)() const;
26
27 typedef uint32_t word_type;
28 enum { wbits = 32, wshift = 5, wmask = 31 };
29
30 inline Bitvector();
31 explicit inline Bitvector(int n);
32 explicit inline Bitvector(bool bit);
33 inline Bitvector(int n, bool bit);
34 inline Bitvector(const Bitvector &x);
35 inline ~Bitvector();
36
37 inline int size() const;
38 inline Bit operator[](int i);
39 inline bool operator[](int i) const;
40 inline Bit force_bit(int i);
41
42 inline int word_size() const;
43 inline int max_word() const;
44 inline word_type *words();
45 inline const word_type *words() const;
46
47 bool zero() const;
48 inline operator unspecified_bool_type() const;
49 inline bool operator!() const;
50
51 Bitvector &assign(int n, bool bit);
52 Bitvector &operator=(const Bitvector &x);
53 void clear();
54 void resize(int n);
55
56 friend inline bool operator==(const Bitvector &a, const Bitvector &b);
57 friend inline bool operator!=(const Bitvector &a, const Bitvector &b);
58 bool nonzero_intersection(const Bitvector &x) const;
59
60 inline Bitvector operator~() const;
61 friend inline Bitvector operator&(Bitvector a, const Bitvector &b);
62 friend inline Bitvector operator|(Bitvector a, const Bitvector &b);
63 friend inline Bitvector operator^(Bitvector a, const Bitvector &b);
64 friend inline Bitvector operator-(Bitvector a, const Bitvector &b);
65
66 void flip();
67 inline void negate();
71 inline Bitvector &operator-=(const Bitvector &x);
72 void offset_or(const Bitvector &x, int offset);
73 void or_with_difference(const Bitvector &x, Bitvector &difference);
74
75 void swap(Bitvector &x);
76 void set_range(int start, int length, bool value);
77 bool range(int start, int length);
78
79 inline int weight() const;
80 inline int popcnt() const;
81 inline int weight_range(int start, int length) const;
82
83 inline int clz() const;
84
85 bool parse(const String &str, int min_val, int max_val, int offset = 0);
86 String unparse(int read_offset = 0, int output_offset = 0) const;
87
89 typedef word_type data_word_type CLICK_DEPRECATED;
90 enum { data_word_bits = wbits };
91 inline word_type *data_words() CLICK_DEPRECATED;
92 inline const word_type *data_words() const CLICK_DEPRECATED;
95 static Bitvector from_mask(unsigned long mask);
96
97 private:
98
99 enum { ninline = 2, inlinebits = ninline * wbits };
100
101 int _max;
102 word_type *_data;
103 word_type _f[ninline];
104
105 void finish_copy_constructor(const Bitvector &);
106 inline void clear_last();
107 void hard_resize(int, bool);
108
109};
110
111
117class Bitvector::Bit { public:
118
119 inline Bit( const Bit & ) = default;
120 inline Bit(Bitvector::word_type &w, int bit_offset);
121
122 inline operator bool() const;
123
124 inline Bit &operator=(bool x);
125 inline Bit &operator=(const Bit &x);
126 inline void flip();
127
128 inline Bit &operator&=(bool x);
129 inline Bit &operator|=(bool x);
130 inline Bit &operator^=(bool x);
131 inline Bit &operator-=(bool x);
132
133 private:
134
135 Bitvector::word_type &_p;
136 Bitvector::word_type _mask;
137
138};
139
140
143 : _max(-1), _data(_f) {
144 _f[0] = 0;
145}
146
150 : _data(_f) {
151 assert(n >= 0);
152 if (n <= inlinebits) {
153 _max = n - 1;
154 memset(_f, 0, sizeof(_f));
155 } else {
156 _max = -1;
157 resize(n);
158 }
159}
160
162inline Bitvector::Bitvector(bool bit)
163 : _max(0), _data(_f) {
164 _f[0] = bit;
165}
166
169inline Bitvector::Bitvector(int n, bool b)
170 : _max(-1), _data(_f) {
171 assign(n, b);
172}
173
176 : _max(x._max), _data(_f) {
177 if (_max < inlinebits)
178 memcpy(_data, x._data, ninline * sizeof(word_type));
179 else
180 finish_copy_constructor(x);
181}
182
187 if (_data != _f)
188 delete[] _data;
189}
190
192inline int Bitvector::size() const {
193 return _max + 1;
194}
195
198inline Bitvector::operator unspecified_bool_type() const {
199 return !zero() ? &Bitvector::zero : 0;
200}
201
204inline bool Bitvector::operator!() const {
205 return zero();
206}
207
211 assert(i >= 0 && i <= _max);
212 return Bit(_data[i>>wshift], i & wmask);
213}
214
216inline bool Bitvector::operator[](int i) const {
217 assert(i >= 0 && i <= _max);
218 return (_data[i>>wshift] & (word_type(1) << (i & wmask))) != 0;
219}
220
229 assert(i >= 0);
230 if (i > _max)
231 resize(i + 1);
232 return Bit(_data[i>>wshift], i & wmask);
233}
234
236inline int Bitvector::word_size() const {
237 return (_max + wbits) >> wshift;
238}
239
241inline int Bitvector::max_word() const {
242 return (_max < 0 ? -1 : _max >> wshift);
243}
244
246inline Bitvector::word_type *Bitvector::words() {
247 return _data;
248}
249
251inline const Bitvector::word_type *Bitvector::words() const {
252 return _data;
253}
254
256inline Bitvector::word_type *Bitvector::data_words() {
257 return _data;
258}
259inline const Bitvector::word_type *Bitvector::data_words() const {
260 return _data;
261}
265inline bool operator==(const Bitvector &a, const Bitvector &b) {
266 return a.size() == b.size()
267 && memcmp(a.words(), b.words(), a.word_size() * sizeof(Bitvector::word_type)) == 0;
268}
269
271inline bool operator!=(const Bitvector &a, const Bitvector &b) {
272 return !(a == b);
273}
274
281 return *this &= ~x;
282}
283
286inline void Bitvector::negate() {
287 flip();
288}
289
292 Bitvector m = *this;
293 m.flip();
294 return m;
295}
296
300 return a &= b;
301}
302
306 return a |= b;
307}
308
312 return a ^= b;
313}
314
320 return a & ~b;
321}
322
323inline void click_swap(Bitvector &a, Bitvector &b) {
324 a.swap(b);
325}
326
327inline void assign_consume(Bitvector &a, Bitvector &b) {
328 a.swap(b);
329}
330
331
333inline Bitvector::Bit::Bit(Bitvector::word_type &w, int bit_offset)
334 : _p(w), _mask(Bitvector::word_type(1) << bit_offset) {
335}
336
338inline Bitvector::Bit::operator bool() const {
339 return (_p & _mask) != 0;
340}
341
344 if (x)
345 _p |= _mask;
346 else
347 _p &= ~_mask;
348 return *this;
349}
350
353 if (x._p & x._mask)
354 _p |= _mask;
355 else
356 _p &= ~_mask;
357 return *this;
358}
359
361inline void Bitvector::Bit::flip() {
362 _p ^= _mask;
363}
364
367 if (!x)
368 _p &= ~_mask;
369 return *this;
370}
371
374 if (x)
375 _p |= _mask;
376 return *this;
377}
378
381 if (x)
382 _p ^= _mask;
383 return *this;
384}
385
388 if (x)
389 _p &= ~_mask;
390 return *this;
391}
392
394inline int Bitvector::weight() const {
395 int n = 0;
396#if HAVE___BUILTIN_POPCOUNT
397 for (int i =0 ; i < (_max + wbits)>>wshift; i++) {
398 n+= __builtin_popcount(_data[i]);
399 }
400#else
401 for (int i = 0; i < size(); i++)
402 if ((*this)[i])
403 n++;
404#endif
405 return n;
406}
407
410inline int Bitvector::popcnt() const {
411 return weight();
412}
413
415inline int Bitvector::weight_range(int start, int length) const {
416 assert(start+length < _max);
417 int w = 0;
418 for (int i = start; i < start + length; i++)
419 if ((*this)[i])
420 w++;
421 return w;
422}
423
425inline int Bitvector::clz() const {
426 int n = 0;
427 for (int i =0 ; i < (_max + wbits)>>wshift; i++) {
428 if (!_data[i])
429 n+=wbits;
430 else {
431#if HAVE___BUILTIN_CTZ
432 return n + __builtin_ctz(_data[i]);
433#else
434 while (!(*this)[n])
435 n++;
436 return n;
437#endif
438 }
439 }
440 return size();
441}
442
443
444CLICK_ENDDECLS
445#endif
Bitvector operator&(Bitvector a, const Bitvector &b)
Return the bitwise and of two bitvectors.
Definition bitvector.hh:299
Bitvector operator^(Bitvector a, const Bitvector &b)
Return the bitwise exclusive or of two bitvectors.
Definition bitvector.hh:311
Bitvector operator|(Bitvector a, const Bitvector &b)
Return the bitwise or of two bitvectors.
Definition bitvector.hh:305
Bitvector operator-(Bitvector a, const Bitvector &b)
Return the bitwise subtraction of two bitvectors.
Definition bitvector.hh:319
bool operator!=(const Bitvector &a, const Bitvector &b)
Test bitvectors for inequality.
Definition bitvector.hh:271
bool operator==(const Bitvector &a, const Bitvector &b)
Test bitvectors for equality.
Definition bitvector.hh:265
A wrapper class that acts like a single bit.
Definition bitvector.hh:117
Bit & operator^=(bool x)
Modify this bit by bitwise exclusive or with x.
Definition bitvector.hh:380
Bit & operator|=(bool x)
Modify this bit by bitwise or with x.
Definition bitvector.hh:373
Bit & operator-=(bool x)
Modify this bit by bitwise subtraction with x.
Definition bitvector.hh:387
void flip()
Flip this bit.
Definition bitvector.hh:361
Bit & operator=(bool x)
Set this bit to x.
Definition bitvector.hh:343
Bit & operator&=(bool x)
Modify this bit by bitwise and with x.
Definition bitvector.hh:366
Vector of bits.
Definition bitvector.hh:21
~Bitvector()
Destroy a bitvector.
Definition bitvector.hh:186
void flip()
Flip all bits in this bitvector.
Definition bitvector.cc:128
int word_size() const
Return the number of valid data words.
Definition bitvector.hh:236
Bit operator[](int i)
Return the bit at position i.
Definition bitvector.hh:210
Bitvector & operator|=(const Bitvector &x)
Modify this bitvector by bitwise or with x.
Definition bitvector.cc:155
Bit force_bit(int i)
Return the bit at position i, extending if necessary.
Definition bitvector.hh:228
Bitvector & operator-=(const Bitvector &x)
Modify this bitvector by bitwise subtraction with x.
Definition bitvector.hh:280
int popcnt() const
Return the number of true bits.
Definition bitvector.hh:410
Bitvector & assign(int n, bool bit)
Set the bitvector to bit-valued length-n.
Definition bitvector.cc:113
void offset_or(const Bitvector &x, int offset)
Modify this bitvector by bitwise or with an offset x.
Definition bitvector.cc:193
bool zero() const
Test if the bitvector's bits are all false.
Definition bitvector.cc:48
friend Bitvector operator&(Bitvector a, const Bitvector &b)
Return the bitwise and of two bitvectors.
Definition bitvector.hh:299
Bitvector & operator&=(const Bitvector &x)
Modify this bitvector by bitwise and with x.
Definition bitvector.cc:141
bool nonzero_intersection(const Bitvector &x) const
Test whether this bitvector and x have a common true bit.
Definition bitvector.cc:254
Bitvector & operator^=(const Bitvector &x)
Modify this bitvector by bitwise exclusive or with x.
Definition bitvector.cc:171
int max_word() const
Return the index of the maximum valid data word.
Definition bitvector.hh:241
void negate()
Flip all bits in this bitvector.
Definition bitvector.hh:286
bool operator!() const
Test if the bitvector contains no true bits.
Definition bitvector.hh:204
friend Bitvector operator^(Bitvector a, const Bitvector &b)
Return the bitwise exclusive or of two bitvectors.
Definition bitvector.hh:311
Bitvector & operator=(const Bitvector &x)
Set this bitvector to a copy of x.
Definition bitvector.cc:92
friend Bitvector operator|(Bitvector a, const Bitvector &b)
Return the bitwise or of two bitvectors.
Definition bitvector.hh:305
Bitvector operator~() const
Return the bitwise negation of this bitvector.
Definition bitvector.hh:291
void swap(Bitvector &x)
Swap the contents of this bitvector and x.
Definition bitvector.cc:268
int weight_range(int start, int length) const
Return the number of true bits.
Definition bitvector.hh:415
void or_with_difference(const Bitvector &x, Bitvector &difference)
Modify this bitvector by bitwise or, returning difference.
Definition bitvector.cc:236
friend Bitvector operator-(Bitvector a, const Bitvector &b)
Return the bitwise subtraction of two bitvectors.
Definition bitvector.hh:319
void clear()
Set all bits to false.
Definition bitvector.cc:59
void resize(int n)
Resize the bitvector to n bits.
Definition bitvector.cc:70
word_type * words()
Return a pointer to this bitvector's data words.
Definition bitvector.hh:246
int weight() const
Return the number of true bits.
Definition bitvector.hh:394
int clz() const
Return the number of leading zeroes.
Definition bitvector.hh:425
friend bool operator!=(const Bitvector &a, const Bitvector &b)
Test bitvectors for inequality.
Definition bitvector.hh:271
int size() const
Return the number of bits in the bitvector.
Definition bitvector.hh:192
static Bitvector from_mask(unsigned long mask)
Definition bitvector.cc:305
Bitvector()
Construct an empty bitvector.
Definition bitvector.hh:142
friend bool operator==(const Bitvector &a, const Bitvector &b)
Test bitvectors for equality.
Definition bitvector.hh:265
A string of characters.
Definition string.hh:22
Click's String class.