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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// A set of integers in the range [0..N], for some N defined by the "Env" (via "BitSetTraits").
//
// Represented as a pointer-sized item. If N bits can fit in this item, the representation is "direct"; otherwise,
// the item is a pointer to an array of K size_t's, where K is the number of size_t's necessary to hold N bits.
#ifndef bitSetAsShortLong_DEFINED
#define bitSetAsShortLong_DEFINED 1
#include "bitset.h"
#include "compilerbitsettraits.h"
typedef size_t* BitSetShortLongRep;
template<typename Env, typename BitSetTraits>
class BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>
{
public:
typedef BitSetShortLongRep Rep;
private:
static const unsigned BitsInSizeT = sizeof(size_t) * BitSetSupport::BitsInByte;
inline static bool IsShort(Env env)
{
return BitSetTraits::GetArrSize(env, sizeof(size_t)) <= 1;
}
// The operations on the "long" (pointer-to-array-of-size_t) versions of the representation.
static void AssignLong(Env env, BitSetShortLongRep& lhs, BitSetShortLongRep rhs);
static BitSetShortLongRep MakeSingletonLong(Env env, unsigned bitNum);
static BitSetShortLongRep MakeCopyLong(Env env, BitSetShortLongRep bs);
static bool IsEmptyLong(Env env, BitSetShortLongRep bs);
static unsigned CountLong(Env env, BitSetShortLongRep bs);
static void UnionDLong(Env env, BitSetShortLongRep& bs1, BitSetShortLongRep bs2);
static void DiffDLong(Env env, BitSetShortLongRep& bs1, BitSetShortLongRep bs2);
static void AddElemDLong(Env env, BitSetShortLongRep& bs, unsigned i);
static void RemoveElemDLong(Env env, BitSetShortLongRep& bs, unsigned i);
static void ClearDLong(Env env, BitSetShortLongRep& bs);
static BitSetShortLongRep MakeUninitArrayBits(Env env);
static BitSetShortLongRep MakeEmptyArrayBits(Env env);
static BitSetShortLongRep MakeFullArrayBits(Env env);
static bool IsMemberLong(Env env, BitSetShortLongRep bs, unsigned i);
static bool EqualLong(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2);
static bool IsSubsetLong(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2);
static bool IsEmptyIntersectionLong(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2);
static void IntersectionDLong(Env env, BitSetShortLongRep& bs1, BitSetShortLongRep bs2);
#ifdef DEBUG
static const char* ToStringLong(Env env, BitSetShortLongRep bs);
#endif
public:
inline static BitSetShortLongRep UninitVal()
{
return nullptr;
}
static bool MayBeUninit(BitSetShortLongRep bs)
{
return bs == UninitVal();
}
static void Assign(Env env, BitSetShortLongRep& lhs, BitSetShortLongRep rhs)
{
// We can't assert that rhs != UninitVal in the Short case, because in that
// case it's a legal value.
if (IsShort(env))
{
// Both are short.
lhs = rhs;
}
else if (lhs == UninitVal())
{
assert(rhs != UninitVal());
lhs = MakeCopy(env, rhs);
}
else
{
AssignLong(env, lhs, rhs);
}
}
static void AssignAllowUninitRhs(Env env, BitSetShortLongRep& lhs, BitSetShortLongRep rhs)
{
if (IsShort(env))
{
// Both are short.
lhs = rhs;
}
else if (rhs == UninitVal())
{
lhs = rhs;
}
else if (lhs == UninitVal())
{
lhs = MakeCopy(env, rhs);
}
else
{
AssignLong(env, lhs, rhs);
}
}
static void AssignNoCopy(Env env, BitSetShortLongRep& lhs, BitSetShortLongRep rhs)
{
lhs = rhs;
}
static void ClearD(Env env, BitSetShortLongRep& bs)
{
if (IsShort(env))
{
bs = (BitSetShortLongRep)0;
}
else
{
assert(bs != UninitVal());
ClearDLong(env, bs);
}
}
static BitSetShortLongRep MakeSingleton(Env env, unsigned bitNum)
{
assert(bitNum < BitSetTraits::GetSize(env));
if (IsShort(env))
{
return BitSetShortLongRep(((size_t)1) << bitNum);
}
else
{
return MakeSingletonLong(env, bitNum);
}
}
static BitSetShortLongRep MakeCopy(Env env, BitSetShortLongRep bs)
{
if (IsShort(env))
{
return bs;
}
else
{
return MakeCopyLong(env, bs);
}
}
static bool IsEmpty(Env env, BitSetShortLongRep bs)
{
if (IsShort(env))
{
return bs == nullptr;
}
else
{
assert(bs != UninitVal());
return IsEmptyLong(env, bs);
}
}
static unsigned Count(Env env, BitSetShortLongRep bs)
{
if (IsShort(env))
{
return BitSetSupport::CountBitsInIntegral(size_t(bs));
}
else
{
assert(bs != UninitVal());
return CountLong(env, bs);
}
}
static void UnionD(Env env, BitSetShortLongRep& bs1, BitSetShortLongRep bs2)
{
if (IsShort(env))
{
bs1 = (BitSetShortLongRep)(((size_t)bs1) | ((size_t)bs2));
}
else
{
UnionDLong(env, bs1, bs2);
}
}
static BitSetShortLongRep Union(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2)
{
BitSetShortLongRep res = MakeCopy(env, bs1);
UnionD(env, res, bs2);
return res;
}
static void DiffD(Env env, BitSetShortLongRep& bs1, BitSetShortLongRep bs2)
{
if (IsShort(env))
{
bs1 = (BitSetShortLongRep)(((size_t)bs1) & (~(size_t)bs2));
}
else
{
DiffDLong(env, bs1, bs2);
}
}
static BitSetShortLongRep Diff(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2)
{
BitSetShortLongRep res = MakeCopy(env, bs1);
DiffD(env, res, bs2);
return res;
}
static void RemoveElemD(Env env, BitSetShortLongRep& bs, unsigned i)
{
assert(i < BitSetTraits::GetSize(env));
if (IsShort(env))
{
size_t mask = ((size_t)1) << i;
mask = ~mask;
bs = (BitSetShortLongRep)(((size_t)bs) & mask);
}
else
{
assert(bs != UninitVal());
RemoveElemDLong(env, bs, i);
}
}
static BitSetShortLongRep RemoveElem(Env env, BitSetShortLongRep bs, unsigned i)
{
BitSetShortLongRep res = MakeCopy(env, bs);
RemoveElemD(env, res, i);
return res;
}
static void AddElemD(Env env, BitSetShortLongRep& bs, unsigned i)
{
assert(i < BitSetTraits::GetSize(env));
if (IsShort(env))
{
size_t mask = ((size_t)1) << i;
bs = (BitSetShortLongRep)(((size_t)bs) | mask);
}
else
{
AddElemDLong(env, bs, i);
}
}
static BitSetShortLongRep AddElem(Env env, BitSetShortLongRep bs, unsigned i)
{
BitSetShortLongRep res = MakeCopy(env, bs);
AddElemD(env, res, i);
return res;
}
static bool IsMember(Env env, const BitSetShortLongRep bs, unsigned i)
{
assert(i < BitSetTraits::GetSize(env));
if (IsShort(env))
{
size_t mask = ((size_t)1) << i;
return (((size_t)bs) & mask) != 0;
}
else
{
assert(bs != UninitVal());
return IsMemberLong(env, bs, i);
}
}
static void IntersectionD(Env env, BitSetShortLongRep& bs1, BitSetShortLongRep bs2)
{
if (IsShort(env))
{
(size_t&)bs1 &= (size_t)bs2;
}
else
{
IntersectionDLong(env, bs1, bs2);
}
}
static BitSetShortLongRep Intersection(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2)
{
BitSetShortLongRep res = MakeCopy(env, bs1);
IntersectionD(env, res, bs2);
return res;
}
static bool IsEmptyIntersection(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2)
{
if (IsShort(env))
{
return (((size_t)bs1) & ((size_t)bs2)) == 0;
}
else
{
return IsEmptyIntersectionLong(env, bs1, bs2);
}
}
static bool IsSubset(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2)
{
if (IsShort(env))
{
size_t u1 = (size_t)bs1;
size_t u2 = (size_t)bs2;
return (u1 & u2) == u1;
}
else
{
return IsSubsetLong(env, bs1, bs2);
}
}
static bool Equal(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2)
{
if (IsShort(env))
{
return (size_t)bs1 == (size_t)bs2;
}
else
{
return EqualLong(env, bs1, bs2);
}
}
#ifdef DEBUG
// Returns a string valid until the allocator releases the memory.
static const char* ToString(Env env, BitSetShortLongRep bs)
{
if (IsShort(env))
{
assert(sizeof(BitSetShortLongRep) == sizeof(size_t));
IAllocator* alloc = BitSetTraits::GetDebugOnlyAllocator(env);
const int CharsForSizeT = sizeof(size_t)*2;
char * res = nullptr;
const int ShortAllocSize = CharsForSizeT + 4;
res = (char*)alloc->Alloc(ShortAllocSize);
size_t bits = (size_t)bs;
unsigned remaining = ShortAllocSize;
char* ptr = res;
if (sizeof(size_t) == sizeof(int64_t))
{
sprintf_s(ptr, remaining, "%016llX", bits);
}
else
{
assert(sizeof(size_t) == sizeof(int));
sprintf_s(ptr, remaining, "%08X", bits);
}
return res;
}
else
{
return ToStringLong(env, bs);
}
}
#endif
static BitSetShortLongRep MakeEmpty(Env env)
{
if (IsShort(env))
{
return nullptr;
}
else
{
return MakeEmptyArrayBits(env);
}
}
static BitSetShortLongRep MakeFull(Env env)
{
if (IsShort(env))
{
// Can't just shift by numBits+1, since that might be 32 (and (1 << 32( == 1, for an unsigned).
unsigned numBits = BitSetTraits::GetSize(env);
if (numBits == BitsInSizeT)
{
// Can't use the implementation below to get all 1's...
return BitSetShortLongRep(size_t(-1));
}
else
{
return BitSetShortLongRep((size_t(1) << numBits) - 1);
}
}
else
{
return MakeFullArrayBits(env);
}
}
class Iter
{
BitSetShortLongRep m_bs; // The BitSet that we're iterating over.
size_t m_bits; // The "current" bits remaining to be iterated over.
// In the "short" case, these are all the remaining bits.
// In the "long" case, these are remaining bits in element "m_index";
// these and the bits in the remaining elements comprise the remaining bits.
unsigned m_index; // If "m_bs" uses the long (indirect) representation, the current index in the array.
// the index of the element in A(bs) that is currently being iterated.
unsigned m_bitNum; // The number of bits that have already been iterated over (set or clear). If you
// add this to the bit number of the next bit in "m_bits", you get the proper bit number of that
// bit in "m_bs".
public:
Iter(Env env, const BitSetShortLongRep& bs): m_bs(bs), m_bitNum(0)
{
if (BitSetOps::IsShort(env))
{
m_index = 0;
m_bits = (size_t)bs;
}
else
{
assert(bs != BitSetOps::UninitVal());
m_index = 0;
m_bits = bs[0];
}
}
bool NextElem(Env env, unsigned* pElem)
{
#if BITSET_TRACK_OPCOUNTS
BitSetStaticsImpl::RecordOp(BitSetStaticsImpl::BSOP_NextBit);
#endif
for (;;)
{
DWORD nextBit;
BOOL hasBit;
#ifdef _WIN64
static_assert_no_msg(sizeof(size_t) == 8);
hasBit = BitScanForward64(&nextBit, m_bits);
#else
static_assert_no_msg(sizeof(size_t) == 4);
hasBit = BitScanForward(&nextBit, m_bits);
#endif
// If there's a bit, doesn't matter if we're short or long.
if (hasBit)
{
*pElem = m_bitNum + nextBit;
m_bitNum += nextBit + 1;
m_bits >>= nextBit; m_bits >>= 1; // Have to do these separately -- if we have 0x80000000, nextBit == 31, and shifting by 32 bits does nothing.
return true;
}
else
{
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
if (len <= 1)
{
return false;
}
else
{
m_index++;
if (m_index == len) return false;
// Otherwise...
m_bitNum = m_index * sizeof(size_t) * BitSetSupport::BitsInByte;
m_bits = m_bs[m_index];
continue;
}
}
}
}
};
friend class Iter;
typedef size_t* ValArgType;
typedef size_t* RetValType;
};
template<typename Env, typename BitSetTraits>
void
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
AssignLong(Env env, BitSetShortLongRep& lhs, BitSetShortLongRep rhs)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
for (unsigned i = 0; i < len; i++) lhs[i] = rhs[i];
}
template<typename Env, typename BitSetTraits>
BitSetShortLongRep
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
MakeSingletonLong(Env env, unsigned bitNum)
{
assert(!IsShort(env));
BitSetShortLongRep res = MakeEmptyArrayBits(env);
unsigned index = bitNum/BitsInSizeT;
res[index] = ((size_t)1) << (bitNum % BitsInSizeT);
return res;
}
template<typename Env, typename BitSetTraits>
BitSetShortLongRep
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
MakeCopyLong(Env env, BitSetShortLongRep bs)
{
assert(!IsShort(env));
BitSetShortLongRep res = MakeUninitArrayBits(env);
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
for (unsigned i = 0; i < len; i++) res[i] = bs[i];
return res;
}
template<typename Env, typename BitSetTraits>
bool
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
IsEmptyLong(Env env, BitSetShortLongRep bs)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
for (unsigned i = 0; i < len; i++)
{
if (bs[i] != 0) return false;
}
return true;
}
template<typename Env, typename BitSetTraits>
unsigned
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
CountLong(Env env, BitSetShortLongRep bs)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
unsigned res = 0;
for (unsigned i = 0; i < len; i++)
{
res += BitSetSupport::CountBitsInIntegral(bs[i]);
}
return res;
}
template<typename Env, typename BitSetTraits>
void
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
UnionDLong(Env env, BitSetShortLongRep& bs1, BitSetShortLongRep bs2)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
for (unsigned i = 0; i < len; i++) bs1[i] |= bs2[i];
}
template<typename Env, typename BitSetTraits>
void
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
DiffDLong(Env env, BitSetShortLongRep& bs1, BitSetShortLongRep bs2)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
for (unsigned i = 0; i < len; i++) bs1[i] &= ~bs2[i];
}
template<typename Env, typename BitSetTraits>
void
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
AddElemDLong(Env env, BitSetShortLongRep& bs, unsigned i)
{
assert(!IsShort(env));
unsigned index = i/BitsInSizeT;
size_t mask = ((size_t)1) << (i % BitsInSizeT);
bs[index] |= mask;
}
template<typename Env, typename BitSetTraits>
void
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
RemoveElemDLong(Env env, BitSetShortLongRep& bs, unsigned i)
{
assert(!IsShort(env));
unsigned index = i/BitsInSizeT;
size_t mask = ((size_t)1) << (i % BitsInSizeT);
mask = ~mask;
bs[index] &= mask;
}
template<typename Env, typename BitSetTraits>
void
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
ClearDLong(Env env, BitSetShortLongRep& bs)
{
assert(!IsShort(env));
// Recall that ClearD does *not* require "bs" to be of the current epoch.
// Therefore, we must allocate a new representation.
bs = MakeEmptyArrayBits(env);
}
template<typename Env, typename BitSetTraits>
BitSetShortLongRep
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
MakeUninitArrayBits(Env env)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
assert(len > 1); // Or else would not require an array.
return (BitSetShortLongRep)(BitSetTraits::GetAllocator(env)->Alloc(len*sizeof(size_t)));
}
template<typename Env, typename BitSetTraits>
BitSetShortLongRep
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
MakeEmptyArrayBits(Env env)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
assert(len > 1); // Or else would not require an array.
BitSetShortLongRep res = (BitSetShortLongRep)(BitSetTraits::GetAllocator(env)->Alloc(len*sizeof(size_t)));
for (unsigned i = 0; i < len; i++) res[i] = 0;
return res;
}
template<typename Env, typename BitSetTraits>
BitSetShortLongRep
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
MakeFullArrayBits(Env env)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
assert(len > 1); // Or else would not require an array.
BitSetShortLongRep res = (BitSetShortLongRep)(BitSetTraits::GetAllocator(env)->Alloc(len*sizeof(size_t)));
for (unsigned i = 0; i < len-1; i++) res[i] = size_t(-1);
// Start with all ones, shift in zeros in the last elem.
unsigned lastElemBits = (BitSetTraits::GetSize(env)-1) % BitsInSizeT + 1;
res[len-1] = (size_t(-1) >> (BitsInSizeT - lastElemBits));
return res;
}
template<typename Env, typename BitSetTraits>
bool
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
IsMemberLong(Env env, BitSetShortLongRep bs, unsigned i)
{
assert(!IsShort(env));
unsigned index = i/BitsInSizeT;
unsigned bitInElem = (i % BitsInSizeT);
size_t mask = ((size_t)1) << bitInElem;
return (bs[index] & mask) != 0;
}
template<typename Env, typename BitSetTraits>
void
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
IntersectionDLong(Env env, BitSetShortLongRep& bs1, BitSetShortLongRep bs2)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
for (unsigned i = 0; i < len; i++) bs1[i] &= bs2[i];
}
template<typename Env, typename BitSetTraits>
bool
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
IsEmptyIntersectionLong(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
for (unsigned i = 0; i < len; i++)
{
if ((bs1[i] & bs2[i]) != 0) return false;
}
return true;
}
template<typename Env, typename BitSetTraits>
bool
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
EqualLong(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
for (unsigned i = 0; i < len; i++)
{
if (bs1[i] != bs2[i]) return false;
}
return true;
}
template<typename Env, typename BitSetTraits>
bool
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
IsSubsetLong(Env env, BitSetShortLongRep bs1, BitSetShortLongRep bs2)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
for (unsigned i = 0; i < len; i++)
{
if ((bs1[i] & bs2[i]) != bs1[i]) return false;
}
return true;
}
#ifdef DEBUG
template<typename Env, typename BitSetTraits>
const char*
BitSetOps</*BitSetType*/BitSetShortLongRep,
/*Brand*/BSShortLong,
/*Env*/Env,
/*BitSetTraits*/BitSetTraits>::
ToStringLong(Env env, BitSetShortLongRep bs)
{
assert(!IsShort(env));
unsigned len = BitSetTraits::GetArrSize(env, sizeof(size_t));
const int CharsForSizeT = sizeof(size_t)*2;
unsigned allocSz = len * CharsForSizeT + 4;
unsigned remaining = allocSz;
IAllocator* alloc = BitSetTraits::GetDebugOnlyAllocator(env);
char * res = (char*)alloc->Alloc(allocSz);
char * temp = res;
for (unsigned i = len; 0 < i; i--)
{
size_t bits = bs[i-1];
for (unsigned bytesDone = 0; bytesDone < sizeof(size_t); bytesDone += sizeof(unsigned))
{
unsigned bits0 = (unsigned)bits;
sprintf_s(temp, remaining, "%08X", bits0);
temp += 8;
remaining -= 8;
bytesDone += 4; assert(sizeof(unsigned) == 4);
// Doing this twice by 16, rather than once by 32, avoids warnings when size_t == unsigned.
bits = bits >> 16; bits = bits >> 16;
}
}
return res;
}
#endif
#endif // bitSetAsShortLong_DEFINED
|