Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BitVector32.cs
Go to the documentation of this file.
3
5
6public struct BitVector32
7{
8 public readonly struct Section
9 {
10 private readonly short _mask;
11
12 private readonly short _offset;
13
14 public short Mask => _mask;
15
16 public short Offset => _offset;
17
18 internal Section(short mask, short offset)
19 {
20 _mask = mask;
22 }
23
24 public override bool Equals([NotNullWhen(true)] object? o)
25 {
26 if (o is Section obj)
27 {
28 return Equals(obj);
29 }
30 return false;
31 }
32
33 public bool Equals(Section obj)
34 {
35 if (obj._mask == _mask)
36 {
37 return obj._offset == _offset;
38 }
39 return false;
40 }
41
42 public static bool operator ==(Section a, Section b)
43 {
44 return a.Equals(b);
45 }
46
47 public static bool operator !=(Section a, Section b)
48 {
49 return !(a == b);
50 }
51
52 public override int GetHashCode()
53 {
54 return HashCode.Combine(_mask, _offset);
55 }
56
57 public static string ToString(Section value)
58 {
59 return $"Section{{0x{value.Mask:x}, 0x{value.Offset:x}}}";
60 }
61
62 public override string ToString()
63 {
64 return ToString(this);
65 }
66 }
67
68 private uint _data;
69
70 public bool this[int bit]
71 {
72 get
73 {
74 return (_data & bit) == (uint)bit;
75 }
76 set
77 {
78 if (value)
79 {
80 _data |= (uint)bit;
81 }
82 else
83 {
84 _data &= (uint)(~bit);
85 }
86 }
87 }
88
89 public int this[Section section]
90 {
91 get
92 {
93 return (int)((_data & (uint)(section.Mask << (int)section.Offset)) >> (int)section.Offset);
94 }
95 set
96 {
97 value <<= (int)section.Offset;
98 int num = (0xFFFF & section.Mask) << (int)section.Offset;
99 _data = (_data & (uint)(~num)) | (uint)(value & num);
100 }
101 }
102
103 public int Data => (int)_data;
104
105 public BitVector32(int data)
106 {
107 _data = (uint)data;
108 }
109
111 {
112 _data = value._data;
113 }
114
115 public static int CreateMask()
116 {
117 return CreateMask(0);
118 }
119
120 public static int CreateMask(int previous)
121 {
122 return previous switch
123 {
124 0 => 1,
125 int.MinValue => throw new InvalidOperationException(System.SR.BitVectorFull),
126 _ => previous << 1,
127 };
128 }
129
130 public static Section CreateSection(short maxValue)
131 {
132 return CreateSectionHelper(maxValue, 0, 0);
133 }
134
135 public static Section CreateSection(short maxValue, Section previous)
136 {
137 return CreateSectionHelper(maxValue, previous.Mask, previous.Offset);
138 }
139
140 private static Section CreateSectionHelper(short maxValue, short priorMask, short priorOffset)
141 {
142 if (maxValue < 1)
143 {
144 throw new ArgumentException(System.SR.Format(System.SR.Argument_InvalidValue_TooSmall, "maxValue", 1), "maxValue");
145 }
146 short num = (short)(priorOffset + BitOperations.PopCount((ushort)priorMask));
147 if (num >= 32)
148 {
150 }
151 short mask = (short)(BitOperations.RoundUpToPowerOf2((uint)((ushort)maxValue + 1)) - 1);
152 return new Section(mask, num);
153 }
154
155 public override bool Equals([NotNullWhen(true)] object? o)
156 {
157 if (o is BitVector32 bitVector)
158 {
159 return _data == bitVector._data;
160 }
161 return false;
162 }
163
164 public override int GetHashCode()
165 {
166 return _data.GetHashCode();
167 }
168
169 public static string ToString(BitVector32 value)
170 {
171 return string.Create(45, value, delegate(Span<char> dst, BitVector32 v)
172 {
173 ReadOnlySpan<char> readOnlySpan = "BitVector32{";
174 readOnlySpan.CopyTo(dst);
175 dst[dst.Length - 1] = '}';
176 int num = (int)v._data;
177 dst = dst.Slice(readOnlySpan.Length, 32);
178 for (int i = 0; i < dst.Length; i++)
179 {
180 dst[i] = (((num & 0x80000000u) != 0L) ? '1' : '0');
181 num <<= 1;
182 }
183 });
184 }
185
186 public override string ToString()
187 {
188 return ToString(this);
189 }
190}
static int PopCount(uint value)
static uint RoundUpToPowerOf2(uint value)
static string Argument_InvalidValue_TooSmall
Definition SR.cs:16
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string BitVectorFull
Definition SR.cs:30
Definition SR.cs:7
static bool operator==(Section a, Section b)
static bool operator!=(Section a, Section b)
override bool Equals([NotNullWhen(true)] object? o)
override bool Equals([NotNullWhen(true)] object? o)
static string ToString(BitVector32 value)
static Section CreateSectionHelper(short maxValue, short priorMask, short priorOffset)
static Section CreateSection(short maxValue, Section previous)
static Section CreateSection(short maxValue)
void CopyTo(Span< T > destination)
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70