atlas  0.6
bitmap.h
Go to the documentation of this file.
1 
4 /*
5  Copyright (C) 2004,2005 Fokko du Cloux
6  part of the Atlas of Lie Groups and Representations
7 
8  For license information see the LICENSE file
9 */
10 
11 #ifndef BITMAP_H /* guard against multiple inclusions */
12 #define BITMAP_H
13 
14 #include <vector>
15 #include <cassert>
16 
17 #include "bitmap_fwd.h"
18 
19 #include "constants.h"
20 
21 /******** type definitions **************************************************/
22 
23 namespace atlas {
24 
25 namespace bitmap {
52 class BitMap
53 {
54  size_t d_capacity;
55  std::vector<unsigned long> d_map;
56  static unsigned long posBits;
57  static unsigned long baseBits;
58  static unsigned long baseShift;
59 
60  public:
61 
62 // type definitions
63 
67  typedef unsigned long value_type;
68  typedef value_type& reference;
69  typedef const value_type& const_reference;
70  typedef value_type* pointer;
71  typedef const value_type* const_pointer;
72  typedef ptrdiff_t difference_type;
73  typedef unsigned long size_type;
74 
75 // iterators
76  class iterator;
77  typedef iterator const_iterator;
78  iterator begin() const;
79  iterator end() const;
80 
81 // constructors and destructors
82  BitMap() : d_capacity(0), d_map() {} // create a bitmap without capacity
83 
90  explicit BitMap(unsigned long n)
91  : d_capacity(n), d_map((d_capacity+posBits)>>baseShift,0)
92  {}
93 
95  BitMap(const BitMap& b) : d_capacity(b.d_capacity), d_map(b.d_map) {}
96 
97  // convert range defined by iterators into a BitMap
98  template <typename I>
99  BitMap(unsigned long n, const I& first, const I& last);
100 
101  // an easier version for a full vector
102  template <typename U> // unsigned integral type
103  BitMap(unsigned long n,const std::vector<U>& v)
104  : d_capacity(n), d_map((d_capacity+posBits)>>baseShift)
105  {
106  for (size_t i=0; i<v.size(); ++i)
107  insert(v[i]);
108  }
109 
111  template <typename I, typename J>
112  BitMap(const I& first, const I& last, const J& fsub, const J& lsub);
113 
114 
115 // assignment
116  BitMap& operator= (const BitMap&);
117 
118 // accessors
124  unsigned long capacity() const { return d_capacity; }
125  size_type size() const; // the number of bits that are set in the bitmap
126 
127  bool operator< (const BitMap& b) const { return d_map < b.d_map; }
128  bool operator== (const BitMap& b) const { return d_map == b.d_map; }
129  bool operator!=(const BitMap& b) const { return d_map != b.d_map; }
130 
131  bool empty() const; // whether |size()==0|
132  bool full() const; // whether |size()==capacity()|
133 
138  bool isMember(unsigned long n) const
139  {
140  if (n >= d_capacity)
141  return false;
142  return (d_map[n >> baseShift] & constants::bitMask[n&posBits])!=0;
143  }
144 
145  // Whether all elements of |b| satisfy |isMember|
146  bool contains(const BitMap& b) const;
147 
148  // Whether none of the elements of |b| satisfy |isMember|
149  bool disjoint(const BitMap& b) const;
150 
152  unsigned long n_th(unsigned long n) const;
153 
155  unsigned long position(unsigned long n) const;
156 
157  unsigned long front() const;
158 
159  // decrement |n| (at least once) until it points to a member
160  // return value indicates whether this succeeded
161  bool back_up(unsigned long& n) const;
162 
163  // get a range of bits as unsigned long value; see bitmap.ccp for details
164  unsigned long range(unsigned long first, unsigned long number) const;
165 
166  BitMap operator& (const BitMap& other) const
167  { BitMap result(*this); result&=other; return result; }
168 
169  BitMap operator| (const BitMap& other) const
170  { BitMap result(*this); result|=other; return result; }
171 
172  BitMap operator^ (const BitMap& other) const
173  { BitMap result(*this); result^=other; return result; }
174 
175 
176 // manipulators
177 
178  // WARNING: the following looks like an accessor, but complements |*this|
179  BitMap& operator~ ();
180 
181  // these operators allow argument to have less capacity than |*this|
182  bool operator&= (const BitMap&);
183 
184  BitMap& operator|= (const BitMap&);
185 
186  BitMap& operator^= (const BitMap&);
187 
188  BitMap& andnot(const BitMap& b); // remove bits of |b|
189 
190  BitMap& operator>>= (unsigned long delta); // shift right (decrease)
191  BitMap& operator<<= (unsigned long delta); // shift left (increase)
192 
197  void insert(unsigned long n)
198  {
199  assert(n<d_capacity);
200  d_map[n >> baseShift] |= constants::bitMask[n & posBits];
201  }
202 
207  void remove(unsigned long n)
208  {
209  assert(n<d_capacity);
210  d_map[n >> baseShift] &= ~constants::bitMask[n & posBits];
211  }
212 
213  void set_to(unsigned long n,bool b)
214  { if (b) insert(n); else remove(n); }
215 
216  void set_mod2(unsigned long n, unsigned long v) { set_to(n,(v&1)!=0); }
217 
218  void flip(unsigned long n)
219  {
220  assert(n<d_capacity);
221  d_map[n >> baseShift] ^= constants::bitMask[n & posBits];
222  }
223 
224 
225  void fill(); // set all bits
226  void reset() { d_map.assign(d_map.size(),0ul); } // clear all bits
227  void fill(size_t start,size_t stop); // set consecutive range of bits
228  void clear(size_t start,size_t stop); // clear consecutive range of bits
229  // insert a range of values (which need not be listed increasingly)
230  template<typename I>
231  void insert(I, I);
232 
233  // this was called |resize|, but sets |capacity()|, whence the new name
234  void set_capacity(unsigned long n); // any new bits will start out cleared
235  void extend_capacity(bool b); // extend capacity by |1|, adding member if |b|
236 
237  // set an interval of bits from those (least significant ones) of source
238  void setRange(unsigned long start, unsigned long amount, unsigned long source);
239 
240  void swap(BitMap&);
241 
242 }; // |class BitMap|
243 
244 
245 
266 class BitMap::iterator // is really a const_iterator
267 {
268  std::vector<unsigned long>::const_iterator d_chunk; // point to current chunk
269  unsigned long d_bitAddress; // value returned if dereferenced
270  unsigned long d_capacity; // beyond-the-end bit-address, normally constant
271 
272  public:
273 
274 // associated types
275  typedef std::forward_iterator_tag iterator_category;
276  typedef unsigned long value_type;
277  typedef ptrdiff_t difference_type;
278  typedef const value_type* pointer;
279  typedef const value_type& reference;
280 
281 // constructors and destructors
282  iterator() {}
283 
284  iterator(const iterator &j):d_chunk(j.d_chunk), d_bitAddress(j.d_bitAddress),
285  d_capacity(j.d_capacity) {}
286 
287  iterator(const std::vector<unsigned long>::const_iterator& p,
288  unsigned long n, unsigned long c)
289  :d_chunk(p), d_bitAddress(n), d_capacity(c) {}
290 
291 // assignment
292  iterator& operator= (const iterator& i);
293 
294 // accessors
295  bool operator== (const iterator& i) const
296  { return d_bitAddress == i.d_bitAddress; } // |d_chunk| is ignored!
297  bool operator!= (const iterator& i) const
298  { return d_bitAddress != i.d_bitAddress; } // |d_chunk| is ignored!
299  bool operator() () const { return d_bitAddress != d_capacity; }
300 
301  const value_type& operator* () const { return d_bitAddress; }
302 
303 // manipulators
304  iterator& operator++ ();
305 
306  iterator operator++ (int);
307 
308  void change_owner(const BitMap& b); // adapt |d_chunk| to point into |b|
309 }; // |class BitMap::iterator|
310 
311 } // |namespace bitmap|
312 
313 } // |namespace atlas|
314 
315 #endif
ptrdiff_t difference_type
Definition: bitmap.h:72
BitMap & operator~()
Definition: bitmap.cpp:368
unsigned long d_capacity
Definition: bitmap.h:270
void extend_capacity(bool b)
Definition: bitmap.cpp:559
bool empty() const
Definition: bitmap.cpp:209
BitMap operator^(const BitMap &other) const
Definition: bitmap.h:172
BitMap operator&(const BitMap &other) const
Definition: bitmap.h:166
std::vector< unsigned long >::const_iterator d_chunk
Definition: bitmap.h:268
unsigned long d_bitAddress
Definition: bitmap.h:269
BitMap()
Definition: bitmap.h:82
bool full() const
Definition: bitmap.cpp:240
iterator begin() const
Definition: bitmap.cpp:130
iterator const_iterator
Definition: bitmap.h:76
BitMap(const BitMap &b)
Copy constructor.
Definition: bitmap.h:95
value_type & reference
Definition: bitmap.h:68
uA p
Definition: lists.cpp:26
unsigned long size_type
Definition: bitmap.h:73
void set_capacity(unsigned long n)
Definition: bitmap.cpp:548
void clear(size_t start, size_t stop)
Sets all the bits in positions |i| with |start<=i<stop|.
Definition: bitmap.cpp:511
BitMap(unsigned long n)
Constructs a zero-initialized bitmap with a capacity of n bits.
Definition: bitmap.h:90
bool contains(const BitMap &b) const
Definition: bitmap.cpp:182
ptrdiff_t difference_type
Definition: bitmap.h:277
BitMap & operator^=(const BitMap &)
Definition: bitmap.cpp:412
unsigned long value_type
Definition: bitmap.h:276
bool operator<(const BitMap &b) const
Definition: bitmap.h:127
iterator(const std::vector< unsigned long >::const_iterator &p, unsigned long n, unsigned long c)
Definition: bitmap.h:287
iterator(const iterator &j)
Definition: bitmap.h:284
void set_to(unsigned long n, bool b)
Definition: bitmap.h:213
static unsigned long baseBits
Definition: bitmap.h:57
std::vector< unsigned long > d_map
Definition: bitmap.h:55
const value_type & reference
Definition: bitmap.h:279
unsigned long position(unsigned long n) const
Number of values |<n| present (set) in the bitmap.
Definition: bitmap.cpp:305
bool operator==(const BitMap &b) const
Definition: bitmap.h:128
std::forward_iterator_tag iterator_category
Definition: bitmap.h:275
iterator()
Definition: bitmap.h:282
const value_type * pointer
Definition: bitmap.h:278
void fill()
Definition: bitmap.cpp:484
unsigned long capacity() const
Definition: bitmap.h:124
const value_type & const_reference
Definition: bitmap.h:69
bool operator&=(const BitMap &)
Definition: bitmap.cpp:383
bool operator!=(const BitMap &b) const
Definition: bitmap.h:129
RationalVector< C2 > operator*(const matrix::Matrix< C1 > &M, const RationalVector< C2 > &v)
Definition: ratvec.cpp:158
bool back_up(unsigned long &n) const
Definition: bitmap.cpp:160
size_type size() const
Definition: bitmap.cpp:345
BitMap & operator<<=(unsigned long delta)
Definition: bitmap.cpp:435
RealFormNbr number
Definition: output.cpp:72
void flip(unsigned long n)
Definition: bitmap.h:218
BitMap & andnot(const BitMap &b)
Definition: bitmap.cpp:426
BitMap(unsigned long n, const std::vector< U > &v)
Definition: bitmap.h:103
static unsigned long posBits
Definition: bitmap.h:56
void reset()
Definition: bitmap.h:226
const value_type * const_pointer
Definition: bitmap.h:71
Definition: constants.h:35
void set_mod2(unsigned long n, unsigned long v)
Definition: bitmap.h:216
BitMap & operator>>=(unsigned long delta)
Definition: bitmap.cpp:457
value_type * pointer
Definition: bitmap.h:70
void setRange(unsigned long start, unsigned long amount, unsigned long source)
Definition: bitmap.cpp:574
iterator end() const
returns the past-the-end iterator for the bitmap.
Definition: bitmap.cpp:148
static unsigned long bitMask[longBits]
Definition: constants.h:48
unsigned long n
Definition: axis.cpp:77
BitMap & operator=(const BitMap &)
Definition: bitmap.cpp:114
size_t d_capacity
Definition: bitmap.h:54
Definition: Atlas.h:38
unsigned long front() const
Definition: bitmap.cpp:223
unsigned long value_type
Definition: bitmap.h:67
BitMap operator|(const BitMap &other) const
Definition: bitmap.h:169
unsigned long n_th(unsigned long n) const
Value at index |n| if viewed as list of |unsigned long| values.
Definition: bitmap.cpp:267
static unsigned long baseShift
Definition: bitmap.h:58
Container of a large (more than twice the machine word size) set of bits.
Definition: bitmap.h:52
void swap(BitMap &)
Definition: bitmap.cpp:583
void insert(unsigned long n)
Definition: bitmap.h:197
BitMap & operator|=(const BitMap &)
Definition: bitmap.cpp:400
bool isMember(unsigned long n) const
Definition: bitmap.h:138
Traverses the set bits of a BitMap.
Definition: bitmap.h:266
unsigned long range(unsigned long first, unsigned long number) const
Definition: bitmap.cpp:331
bool disjoint(const BitMap &b) const
Definition: bitmap.cpp:194
Vertex v
Definition: graph.cpp:116