summaryrefslogtreecommitdiff
path: root/util/bitset.c
blob: 6109f9edb5ba7a7c5806be315e2647f4ff827c6e (plain)
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
280
281
282
283
284
285
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <assert.h>
#include <limits.h>
#include <string.h>

#include "util.h"

#define Sizetbits (CHAR_BIT * sizeof(size_t)) /* used in graph reprs */

/* Equalizes the size of a and b by
 * growing the smaller to the size of the
 * larger, zeroing out the new elements.
 * This allows the code to simply iterate
 * over both without keeping track of the
 * minimum size.
 */
static void
eqsz(Bitset *a, Bitset *b)
{
	size_t sz;
	size_t i;
	size_t *p;

	if (a->nchunks > b->nchunks)
		sz = a->nchunks;
	else
		sz = b->nchunks;

	if (a->nchunks != sz) {
		p = zalloc(sz * sizeof(size_t));
		for (i = 0; i < a->nchunks; i++)
			p[i] = a->chunks[i];
		free(a->chunks);
		a->chunks = p;
		a->nchunks = sz;
	}

	if (b->nchunks != sz) {
		p = zalloc(sz * sizeof(size_t));
		for (i = 0; i < b->nchunks; i++)
			p[i] = b->chunks[i];
		free(b->chunks);
		b->chunks = p;
		b->nchunks = sz;
	}
}

/* Creates a new all-zero bit set */
Bitset *
mkbs()
{
	Bitset *bs;

	bs = xalloc(sizeof(Bitset));
	bs->nchunks = 1;
	bs->chunks = zalloc(1 * sizeof(size_t));
	return bs;
}

/* Frees a bitset. Safe to call on NULL. */
void
bsfree(Bitset *bs)
{
	if (!bs)
		return;
	free(bs->chunks);
	free(bs);
}

/* Duplicates a bitset. NULL is duplicated to NULL. */
Bitset *
bsdup(Bitset *a)
{
	Bitset *bs;

	if (!a)
		return NULL;
	bs = xalloc(sizeof(Bitset));
	bs->nchunks = a->nchunks;
	bs->chunks = xalloc(a->nchunks * sizeof(size_t));
	memcpy(bs->chunks, a->chunks, a->nchunks * sizeof(size_t));
	return bs;
}

/* Zeroes all values in a bit set */
Bitset *
bsclear(Bitset *bs)
{
	size_t i;

	if (!bs)
		return mkbs();
	for (i = 0; i < bs->nchunks; i++)
		bs->chunks[i] = 0;
	return bs;
}

/* Counts the number of values held in a bit set */
size_t
bscount(Bitset *bs)
{
	size_t i, j, n;

	n = 0;
	for (i = 0; i < bs->nchunks; i++)
		for (j = 0; j < sizeof(size_t) * CHAR_BIT; j++)
			if (bs->chunks[i] & 1ULL << j)
				n++;
	return n;
}

inline
static int firstbit(size_t b)
{
	int n;
	static const char bits[] = {
		4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0
	};

	n = 0;
	if (!(b & 0xffffffff)) {
		n += 32;
		b >>= 32;
	}
	if (!(b & 0xffff)) {
		n += 16;
		b >>= 16;
	}
	if (!(b & 0xff)) {
		n += 8;
		b >>= 8;
	}
	if (!(b & 0xf)) {
		n += 4;
		b >>= 4;
	}
	n += bits[b & 0xf];
	return n;
}

/* A slightly tricky function to iterate over the content
 * of a bitset. It returns true immediately if 'elt' is in
 * the bitset, otherwise it seeks forward to the next value
 * held in the bitset and stores it in elt. If there are no
 * more values, it returns false to stop iteration. Note,
 * this means that you need to increment elt every time you
 * pass through.
 *
 * Typical usage of this function:
 *
 *	  for (i = 0; bsiter(set, &i); i++)
 *		  use(i);
 *
 * The increment of 'i' in the for loop is needed in order
 * to prevent the function from returning the same value
 * repeatedly.
 */
int
bsiter(Bitset *bs, size_t *elt)
{
	size_t b, t, i;

	i = *elt;
	t = i/Sizetbits;
	if (t >= bs->nchunks)
		return 0;
	b = bs->chunks[t];
	b &= ~((1ull << (i%Sizetbits)) - 1);
	while (!b) {
		++t;
		if (t >= bs->nchunks)
			return 0;
		b = bs->chunks[t];
	}
	*elt = Sizetbits*t + firstbit(b);
	return 1;
}


/* Returns the largest value that the bitset can possibly
 * hold. It's conservative, but scanning the entire bitset
 * is a bit slow. This is mostly an aid to iterate over it. */
size_t
bsmax(Bitset *bs) { return bs->nchunks * Sizetbits; }

void
bsput(Bitset *bs, size_t elt)
{
	size_t sz;
	if (elt >= bs->nchunks * Sizetbits) {
		sz = (elt / Sizetbits) + 1;
		bs->chunks = zrealloc(bs->chunks, bs->nchunks * sizeof(size_t), sz * sizeof(size_t));
		bs->nchunks = sz;
	}
	bs->chunks[elt / Sizetbits] |= 1ULL << (elt % Sizetbits);
}

void
bsdel(Bitset *bs, size_t elt)
{
	if (elt < bs->nchunks * Sizetbits)
		bs->chunks[elt / Sizetbits] &= ~(1ULL << (elt % Sizetbits));
}

void
bsunion(Bitset *a, Bitset *b)
{
	size_t i;

	eqsz(a, b);
	for (i = 0; i < a->nchunks; i++)
		a->chunks[i] |= b->chunks[i];
}

void
bsintersect(Bitset *a, Bitset *b)
{
	size_t i;

	eqsz(a, b);
	for (i = 0; i < a->nchunks; i++)
		a->chunks[i] &= b->chunks[i];
}

ulong
bshash(Bitset *bs)
{
	if (!bs)
		return 14247517;
	else
		return murmurhash2(bs->chunks, bs->nchunks * sizeof(size_t));
}

void
bsdiff(Bitset *a, Bitset *b)
{
	size_t i;

	eqsz(a, b);
	for (i = 0; i < a->nchunks; i++)
		a->chunks[i] &= ~b->chunks[i];
}

int
bseq(Bitset *a, Bitset *b)
{
	size_t i;

	if (!a || !b)
		return bsisempty(a) && bsisempty(b);
	eqsz(a, b);
	for (i = 0; i < a->nchunks; i++) {
		if (a->chunks[i] != b->chunks[i])
			return 0;
	}
	return 1;
}

int
bsissubset(Bitset *set, Bitset *sub)
{
	size_t i;

	eqsz(set, sub);
	for (i = 0; i < set->nchunks; i++)
		if ((sub->chunks[i] & set->chunks[i]) != set->chunks[i])
			return 0;
	return 1;
}

int
bsisempty(Bitset *set)
{
	size_t i;

	if (!set)
		return 1;
	for (i = 0; i < set->nchunks; i++)
		if (set->chunks[i])
			return 0;
	return 1;
}