-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcode.h
279 lines (234 loc) · 8.16 KB
/
bitcode.h
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* PET
* Platform for Experimentation with efficient HPSG processing Techniques
* (C) 1999 - 2002 Ulrich Callmeier [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** \file bitcode.h
* Class to represent bitvectors of fixed size
* represents set of integers in the interval [ 0 .. sz [
* performance of some operations is critical for efficient glb computation
*
* \todo
* Could try a `zoning' approach here: since most bits are 0, restrict
* operations like subtype to just the region containing 1. Need to
* keep track of left and right limit for this. See #ifdef ZONING
*/
#ifndef _BITCODE_H_
#define _BITCODE_H_
#include <cassert>
#include <cstdio>
#include <iosfwd>
/** \c CODEWORD should be an unsigned numeric type that fits best into a CPU
* register.
*/
typedef unsigned int CODEWORD;
/** Implementation of efficient fixed size bit vectors.
* \attention Most functions assume that all bit vectors are of the same size.
*/
class bitcode {
static const int SIZE_OF_WORD = (8*sizeof(CODEWORD));
inline CODEWORD *end() const { return stop ; }
inline int wordindex(int pos) const { return pos / SIZE_OF_WORD ; }
inline CODEWORD bitmask(int pos) const { return (1 << (pos % SIZE_OF_WORD));}
/** compare this bitcode and \a S2 in lexicographic order
* \return 0 if \a this == \a S2, -1 if \a this < \a S2
* , +1 if \a this < \a S2
*/
int compare(const bitcode &S2) const {
CODEWORD *p, *q;
for(p = V, q = S2.V; p < end() ; ++p, ++q) {
if(*p != *q) {
if (*p < *q) return -1; else return 1;
}
}
return 0;
}
/** Destructive bitwise OR */
bitcode& join(const bitcode& b) {
assert(sz == b.sz);
CODEWORD *p, *q;
for(p = V, q = b.V; p < end(); ++p, ++q) *p |= *q;
return *this;
}
/** Destructive bitwise AND */
bitcode& intersect(const bitcode& b) {
assert(sz == b.sz);
CODEWORD *p, *q;
for(p = V, q = b.V; p < end(); ++p, ++q) *p &= *q;
return *this;
}
CODEWORD *V, *stop;
int sz;
#ifdef ZONING
/** Zoning is not tested. Does it work? */
int first_set, last_set;
#endif
public:
/** Create a bit vector of \a n bits */
bitcode(int n);
/** Clone a bit vector */
bitcode(const bitcode&);
/** Destroy bit vector */
~bitcode() { delete[] V; }
/** Set bit at position \a x to one */
void insert(int x) { V[ wordindex(x) ] |= bitmask(x); }
/** Set bit at position \a x to zero */
void del(int x){ V[ wordindex(x) ] &= ~ bitmask(x); }
/** Return true if the bit at position \a x is set to one */
bool member(int x) const { return ((V[ wordindex(x) ] & bitmask(x)) != 0); }
/** Collect the positions of all bits that are 1 into the result list */
struct list_int *get_elements();
/** Return the index of the last bit in this bitvector (== size() - 1) */
int max() const { return sz - 1; }
/** Return the number of bits in this bitvector */
int size() const { return sz; }
/** Set all bits to zero */
void clear() {
register CODEWORD *p = V;
while (p < end()) *p++ = 0;
}
/** Test if the bitvector only contains zeros */
bool empty() const {
for(CODEWORD *p = V; p < end(); ++p) if(*p != 0) return false;
return true;
}
#ifdef ZONING
void find_relevant_parts() const; // update first_set/last_set
#endif
/** Print bitcode for debugging purposes */
void print(FILE *f) const {
for(CODEWORD *p = V; p < end(); ++p)
fprintf(f, "%.8X", *p);
}
/** @name Serialize/Deserialize bitvector.
* The Serialization is optimized for bitvectors containing sequences of
* empty codewords, which are stored using a run-length encoding.
*
* Plain (naive) bitcode dumping can be used by defining the symbol
* \c NAIVE_BITCODE_DUMP, but the same method has to be used in the undumping
* application then.
*/
/*@{*/
void dump(class dumper *f);
void undump(class dumper *f);
/*@}*/
/** Return \c true if the bitvector \f$ \mbox{this}\wedge a = \mbox{this}\f$,
* i.e., the bits set in this bitvector are a subset of the bits set in \a a
* \pre The bitvectors have to be of equal size for this function to work
* correctly.
*/
bool subset(const bitcode& a);
/** Assignment. Works also for bit vectors of different size. */
bitcode& operator=(const bitcode& S1);
/** Destructive bitwise OR
* \pre The bitvectors have to be of equal size.
*/
bitcode& operator|=(const bitcode& s) { return join(s); }
/** Destructive bitwise AND
* \pre The bitvectors have to be of equal size.
*/
bitcode& operator&=(const bitcode& s) { return intersect(s); }
/** Destructive bitwise NOT */
bitcode& complement(){
for(CODEWORD *p = V; p<end(); ++p) *p = ~(*p);
/* Delete the bits that are not used because they are beyond max()
--p;
*p &= (((CODEWORD) -1) >> SIZE_OF_WORD - (sz % SIZE_OF_WORD))
*/
return *this;
}
/** Non-destructive bitwise OR
* \pre The bitvectors have to be of equal size.
*/
bitcode& operator|(const bitcode& b){
bitcode res(*this);
return res.intersect(b);
}
/** Non-destructive bitwise AND
* \pre The bitvectors have to be of equal size.
*/
bitcode& operator&(const bitcode& b){
bitcode res(*this);
return res.join(b);
}
/** Non-destructive bitwise NOT.
* \pre The bitvectors have to be of equal size.
*/
bitcode operator~(){
bitcode res(*this);
return res.complement();
}
/** Return \c true if this bitvector is bitwise equal to \a T
* \pre The bitvectors have to be of equal size for this function to work
* correctly.
*/
bool operator==(const bitcode& T) const {
CODEWORD *p, *q;
assert(sz == T.sz);
for(p = V, q = T.V; p < end(); ++p, ++q)
if(*p != *q) return 0;
return 1;
}
/** A more efficient hash function for bitcodes in type hierarchies: return
* the number of the first nonzero bit.
*/
friend int Hash(const bitcode& C);
/** Check subset relations between \a A and \a B in parallel and store result
* in \a a and \a b.
* \return \a a is true iff \f$ A \subseteq B \f$ and \a b is true iff \f$ B
* \subseteq A \f$
* \pre The bitvectors have to be of equal size for this function to work
* correctly.
*/
friend void subset_bidir(const bitcode&A, const bitcode &B, bool &a, bool &b);
/** Return true if the bitwise \c and of \a A and \a B is empty, and return
* the result of the \c and operation in \a C.
* \pre All bitvectors have to be of equal size for this function to work
* correctly.
*/
friend bool intersect_empty(const bitcode&A, const bitcode&B, bitcode *C);
/** Print bitcode for debugging purposes */
friend std::ostream& operator<<(std::ostream& O, const bitcode& C);
/** @name Comparison
* Compare bitvectors in reverse lexicographic order
*/
/*@{*/
friend bool operator<(const bitcode &, const bitcode&);
friend bool operator>(const bitcode &, const bitcode&);
/*@}*/
};
/** Print bitcode for debugging purposes */
std::ostream& operator<<(std::ostream& O, const bitcode& C);
/** @name Comparison
* Compare bitvectors in reverse lexicographic order
*/
/*@{*/
inline bool operator<(const bitcode& a, const bitcode &b)
{ return a.compare(b) == -1; }
inline bool operator>(const bitcode& a, const bitcode &b)
{ return a.compare(b) == 1; }
/*@}*/
#include "hashing.h"
#if defined(HAVE_HASH_MAP) || defined(HAVE_EXT_HASH_MAP)
namespace HASH_SPACE {
template<> struct hash<bitcode> {
inline size_t operator()(const bitcode &key) const {
return Hash(key);
}
};
}
#endif
#endif