forked from AlexCheero/CodexECS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitMask.cs
300 lines (260 loc) · 8.69 KB
/
BitMask.cs
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
using System;
using System.Runtime.CompilerServices;
using System.Text;
namespace ECS
{
using MaskInternal = UInt32;
public struct BitMask
{
private const int SizeOfPartInBits = sizeof(MaskInternal) * 8;
private MaskInternal _m1;
private MaskInternal[] _mn;
public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private set;
}
public BitMask(MaskInternal m1 = 0, MaskInternal[] mn = null)
{
_m1 = m1;
_mn = mn;
Length = 0;
}
public BitMask(params int[] positions)
{
_m1 = 0;
_mn = null;
Length = 0;
Set(positions);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Copy(in BitMask other)
{
_m1 = other._m1;
Length = other.Length;
var chunksLength = Length / SizeOfPartInBits;
if (chunksLength > 1)
{
if (_mn == null || _mn.Length < Length)
{
var newChunksLength = 2;
while (newChunksLength < chunksLength)
newChunksLength <<= 1;
_mn = new MaskInternal[newChunksLength];
}
for (int i = 0; i < chunksLength; i++)
_mn[i] = other._mn[i];
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public BitMask Duplicate()
{
var copy = new BitMask();
copy.Copy(this);
return copy;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set(params int[] positions)
{
for (int i = 0; i < positions.Length; i++)
Set(positions[i]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set(int i)
{
var chunkIdx = i / SizeOfPartInBits;
ref var m = ref _m1;
if (chunkIdx > 0)
{
chunkIdx--;
//resize if needed
if (_mn == null || _mn.Length <= chunkIdx)
{
var newChunksLength = 2;
while (newChunksLength < chunkIdx + 1)
newChunksLength <<= 1;
if (_mn == null)
_mn = new MaskInternal[newChunksLength];
else
Array.Resize(ref _mn, newChunksLength);
}
m = ref _mn[chunkIdx];
}
int position = i % SizeOfPartInBits;
MaskInternal shifted = 1;
m |= (MaskInternal)(shifted << position);
//update length
i++;
if (Length < i)
Length = i;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool CheckChunkIdx(int idx) => idx > 0 && (_mn == null || _mn.Length <= idx - 1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unset(int i)
{
int chunkIdx = i / SizeOfPartInBits;
if (CheckChunkIdx(chunkIdx))
return;
ref var m = ref _m1;
if (chunkIdx > 0)
m = ref _mn[chunkIdx - 1];
int position = i % SizeOfPartInBits;
MaskInternal shifted = 1;
m &= (MaskInternal)~(shifted << position);
//update length
if (chunkIdx == (Length - 1) / SizeOfPartInBits)
{
int j = chunkIdx - 1;
var msb = 0;
for (; j >= 0; j--)
{
if (_mn[j] == 0)
continue;
var chunk = _mn[j];
while (chunk != 0)
{
chunk >>= 1;
msb++;
}
break;
}
if (j < 0)
{
var chunk = _m1;
while (chunk != 0)
{
chunk >>= 1;
msb++;
}
}
j++;
Length = j * SizeOfPartInBits + msb;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Check(int i)
{
int chunkIdx = i / SizeOfPartInBits;
if (CheckChunkIdx(chunkIdx))
return false;
var m = _m1;
if (chunkIdx > 0)
m = _mn[chunkIdx - 1];
int position = i % SizeOfPartInBits;
return (m & (1 << position)) != 0;
}
/* usage:
* var nextSetBit = mask.GetNextSetBit(0);
* while (nextSetBit != -1)
* {
* your code here
* nextSetBit = mask.GetNextSetBit(nextSetBit + 1);
* }
*/
public int GetNextSetBit(int fromPosition)
{
for (int i = fromPosition; i < Length; i++)
{
int chunkIdx = i / SizeOfPartInBits;
if (CheckChunkIdx(chunkIdx))
return -1;
var m = _m1;
if (chunkIdx > 0)
m = _mn[chunkIdx - 1];
for (int j = i % SizeOfPartInBits; j < SizeOfPartInBits; j++)
{
if ((m & (1 << j)) != 0)
return j + (chunkIdx * SizeOfPartInBits);
}
}
return -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
_m1 = 0;
if (_mn != null)
{
for (int i = 0; i < _mn.Length; i++)
_mn[i] = 0;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool InclusivePass_Internal(MaskInternal value, MaskInternal filter) => (filter & (value ^ filter)) == 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool InclusivePass(in BitMask filter)
{
if (filter.Length > Length)
return false;
if (!InclusivePass_Internal(_m1, filter._m1))
return false;
var chunksCount = filter.Length / SizeOfPartInBits;
for (int i = 0; i < chunksCount - 1; i++)
{
var filterChunk = filter._mn[i];
if (filterChunk == 0)
continue;
if (!InclusivePass_Internal(_mn[i], filterChunk))
return false;
}
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ExclusivePass(in BitMask filter)
{
if ((filter._m1 & _m1) != 0)
return false;
if (filter._mn != null && _mn != null)
{
var chunksCount = filter.Length / SizeOfPartInBits;
for (int i = 0; i < chunksCount && i < _mn.Length; i++)
{
if ((filter._mn[i] & _mn[i]) != 0)
return false;
}
}
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(in BitMask other)
{
if (Length != other.Length)
return false;
if (_m1 != other._m1)
return false;
if (_mn != null)
{
int length = (Length / SizeOfPartInBits) - 1;
for (int i = 0; i < length; i++)
{
if (_mn[i] != other._mn[i])
return false;
}
}
return true;
}
#if DEBUG
public override string ToString()
{
var sb = new StringBuilder();
if (_mn != null)
for (int i = _mn.Length - 1; i > -1; i--)
sb.Append(Convert.ToString(_mn[i], 2).PadLeft(SizeOfPartInBits, '0'));
sb.Append(Convert.ToString(_m1, 2).PadLeft(SizeOfPartInBits, '0'));
sb.Append(". Length: " + Length);
return sb.ToString();
}
public string ChunkToString(MaskInternal chunk) => Convert.ToString(chunk, 2).PadLeft(SizeOfPartInBits, '0');
public void SetBits(int[] bits)
{
int j = 0;
for (int i = bits.Length - 1; i >= 0; i--, j++)
if (bits[i] != 0)
Set(j);
}
#endif
}
}