forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashbv.cpp
1895 lines (1625 loc) · 46 KB
/
hashbv.cpp
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
// --------------------------------------------------------------------
// --------------------------------------------------------------------
#ifdef DEBUG
void hashBvNode::dump()
{
printf("base: %d { ", baseIndex);
this->foreachBit(pBit);
printf("}\n");
}
#endif // DEBUG
void hashBvNode::Reconstruct(indexType base)
{
baseIndex = base;
assert(!(baseIndex % BITS_PER_NODE));
for (int i=0; i< this->numElements(); i++)
{
elements[i] = 0;
}
next = NULL;
}
hashBvNode::hashBvNode(indexType base)
{
this->Reconstruct(base);
}
hashBvNode *hashBvNode::Create(indexType base, Compiler *compiler)
{
hashBvNode *result = NULL;
if (compiler->hbvGlobalData.hbvNodeFreeList)
{
result = compiler->hbvGlobalData.hbvNodeFreeList;
compiler->hbvGlobalData.hbvNodeFreeList = result->next;
}
else
{
result = new(compiler, CMK_hashBv) hashBvNode;
}
result->Reconstruct(base);
return result;
}
void hashBvNode::freeNode(hashBvGlobalData *glob)
{
this->next = glob->hbvNodeFreeList;
glob->hbvNodeFreeList = this;
}
void hashBvNode::setBit(indexType base)
{
assert(base >= baseIndex);
assert(base - baseIndex < BITS_PER_NODE);
base -= baseIndex;
indexType elem = base / BITS_PER_ELEMENT;
indexType posi = base % BITS_PER_ELEMENT;
elements[elem] |= indexType(1) << posi;
}
void hashBvNode::setLowest(indexType numToSet)
{
assert(numToSet <= BITS_PER_NODE);
int elemIndex = 0;
while (numToSet > BITS_PER_ELEMENT)
{
elements[elemIndex] = ~(elemType(0));
numToSet -= BITS_PER_ELEMENT;
elemIndex++;
}
if (numToSet)
{
elemType allOnes = ~(elemType(0));
int numToShift = (int)(BITS_PER_ELEMENT - numToSet);
elements[elemIndex] = allOnes >> numToShift;
}
}
void hashBvNode::clrBit(indexType base)
{
assert(base >= baseIndex);
assert(base - baseIndex < BITS_PER_NODE);
base -= baseIndex;
indexType elem = base / BITS_PER_ELEMENT;
indexType posi = base % BITS_PER_ELEMENT;
elements[elem] &= ~(indexType(1) << posi);
}
bool hashBvNode::belongsIn(indexType index)
{
if (index < baseIndex)
return false;
if (index >= baseIndex + BITS_PER_NODE)
return false;
return true;
}
int countBitsInWord(unsigned int bits)
{
// In-place adder tree: perform 16 1-bit adds, 8 2-bit adds,
// 4 4-bit adds, 2 8=bit adds, and 1 16-bit add.
bits = ((bits >> 1) & 0x55555555) + (bits & 0x55555555);
bits = ((bits >> 2) & 0x33333333) + (bits & 0x33333333);
bits = ((bits >> 4) & 0x0F0F0F0F) + (bits & 0x0F0F0F0F);
bits = ((bits >> 8) & 0x00FF00FF) + (bits & 0x00FF00FF);
bits = ((bits >>16) & 0x0000FFFF) + (bits & 0x0000FFFF);
return (int) bits;
}
int countBitsInWord(unsigned __int64 bits)
{
bits = ((bits >> 1) & 0x5555555555555555) + (bits & 0x5555555555555555);
bits = ((bits >> 2) & 0x3333333333333333) + (bits & 0x3333333333333333);
bits = ((bits >> 4) & 0x0F0F0F0F0F0F0F0F) + (bits & 0x0F0F0F0F0F0F0F0F);
bits = ((bits >> 8) & 0x00FF00FF00FF00FF) + (bits & 0x00FF00FF00FF00FF);
bits = ((bits >>16) & 0x0000FFFF0000FFFF) + (bits & 0x0000FFFF0000FFFF);
bits = ((bits >>32) & 0x00000000FFFFFFFF) + (bits & 0x00000000FFFFFFFF);
return (int) bits;
}
int hashBvNode::countBits()
{
int result = 0;
for (int i=0; i< this->numElements(); i++)
{
elemType bits = elements[i];
result += countBitsInWord(bits);
result += (int)bits;
}
return result;
}
bool hashBvNode::anyBits()
{
for (int i=0; i< this->numElements(); i++)
{
if (elements[i])
return true;
}
return false;
}
bool hashBvNode::getBit(indexType base)
{
assert(base >= baseIndex);
assert(base - baseIndex < BITS_PER_NODE);
base -= baseIndex;
indexType elem = base / BITS_PER_ELEMENT;
indexType posi = base % BITS_PER_ELEMENT;
if (elements[elem] & (indexType(1) << posi))
return true;
else
return false;
}
bool hashBvNode::anySet()
{
for (int i=0; i< this->numElements(); i++)
{
if (elements[i])
return true;
}
return false;
}
void hashBvNode::copyFrom(hashBvNode *other)
{
this->baseIndex = other->baseIndex;
for (int i=0; i< this->numElements(); i++)
{
this->elements[i] = other->elements[i];
}
}
void hashBvNode::foreachBit(bitAction a)
{
indexType base;
for (int i=0; i< this->numElements(); i++)
{
base = baseIndex + i*BITS_PER_ELEMENT;
elemType e = elements[i];
while (e)
{
if (e&1)
a(base);
e >>= 1;
base++;
}
}
}
elemType hashBvNode::AndWithChange(hashBvNode *other)
{
elemType result = 0;
for (int i=0; i< this->numElements(); i++)
{
elemType src = this->elements[i];
elemType dst;
dst = src & other->elements[i];
result |= src ^ dst;
this->elements[i] = dst;
}
return result;
}
elemType hashBvNode::OrWithChange(hashBvNode *other)
{
elemType result = 0;
for (int i=0; i< this->numElements(); i++)
{
elemType src = this->elements[i];
elemType dst;
dst = src | other->elements[i];
result |= src ^ dst;
this->elements[i] = dst;
}
return result;
}
elemType hashBvNode::XorWithChange(hashBvNode *other)
{
elemType result = 0;
for (int i=0; i< this->numElements(); i++)
{
elemType src = this->elements[i];
elemType dst;
dst = src ^ other->elements[i];
result |= src ^ dst;
this->elements[i] = dst;
}
return result;
}
elemType hashBvNode::SubtractWithChange(hashBvNode *other)
{
elemType result = 0;
for (int i=0; i< this->numElements(); i++)
{
elemType src = this->elements[i];
elemType dst;
dst = src & ~other->elements[i];
result |= src ^ dst;
this->elements[i] = dst;
}
return result;
}
void hashBvNode::AndWith(hashBvNode *other)
{
for (int i=0; i< this->numElements(); i++)
{
this->elements[i] &= other->elements[i];
}
}
void hashBvNode::OrWith(hashBvNode *other)
{
for (int i=0; i< this->numElements(); i++)
{
this->elements[i] |= other->elements[i];
}
}
void hashBvNode::XorWith(hashBvNode *other)
{
for (int i=0; i< this->numElements(); i++)
{
this->elements[i] ^= other->elements[i];
}
}
void hashBvNode::Subtract(hashBvNode *other)
{
for (int i=0; i< this->numElements(); i++)
{
this->elements[i] &= ~other->elements[i];
}
}
bool hashBvNode::sameAs(hashBvNode *other)
{
if (this->baseIndex != other->baseIndex)
return false;
for (int i=0; i<this->numElements(); i++)
{
if (this->elements[i] != other->elements[i])
return false;
}
return true;
}
// --------------------------------------------------------------------
// --------------------------------------------------------------------
hashBv::hashBv(
Compiler *comp
)
{
this->compiler = comp;
this->log2_hashSize = globalData()->hbvHashSizeLog2;
int hts = hashtable_size();
nodeArr = getNewVector(hts);
for (int i=0; i<hts; i++)
{
nodeArr[i] = NULL;
}
this->numNodes = 0;
}
hashBv *hashBv::Create(
Compiler *compiler
)
{
hashBv *result;
hashBvGlobalData *gd = &compiler->hbvGlobalData;
if (hbvFreeList(gd))
{
result = hbvFreeList(gd);
hbvFreeList(gd) = result->next;
assert(result->nodeArr);
}
else
{
result = new(compiler, CMK_hashBv) hashBv(compiler);
memset(result, 0, sizeof(hashBv));
result->nodeArr = result->initialVector;
}
result->compiler = compiler;
result->log2_hashSize = 0;
result->numNodes = 0;
return result;
}
void hashBv::Init(Compiler *compiler)
{
memset(&compiler->hbvGlobalData, 0, sizeof(hashBvGlobalData));
}
hashBvGlobalData *hashBv::globalData()
{
return &compiler->hbvGlobalData;
}
hashBvNode ** hashBv::getNewVector(int vectorLength)
{
assert(vectorLength > 0);
assert(isPow2(vectorLength));
hashBvNode ** newVector = new(compiler, CMK_hashBv) hashBvNode*[vectorLength]();
return newVector;
}
hashBvNode *&hashBv::nodeFreeList(hashBvGlobalData *data)
{
return data->hbvNodeFreeList;
}
hashBv *&hashBv::hbvFreeList(hashBvGlobalData *data)
{
return data->hbvFreeList;
}
void hashBv::freeVector(hashBvNode *vect, int vectorLength)
{
// not enough space to do anything with it
if (vectorLength < 2)
return;
hbvFreeListNode *f = (hbvFreeListNode *) vect;
f->next = globalData()->hbvFreeVectorList;
globalData()->hbvFreeVectorList = f;
f->size = vectorLength;
}
void hashBv::hbvFree()
{
Compiler *comp = this->compiler;
int hts = hashtable_size();
for (int i=0; i<hts; i++)
{
while (nodeArr[i])
{
hashBvNode *curr = nodeArr[i];
nodeArr[i] = curr->next;
curr->freeNode(globalData());
}
}
// keep the vector attached because the whole thing is freelisted
// plus you don't even know if it's freeable
this->next = hbvFreeList(globalData());
hbvFreeList(globalData()) = this;
}
hashBv *hashBv::CreateFrom(hashBv *other, Compiler *comp)
{
hashBv *result = hashBv::Create(comp);
result->copyFrom(other, comp);
return result;
}
void hashBv::MergeLists(hashBvNode **root1, hashBvNode **root2)
{
}
bool hashBv::TooSmall()
{
return this->numNodes > this->hashtable_size() * 4;
}
bool hashBv::TooBig()
{
return this->hashtable_size() > this->numNodes * 4;
}
int hashBv::getNodeCount()
{
int size = hashtable_size();
int result = 0;
for (int i=0; i<size; i++)
{
hashBvNode *last = nodeArr[i];
while (last)
{
last = last->next;
result++;
}
}
return result;
}
bool hashBv::IsValid()
{
int size = hashtable_size();
// is power of 2
assert(((size-1) & size) == 0);
for (int i=0; i<size; i++)
{
hashBvNode *last = nodeArr[i];
hashBvNode *curr;
int lastIndex = -1;
while (last)
{
// the node has been hashed correctly
assert((int)last->baseIndex > lastIndex);
lastIndex = (int) last->baseIndex;
assert(i == getHashForIndex(last->baseIndex, size));
curr = last->next;
// the order is monotonically increasing bases
if (curr)
assert(curr->baseIndex > last->baseIndex);
last = curr;
}
}
return true;
}
void hashBv::Resize()
{
// resize to 'optimal' size
this->Resize(this->numNodes);
}
void hashBv::Resize(int newSize)
{
assert(newSize>0);
newSize = nearest_pow2(newSize);
int oldSize = hashtable_size();
if (newSize == oldSize)
return;
int oldSizeLog2 = log2_hashSize;
int log2_newSize = genLog2((unsigned)newSize);
int size;
hashBvNode ** newNodes = this->getNewVector(newSize);
hashBvNode *** insertionPoints = (hashBvNode ***) alloca(sizeof(hashBvNode *)* newSize);
memset(insertionPoints, 0, sizeof(hashBvNode *)* newSize);
for (int i=0; i<newSize; i++)
{
insertionPoints[i] = &(newNodes[i]);
}
if (newSize > oldSize)
{
// for each src list, expand it into multiple dst lists
for (int i=0; i<oldSize; i++)
{
hashBvNode *next = nodeArr[i];
while (next)
{
hashBvNode *curr = next;
next = curr->next;
int destination = getHashForIndex(curr->baseIndex, newSize);
// ...
// stick the current node on the end of the selected list
*(insertionPoints[destination]) = curr;
insertionPoints[destination] = &(curr->next);
curr->next = NULL;
}
}
nodeArr = newNodes;
log2_hashSize = (unsigned short) log2_newSize;
}
else if (oldSize > newSize)
{
int shrinkFactor = oldSize / newSize;
// shrink multiple lists into one list
// more efficient ways to do this but...
// if the lists are long, you shouldn't be shrinking.
for (int i=0; i<oldSize; i++)
{
hashBvNode *next = nodeArr[i];
if (next)
{
// all nodes in this list should have the same destination list
int destination = getHashForIndex(next->baseIndex, newSize);
hashBvNode ** insertionPoint = &newNodes[destination];
do
{
hashBvNode *curr = next;
// figure out where to insert it
while (*insertionPoint && (*insertionPoint)->baseIndex < curr->baseIndex)
insertionPoint = &((*insertionPoint)->next);
next = curr->next;
hashBvNode *temp = *insertionPoint;
*insertionPoint = curr;
curr->next = temp;
}
while (next);
}
}
nodeArr = newNodes;
log2_hashSize = (unsigned short) log2_newSize;
}
else
{
// same size
assert(oldSize == newSize);
}
assert(this->IsValid());
}
#ifdef DEBUG
void hashBv::dump()
{
bool first = true;
indexType index;
// uncomment to print internal implementation details
//DBEXEC(TRUE, printf("[%d(%d)(nodes:%d)]{ ", hashtable_size(), countBits(), this->numNodes));
printf("{");
FOREACH_HBV_BIT_SET(index, this)
{
if (!first)
printf(" ");
printf("%d", index);
first = false;
}
NEXT_HBV_BIT_SET;
printf("}\n");
}
void hashBv::dumpFancy()
{
indexType index;
indexType last_1 = -1;
indexType last_0 = -1;
printf("{");
printf("count:%d", this->countBits());
FOREACH_HBV_BIT_SET(index, this)
{
if (last_1 != index-1)
{
if (last_0+1 != last_1)
{
printf(" %d-%d", last_0+1, last_1);
}
else
{
printf(" %d", last_1);
}
last_0 = index-1;
}
last_1 = index;
}
NEXT_HBV_BIT_SET;
// Print the last one
if (last_0+1 != last_1)
{
printf(" %d-%d", last_0+1, last_1);
}
else
{
printf(" %d", last_1);
}
printf("}\n");
}
#endif // DEBUG
void hashBv::removeNodeAtBase(indexType index)
{
hashBvNode **insertionPoint =
this->getInsertionPointForIndex(index);
hashBvNode *node = *insertionPoint;
// make sure that we were called to remove something
// that really was there
assert(node);
// splice it out
*insertionPoint = node->next;
this->numNodes--;
}
int hashBv::getHashForIndex(indexType index, int table_size)
{
indexType hashIndex;
hashIndex = index >> LOG2_BITS_PER_NODE;
hashIndex &= (table_size - 1);
return (int) hashIndex;
}
int hashBv::getRehashForIndex(indexType thisIndex, int thisTableSize, int newTableSize)
{
assert(0);
return 0;
}
hashBvNode **hashBv::getInsertionPointForIndex(indexType index)
{
indexType indexInNode;
indexType hashIndex;
indexType baseIndex;
hashBvNode *result;
hashIndex = getHashForIndex(index, hashtable_size());
baseIndex = index & ~(BITS_PER_NODE - 1);
indexInNode = index & (BITS_PER_NODE - 1);
//printf("(%x) : hsh=%x, base=%x, index=%x\n", index,
// hashIndex, baseIndex, indexInNode);
// find the node
hashBvNode **prev = &nodeArr[hashIndex];
result = nodeArr[hashIndex];
while (result)
{
if (result->baseIndex == baseIndex)
{
return prev;
}
else if (result->baseIndex > baseIndex)
{
return prev;
}
else
{
prev = &(result->next);
result = result->next;
}
}
return prev;
}
hashBvNode *hashBv::getNodeForIndexHelper(indexType index, bool canAdd)
{
// determine the base index of the node containing this index
index = index & ~(BITS_PER_NODE - 1);
hashBvNode **prev = getInsertionPointForIndex(index);
hashBvNode *node = *prev;
if (node && node->belongsIn(index))
{
return node;
}
else if (canAdd)
{
// missing node, insert it before the current one
hashBvNode *temp = hashBvNode::Create(index, this->compiler);
temp->next = node;
*prev = temp;
this->numNodes++;
return temp;
}
else
return NULL;
}
hashBvNode *hashBv::getNodeForIndex(indexType index)
{
// determine the base index of the node containing this index
index = index & ~(BITS_PER_NODE - 1);
hashBvNode **prev = getInsertionPointForIndex(index);
hashBvNode *node = *prev;
if (node && node->belongsIn(index))
return node;
else
return NULL;
}
void hashBv::setBit(indexType index)
{
assert(index >= 0);
assert(this->numNodes == this->getNodeCount());
hashBvNode *result = NULL;
indexType baseIndex = index & ~(BITS_PER_NODE - 1);
indexType base = index - baseIndex;
indexType elem = base / BITS_PER_ELEMENT;
indexType posi = base % BITS_PER_ELEMENT;
// this should be the 99% case : when there is only one node in the structure
if ((result = nodeArr[0]) && result->baseIndex == baseIndex)
{
result->elements[elem] |= indexType(1) << posi;
return;
}
result = getOrAddNodeForIndex(index);
result->setBit(index);
assert(this->numNodes == this->getNodeCount());
// if it's getting out of control resize it
if (this->numNodes > this->hashtable_size() * 4)
{
this->Resize();
}
return;
}
void hashBv::setAll(indexType numToSet)
{
// TODO-Throughput: this could be more efficient
for (unsigned int i=0; i<numToSet; i+= BITS_PER_NODE)
{
hashBvNode *node = getOrAddNodeForIndex(i);
indexType bits_to_set = min(BITS_PER_NODE, numToSet - i);
node->setLowest(bits_to_set);
}
}
void hashBv::clearBit(indexType index)
{
assert(index >= 0);
assert(this->numNodes == this->getNodeCount());
hashBvNode *result = NULL;
indexType baseIndex = index & ~(BITS_PER_NODE - 1);
indexType hashIndex = getHashForIndex(index, hashtable_size());
hashBvNode **prev = &nodeArr[hashIndex];
result = nodeArr[hashIndex];
while (result)
{
if (result->baseIndex == baseIndex)
{
result->clrBit(index);
// if nothing left set free it
if (!result->anySet())
{
*prev = result->next;
result->freeNode(globalData());
this->numNodes--;
}
return;
}
else if (result->baseIndex > baseIndex)
{
return;
}
else
{
prev = &(result->next);
result = result->next;
}
}
assert(this->numNodes == this->getNodeCount());
return;
}
bool hashBv::testBit(indexType index)
{
// determine the base index of the node containing this index
indexType baseIndex = index & ~(BITS_PER_NODE - 1);
// 99% case
if (nodeArr[0] && nodeArr[0]->baseIndex == baseIndex)
{
return nodeArr[0]->getBit(index);
}
indexType hashIndex = getHashForIndex(baseIndex, hashtable_size());
hashBvNode *iter = nodeArr[hashIndex];
while (iter)
{
if (iter->baseIndex == baseIndex)
{
return iter->getBit(index);
}
else
{
iter = iter->next;
}
}
return false;
}
int hashBv::countBits()
{
int result = 0;
int hts = this->hashtable_size();
for (int hashNum =0 ; hashNum < hts; hashNum++)
{
hashBvNode *node = nodeArr[hashNum];
while (node)
{
result += node->countBits();
node = node->next;
}
}
return result;
}
bool hashBv::anySet()
{
int result = 0;
int hts = this->hashtable_size();
for (int hashNum =0 ; hashNum < hts; hashNum++)
{
hashBvNode *node = nodeArr[hashNum];
while (node)
{
if (node->anySet())
return true;
node = node->next;
}
}
return false;
}
class AndAction
{
public:
static inline void PreAction(hashBv *lhs, hashBv *rhs)
{
}
static inline void PostAction(hashBv *lhs, hashBv *rhs)
{
}
static inline bool DefaultResult()
{
return false;
}
static inline void LeftGap(hashBv *lhs, hashBvNode **&l, hashBvNode *&r, bool &result, bool &terminate)
{
// it's in other, not this
// so skip it
r = r->next;
}
static inline void RightGap(hashBv *lhs, hashBvNode **&l, hashBvNode *&r, bool &result, bool &terminate)
{
// it's in LHS, not RHS
// so have to remove it
hashBvNode *old = *l;
*l = (*l)->next;
// splice it out
old->freeNode(lhs->globalData());
lhs->numNodes--;
result = true;
}
static inline void BothPresent(hashBv *lhs, hashBvNode **&l, hashBvNode *&r, bool &result, bool &terminate)
{
if ((*l)->AndWithChange(r))
{
r = r->next;
result = true;
if ((*l)->anySet())
{
l = &((*l)->next);
}
else
{
hashBvNode *old = *l;
*l = (*l)->next;
old->freeNode(lhs->globalData());
lhs->numNodes--;
}
}
else
{
r = r->next;
l = &((*l)->next);
}
}
static inline void LeftEmpty(hashBv *lhs, hashBvNode **&l, hashBvNode *&r, bool &result, bool &terminate)
{
r = r->next;
}
};
class SubtractAction
{
public:
static inline void PreAction(hashBv *lhs, hashBv *rhs)
{
}
static inline void PostAction(hashBv *lhs, hashBv *rhs)
{