-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBitFieldTemplate.txt
794 lines (698 loc) · 23.7 KB
/
BitFieldTemplate.txt
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
using System;
#pragma warning disable 649
namespace BitFields
{
/// <summary>
/// Unmanaged ${BITCOUNT} bit struct used to represent a collection of flags.
/// All standard bitwise operators are implemented ( & | ^ ~ << >> )
/// </summary>
public unsafe struct ${TYPE} : IEquatable<${TYPE}>
{
// PROPERTIES AND FIELDS
#region DATA
/// <summary>
/// Number of words that make up the bit field
/// </summary>
public const int wordCount = ${WORDCOUNT};
/// <summary>
/// Number of bits in each word
/// </summary>
public const int wordLength = 32;
/// <summary>
/// Total number of bits stored in this bitfield
/// </summary>
public const int bitCount = wordLength * wordCount;
/// <summary>
/// Unmanaged array of uint32 words that make up the bitfield
/// </summary>
private fixed uint words[wordCount];
#endregion
// PROPERTIES
#region PROPERTIES
/// <summary>
/// The default bitfield (all zeros)
/// </summary>
public static ${TYPE} none => new ${TYPE}();
/// <summary>
/// A new bitfield with all bits set to 1
/// </summary>
public static ${TYPE} all => ~new ${TYPE}();
#endregion
// BIT MANIPULATION
#region BIT_MANIPULATION
/// <summary>
/// Sets a single bit to 1
/// </summary>
///
/// <param name="index">
/// Index of the bit to set. Throws if out of range [0:bitCount].
/// </param>
public void SetBit(int index)
{
IndexInBounds(index);
GetMask(index, out var wordIndex, out var mask);
words[wordIndex] |= mask;
}
/// <summary>
/// Sets a single bit to 0
/// </summary>
///
/// <param name="index">
/// Index of the bit to set. Throws if out of range [0:bitCount].
/// </param>
public void UnsetBit(int index)
{
IndexInBounds(index);
GetMask(index, out var wordIndex, out var mask);
words[wordIndex] &= ~mask;
}
/// <summary>
/// Flips a single bit
/// </summary>
///
/// <param name="index">
/// Index of the bit to flip. Throws if out of range [0:bitCount].
/// </param>
public void FlipBit(int index)
{
IndexInBounds(index);
GetMask(index, out var wordIndex, out var mask);
words[wordIndex] ^= mask;
}
/// <summary>
/// Sets all the bits at the supplied indices 1
/// </summary>
///
/// <param name="indices">
/// Array of bit indices. Throws if any are out of range [0:bitCount].
/// </param>
public void SetBits(params int[] indices)
{
foreach (var i in indices) SetBit(i);
}
/// <summary>
/// Sets all the bits that match a mask to 1 ( bits |= mask )
/// </summary>
///
/// <param name="mask">
/// Mask of bits to set
/// </param>
public void SetBits(in ${TYPE} mask) => Or(in mask);
/// <summary>
/// Sets all the bits that match a mask to 0 ( bits &= ~mask )
/// </summary>
///
/// <param name="mask">
/// Mask of bits to unset
/// </param>
public void UnsetBits(in ${TYPE} mask) => AndNot(in mask);
/// <summary>
/// Sets all the bits at the supplied indices 0
/// </summary>
///
/// <param name="indices">
/// Array of bit indices. Throws if any are out of range [0:bitCount].
/// </param>
public void UnsetBits(params int[] indices)
{
foreach (var i in indices) UnsetBit(i);
}
/// <summary>
/// Flips all the bits that match a mask ( bits ^= mask )
/// </summary>
///
/// <param name="mask">
/// Mask of bits to flip
/// </param>
public void FlipBits(in ${TYPE} mask) => XOr(in mask);
/// <summary>
/// Flips all the bits at the supplied indices
/// </summary>
///
/// <param name="indices">
/// Array of bit indices. Throws if any are out of range [0:bitCount].
/// </param>
public void FlipBits(params int[] indices)
{
foreach (var i in indices) FlipBit( i);
}
#endregion
// QUERIES
#region QUERIES
/// <summary>
/// Gets the state of a single bit
/// </summary>
///
/// <param name="index">
/// Index of the bit. Throws if index out of range [0:bitCount].
/// </param>
public bool GetBit(int index)
{
IndexInBounds(index);
GetMask(index, out var wordIndex, out var mask);
return (words[wordIndex] & mask) == mask;
}
/// <summary>
/// Determines if all bits are set to 0
/// </summary>
public bool IsEmpty()
{
for (var i = 0; i < wordCount; i++)
if (words[i] != 0)
return false;
return true;
}
/// <summary>
/// Determines if ALL of the bits from a given mask are set
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public bool HasAllOf(in ${TYPE} mask) => (this & mask) == mask;
/// <summary>
/// Determines if ANY of the bits from a given mask are set
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public bool HasAnyOf(in ${TYPE} mask) => !(this & mask).IsEmpty();
/// <summary>
/// Determines if NONE of the bits from a given mask are set
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public bool HasNoneOf(in ${TYPE} mask) => (this & mask).IsEmpty();
#endregion
// BOOLEAN OPERATIONS
#region BOOLEAN_OPERATIONS
// And, Not, Or and XOr are the same operations performed element-by-element on the underlying uints
/// <summary>
/// Performs the bitwise boolean operation AND (bits & mask)
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public void And(in ${TYPE} mask)
{
for (var i = 0; i < wordCount; i++)
words[i] &= mask.words[i];
}
/// <summary>
/// Performs the bitwise boolean operation AND-NOT (bits & ~mask)
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public void AndNot(in ${TYPE} mask)
{
for (var i = 0; i < wordCount; i++)
words[i] &= ~mask.words[i];
}
/// <summary>
/// Performs the bitwise boolean operation NAND ~(bits & mask)
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public void Nand(in ${TYPE} mask)
{
for (var i = 0; i < wordCount; i++)
words[i] = ~(words[i] & mask.words[i]);
}
/// <summary>
/// Performs the bitwise boolean operation OR (bits | mask)
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public void Or(in ${TYPE} mask)
{
for (var i = 0; i < wordCount; i++)
words[i] |= mask.words[i];
}
/// <summary>
/// Performs the bitwise boolean operation OR-NOT (bits | ~mask)
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public void OrNot(in ${TYPE} mask)
{
for (var i = 0; i < wordCount; i++)
words[i] |= ~mask.words[i];
}
/// <summary>
/// Performs the bitwise boolean operation NOR ~(bits | mask)
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public void Nor(in ${TYPE} mask)
{
for (var i = 0; i < wordCount; i++)
words[i] = ~(words[i] | mask.words[i]);
}
/// <summary>
/// Performs the bitwise boolean operation XOR (bits ^ mask)
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public void XOr(in ${TYPE} mask)
{
for (var i = 0; i < wordCount; i++)
words[i] ^= mask.words[i];
}
/// <summary>
/// Performs the bitwise boolean operation XOR-NOT (bits ^ ~mask)
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public void XOrNot(in ${TYPE} mask)
{
for (var i = 0; i < wordCount; i++)
words[i] ^= ~mask.words[i];
}
/// <summary>
/// Performs the bitwise boolean operation NOT-XOR ~(bits ^ ~mask)
/// </summary>
///
/// <param name="mask">
/// Query mask
/// </param>
public void NotXOr(in ${TYPE} mask)
{
for (var i = 0; i < wordCount; i++)
words[i] = ~(words[i] ^ mask.words[i]);
}
/// <summary>
/// Performs the bitwise boolean operation NOT (~bits)
/// </summary>
public void Not()
{
for (var i = 0; i < wordCount; i++)
words[i] = ~words[i];
}
/// <summary>
/// Performs the bitwise boolean operation SHIFT
/// </summary>
///
/// <param name="count">
/// The number of bits by which to shift.
/// Positive count shift digits to the right, negative count shift them to the left.
/// </param>
public void Shift(int count)
{
if (count == 0) return;
if (Abs(count) >= bitCount)
{
for (var i = 0; i < wordCount; i++) words[i] = 0;
return;
}
var rightShift = count > 0;
var shiftAmount = Abs(count);
var wholeWordCount = shiftAmount / wordLength;
shiftAmount %= wordLength;
if (shiftAmount != 0)
{
var overflowAmount = wordLength - shiftAmount;
var carryBits = 0u;
if (rightShift)
{
for (var i = wordCount - 1; i >= wholeWordCount; i--)
{
var word = words[i];
words[i] = word >> shiftAmount | carryBits;
carryBits = word << overflowAmount;
}
}
else
{
for (var i = 0; i < wordCount - wholeWordCount; i++)
{
var word = words[i];
words[i] = word << shiftAmount | carryBits;
carryBits = word >> overflowAmount;
}
}
}
if (rightShift)
{
for (var i = 0; i < wordCount; i++)
{
if (i + wholeWordCount < wordCount) words[i] = words[i + wholeWordCount];
else words[i] = 0;
}
}
else
{
for (var i = wordCount - 1; i >= 0; i--)
{
if (i - wholeWordCount >= 0) words[i] = words[i - wholeWordCount];
else words[i] = 0;
}
}
}
#endregion
// OPERATORS
#region OPERATORS
/// <summary>
/// Bitwise AND
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>a & b</returns>
public static ${TYPE} operator &(${TYPE} a, ${TYPE} b)
{
var c = a;
c.And(in b);
return c;
}
/// <summary>
/// Bitwise AND on a single bit
/// </summary>
/// <param name="a">Input bits</param>
/// <param name="index">Bit index . Throws if index out of range [0:bitCount].</param>
/// <returns>a & single index mask</returns>
public static ${TYPE} operator &(${TYPE} a, int index)
{
var c = a;
c.words[index / wordLength] &= 1u << index % wordLength;
return c;
}
/// <summary>
/// Bitwise OR
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>a | b</returns>
public static ${TYPE} operator |(${TYPE} a, ${TYPE} b)
{
var c = a;
c.Or(in b);
return c;
}
/// <summary>
/// Bitwise OR on a single bit
/// Equivalent to SetBit(index)
/// </summary>
/// <param name="a">Input bits</param>
/// <param name="index">Bit index. Throws if index out of range [0:bitCount].</param>
/// <returns>a | single index mask</returns>
public static ${TYPE} operator |(${TYPE} a, int index)
{
var c = a;
c.words[index / wordLength] |= 1u << index % wordLength;
return c;
}
/// <summary>
/// Bitwise XOR
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>a ^ b</returns>
public static ${TYPE} operator ^(${TYPE} a, ${TYPE} b)
{
var c = a;
c.XOr(in b);
return c;
}
/// <summary>
/// Bitwise XOR on a single bit
/// Equivalent to ToggleBit(index)
/// </summary>
/// <param name="a">Input bits</param>
/// <param name="index">Bit index. Throws if index out of range [0:bitCount].</param>
/// <returns>a ^ single index mask</returns>
public static ${TYPE} operator ^(${TYPE} a, int index)
{
var c = a;
c.words[index / wordLength] ^= 1u << index % wordLength;
return c;
}
/// <summary>
/// Bitwise NOT
/// </summary>
/// <param name="a">First operand</param>
/// <returns>~a</returns>
public static ${TYPE} operator ~(${TYPE} a)
{
var c = a;
c.Not();
return c;
}
/// <summary>
/// Bitwise LEFT-SHIFT
/// Shifts the digits of a to the left, d times
/// Negative arguments shift by -d to the right
/// </summary>
/// <param name="a">Input bits</param>
/// <param name="d">Shift amount</param>
/// <returns> a << d</returns>
public static ${TYPE} operator <<(${TYPE} a, int d)
{
var c = a;
c.Shift(-d);
return c;
}
/// <summary>
/// Bitwise RIGHT-SHIFT
/// Shifts the digits of a to the right, d times
/// Negative arguments shift by -d to the left
/// </summary>
/// <param name="a">Input bits</param>
/// <param name="d">Shift amount</param>
/// <returns> a >> d</returns>
public static ${TYPE} operator >> (${TYPE} a, int d)
{
var c = a;
c.Shift(d);
return c;
}
/// <summary>
/// Determines if the bits of a and b are equal
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>a == b</returns>
public static bool operator ==(${TYPE} a, ${TYPE} b) => a.Equals(b);
/// <summary>
/// Determines if the bits of a and b are not equal
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>a != b</returns>
public static bool operator !=(${TYPE} a, ${TYPE} b) => !a.Equals(b);
#endregion
// INDEXER
#region INDEXER
/// <summary>
/// Indexer into the bits for direct setting or querying.
/// </summary>
/// <param name="index">Index of the bit. Throws if not in the range [0:bitCount]</param>
public bool this[int index]
{
get => GetBit(index);
set
{
if (value) SetBit(index);
else UnsetBit(index);
}
}
#endregion
// ENUMERATOR
#region ENUMERATOR
/// <summary>
/// Enumerates the bits of the array from least-significant to most-significant.
/// It is safe to change the array while enumerating.
/// </summary>
public ref struct Enumerator
{
/// <summary>
/// Pointer to the bits
/// </summary>
private uint* _ptr;
/// <summary>
/// Index of the current bit
/// </summary>
private int _index;
/// <summary>
/// Create the enumerator with index at -1
/// </summary>
///
/// <param name="ptr">
/// Bits to enumerate
/// </param>
public Enumerator(uint* ptr)
{
_ptr = ptr;
_index = -1;
}
/// <summary>
/// Move to the next bit
/// </summary>
///
/// <returns>
/// If a bit is available via <see cref="Current"/>. If not, enumeration
/// is done.
/// </returns>
public bool MoveNext()
{
_index++;
if (_index > 0 && _index % wordLength == 0) _ptr++;
return _index < bitCount;
}
/// <summary>
/// Get the current bit. If <see cref="MoveNext"/> has not been called
/// or the last call of <see cref="MoveNext"/> returned false, this
/// function throws.
/// </summary>
///
/// <value>
/// The current bit
/// </value>
public bool Current
{
get
{
IndexInBounds();
var mask = 1u << _index;
return (*_ptr & mask) == mask;
}
}
/// <summary>
/// Bounds check
/// </summary>
private void IndexInBounds()
{
if (_index < 0 && _index >= bitCount)
throw new IndexOutOfRangeException($"Index out of bounds: {_index}");
}
}
/// <summary>
/// Get an enumerator for the bits of this bit-field
/// </summary>
///
/// <returns>
/// An enumerator for the bits of this bit-field
/// </returns>
public Enumerator GetEnumerator()
{
// Safe because Enumerator is a 'ref struct'
fixed (uint* ptr = words) return new Enumerator(ptr);
}
#endregion
// UTILITY
#region UTILITY
/// <summary>
/// Get the positive absolute value of an integer
/// </summary>
private int Abs(int i)
{
if (i >= 0) return i;
return -i;
}
/// <summary>
/// Perform the modulo-remainder operation to decompose a full index index into a word-index and a bit-index.
/// Convert the bit index into a mask.
/// </summary>
private static void GetMask(int index, out int wordIndex, out uint mask)
{
wordIndex = index / wordLength;
mask = 1u << index % wordLength;
}
/// <summary>
/// Bounds check for bit indices. Throws IndexOutOfRangeException when failed.
/// </summary>
private static void IndexInBounds(int index)
{
if (index < 0 || index >= bitCount) throw new IndexOutOfRangeException();
}
/// <summary>
/// Get a string representation of the bit field
/// </summary>
///
/// <returns>
/// A newly-allocated string representing the bits of the array.
/// </returns>
public override string ToString()
{
const string typeName = nameof(${TYPE});
var chars = new char[typeName.Length + bitCount + wordCount + 4];
var i = 0;
for (; i < typeName.Length; ++i) chars[i] = typeName[i];
chars[i++] = ' ';
chars[i++] = '(';
chars[i++] = ' ';
for (var word = wordCount - 1; word >= 0; word--, ++i)
{
for (var digit = wordLength - 1; digit >= 0; digit--, ++i)
{
var mask = 1u << digit;
chars[i] = (words[word] & mask) == mask ? '1' : '0';
}
chars[i] = ' ';
}
chars[i] = ')';
return new string(chars);
}
#endregion
// EQUATABLE
#region EQUATABLE
/// <summary>
/// Check if this object equals another object
/// </summary>
///
/// <param name="obj">
/// Object to check. May be null.
/// </param>
///
/// <returns>
/// True if the given object is a BitArray32 and its bits are the same as this
/// array's bits
/// </returns>
public override bool Equals(object obj) => obj is ${TYPE} other && Equals(other);
/// <summary>
/// Check if this BitField equals another BitField
/// </summary>
///
/// <param name="other">
/// Other BitField.
/// </param>
///
/// <returns>
/// If the given BitField's bits are the same as this BitField's bits
/// </returns>
public bool Equals(${TYPE} other)
{
for (var i = 0; i < wordCount; i++)
if (words[i] != other.words[i])
return false;
return true;
}
/// <summary>
/// Get the hash code of this bitfield (by offsetting and hashing each word)
/// </summary>
public override int GetHashCode()
{
var hash = 17;
// ReSharper disable once NonReadonlyMemberInGetHashCode
for (var i = 0; i < wordCount; i++) hash = 31 * hash + words[i].GetHashCode();
return hash;
}
#endregion
}
}