A game about forced loneliness, made by TACStudios
1using UnityEngine.InputSystem.Utilities;
2
3namespace UnityEngine.InputSystem
4{
5 /// <summary>
6 /// Struct replacement for System.Collections.Bitfield.
7 /// </summary>
8 /// <remarks>
9 /// We don't want the extra heap object just for keeping the header
10 /// state of the bitfield. This struct directly embeds the header
11 /// into the owner. Also doesn't allocate any array while length is
12 /// less than or equal to 64 bits.
13 /// </remarks>
14 internal struct DynamicBitfield
15 {
16 public InlinedArray<ulong> array;
17 public int length;
18
19 public void SetLength(int newLength)
20 {
21 // Don't touch array size if we don't have to. We're fine having a
22 // larger array to work with if it's already in place.
23 var ulongCount = BitCountToULongCount(newLength);
24 if (array.length < ulongCount)
25 array.SetLength(ulongCount);
26
27 length = newLength;
28 }
29
30 public void SetBit(int bitIndex)
31 {
32 Debug.Assert(bitIndex >= 0);
33 Debug.Assert(bitIndex < length);
34
35 array[bitIndex / 64] |= 1UL << (bitIndex % 64);
36 }
37
38 public bool TestBit(int bitIndex)
39 {
40 Debug.Assert(bitIndex >= 0);
41 Debug.Assert(bitIndex < length);
42
43 return (array[bitIndex / 64] & (1UL << (bitIndex % 64))) != 0;
44 }
45
46 public void ClearBit(int bitIndex)
47 {
48 Debug.Assert(bitIndex >= 0);
49 Debug.Assert(bitIndex < length);
50
51 array[bitIndex / 64] &= ~(1UL << (bitIndex % 64));
52 }
53
54 public bool AnyBitIsSet()
55 {
56 for (var i = 0; i < array.length; ++i)
57 {
58 if (array[i] != 0)
59 return true;
60 }
61 return false;
62 }
63
64 private static int BitCountToULongCount(int bitCount)
65 {
66 return (bitCount + 63) / 64;
67 }
68 }
69}