-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitvec.h
More file actions
179 lines (130 loc) · 3.61 KB
/
bitvec.h
File metadata and controls
179 lines (130 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//========= Copyright © 1996-2002, Valve LLC, All rights reserved. ============
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#ifndef BITVEC_H
#define BITVEC_H
#ifdef _WIN32
#pragma once
#endif
#include <assert.h>
class CBitVecAccessor
{
public:
CBitVecAccessor(unsigned long *pDWords, int iBit);
void operator=(int val);
operator unsigned long();
private:
unsigned long *m_pDWords;
int m_iBit;
};
// CBitVec allows you to store a list of bits and do operations on them like they were
// an atomic type.
template<int NUM_BITS>
class CBitVec
{
public:
CBitVec();
// Set all values to the specified value (0 or 1..)
void Init(int val = 0);
// Access the bits like an array.
CBitVecAccessor operator[](int i);
// Operations on other bit vectors.
CBitVec& operator=(CBitVec<NUM_BITS> const &other);
bool operator==(CBitVec<NUM_BITS> const &other);
bool operator!=(CBitVec<NUM_BITS> const &other);
// Get underlying dword representations of the bits.
int GetNumDWords();
unsigned long GetDWord(int i);
void SetDWord(int i, unsigned long val);
int GetNumBits();
private:
enum {NUM_DWORDS = NUM_BITS/32 + !!(NUM_BITS & 31)};
unsigned long m_DWords[NUM_DWORDS];
};
// ------------------------------------------------------------------------ //
// CBitVecAccessor inlines.
// ------------------------------------------------------------------------ //
inline CBitVecAccessor::CBitVecAccessor(unsigned long *pDWords, int iBit)
{
m_pDWords = pDWords;
m_iBit = iBit;
}
inline void CBitVecAccessor::operator=(int val)
{
if(val)
m_pDWords[m_iBit >> 5] |= (1 << (m_iBit & 31));
else
m_pDWords[m_iBit >> 5] &= ~(unsigned long)(1 << (m_iBit & 31));
}
inline CBitVecAccessor::operator unsigned long()
{
return m_pDWords[m_iBit >> 5] & (1 << (m_iBit & 31));
}
// ------------------------------------------------------------------------ //
// CBitVec inlines.
// ------------------------------------------------------------------------ //
template<int NUM_BITS>
inline int CBitVec<NUM_BITS>::GetNumBits()
{
return NUM_BITS;
}
template<int NUM_BITS>
inline CBitVec<NUM_BITS>::CBitVec()
{
for(int i=0; i < NUM_DWORDS; i++)
m_DWords[i] = 0;
}
template<int NUM_BITS>
inline void CBitVec<NUM_BITS>::Init(int val)
{
for(int i=0; i < GetNumBits(); i++)
{
(*this)[i] = val;
}
}
template<int NUM_BITS>
inline CBitVec<NUM_BITS>& CBitVec<NUM_BITS>::operator=(CBitVec<NUM_BITS> const &other)
{
memcpy(m_DWords, other.m_DWords, sizeof(m_DWords));
return *this;
}
template<int NUM_BITS>
inline CBitVecAccessor CBitVec<NUM_BITS>::operator[](int i)
{
assert(i >= 0 && i < GetNumBits());
return CBitVecAccessor(m_DWords, i);
}
template<int NUM_BITS>
inline bool CBitVec<NUM_BITS>::operator==(CBitVec<NUM_BITS> const &other)
{
for(int i=0; i < NUM_DWORDS; i++)
if(m_DWords[i] != other.m_DWords[i])
return false;
return true;
}
template<int NUM_BITS>
inline bool CBitVec<NUM_BITS>::operator!=(CBitVec<NUM_BITS> const &other)
{
return !(*this == other);
}
template<int NUM_BITS>
inline int CBitVec<NUM_BITS>::GetNumDWords()
{
return NUM_DWORDS;
}
template<int NUM_BITS>
inline unsigned long CBitVec<NUM_BITS>::GetDWord(int i)
{
assert(i >= 0 && i < NUM_DWORDS);
return m_DWords[i];
}
template<int NUM_BITS>
inline void CBitVec<NUM_BITS>::SetDWord(int i, unsigned long val)
{
assert(i >= 0 && i < NUM_DWORDS);
m_DWords[i] = val;
}
#endif // BITVEC_H