[657] | 1 |
|
---|
| 2 | #ifndef _BITARRAY_H_
|
---|
| 3 | #define _BITARRAY_H_
|
---|
| 4 |
|
---|
| 5 | class BitArray
|
---|
| 6 | {
|
---|
| 7 | private:
|
---|
| 8 | /** The number of bits in this array
|
---|
| 9 | */
|
---|
| 10 | unsigned long arraysize;
|
---|
| 11 | /** The number of unsigned longs for storing at least arraysize bits
|
---|
| 12 | */
|
---|
| 13 | unsigned long bitlongs;
|
---|
| 14 | /** The array of unsigned longs containing the bits
|
---|
| 15 | */
|
---|
| 16 | unsigned long *bits;
|
---|
| 17 | public:
|
---|
| 18 | /** Constructors.
|
---|
| 19 | */
|
---|
| 20 | BitArray(unsigned long newsize);
|
---|
| 21 | BitArray(const BitArray& b);
|
---|
| 22 |
|
---|
| 23 | BitArray(unsigned long newsize, bool setclear);
|
---|
| 24 | BitArray(unsigned long newsize, unsigned long *newbits);
|
---|
| 25 |
|
---|
| 26 | /** Destructor.
|
---|
| 27 | */
|
---|
| 28 | ~BitArray();
|
---|
| 29 |
|
---|
| 30 | BitArray& operator =(const BitArray& b);
|
---|
| 31 | BitArray operator ~(void);
|
---|
| 32 | BitArray& operator ^=(const BitArray& b);
|
---|
| 33 | BitArray& operator &=(const BitArray& b);
|
---|
| 34 | BitArray& operator |=(const BitArray& b);
|
---|
| 35 | BitArray operator ^(const BitArray& b);
|
---|
| 36 | BitArray operator &(const BitArray& b);
|
---|
| 37 | BitArray operator |(const BitArray& b);
|
---|
| 38 |
|
---|
| 39 | /** Test to see if a single bit is set.
|
---|
| 40 | */
|
---|
| 41 | inline bool bitSet(unsigned long index) const
|
---|
| 42 | {
|
---|
| 43 | return bits[(index>>5)] >> (index & 0x0000001f) & 0x00000001;
|
---|
| 44 | }
|
---|
| 45 | /** Clear all bits in this array.
|
---|
| 46 | */
|
---|
| 47 | inline void clear(void)
|
---|
| 48 | {
|
---|
| 49 | fillBitArray(0x00000000);
|
---|
| 50 | }
|
---|
| 51 | /** Clear a single bit.
|
---|
| 52 | */
|
---|
| 53 | inline void clearBit(unsigned long index)
|
---|
| 54 | {
|
---|
| 55 | bits[index >> 5] &= ~(0x00000001 << (index & 0x0000001f));
|
---|
| 56 | }
|
---|
| 57 | /** fill with a 32-bit pattern.
|
---|
| 58 | */
|
---|
| 59 | inline void fillBitArray(unsigned long pattern)
|
---|
| 60 | {
|
---|
| 61 | for (unsigned long i=0; i < bitlongs; bits[i++]=pattern);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /** flip a single bit.
|
---|
| 65 | */
|
---|
| 66 | inline void flipBit(unsigned long index)
|
---|
| 67 | {
|
---|
| 68 | if (bitSet(index))
|
---|
| 69 | clearBit(index);
|
---|
| 70 | else
|
---|
| 71 | setBit(index);
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 | /** Returns index of next set bit in array (wraps around)
|
---|
| 75 | */
|
---|
| 76 | inline long getNextSet(unsigned long index)
|
---|
| 77 | {
|
---|
| 78 | unsigned long i;
|
---|
| 79 | for (i=index+1;i<arraysize;i++) if (bitSet(i)) return i;
|
---|
| 80 | for (i=0;i<index-1;i++) if (bitSet(i)) return i;
|
---|
| 81 | return -1;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /** Returns index of previous set bit in array (wraps around)
|
---|
| 85 | */
|
---|
| 86 | inline long getPreviousSet(unsigned long index)
|
---|
| 87 | {
|
---|
| 88 | unsigned long i;
|
---|
| 89 | if (index != 0)
|
---|
| 90 | {
|
---|
| 91 | for (i=index-1;i>0;i--) if (bitSet(i)) return i;
|
---|
| 92 | if (bitSet(0)) return 0;
|
---|
| 93 | }
|
---|
| 94 | for (i=arraysize-1;i>index;i--) if (bitSet(i)) return i;
|
---|
| 95 | return -1;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /** Set all bits in this array.
|
---|
| 99 | */
|
---|
| 100 | inline void set(void)
|
---|
| 101 | {
|
---|
| 102 | fillBitArray(0xffffffff);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /** Set a single bit.
|
---|
| 106 | */
|
---|
| 107 | inline void setBit(unsigned long index)
|
---|
| 108 | {
|
---|
| 109 | bits[index >> 5] |= 0x00000001 << (index & 0x0000001f);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /** return the number of bits in this bit array..
|
---|
| 113 | */
|
---|
| 114 | inline unsigned long size(void)
|
---|
| 115 | {
|
---|
| 116 | return arraysize;
|
---|
| 117 | }
|
---|
| 118 | };
|
---|
| 119 |
|
---|
| 120 | #endif // _BITARRAY_H_
|
---|
| 121 |
|
---|