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