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
|
// 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.
#ifndef bitSetAsUint64InClass_DEFINED
#define bitSetAsUint64InClass_DEFINED 1
#include "bitset.h"
#include "bitsetasuint64.h"
#include "stdmacros.h"
template<typename Env, typename BitSetTraits>
class BitSetUint64ValueRetType;
template<typename Env, typename BitSetTraits>
class BitSetUint64Iter;
template<typename Env, typename BitSetTraits>
class BitSetUint64
{
public:
typedef BitSetUint64<Env,BitSetTraits> Rep;
private:
friend class BitSetOps</*BitSetType*/BitSetUint64<Env,BitSetTraits>,
/*Brand*/BSUInt64Class,
/*Env*/Env, /*BitSetTraits*/BitSetTraits>;
friend class BitSetUint64ValueRetType<Env, BitSetTraits>;
friend class BitSetUint64Iter<Env, BitSetTraits>;
UINT64 m_bits;
#ifdef DEBUG
unsigned m_epoch;
#endif
typedef BitSetOps<UINT64, BSUInt64, Env, BitSetTraits> Uint64BitSetOps;
void CheckEpoch(Env env) const
{
#ifdef DEBUG
assert(m_epoch == BitSetTraits::GetEpoch(env));
#endif
}
#ifdef DEBUG
// In debug, make sure we don't have any public assignment, by making this private.
BitSetUint64& operator=(const BitSetUint64& bs)
{
m_bits = bs.m_bits;
m_epoch = bs.m_epoch;
return (*this);
}
#endif // DEBUG
bool operator==(const BitSetUint64& bs) const
{
return m_bits == bs.m_bits
#ifdef DEBUG
&& m_epoch == bs.m_epoch
#endif
;
}
// In debug we also want the default copy constructor to be private, to make inadvertent
// default initializations illegal. Debug builds therefore arrange to use the
// non-default constructor defined below that takes an extra argument where one would
// otherwise use a copy constructor. In non-debug builds, we don't pass the extra dummy
// int argument, and just make copy constructor defined here visible.
#ifndef DEBUG
public:
#endif
BitSetUint64(const BitSetUint64& bs) : m_bits(bs.m_bits)
#ifdef DEBUG
, m_epoch(bs.m_epoch)
#endif
{}
#ifdef DEBUG
public:
// But we add a public constructor that's *almost* the default constructor.
BitSetUint64(const BitSetUint64& bs, int xxx) : m_bits(bs.m_bits)
, m_epoch(bs.m_epoch)
{}
#endif
private:
// Return the number of bits set in the BitSet.
inline unsigned Count(Env env) const
{
CheckEpoch(env);
return Uint64BitSetOps::Count(env, m_bits);
}
inline void DiffD(Env env, const BitSetUint64& bs2)
{
CheckEpoch(env);
bs2.CheckEpoch(env);
Uint64BitSetOps::DiffD(env, m_bits, bs2.m_bits);
}
inline BitSetUint64 Diff(Env env, const BitSetUint64& bs2) const
{
CheckEpoch(env);
bs2.CheckEpoch(env);
BitSetUint64 res(*this);
Uint64BitSetOps::DiffD(env, res.m_bits, bs2.m_bits);
return res;
}
inline void RemoveElemD(Env env, unsigned i)
{
CheckEpoch(env);
Uint64BitSetOps::RemoveElemD(env, m_bits, i);
}
inline BitSetUint64 RemoveElem(Env env, unsigned i) const
{
CheckEpoch(env);
BitSetUint64 res(*this);
Uint64BitSetOps::RemoveElemD(env, res.m_bits, i);
return res;
}
inline void AddElemD(Env env, unsigned i)
{
CheckEpoch(env);
Uint64BitSetOps::AddElemD(env, m_bits, i);
}
inline BitSetUint64 AddElem(Env env, unsigned i) const
{
CheckEpoch(env);
BitSetUint64 res(*this);
Uint64BitSetOps::AddElemD(env, res.m_bits, i);
return res;
}
inline bool IsMember(Env env, unsigned i) const
{
CheckEpoch(env);
return Uint64BitSetOps::IsMember(env, m_bits, i);
}
inline void IntersectionD(Env env, const BitSetUint64& bs2)
{
CheckEpoch(env);
bs2.CheckEpoch(env);
m_bits = m_bits & bs2.m_bits;
}
inline BitSetUint64 Intersection(Env env, const BitSetUint64& bs2) const
{
CheckEpoch(env);
bs2.CheckEpoch(env);
BitSetUint64 res(*this);
Uint64BitSetOps::IntersectionD(env, res.m_bits, bs2.m_bits);
return res;
}
inline void UnionD(Env env, const BitSetUint64& bs2)
{
CheckEpoch(env);
bs2.CheckEpoch(env);
Uint64BitSetOps::UnionD(env, m_bits, bs2.m_bits);
}
inline BitSetUint64 Union(Env env, const BitSetUint64& bs2) const
{
CheckEpoch(env);
bs2.CheckEpoch(env);
BitSetUint64 res(*this);
Uint64BitSetOps::UnionD(env, res.m_bits, bs2.m_bits);
return res;
}
inline void ClearD(Env env)
{
// Recall that ClearD does *not* require "*this" to be of the current epoch.
Uint64BitSetOps::ClearD(env, m_bits);
#ifdef DEBUG
// But it updates it to of the current epoch.
m_epoch = BitSetTraits::GetEpoch(env);
#endif
}
inline bool IsEmpty(Env env) const
{
CheckEpoch(env);
return Uint64BitSetOps::IsEmpty(env, m_bits);
}
inline bool IsSubset(Env env, const BitSetUint64& bs2) const
{
CheckEpoch(env);
bs2.CheckEpoch(env);
return Uint64BitSetOps::IsSubset(env, m_bits, bs2.m_bits);
}
inline bool IsEmptyIntersection(Env env, const BitSetUint64& bs2) const
{
CheckEpoch(env);
bs2.CheckEpoch(env);
return Uint64BitSetOps::IsEmptyIntersection(env, m_bits, bs2.m_bits);
}
inline bool Equal(Env env, const BitSetUint64& bs2) const
{
CheckEpoch(env);
bs2.CheckEpoch(env);
return Uint64BitSetOps::Equal(env, m_bits, bs2.m_bits);
}
const char* ToString(Env env) const
{
return Uint64BitSetOps::ToString(env, m_bits);
}
public:
// Uninint
BitSetUint64() :
m_bits(0)
#ifdef DEBUG
, m_epoch(UINT32_MAX) // Undefined.
#endif
{}
BitSetUint64(Env env, bool full = false) :
m_bits(0)
#ifdef DEBUG
, m_epoch(BitSetTraits::GetEpoch(env))
#endif
{
if (full) m_bits = Uint64BitSetOps::MakeFull(env);
}
inline BitSetUint64(const BitSetUint64ValueRetType<Env, BitSetTraits>& rt);
BitSetUint64(Env env, unsigned bitNum) :
m_bits(Uint64BitSetOps::MakeSingleton(env, bitNum))
#ifdef DEBUG
, m_epoch(BitSetTraits::GetEpoch(env))
#endif
{
assert(bitNum < BitSetTraits::GetSize(env));
}
};
template<typename Env, typename BitSetTraits>
class BitSetUint64ValueRetType
{
friend class BitSetUint64<Env, BitSetTraits>;
BitSetUint64<Env, BitSetTraits> m_bs;
public:
BitSetUint64ValueRetType(const BitSetUint64<Env, BitSetTraits>& bs) : m_bs(bs) {}
};
template<typename Env, typename BitSetTraits>
BitSetUint64<Env, BitSetTraits>::BitSetUint64(const BitSetUint64ValueRetType<Env, BitSetTraits>& rt) : m_bits(rt.m_bs.m_bits)
#ifdef DEBUG
, m_epoch(rt.m_bs.m_epoch)
#endif
{}
// You *can* clear a bit after it's been iterated. But you shouldn't otherwise mutate the
// bitset during bit iteration.
template<typename Env, typename BitSetTraits>
class BitSetUint64Iter
{
UINT64 m_bits;
unsigned m_bitNum;
public:
BitSetUint64Iter(Env env, const BitSetUint64<Env, BitSetTraits>& bs): m_bits(bs.m_bits), m_bitNum(0) {}
bool NextElem(Env env, unsigned* pElem)
{
static const unsigned UINT64_SIZE = 64;
if ((m_bits & 0x1) != 0)
{
*pElem = m_bitNum; m_bitNum++; m_bits >>= 1; return true;
}
else
{
// Skip groups of 4 zeros -- an optimization for sparse bitsets.
while (m_bitNum < UINT64_SIZE && (m_bits & 0xf) == 0)
{
m_bitNum += 4; m_bits >>= 4;
}
while (m_bitNum < UINT64_SIZE && (m_bits & 0x1) == 0)
{
m_bitNum += 1; m_bits >>= 1;
}
if (m_bitNum < UINT64_SIZE)
{
*pElem = m_bitNum; m_bitNum++; m_bits >>= 1; return true;
}
else
{
return false;
}
}
}
};
template<typename Env, typename BitSetTraits>
class BitSetOps</*BitSetType*/BitSetUint64<Env,BitSetTraits>,
/*Brand*/BSUInt64Class,
/*Env*/Env, /*BitSetTraits*/BitSetTraits>
{
typedef BitSetUint64<Env,BitSetTraits> BST;
typedef const BitSetUint64<Env,BitSetTraits>& BSTValArg;
typedef BitSetUint64ValueRetType<Env,BitSetTraits> BSTRetVal;
public:
static BSTRetVal UninitVal()
{
return BitSetUint64<Env,BitSetTraits>();
}
static bool MayBeUninit(BSTValArg bs)
{
return bs == UninitVal();
}
static void Assign(Env env, BST& lhs, BSTValArg rhs)
{
lhs = rhs;
}
static void AssignNouninit(Env env, BST& lhs, BSTValArg rhs)
{
lhs = rhs;
}
static void AssignAllowUninitRhs(Env env, BST& lhs, BSTValArg rhs)
{
lhs = rhs;
}
static void AssignNoCopy(Env env, BST& lhs, BSTValArg rhs)
{
lhs = rhs;
}
static void ClearD(Env env, BST& bs)
{
bs.ClearD(env);
}
static BSTRetVal MakeSingleton(Env env, unsigned bitNum)
{
assert(bitNum < BitSetTraits::GetSize(env));
return BST(env, bitNum);
}
static BSTRetVal MakeCopy(Env env, BSTValArg bs)
{
return bs;
}
static bool IsEmpty(Env env, BSTValArg bs)
{
return bs.IsEmpty(env);
}
static unsigned Count(Env env, BSTValArg bs)
{
return bs.Count(env);
}
static void UnionD(Env env, BST& bs1, BSTValArg bs2)
{
bs1.UnionD(env, bs2);
}
static BSTRetVal Union(Env env, BSTValArg bs1, BSTValArg bs2)
{
return bs1.Union(env, bs2);
}
static void DiffD(Env env, BST& bs1, BSTValArg bs2)
{
bs1.DiffD(env, bs2);
}
static BSTRetVal Diff(Env env, BSTValArg bs1, BSTValArg bs2)
{
return bs1.Diff(env, bs2);
}
static void RemoveElemD(Env env, BST& bs1, unsigned i)
{
assert(i < BitSetTraits::GetSize(env));
bs1.RemoveElemD(env, i);
}
static BSTRetVal RemoveElem(Env env, BSTValArg bs1, unsigned i)
{
assert(i < BitSetTraits::GetSize(env));
return bs1.RemoveElem(env, i);
}
static void AddElemD(Env env, BST& bs1, unsigned i)
{
assert(i < BitSetTraits::GetSize(env));
bs1.AddElemD(env, i);
}
static BSTRetVal AddElem(Env env, BSTValArg bs1, unsigned i)
{
assert(i < BitSetTraits::GetSize(env));
return bs1.AddElem(env, i);
}
static bool IsMember(Env env, BSTValArg bs1, unsigned i)
{
assert(i < BitSetTraits::GetSize(env));
return bs1.IsMember(env, i);
}
static void IntersectionD(Env env, BST& bs1, BSTValArg bs2)
{
bs1.IntersectionD(env, bs2);
}
static BSTRetVal Intersection(Env env, BSTValArg bs1, BSTValArg bs2)
{
return bs1.Intersection(env, bs2);
}
static bool IsEmptyIntersection(Env env, BSTValArg bs1, BSTValArg bs2)
{
return bs1.IsEmptyIntersection(env, bs2);
}
static bool IsSubset(Env env, BSTValArg bs1, BSTValArg bs2)
{
return bs1.IsSubset(env, bs2);
}
static bool Equal(Env env, BSTValArg bs1, BSTValArg bs2)
{
return bs1.Equal(env, bs2);
}
static bool NotEqual(Env env, BSTValArg bs1, BSTValArg bs2)
{
return !bs1.Equal(env, bs2);
}
static BSTRetVal MakeEmpty(Env env)
{
return BST(env);
}
static BSTRetVal MakeFull(Env env)
{
return BST(env, /*full*/true);
}
#ifdef DEBUG
static const char* ToString(Env env, BSTValArg bs)
{
return bs.ToString(env);
}
#endif
typedef BitSetUint64Iter<Env, BitSetTraits> Iter;
typedef const BitSetUint64<Env, BitSetTraits>& ValArgType;
typedef BitSetUint64ValueRetType<Env, BitSetTraits> RetValType;
};
#endif // bitSetAsUint64InClass_DEFINED
|