1 #ifndef __BIT_VECTOR_HPP__
2 #define __BIT_VECTOR_HPP__
10 #define W (sizeof(uint32_t)*CHAR_BIT)
15 /** Simple class implementing a vector of bits as an uint32_t array.
21 /** Builds a bit vector capable of holding len elements of blen bits each,
22 initializes the vector with the copies len times the blen lowermost bits
24 Throws an exception if blen == 0 or blen > 32.
26 bit_vector (size_t len=0, size_t blen=1, uint32_t init=0);
28 /** Load the bit_vector from a file descriptor */
31 /** copy constructor */
32 bit_vector (const bit_vector& src);
34 bit_vector & operator=(const bit_vector& src);
40 /** Writes the content of the the bit_vector to the file descriptor fd, in
43 void save(int fd) const;
45 /** Returns the size of the bit_vector in bits */
46 size_t size() const { return _size; };
48 /** Returns the maximum number of bits that can be added without
51 size_t max_size() const { return _alloc*W; };
53 /** Resizes the vector so that it can store size() bits and such that at most
54 sizeof(uint32_t) - 1 bits are wasted.*/
57 /** Sets the idxth bit to b. The bit_vector is resized if necessary */
58 void set(size_t idx, bool b);
59 void set_le(size_t idx, bool b);
60 /** Adds bit b at the end of the bit_vector. The bit_vector is resized if
62 void push_back(bool b);
64 /** Returns the last bit of the bit vector, which is removed */
67 /** Sets bits [idx*len - (idx+1)*len] of the bit vector to the len
69 Throws an exception if len == 0 or len > W
70 The bit_vector is resized if necessary.
72 void set_field(size_t idx, size_t len, uint32_t v);
74 /** Flips the idxth bit of the bit_vector. Returns the value of the bit
76 bool flip(size_t idx);
78 /** Returns the idxth bit. Performs bound checking */
79 bool get(size_t idx) const;
81 /** Returns the bits idx*len to (idx+1)*len, packed into an uint32_t.
82 Throws an exception if len == 0 or len > sizeof(uint32_t).
83 Performs bound checking.
85 uint32_t get_field(size_t idx, size_t len) const;
87 /** returns a copy of the internal vector
89 uint32_t* get_vector() const;
90 /** returns a pointer to the internal vector
92 uint32_t* get_vector_ptr() { _leaked = true; return _vector; }
94 /** Efficient unsafe get/get_field */
95 bool unsafe_get(size_t idx) const;
96 uint32_t unsafe_get_field(size_t idx, size_t len) const;
97 void debug(void) const;
100 // true if get_vector_ptr() was called
104 // Allocated space in sizeof(uint32_t)
107 /** Increase the size of _vector
110 size_t allocforsize(size_t bits);
111 void grow(size_t alloc);
116 /** Unsafe version of get. Does not perform bound checking
118 inline bool bit_vector::unsafe_get(size_t idx) const
122 return (_vector[i] >> (W - 1 - j)) & true;
125 /** Unsafe version of get_field. Does not perform bound checking.
126 Assumes 1<= len <= sizeof(uint32_t).
128 inline uint32_t bit_vector::unsafe_get_field(size_t idx, size_t len) const
130 //TODO FIX TO REFLECT BIG ENDIAN ORDER.
133 return ((uint8_t*)_vector)[idx];
135 return ((uint16_t*)_vector)[idx];
139 return unsafe_get(idx);
141 size_t idxlen = idx*len;
142 size_t j = idxlen % W;
143 size_t i = idxlen / W;
144 size_t offset = W - len;
146 ? (_vector[i] << (offset-j)) >> offset
147 : (_vector[i] >> j) | (_vector [i+1] << (W+offset-j)) >> offset;