1 /**
2  * @file
3  * @brief Bit array data type.
4  *
5  * Just contains the operations required by los.cc
6  * for the moment.
7 **/
8 
9 #pragma once
10 
11 #include <bitset>
12 #include <vector>
13 
14 #include "debug.h"
15 #include "defines.h"
16 
17 using std::bitset;
18 using std::vector;
19 
20 class bit_vector
21 {
22 public:
23     bit_vector(unsigned long size = 0);
24     bit_vector(const bit_vector& other);
25     ~bit_vector();
26 
27     void reset();
28 
29     bool get(unsigned long index) const;
30     void set(unsigned long index, bool value = true);
31 
32     bit_vector& operator |= (const bit_vector& other);
33     bit_vector& operator &= (const bit_vector& other);
34     bit_vector  operator & (const bit_vector& other) const;
35 
36 protected:
37     unsigned long size;
38     int nwords;
39     unsigned long *data;
40 };
41 
42 #define LONGSIZE (sizeof(unsigned long)*8)
43 #ifndef ULONG_MAX
44 #define ULONG_MAX ((unsigned long)(-1))
45 #endif
46 
47 template <unsigned int SIZE> class FixedBitVector
48 {
49 protected:
50     bitset<SIZE> data;
51 public:
reset()52     void reset()
53     {
54         data.reset();
55     }
56 
FixedBitVector()57     FixedBitVector()
58     {
59     }
60 
get(unsigned int i)61     inline bool get(unsigned int i) const
62     {
63 #ifdef ASSERTS
64         // printed as signed, as in FixedVector
65         if (i >= SIZE)
66             die("bit vector range error: %d / %u", (int)i, SIZE);
67 #endif
68         return data[i];
69     }
70 
71     inline bool operator[](unsigned int i) const
72     {
73         return get(i);
74     }
75 
76     inline virtual void set(unsigned int i, bool value = true)
77     {
78 #ifdef ASSERTS
79         if (i >= SIZE)
80             die("bit vector range error: %d / %u", (int)i, SIZE);
81 #endif
82         data[i] = value;
83     }
84 
count()85     inline unsigned int count() const
86     {
87         return data.count();
88     }
89 
any()90     inline bool any() const
91     {
92         return data.any();
93     }
94 
95     inline FixedBitVector<SIZE>& operator|=(const FixedBitVector<SIZE>&x)
96     {
97         data |= x.data;
98         return *this;
99     }
100 
101     inline FixedBitVector<SIZE>& operator&=(const FixedBitVector<SIZE>&x)
102     {
103         data &= x.data;
104         return *this;
105     }
106 
init(bool value)107     void init(bool value)
108     {
109         data.reset();
110         if (value)
111             data.flip();
112     }
113 };
114 
115 template <unsigned int SIZEX, unsigned int SIZEY> class FixedBitArray
116 {
117 protected:
118     std::bitset<SIZEX*SIZEY> data;
119 public:
reset()120     void reset()
121     {
122         data.reset();
123     }
124 
init(bool def)125     void init(bool def)
126     {
127         data.reset();
128         if (def)
129             data.flip();
130     }
131 
FixedBitArray()132     FixedBitArray()
133     {
134         reset();
135     }
136 
FixedBitArray(bool def)137     FixedBitArray(bool def)
138     {
139         init(def);
140     }
141 
get(int x,int y)142     inline bool get(int x, int y) const
143     {
144 #ifdef ASSERTS
145         // printed as signed, as in FixedArray
146         if (x < 0 || y < 0 || x >= (int)SIZEX || y >= (int)SIZEY)
147             die("bit array range error: %d,%d / %u,%u", x, y, SIZEX, SIZEY);
148 #endif
149         unsigned int i = y * SIZEX + x;
150         return data[i];
151     }
152 
get(const Indexer & i)153     template<class Indexer> inline bool get(const Indexer &i) const
154     {
155         return get(i.x, i.y);
156     }
157 
operator()158     inline bool operator () (int x, int y) const
159     {
160         return get(x, y);
161     }
162 
operator()163     template<class Indexer> inline bool operator () (const Indexer &i) const
164     {
165         return get(i.x, i.y);
166     }
167 
168     inline void set(int x, int y, bool value = true)
169     {
170 #ifdef ASSERTS
171         if (x < 0 || y < 0 || x >= (int)SIZEX || y >= (int)SIZEY)
172             die("bit array range error: %d,%d / %u,%u", x, y, SIZEX, SIZEY);
173 #endif
174         unsigned int i = y * SIZEX + x;
175         data[i] = value;
176     }
177 
178     template<class Indexer> inline void set(const Indexer &i, bool value = true)
179     {
180         return set(i.x, i.y, value);
181     }
182 
183     inline FixedBitArray<SIZEX, SIZEY>& operator|=(const FixedBitArray<SIZEX, SIZEY>&x)
184     {
185         data |= x.data;
186         return *this;
187     }
188 
189     inline FixedBitArray<SIZEX, SIZEY>& operator&=(const FixedBitArray<SIZEX, SIZEY>&x)
190     {
191         data &= x.data;
192         return *this;
193     }
194 };
195