-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBzet.h
2629 lines (2255 loc) · 73.9 KB
/
Bzet.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
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* Generic code for Bzet4/Bzet8/Bzet16/Bzet32/Bzet64
*
* Things to do:
* - Good error messages, better error handling in general (?)
* - Performance can be improved by using popcount compiler intrinsics
* instead of manually counting bits
*
* Defines:
*
* NODE_ELS The flavor of Bzet you want to use (e.g. to use Bzet8, define
* NODE_ELS=8 prior to including Bzet.h), default is 8.
*
* STEP_BYTES The number of bytes you would like to allocate to the auxiliary
* step array. For Bzet8+, STEP_BYTES can be either 1 or 2. For
* Bzet4, STEP_BYTES can also be 4. Default is 4 for Bzet4 and
* 2 otherwise. For best performance, use defaults. Change this if
* memory is an issue.
*
* BZET_IMPL_ Define this to also include the implementation. If not defined,
* this is only a header file.
*
* Author: Alex Chow
*****************************************************************************/
#ifndef BZET_H_
#define BZET_H_
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#ifndef NODE_ELS
#define NODE_ELS 8
#endif
//#define USE_LITERAL
#define PASTE_(x,y) x ## y
#define PASTE(x,y) PASTE_(x,y)
#define PASTE_UNDER_(x,y) x ## _ ## y
#define PASTE_UNDER(x,y) PASTE_UNDER_(x,y)
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
#ifndef BZET
#define BZET PASTE(Bzet, NODE_ELS)
#endif
#define POW PASTE(pow, NODE_ELS)
#if NODE_ELS == 64
typedef uint64_t halfnode_t;
#define NPOWERS 6
size_t const PASTE(powersof, NODE_ELS)[NPOWERS] =
{ 1, 64, 4096, 262144, 16777216, 1073741824 };
#elif NODE_ELS == 32
typedef uint32_t halfnode_t;
#define NPOWERS 7
size_t const PASTE(powersof, NODE_ELS)[NPOWERS] =
{ 1, 32, 1024, 32768, 1048576, 33554432, 1073741824 };
#elif NODE_ELS == 16
typedef uint16_t halfnode_t;
#define NPOWERS 8
size_t const PASTE(powersof, NODE_ELS)[NPOWERS] =
{ 1, 16, 256, 4096, 65536, 1048576, 16777216, 268435456 };
#elif NODE_ELS == 8
typedef uint8_t halfnode_t;
#define NPOWERS 11
size_t const PASTE(powersof, NODE_ELS)[NPOWERS] =
{ 1, 8, 64, 512, 4096, 32768, 262144, 2097152, 16777216, 134217728,
1073741824 };
#elif NODE_ELS == 4
typedef uint8_t node_t;
#define NPOWERS 17
size_t const PASTE(powersof, NODE_ELS)[NPOWERS] =
{ 1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304,
16777216, 67108864, 268435456, 1073741824};
#else
#error "Invalid NODE_ELS provided"
#endif
#ifndef STEP_BYTES
#if NODE_ELS == 4
#define STEP_BYTES 4
#else
#define STEP_BYTES 2
#endif
#endif
#if (STEP_BYTES == 4 && NODE_ELS == 4)
typedef uint32_t step_t;
#elif STEP_BYTES == 2
typedef uint16_t step_t;
#elif STEP_BYTES == 1
typedef uint8_t step_t;
#else
#error "Invalid STEP_BYTES provided"
#endif
typedef uint8_t byte_t;
typedef int64_t bitidx_t;
#define STEP_T_MAX ((size_t) 1 << (STEP_BYTES * 8))
#define INITIAL_ALLOC 1024
#define RESIZE_SCALE 2
enum OP {
OP_0000, OP_0001, OP_0010, OP_0011, OP_0100, OP_0101, OP_0110, OP_0111,
OP_1000, OP_1001, OP_1010, OP_1011, OP_1100, OP_1101, OP_1110, OP_1111,
OP_AND = 1, OP_XOR = 6, OP_OR = 7, OP_NOR = 8, OP_NOT = 10, OP_NAND = 14 };
enum ACTION { DA0, DA1, DB0, DB1, CA, CB, NA, NB };
#ifdef USE_LITERAL
enum NODETYPE { SATURATED, EMPTY, NORMAL, LITERAL };
#else
enum NODETYPE { SATURATED, EMPTY, NORMAL };
#endif
static const ACTION optable[16][4] = {
{ DB0, DA0, DB0, DA0 }, //00 0000 FALSE Result
{ DB0, DA0, CB, CA }, //01 0001 AND |
{ CB, CA, NB, DA0 }, //02 0010 A<-B |
{ DB0, CA, DB0, CA }, //03 0011 A V
{ DB0, DA0, DB0, CA }, //04 0100 ????
{ CB, DA0, CB, DA0 }, //05 0101 B
{ CB, CA, NB, NA }, //06 0110 XOR
{ CB, CA, DB1, DA1 }, //07 0111 OR
{ NB, NA, DB0, DA0 }, //08 1000 NOR
{ NB, NA, CB, CA }, //09 1001 EQ
{ NB, DA0, NB, DA0 }, //10 1010 ~B
{ CB, CA, CB, CA }, //11 1011 ????
{ DB0, NA, DB0, NA }, //12 1100 ~A
{ DB0, NA, CB, CA }, //13 1101 A->B
{ DB1, DA1, NB, NA }, //14 1110 NAND
{ DB1, DA1, DB1, DA1 } }; //15 1111 TRUE
// 0T T0 1T T1
// 0 1 2 3
// Bzet class
class BZET {
public:
BZET();
BZET(bitidx_t bit);
BZET(bitidx_t startbit, bitidx_t len);
BZET(const BZET& b);
~BZET();
BZET& operator=(const BZET& right);
BZET operator~() const;
BZET operator|(BZET& right);
BZET operator&(BZET& right);
BZET operator^(BZET& right);
bool operator==(const BZET& right) const;
static BZET binop(BZET& left, BZET& right, OP op);
bool at(bitidx_t bit) const;
void setRange(bitidx_t start, bitidx_t len);
void set(bitidx_t bit);
void seqset(bitidx_t bit);
void unset(bitidx_t bit);
int depth() const;
size_t size() const;
static void align(BZET& b1, BZET& b2);
void normalize();
void clear();
void hex(void* str) const;
void printBzet(int stdOffset = 0, FILE* target = stdout) const;
void exportTo(FILE* stream) const;
void importFrom(FILE* stream, size_t size);
bitidx_t firstBit() const;
bitidx_t lastBit() const;
bitidx_t count() const;
bitidx_t getBits(bitidx_t* bits, bitidx_t limit = 0, bitidx_t start = 0);
bool empty() const;
// DEBUG ONLY
void printStep() {
#if NODE_ELS == 4
for (size_t i = 0; i < m_nnodes; i++) {
#else
for (size_t i = 0; i < m_nhalfnodes; i++) {
#endif
if ((i % 10) == 0)
printf("\n");
printf("0x%.*X ", (unsigned int) sizeof(step_t) * 2,
(step_t) m_step[i]);
//printf("%d ", m_step[i]);
}
printf("\n");
}
private:
#ifdef USE_LITERAL
void bits_to_bzet(byte_t* bits, size_t size, Bzet& b);
//BZET_PTR bitstobzet(void *data, size_t len);
//void treetobits(byte_t *buf, halfnode_t *node, int depth);
#endif
NODETYPE _binop(BZET& left, BZET& right, OP op, int lev,
size_t left_loc = 0, size_t right_loc = 0);
void _printBzet(int stdOffset, FILE* target, int depth, size_t loc = 0,
int offset = 0, bool pad = 0) const;
bitidx_t _count(size_t loc, int depth) const;
void subtree_not(size_t loc, int depth);
static NODETYPE _seqset(BZET& b, size_t locb, BZET& right,
size_t locright, int depth);
static bool _at(BZET& b, size_t locb, BZET& right, size_t locright,
int depth);
size_t build_step(size_t loc, int depth);
size_t step_through(size_t loc, int depth) const;
// Inline auxiliary functions
static void display_error(const char* message, bool fatal = false,
FILE* output = stderr);
void init(size_t initial_alloc = INITIAL_ALLOC);
#if NODE_ELS == 4
void resize(size_t nnodes);
#else
void resize(size_t nhalfnodes);
#endif
static size_t POW(int n);
static int do_data_op(OP op, int left_data_bit, int right_data_bit);
void append_subtree(BZET& src, size_t loc, int depth);
void set_step(size_t loc, int depth);
//void mod_step(size_t loc, int depth, int diff);
#if NODE_ELS == 4
size_t m_nbufnodes;
size_t m_nnodes;
node_t* m_bzet;
#else
size_t m_nbufhalfnodes;
size_t m_nhalfnodes;
halfnode_t* m_bzet; //points to the bzet
#endif
step_t* m_step; //points to an array that holds step_through values
byte_t m_depth;
};
// Inline auxiliary functions
// Error message printing and optional exiting
inline
void BZET::display_error(const char* message, bool fatal, FILE* output) {
fprintf(output, "%s\n", message);
if (fatal) {
exit(1);
}
}
// Common Bzet constructor initialization
// TODO: Add error messages
inline
void BZET::init(size_t initial_alloc) {
// Allocate bzet node array
#if NODE_ELS == 4
m_bzet = (node_t *) malloc(initial_alloc * sizeof(node_t));
#else
m_bzet = (halfnode_t *) malloc(initial_alloc * sizeof(halfnode_t));
#endif
if (!m_bzet) {
display_error("init malloc failed\n", true);
}
// Allocate step array
m_step = (step_t *) malloc(initial_alloc * sizeof(step_t));
if (!m_step) {
display_error("init malloc failed\n", true);
free(m_bzet);
}
m_depth = 0;
#if NODE_ELS == 4
m_nnodes = 0;
m_nbufnodes = INITIAL_ALLOC;
#else
m_nhalfnodes = 0;
m_nbufhalfnodes = INITIAL_ALLOC;
#endif
}
// Resizes the buffers in the Bzet if necessary
inline
#if NODE_ELS == 4
void BZET::resize(size_t nnodes) {
// If reallocation is required
if (nnodes > m_nbufnodes) {
// Get new size required by growing it by RESIZE_SCALE repeatedly
while (nnodes > m_nbufnodes)
m_nbufnodes *= RESIZE_SCALE;
// Reallocate bzet
node_t *bzet_temp = (node_t *) realloc(m_bzet, m_nbufnodes * sizeof(node_t));
// Reallocate step
step_t *step_temp = (step_t *) realloc(m_step, m_nbufnodes * sizeof(step_t));
// Check that realloc succeeded
// TODO: Replace with better checking above
if (!m_bzet || !m_step) {
fprintf(stderr, "Fatal error: Resizing bzet failed attempting to allocate %zu bytes\n", m_nbufnodes * sizeof(node_t));
display_error("", true);
}
m_bzet = bzet_temp;
m_step = step_temp;
}
// Update internal halfnode counter
m_nnodes = nnodes;
}
#else
void BZET::resize(size_t nhalfnodes) {
// If reallocation is required
if (nhalfnodes > m_nbufhalfnodes) {
// Get new size required by growing it by RESIZE_SCALE repeatedly
while (nhalfnodes > m_nbufhalfnodes)
m_nbufhalfnodes *= RESIZE_SCALE;
// Reallocate bzet
halfnode_t *bzet_temp = (halfnode_t *) realloc(m_bzet, m_nbufhalfnodes * sizeof(halfnode_t));
// Reallocate step
step_t *step_temp = (step_t *) realloc(m_step, m_nbufhalfnodes * sizeof(step_t));
// Check that realloc succeeded
// TODO: Replace with better checking above
if (!m_bzet || !m_step) {
fprintf(stderr, "Fatal error: Resizing bzet failed attempting to allocate %zu bytes\n", (m_nbufhalfnodes * sizeof(halfnode_t)));
display_error("", true);
}
m_bzet = bzet_temp;
m_step = step_temp;
}
// Update internal halfnode counter
m_nhalfnodes = nhalfnodes;
}
#endif
// Trusty fast pow8/pow16/pow32 (assumes n >= 0)
inline
size_t BZET::POW(int n) {
// Lookup power in table if possible
if (n < NPOWERS)
return PASTE(powersof, NODE_ELS)[n];
return (size_t) pow((double) NODE_ELS, n);
}
// Does operations for two data bits, used in Bzet_binop
inline
int BZET::do_data_op(OP op, int left_data_bit, int right_data_bit) {
// Use op directly to build result bit
return (op >> (3 - (((int) left_data_bit << 1) | (int) right_data_bit))) & 0x1;
}
// Append a subtree from src to dest starting at loc in src
inline
void BZET::append_subtree(BZET& src, size_t loc, int depth) {
// Calculate copy size and cache copy destination
size_t copy_size = src.step_through(loc, depth) - loc;
#if NODE_ELS == 4
size_t dst_loc = m_nnodes;
// Resize to accomodate copy_size new elements
resize(m_nnodes + copy_size);
// Do copy
memcpy(m_bzet + dst_loc, src.m_bzet + loc, copy_size * sizeof(node_t));
memcpy(m_step + dst_loc, src.m_step + loc, copy_size * sizeof(step_t));
#else
size_t dst_loc = m_nhalfnodes;
// Resize to accomodate copy_size new elements
resize(m_nhalfnodes + copy_size);
// Do copy
memcpy(m_bzet + dst_loc, src.m_bzet + loc, copy_size * sizeof(halfnode_t));
memcpy(m_step + dst_loc, src.m_step + loc, copy_size * sizeof(step_t));
#endif
}
// Build step at loc
inline
void BZET::set_step(size_t loc, int depth) {
#if NODE_ELS == 4
if (depth == 1) {
m_step[loc] = 2;
return;
}
size_t locdiff = m_nnodes - loc;
if (locdiff >= STEP_T_MAX)
m_step[loc] = 0;
else
m_step[loc] = (step_t) locdiff;
#else
if (depth == 0) {
m_step[loc] = 1;
return;
}
size_t locdiff = m_nhalfnodes - loc;
if (locdiff >= STEP_T_MAX) {
if (locdiff <= STEP_T_MAX * STEP_T_MAX - 1) {
// MSB first
m_step[loc] = (step_t) (locdiff / STEP_T_MAX);
m_step[loc + 1] = (step_t) (locdiff % STEP_T_MAX);
}
else {
m_step[loc] = 0;
m_step[loc + 1] = 0;
}
}
else {
m_step[loc] = 0;
m_step[loc + 1] = (step_t) locdiff;
}
#endif
}
/*inline
void BZET::mod_step(size_t loc, int depth, int diff) {
if (depth == 0)
return;
size_t step = m_step[loc] * STEP_T_MAX + m_step[loc + 1];
step -= diff;
m_step[loc] = (step_t) (step / STEP_T_MAX);
m_step[loc + 1] = (step_t) (step % STEP_T_MAX);
}*/
#ifdef BZET_IMPL_
// -- CONSTRUCTORS / DESTRUCTORS --
// Default constructor. Creates an empty bzet.
BZET::BZET() {
init();
}
// Creates a bzet with bit bit set
BZET::BZET(bitidx_t bit) {
if (bit < 0) {
display_error("bit < 0 in Bzet_new(bit)\n", true);
}
init();
#if NODE_ELS == 4
// Build depth
int depth = 1;
bitidx_t tempbit = bit;
// Level 1 holds 16 bits, numbers 0 to 15
while (tempbit > 15) {
tempbit /= (bitidx_t) NODE_ELS;
++depth;
}
// Resize to accomodate full bzet
// +1 since level 1 requires 2 bytes to store data literals
resize(depth + 1);
//set depth bit
m_depth = (byte_t) depth;
// Set data nodes at level 1
// Set bits containing level 0 nodes 0 and 1 (bits 0-7)
m_bzet[depth - 1] = (node_t) 0x80 >> (bit & 0xF);
// Set bits containing level 0 nodes 2 and 3 (bits 8-15) if previous node not set
m_bzet[depth] = (m_bzet[depth - 1]) ? 0x00 : (node_t) 0x80 >> (bit & 0x7);
// Since level 1 set, divide by 16 to see if tree nodes needed
bit >>= 4;
// Set tree nodes if needed
int loc = depth - 1;
while (bit > 0) {
m_bzet[--loc] = (node_t) 0x08 >> (bit & 0x3);
bit >>= 2;
}
// Set m_step
// No need to check m_size <= 255 to make sure m_step values doesn't overflow
// Since it would never happen (4^255 ~ 10^153)
for (unsigned int i = 0; i < m_nnodes; ++i)
m_step[i] = (step_t) (m_nnodes - i);
#else
// Build depth
int depth = 0;
while (POW(depth + 1) <= (size_t) bit)
depth++;
// Resize to accomodate full bzet
resize(2*depth + 1);
//set depth byte
m_depth = (byte_t) depth;
// Set level 0 data node
// Each depth contains 2 halfnodes, b->bzet[2*depth] is the data node
m_bzet[2*depth] = (halfnode_t) 0x1 << (NODE_ELS - 1 - (bit % NODE_ELS));
// Zero out other nodes, since we will be only filling the tree portions next
memset(m_bzet, 0x00, 2*depth * sizeof(halfnode_t));
// Set tree bits, working linearly from the node at the head of the bzet
for (int i = depth; i >= 1; i--) {
// Get weight of bits at this level
size_t cpow = POW(i);
// Calculate tree bit to set
int setbit = (int) (bit / cpow) % NODE_ELS;
// Set the tree bit
m_bzet[2*(depth - i) + 1] = (halfnode_t) 0x1 << (NODE_ELS - 1 - setbit);
}
// Set b->step
// No need to check b->size <= 255 to make sure b->step values doesn't
// overflow since it would never happen (4^255 ~ 10^153)
for (unsigned int i = 0; i < m_nhalfnodes; i += 2) {
m_step[i] = 0;
m_step[i + 1] = (step_t) (m_nhalfnodes - i);
}
m_step[m_nhalfnodes - 1] = 1;
#endif
}
// Range constructor
BZET::BZET(bitidx_t startbit, bitidx_t len) {
if (len <= 0 || startbit < 0)
display_error("len <= 0 || startbit < 0", true);
init();
setRange(startbit, len);
}
// Copy constructor
BZET::BZET(const BZET& b) {
// Copy contents over
m_depth = b.m_depth;
#if NODE_ELS == 4
m_nbufnodes = b.m_nbufnodes;
m_nnodes = b.m_nnodes;
// Deep copy of bzet and step
m_bzet = (node_t *) malloc(m_nbufnodes * sizeof(node_t));
if (!m_bzet) {
display_error("copy malloc failed\n", true);
}
m_step = (step_t *) malloc(m_nbufnodes * sizeof(step_t));
if (!m_step) {
display_error("copy malloc failed\n", true);
free(m_bzet);
}
memcpy(m_bzet, b.m_bzet, m_nnodes * sizeof(node_t));
memcpy(m_step, b.m_step, m_nnodes * sizeof(step_t));
#else
m_nbufhalfnodes = b.m_nbufhalfnodes;
m_nhalfnodes = b.m_nhalfnodes;
// Deep copy of bzet and step
m_bzet = (halfnode_t *) malloc(m_nbufhalfnodes * sizeof(halfnode_t));
if (!m_bzet) {
display_error("copy malloc failed\n", true);
}
m_step = (step_t *) malloc(m_nbufhalfnodes * sizeof(step_t));
if (!m_step) {
display_error("copy malloc failed\n", true);
free(m_bzet);
}
memcpy(m_bzet, b.m_bzet, m_nhalfnodes * sizeof(halfnode_t));
memcpy(m_step, b.m_step, m_nhalfnodes * sizeof(step_t));
#endif
}
// Destructor
BZET::~BZET() {
free(m_bzet);
free(m_step);
}
// -- OPERATIONS ---
// Assignment operator
BZET& BZET::operator=(const BZET& right) {
#if NODE_ELS == 4
// Resize left's bzet and step buffers
resize(right.m_nnodes);
// Copy contents of bzet over
m_depth = right.m_depth;
memcpy(m_bzet, right.m_bzet, m_nnodes * sizeof(node_t));
memcpy(m_step, right.m_step, m_nnodes * sizeof(step_t));
#else
// Resize left's bzet and step buffers
resize(right.m_nhalfnodes);
// Copy contents of bzet over
m_depth = right.m_depth;
memcpy(m_bzet, right.m_bzet, m_nhalfnodes * sizeof(halfnode_t));
memcpy(m_step, right.m_step, m_nhalfnodes * sizeof(step_t));
#endif
return *this;
}
// Bitwise NOT
BZET BZET::operator~() const {
// Clone this bzet
BZET copy(*this);
// NOT it in place
copy.subtree_not(0, copy.m_depth);
return copy;
}
// Bitwise OR
BZET BZET::operator|(BZET& right) {
// If left bzet is empty, the bitwise OR will be equal to the right bzet
if (empty())
return BZET(right);
// If right bzet is empty, the bitwise OR will be equal to the left bzet
else if (right.empty())
return BZET(*this);
// Otherwise just operate on them
else {
return binop(*((BZET *) this), right, OP_OR);
}
}
// Bitwise AND
BZET BZET::operator&(BZET& right) {
// If either bzet is empty, the bitwise AND will be an empty bzet
if (empty() || right.empty())
return BZET();
// Otherwise just operate on them
else {
return binop(*((BZET *) this), right, OP_AND);
}
}
// Bitwise XOR
BZET BZET::operator^(BZET& right) {
// If left bzet is empty, the bitwise XOR will be equal to the right bzet
if (empty())
return BZET(right);
// If right bzet is empty, the bitwise XOR will be equal to the left bzet
else if (right.empty())
return BZET(*this);
// Otherwise just operate on them
else {
return binop(*((BZET *) this), right, OP_XOR);
}
}
// Comparison operator
bool BZET::operator==(const BZET& right) const {
if (m_depth != right.m_depth ||
#if NODE_ELS == 4
m_nnodes != right.m_nnodes ||
memcmp(m_bzet, right.m_bzet, m_nnodes * sizeof(node_t)))
#else
m_nhalfnodes != right.m_nhalfnodes ||
memcmp(m_bzet, right.m_bzet, m_nhalfnodes * sizeof(halfnode_t)))
#endif
return false;
return true;
}
// Generic binary operation
BZET BZET::binop(BZET& left, BZET& right, OP op) {
// Make result bzet
BZET result;
// Align both bzets
align(left, right);
// Set depth
result.m_depth = left.m_depth;
// Operate
result._binop(left, right, op, left.m_depth);
// Normalize all bzets
result.normalize();
left.normalize();
right.normalize();
return result;
}
// -- BIT OPERATIONS --
// Test if a bit is set
bool BZET::at(bitidx_t bit) const {
// Test if a bit is set by ANDing the bzet with a bzet with only bit set
// and checking if the result is empty
// *this & Bzet(bit)
if (empty())
return false;
BZET temp(bit);
align(*((BZET *) this), temp);
return _at(*((BZET *) this), 0, temp, 0, m_depth);
//return !(*((BZET*) this) & temp).empty();
}
// Set a range of bits
void BZET::setRange(bitidx_t start, bitidx_t len) {
// Create bzet mask
BZET mask;
for (bitidx_t i = start; i < start + len; i++)
mask.set(i);
// OR the mask into the original bzet
*this = *this | mask;
}
// Set a bit
void BZET::set(bitidx_t bit) {
// Set a bit by ORing a bzet with bit set into b
// b | Bzet(bit)
if (empty())
*this = BZET(bit);
BZET temp(bit);
// Make result bzet
BZET result;
// Align both bzets
align(*this, temp);
result.m_depth = m_depth;
// Operate
result._binop(*this, temp, OP_OR, m_depth);
// Normalize
result.normalize();
// Shallow copy result to *this
#if NODE_ELS == 4
m_nnodes = result.m_nnodes;
m_nbufnodes = result.m_nbufnodes;
m_depth = result.m_depth;
#else
m_nhalfnodes = result.m_nhalfnodes;
m_nbufhalfnodes = result.m_nbufhalfnodes;
m_depth = result.m_depth;
#endif
free(m_bzet);
free(m_step);
m_bzet = result.m_bzet;
m_step = result.m_step;
result.m_bzet = NULL;
result.m_step = NULL;
}
// Sequential set, bit must be greater than the last bit set in the bzet
void BZET::seqset(bitidx_t bit) {
if (empty())
*this = BZET(bit);
else {
BZET temp(bit);
align(*this, temp);
_seqset(*this, 0, temp, 0, m_depth);
normalize();
}
}
// Unset a bit
void BZET::unset(bitidx_t bit) {
// Set a bit by ANDing b with the negation of a bzet with bit set
// b & ~Bzet(bit)
BZET nb = BZET(bit);
align(*this, nb);
nb = ~nb;
BZET result;
// Operate
result._binop(*this, nb, OP_AND, m_depth);
// Shallow copy result to *this
#if NODE_ELS == 4
m_nnodes = result.m_nnodes;
m_nbufnodes = result.m_nbufnodes;
#else
m_nhalfnodes = result.m_nhalfnodes;
m_nbufhalfnodes = result.m_nbufhalfnodes;
#endif
free(m_bzet);
free(m_step);
m_bzet = result.m_bzet;
m_step = result.m_step;
result.m_bzet = NULL;
result.m_step = NULL;
// Normalize now instead of normalizing immediately after the operation.
// For some reason that doesn't work.
normalize();
}
// Get the first bit set
// TODO: Add support for bit literal subtrees
bitidx_t BZET::firstBit() const {
if (empty())
return -1;
#if NODE_ELS == 4
int level = m_depth;
bitidx_t bit = 0;
size_t loc = 0;
// Move through the tree to find the first bit
while (true) {
// Special calculation at level 1
if (level == 1) {
// Check data nodes 0 and 1
node_t c = m_bzet[loc];
// If any bit is set in node 0 and 1, first bit found
if (c)
for (int i = 7; i >= 0; i--)
if ((c >> i) & 1)
return bit + 7 - (bitidx_t) i;
// If no data bit set in first 2 nodes, check next 2 nodes
c = m_bzet[loc + 1];
for (int i = 7; i >= 0; i--)
if ((c >> i) & 1)
return bit + 15 - i;
}
// Calculation for all other nodes
byte_t data_bits = (m_bzet[loc] >> NODE_ELS) & 0xF;
byte_t tree_bits = m_bzet[loc] & 0xF;
// Check each data bit then the corresponding tree bit
for (int i = NODE_ELS - 1; i >= 0; i--) {
// Check data bit
// If on, return the lowest bit
if ((data_bits >> i) & 1)
return bit + (NODE_ELS - 1 - i) * pow4(level);
// Check tree bit
if ((tree_bits >> i) & 1) {
// If on, advance bitBase to the correct prefix and check that node
bit += (NODE_ELS - 1 - i) * pow4(level);
break;
}
}
level--;
loc++;
}
#else
bitidx_t bit = 0;
int depth = m_depth;
size_t loc = 0;
// Move through the tree to find the first bit
while (true) {
halfnode_t node_data = m_bzet[loc];
// At level 0 nodes, first bit will be here somewhere
if (depth == 0) {
for (int i = NODE_ELS - 1; i >= 0; i--)
if ((node_data >> i) & 1)
return bit + ((NODE_ELS - 1) - i);
}
// For all other nodes, look for first data bit
halfnode_t node_tree = m_bzet[loc + 1];
for (int i = NODE_ELS - 1; i >= 0; i--) {
int data_bit = (node_data >> i) & 1;
int tree_bit = (node_tree >> i) & 1;
// If data bit set, first bit found
if (data_bit) {
return bit + ((NODE_ELS - 1) - i) * POW(depth);
}
// If tree bit is on, break out and examine subtree for first bit
else if (tree_bit) {
// Add weight of empty subtrees before this element as bit offset
bit += ((NODE_ELS - 1) - i) * POW(depth);
break;
}
}
// Move on to subtree
depth--;
loc += 2;
}
#endif
}
// Bzet_LAST(b)
// TODO: Add support for bit literal subtrees
bitidx_t BZET::lastBit() const {
if (empty())
return -1;
#if NODE_ELS == 4
// TODO: Fix lastBit() for Bzet4
size_t loc = 0;
int level = m_depth;
bitidx_t bit = 0;
do {
// Special calculation at level 1
if (level == 1) {
// Check data nodes 2 and 3
node_t c = m_bzet[loc + 1];
if (c)
for (int i = 0; i < 8; ++i)
if ((c >> i) & 1)
return bit + 15 - i;
// If no data bit set in last 2 nodes, check first 2
c = m_bzet[loc];
for (int i = 0; i < 8; ++i)
if ((c >> i) & 1)
return bit + 7 - i;
}
node_t c = m_bzet[loc];
node_t data_bits = (c >> NODE_ELS) & 0xF;
node_t tree_bits = c & 0xF;
// Get last tree and data bit
int last_tree_bit = 0;
int last_data_bit = 0;
for (int i = NODE_ELS - 1; i >= 0; i--) {
if ((tree_bits >> i) & 1)
last_tree_bit = NODE_ELS - 1 - i;
if ((data_bits >> i) & 1)
last_data_bit = NODE_ELS - 1 - i;
}
// If this node has the farthest data bit set
// If so, last bit found
if (last_data_bit > last_tree_bit) {
return bit + (last_data_bit + 1) * pow4(level) - 1;
}
// If no tree bits are on, we're at the end
if (!tree_bits) {
//get the location of the last data bit
int last_data_bit = 0;
for (int i = NODE_ELS - 1; i >= 0; i--)
if ((data_bits >> i) & 1)
last_data_bit = NODE_ELS - 1 - i;
return bit + (last_data_bit + 1) * pow4(level) - 1;
}
// Otherwise step through the nodes until you get to the node
// corresponding to the last tree bit on in the current node
++loc;
for (int i = NODE_ELS - 1; i > NODE_ELS - 1 - last_tree_bit; i--)
if ((tree_bits >> i) & 1)
loc = step_through(loc, level - 1);
bit += last_tree_bit * pow4(level);
// Loc now points to location of the node corresponding to the last
// tree bit, so repeat this process
level--;
} while (loc < m_nnodes);