libcommonc++  0.7
BitSet.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library 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 GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_BitSet_hxx
24 #define __ccxx_BitSet_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/String.h++>
28 
29 #include <iostream>
30 
31 namespace ccxx {
32 
43 {
44  public:
45 
51  {
52  friend class BitSet;
53 
54  public:
55 
57  Bit(const Bit& other);
58 
60  Bit& operator=(const Bit& other);
61 
63  Bit& operator=(bool val);
64 
66  ~Bit();
67 
69  operator bool() const;
70 
72  Bit& flip();
73 
74  private:
75 
76  Bit(BitSet& set, uint_t pos);
77 
78  BitSet& _set;
79  uint_t _pos;
80  };
81 
87  BitSet(uint_t size);
88 
90  BitSet(const BitSet& other);
91 
98  BitSet(const String& bits);
99 
101  virtual ~BitSet();
102 
109  BitSet& set(uint_t pos, bool value = true);
110 
118  BitSet& setRange(uint_t startPos, uint_t endPos, bool value = true);
119 
125  BitSet& setAll(bool value = true);
126 
132  inline BitSet& clear(uint_t pos)
133  { return(set(pos, false)); }
134 
141  inline BitSet& clearRange(uint_t startPos, uint_t endPos)
142  { return(setRange(startPos, endPos, false)); }
143 
145  inline BitSet& clearAll()
146  { return(setAll(false)); }
147 
149  bool isSet(int pos) const;
150 
152  inline bool get(uint_t pos) const
153  { return(isSet(pos)); }
154 
156  inline bool isClear(uint_t pos) const
157  { return(! isSet(pos)); }
158 
160  bool isAnySet() const;
161 
163  bool isAllSet() const;
164 
166  inline bool isAllClear() const
167  { return(! isAnySet()); }
168 
177  inline int nextSetBit(uint_t startPos) const
178  { return(_nextBit(startPos, true)); }
179 
186  inline int firstSetBit() const
187  { return(_nextBit(0, true)); }
188 
197  inline int nextClearBit(uint_t startPos) const
198  { return(_nextBit(startPos, false)); }
199 
206  inline int firstClearBit() const
207  { return(_nextBit(0, false)); }
208 
213  inline bool operator!() const
214  { return(! isAnySet()); }
215 
217  BitSet& flip();
218 
224  BitSet& flip(uint_t pos);
225 
231  void swap(BitSet &other);
232 
240  Bit operator[](int pos);
241 
249  inline bool operator[](int pos) const
250  { return(isSet(pos)); }
251 
253  inline uint_t getSize() const
254  { return(_size); }
255 
260  inline uint_t getWordCount() const
261  { return(_length); }
262 
270  inline uint32_t getWord(uint_t index) const
271  { return(index >= _length ? 0 : _words[index]); }
272 
283  inline uint32_t getWordDiff(uint_t index, uint32_t other) const
284  { return(getWord(index) ^ other); }
285 
293  void setWord(uint_t index, uint32_t word);
294 
296  String toString() const;
297 
299  void write(std::ostream &stream) const;
300 
302  bool operator==(const BitSet& other) const;
303 
305  inline bool operator!=(const BitSet& other) const
306  { return(! operator==(other)); }
307 
309  BitSet& operator=(const BitSet& other);
310 
316  BitSet& operator|=(const BitSet& other);
317 
323  BitSet& operator&=(const BitSet& other);
324 
330  BitSet& operator^=(const BitSet& other);
331 
337  BitSet& operator>>=(uint_t n);
338 
344  BitSet& operator<<=(uint_t n);
345 
352  inline BitSet operator>>(uint_t n) const
353  { return(BitSet(*this) >>= n); }
354 
361  inline BitSet operator<<(uint_t n) const
362  { return(BitSet(*this) <<= n); }
363 
369  inline BitSet operator~() const
370  { return(BitSet(*this).flip()); }
371 
372  private:
373 
374  void _init(uint_t size);
375  int _nextBit(uint_t startPos, bool set) const;
376 
377  uint_t _size;
378  uint_t _length;
379  uint32_t* _words;
380  uint32_t _tailMask;
381 };
382 
383 inline std::ostream& operator<<(std::ostream& stream, const BitSet& bs)
384 {
385  bs.write(stream);
386  return(stream);
387 }
388 
389 } // namespace ccxx
390 
391 #endif // __ccxx_BitSet_hxx
int firstSetBit() const
Find the index of the first bit that is set.
Definition: BitSet.h++:186
bool operator!=(const BitSet &other) const
Inequality operator.
Definition: BitSet.h++:305
void swap(ScopedPtr< T > &a, ScopedPtr< T > &b)
Swap two ScopedPtr values.
Definition: ScopedPtr.h++:133
uint_t getWordCount() const
Get the number of words in the BitSet.
Definition: BitSet.h++:260
int nextSetBit(uint_t startPos) const
Find the index of the next bit that is set, beginning at the given position.
Definition: BitSet.h++:177
BitSet & clearAll()
Clear all of the bits in the BitSet.
Definition: BitSet.h++:145
uint_t getSize() const
Get the size of the BitSet, in bits.
Definition: BitSet.h++:253
bool isClear(uint_t pos) const
Test if a bit at the given position is clear.
Definition: BitSet.h++:156
int nextClearBit(uint_t startPos) const
Find the index of the next bit that is clear, beginning at the given position.
Definition: BitSet.h++:197
uint32_t getWordDiff(uint_t index, uint32_t other) const
"Diff" the word at the specificed index in the BitSet against another word.
Definition: BitSet.h++:283
bool operator==(const Blob &b1, const Blob &b2)
Definition: Blob.h++:344
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
A compact representation of an array of bits (boolean values).
Definition: BitSet.h++:42
BitSet & clearRange(uint_t startPos, uint_t endPos)
Clear the bits in the given (inclusive) range.
Definition: BitSet.h++:141
uint32_t getWord(uint_t index) const
Get the word at the specified index in the BitSet.
Definition: BitSet.h++:270
std::ostream & operator<<(std::ostream &stream, const BitSet &bs)
Definition: BitSet.h++:383
#define COMMONCPP_API
Definition: Common.h++:126
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
A "reference" to a bit within a BitSet.
Definition: BitSet.h++:50
bool operator!() const
Test if all of the bits in the BitSet are cleared.
Definition: BitSet.h++:213
bool operator[](int pos) const
Get the value of a bit at the given position.
Definition: BitSet.h++:249
void write(std::ostream &stream) const
Write a string representation of the BitSet to a stream.
Definition: BitSet.c++:549
BitSet operator<<(uint_t n) const
Bitwise left-shift operator.
Definition: BitSet.h++:361
BitSet operator>>(uint_t n) const
Bitwise right-shift operator.
Definition: BitSet.h++:352
BitSet & clear(uint_t pos)
Clear the bit at the given position.
Definition: BitSet.h++:132
Definition: AllocationMap.c++:25
bool isAllClear() const
Test if all of the bits are clear in the BitSet.
Definition: BitSet.h++:166
BitSet operator~() const
Bitwise NOT operator.
Definition: BitSet.h++:369
int firstClearBit() const
Find the index of the first bit that is clear.
Definition: BitSet.h++:206
BitSet & flip()
Flip (toggle) all of the bits in the BitSet.
Definition: BitSet.c++:292