-
Notifications
You must be signed in to change notification settings - Fork 26
/
bitvector.c
648 lines (531 loc) · 12 KB
/
bitvector.c
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* Handle bit vector as a run length encoded array of
* 32bit words.
*
* Copyright (C) 2007 Olaf Kirch <[email protected]>
*/
#include <stdlib.h>
#include <string.h>
#include <libisns/isns.h>
#include <libisns/util.h>
struct isns_bitvector {
unsigned int ib_count;
uint32_t * ib_words;
};
void
isns_bitvector_init(isns_bitvector_t *bv)
{
memset(bv, 0, sizeof(*bv));
}
void
isns_bitvector_destroy(isns_bitvector_t *bv)
{
isns_free(bv->ib_words);
memset(bv, 0, sizeof(*bv));
}
isns_bitvector_t *
isns_bitvector_alloc(void)
{
return isns_calloc(1, sizeof(isns_bitvector_t));
}
void
isns_bitvector_free(isns_bitvector_t *bv)
{
if (bv) {
isns_free(bv->ib_words);
memset(bv, 0xa5, sizeof(*bv));
isns_free(bv);
}
}
/*
* Helper function to locate bit
*/
uint32_t *
__isns_bitvector_find_word(const isns_bitvector_t *bv, unsigned int bit)
{
uint32_t *wp, *end;
if (bv->ib_words == NULL)
return NULL;
wp = bv->ib_words;
end = wp + bv->ib_count;
while (wp < end) {
unsigned int base, rlen;
base = wp[0];
rlen = wp[1];
isns_assert(!(base % 32));
if (base <= bit && bit < base + rlen * 32)
return wp + 2 + ((bit - base) / 32);
wp += 2 + rlen;
isns_assert(wp <= end);
}
return NULL;
}
/*
* Insert words in the middle of the array
*/
static inline void
__isns_bitvector_insert_words(isns_bitvector_t *bv,
unsigned int offset, unsigned int count)
{
bv->ib_words = isns_realloc(bv->ib_words,
(bv->ib_count + count) * sizeof(uint32_t));
/* If we insert in the middle, shift out the tail
* to make room for the new range. */
isns_assert(offset <= bv->ib_count);
if (offset < bv->ib_count) {
memmove(bv->ib_words + offset + count,
bv->ib_words + offset,
(bv->ib_count - offset) * sizeof(uint32_t));
}
memset(bv->ib_words + offset, 0, count * sizeof(uint32_t));
bv->ib_count += count;
}
/*
* Insert a new range
*/
static inline uint32_t *
__isns_bitvector_insert_range(isns_bitvector_t *bv,
unsigned int offset, unsigned int base)
{
uint32_t *pos;
__isns_bitvector_insert_words(bv, offset, 3);
pos = bv->ib_words + offset;
*pos++ = base & ~31;
*pos++ = 1;
return pos;
}
/*
* Extend an existing range
* @offset marks the beginning of the existing range.
*/
static inline uint32_t *
__isns_bitvector_extend_range(isns_bitvector_t *bv,
unsigned int offset, unsigned int count)
{
uint32_t *pos, rlen;
/* Find the end of the range */
pos = bv->ib_words + offset;
rlen = pos[1];
__isns_bitvector_insert_words(bv, offset + 2 + rlen, count);
pos = bv->ib_words + offset;
pos[1] += count;
/* Return pointer to the last word of the new range. */
return pos + 2 + rlen + count - 1;
}
/*
* Find a suitable range for insertion
*/
static uint32_t *
__isns_bitvector_find_insert_word(isns_bitvector_t *bv, unsigned int bit)
{
uint32_t *wp, *end;
if (bv->ib_words == NULL)
return __isns_bitvector_insert_range(bv, 0, bit);
wp = bv->ib_words;
end = wp + bv->ib_count;
while (wp < end) {
unsigned int base, rlen, distance;
base = wp[0];
rlen = wp[1];
isns_assert(!(base % 32));
if (bit < base) {
return __isns_bitvector_insert_range(bv,
wp - bv->ib_words, bit);
}
distance = (bit - base) / 32;
if (distance < rlen) {
/* This bit is within range */
return wp + 2 + distance;
}
/* Is it efficient to extend this range?
* The break even point is if we have to add
* 3 words to extend the range, because a new
* range would be at least that much.
*/
if (distance + 1 <= rlen + 3) {
return __isns_bitvector_extend_range(bv,
wp - bv->ib_words,
distance + 1 - rlen);
}
wp += 2 + rlen;
isns_assert(wp <= end);
}
/* No suitable range found. Append one at the end */
return __isns_bitvector_insert_range(bv,
bv->ib_count, bit);
}
/*
* After clearing a bit, check if the bitvector can be
* compacted.
*/
static void
__isns_bitvector_compact(isns_bitvector_t *bv)
{
uint32_t *src, *dst, *end;
unsigned int dst_base = 0, dst_len = 0;
if (bv->ib_words == NULL)
return;
src = dst = bv->ib_words;
end = src + bv->ib_count;
while (src < end) {
unsigned int base, rlen;
base = *src++;
rlen = *src++;
/* Consume leading NUL words */
while (rlen && *src == 0) {
base += 32;
src++;
rlen--;
}
/* Consume trailing NUL words */
while (rlen && src[rlen-1] == 0)
rlen--;
if (rlen != 0) {
if (dst_len && dst_base + 32 * dst_len == base) {
/* We can extend the previous run */
} else {
/* New run. Close off the previous one,
* if we had one. */
if (dst_len != 0) {
dst[0] = dst_base;
dst[1] = dst_len;
dst += 2 + dst_len;
}
dst_base = base;
dst_len = 0;
}
while (rlen--)
dst[2 + dst_len++] = *src++;
}
isns_assert(src <= end);
}
if (dst_len != 0) {
dst[0] = dst_base;
dst[1] = dst_len;
dst += 2 + dst_len;
}
bv->ib_count = dst - bv->ib_words;
if (bv->ib_count == 0)
isns_bitvector_destroy(bv);
}
/*
* Test the value of a single bit
*/
int
isns_bitvector_test_bit(const isns_bitvector_t *bv, unsigned int bit)
{
const uint32_t *pos;
uint32_t mask;
pos = __isns_bitvector_find_word(bv, bit);
if (pos == NULL)
return 0;
mask = 1 << (bit % 32);
return !!(*pos & mask);
}
int
isns_bitvector_clear_bit(isns_bitvector_t *bv, unsigned int bit)
{
uint32_t *pos, oldval, mask;
pos = __isns_bitvector_find_word(bv, bit);
if (pos == NULL)
return 0;
mask = 1 << (bit % 32);
oldval = *pos;
*pos &= ~mask;
__isns_bitvector_compact(bv);
return !!(oldval & mask);
}
int
isns_bitvector_set_bit(isns_bitvector_t *bv, unsigned int bit)
{
uint32_t *pos, oldval = 0, mask;
mask = 1 << (bit % 32);
pos = __isns_bitvector_find_insert_word(bv, bit);
if (pos != NULL) {
oldval = *pos;
*pos |= mask;
return !!(oldval & mask);
}
return 0;
}
int
isns_bitvector_is_empty(const isns_bitvector_t *bv)
{
uint32_t *wp, *end;
if (bv == NULL || bv->ib_count == 0)
return 1;
/* In theory, we should never have a non-compacted
* empty bitvector, as the only way to get one
* is through clear_bit.
* Better safe than sorry...
*/
wp = bv->ib_words;
end = wp + bv->ib_count;
while (wp < end) {
unsigned int rlen;
rlen = wp[1];
wp += 2;
while (rlen--) {
if (*wp++)
return 0;
}
isns_assert(wp <= end);
}
return 1;
}
int
isns_bitvector_intersect(const isns_bitvector_t *a,
const isns_bitvector_t *b,
isns_bitvector_t *result)
{
const uint32_t *runa, *runb, *enda, *endb;
const uint32_t *wpa = NULL, *wpb = NULL;
uint32_t bita = 0, lena = 0, bitb = 0, lenb = 0;
int found = -1;
if (a == NULL || b == NULL)
return -1;
/* Returning the intersect is not implemented yet. */
isns_assert(result == NULL);
runa = a->ib_words;
enda = runa + a->ib_count;
runb = b->ib_words;
endb = runb + b->ib_count;
while (1) {
unsigned int skip;
if (lena == 0) {
next_a:
if (runa >= enda)
break;
bita = *runa++;
lena = *runa++;
wpa = runa;
runa += lena;
lena *= 32;
}
if (lenb == 0) {
next_b:
if (runb >= endb)
break;
bitb = *runb++;
lenb = *runb++;
wpb = runb;
runb += lenb;
lenb *= 32;
}
if (bita < bitb) {
skip = bitb - bita;
/* range A ends before range B starts.
* Proceed to next run in vector A. */
if (skip >= lena)
goto next_a;
bita += skip;
lena -= skip;
wpa += skip / 32;
} else
if (bitb < bita) {
skip = bita - bitb;
/* range B ends before range A starts.
* Proceed to next run in vector B. */
if (skip >= lenb)
goto next_b;
bitb += skip;
lenb -= skip;
wpb += skip / 32;
}
isns_assert(bita == bitb);
while (lena && lenb) {
uint32_t intersect;
intersect = *wpa & *wpb;
if (!intersect)
goto next_word;
/* Find the bit */
if (found < 0) {
uint32_t mask = intersect;
found = bita;
while (!(mask & 1)) {
found++;
mask >>= 1;
}
}
if (result == NULL)
return found;
/* Append to result vector */
/* FIXME: TBD */
next_word:
bita += 32; lena -= 32; wpa++;
bitb += 32; lenb -= 32; wpb++;
}
}
return found;
}
/*
* Iterate over the bit vector
*/
void
isns_bitvector_foreach(const isns_bitvector_t *bv,
int (*cb)(uint32_t, void *),
void *user_data)
{
uint32_t *wp, *end;
wp = bv->ib_words;
end = wp + bv->ib_count;
while (wp < end) {
unsigned int base, rlen;
base = wp[0];
rlen = wp[1];
wp += 2;
while (rlen--) {
uint32_t mask, word;
word = *wp++;
for (mask = 1; mask; mask <<= 1, ++base) {
if (word & mask)
cb(base, user_data);
}
}
isns_assert(wp <= end);
}
}
void
isns_bitvector_dump(const isns_bitvector_t *bv, isns_print_fn_t *fn)
{
uint32_t *wp, *end;
fn("Bit Vector %p (%u words):", bv, bv->ib_count);
wp = bv->ib_words;
end = wp + bv->ib_count;
while (wp < end) {
unsigned int base, rlen;
base = wp[0];
rlen = wp[1];
wp += 2;
fn(" <%u:", base);
while (rlen--)
fn(" 0x%x", *wp++);
fn(">");
isns_assert(wp <= end);
}
if (bv->ib_count == 0)
fn("<empty>");
fn("\n");
}
static inline void
__isns_bitvector_print_next(uint32_t first, uint32_t last,
isns_print_fn_t *fn)
{
switch (last - first) {
case 0:
return;
case 1:
fn(", %u", last);
break;
default:
fn("-%u", last);
break;
}
}
void
isns_bitvector_print(const isns_bitvector_t *bv,
isns_print_fn_t *fn)
{
uint32_t *wp, *end, first = 0, next = 0;
const char *sepa = "";
wp = bv->ib_words;
end = wp + bv->ib_count;
while (wp < end) {
unsigned int base, rlen;
base = wp[0];
rlen = wp[1];
wp += 2;
while (rlen--) {
uint32_t mask, word;
word = *wp++;
for (mask = 1; mask; mask <<= 1, ++base) {
if (word & mask) {
if (next++)
continue;
fn("%s%u", sepa, base);
sepa = ", ";
first = base;
next = base + 1;
} else {
if (next)
__isns_bitvector_print_next(first, next - 1, fn);
first = next = 0;
}
}
}
isns_assert(wp <= end);
}
if (next)
__isns_bitvector_print_next(first, next - 1, fn);
if (*sepa == '\0')
fn("<empty>");
fn("\n");
}
#ifdef TEST
int
main(void)
{
isns_bitvector_t a, b;
int i;
isns_bitvector_init(&a);
isns_bitvector_set_bit(&a, 0);
isns_bitvector_dump(&a, isns_print_stdout);
isns_bitvector_set_bit(&a, 1);
isns_bitvector_set_bit(&a, 16);
isns_bitvector_set_bit(&a, 32);
isns_bitvector_set_bit(&a, 64);
isns_bitvector_dump(&a, isns_print_stdout);
isns_bitvector_set_bit(&a, 8192);
isns_bitvector_set_bit(&a, 8196);
isns_bitvector_set_bit(&a, 8194);
isns_bitvector_dump(&a, isns_print_stdout);
isns_bitvector_set_bit(&a, 2052);
isns_bitvector_set_bit(&a, 2049);
isns_bitvector_set_bit(&a, 2051);
isns_bitvector_set_bit(&a, 2050);
isns_bitvector_dump(&a, isns_print_stdout);
isns_bitvector_print(&a, isns_print_stdout);
isns_bitvector_destroy(&a);
isns_bitvector_init(&a);
for (i = 127; i >= 0; --i)
isns_bitvector_set_bit(&a, i);
isns_bitvector_dump(&a, isns_print_stdout);
printf("[Compacting]\n");
__isns_bitvector_compact(&a);
isns_bitvector_dump(&a, isns_print_stdout);
isns_bitvector_print(&a, isns_print_stdout);
isns_bitvector_destroy(&a);
isns_bitvector_init(&a);
for (i = 0; i < 128; ++i)
isns_bitvector_set_bit(&a, i);
isns_bitvector_dump(&a, isns_print_stdout);
isns_bitvector_print(&a, isns_print_stdout);
isns_bitvector_destroy(&a);
isns_bitvector_init(&a);
isns_bitvector_init(&b);
isns_bitvector_set_bit(&a, 0);
isns_bitvector_set_bit(&a, 77);
isns_bitvector_set_bit(&a, 249);
isns_bitvector_set_bit(&a, 102);
isns_bitvector_set_bit(&b, 1);
isns_bitvector_set_bit(&b, 76);
isns_bitvector_set_bit(&b, 250);
isns_bitvector_set_bit(&b, 102);
i = isns_bitvector_intersect(&a, &b, NULL);
if (i != 102)
fprintf(stderr, "*** BAD: Intersect should return 102 (got %d)! ***\n", i);
else
printf("Intersect okay: %d\n", i);
isns_bitvector_destroy(&a);
isns_bitvector_destroy(&b);
isns_bitvector_init(&a);
isns_bitvector_set_bit(&a, 0);
isns_bitvector_set_bit(&a, 1);
isns_bitvector_clear_bit(&a, 1);
isns_bitvector_clear_bit(&a, 0);
isns_bitvector_dump(&a, isns_print_stdout);
isns_bitvector_print(&a, isns_print_stdout);
isns_bitvector_destroy(&a);
return 0;
}
#endif