2#ifndef CLICK_BITVECTOR_HH
3#define CLICK_BITVECTOR_HH
5#include <click/glue.hh>
25 typedef bool (
Bitvector::*unspecified_bool_type)()
const;
27 typedef uint32_t word_type;
28 enum { wbits = 32, wshift = 5, wmask = 31 };
37 inline int size()
const;
44 inline word_type *
words();
45 inline const word_type *
words()
const;
48 inline operator unspecified_bool_type()
const;
76 void set_range(
int start,
int length,
bool value);
77 bool range(
int start,
int length);
83 inline int clz()
const;
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;
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;
99 enum { ninline = 2, inlinebits = ninline * wbits };
103 word_type _f[ninline];
105 void finish_copy_constructor(
const Bitvector &);
106 inline void clear_last();
107 void hard_resize(
int,
bool);
119 inline Bit(
const Bit & ) =
default;
120 inline Bit(Bitvector::word_type &w,
int bit_offset);
122 inline operator bool()
const;
135 Bitvector::word_type &_p;
136 Bitvector::word_type _mask;
143 : _max(-1), _data(_f) {
152 if (n <= inlinebits) {
154 memset(_f, 0,
sizeof(_f));
163 : _max(0), _data(_f) {
170 : _max(-1), _data(_f) {
176 : _max(x._max), _data(_f) {
177 if (_max < inlinebits)
178 memcpy(_data, x._data, ninline *
sizeof(word_type));
180 finish_copy_constructor(x);
198inline Bitvector::operator unspecified_bool_type()
const {
211 assert(i >= 0 && i <= _max);
212 return Bit(_data[i>>wshift], i & wmask);
217 assert(i >= 0 && i <= _max);
218 return (_data[i>>wshift] & (word_type(1) << (i & wmask))) != 0;
232 return Bit(_data[i>>wshift], i & wmask);
237 return (_max + wbits) >> wshift;
242 return (_max < 0 ? -1 : _max >> wshift);
256inline Bitvector::word_type *Bitvector::data_words() {
259inline const Bitvector::word_type *Bitvector::data_words()
const {
333inline Bitvector::Bit::Bit(Bitvector::word_type &w,
int bit_offset)
334 : _p(w), _mask(
Bitvector::word_type(1) << bit_offset) {
338inline Bitvector::Bit::operator bool()
const {
339 return (_p & _mask) != 0;
396#if HAVE___BUILTIN_POPCOUNT
397 for (
int i =0 ; i < (_max + wbits)>>wshift; i++) {
398 n+= __builtin_popcount(_data[i]);
401 for (
int i = 0; i <
size(); i++)
416 assert(start+length < _max);
418 for (
int i = start; i < start + length; i++)
427 for (
int i =0 ; i < (_max + wbits)>>wshift; i++) {
431#if HAVE___BUILTIN_CTZ
432 return n + __builtin_ctz(_data[i]);
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