-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBitArrayExtensions.cs
More file actions
347 lines (313 loc) · 10.5 KB
/
BitArrayExtensions.cs
File metadata and controls
347 lines (313 loc) · 10.5 KB
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
using System.Collections;
using System.Security.Cryptography;
namespace BytecodeApi.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="BitArray" /> objects.
/// </summary>
public static class BitArrayExtensions
{
extension(BitArray array)
{
/// <summary>
/// Returns the number of values in this <see cref="BitArray" /> whose value is <see langword="true" />.
/// </summary>
public int CountTrue
{
get
{
Check.ArgumentNull(array);
int count = 0;
foreach (bool value in array)
{
if (value)
{
count++;
}
}
return count;
}
}
/// <summary>
/// Returns the number of values in this <see cref="BitArray" /> whose value is <see langword="false" />.
/// </summary>
public int CountFalse
{
get
{
Check.ArgumentNull(array);
return array.Length - array.CountTrue;
}
}
/// <summary>
/// Returns a value indicating whether all elements of this <see cref="BitArray" /> are <see langword="true" />.
/// </summary>
public bool AllTrue
{
get
{
Check.ArgumentNull(array);
foreach (bool value in array)
{
if (!value)
{
return false;
}
}
return true;
}
}
/// <summary>
/// Returns a value indicating whether all elements of this <see cref="BitArray" /> are <see langword="false" />.
/// </summary>
public bool AllFalse
{
get
{
Check.ArgumentNull(array);
foreach (bool value in array)
{
if (value)
{
return false;
}
}
return true;
}
}
/// <summary>
/// Converts the bits of this <see cref="BitArray" /> to an equivalent <see cref="bool" />[].
/// </summary>
/// <returns>
/// An equivalent <see cref="bool" />[] representing this <see cref="BitArray" />.
/// </returns>
public bool[] ToBooleanArray()
{
Check.ArgumentNull(array);
bool[] values = new bool[array.Length];
for (int i = 0; i < values.Length; i++)
{
values[i] = array[i];
}
return values;
}
/// <summary>
/// Converts the bits of this <see cref="BitArray" /> to an equivalent <see cref="byte" />[]. If there is a padding of less than 8 bits, the last byte contains the remaining bits.
/// </summary>
/// <returns>
/// An equivalent <see cref="byte" />[] representing this <see cref="BitArray" />.
/// </returns>
public byte[] ToByteArray()
{
Check.ArgumentNull(array);
byte[] bytes = new byte[(array.Length + 7) / 8];
for (int i = 0, position = 0; i < bytes.Length; i++)
{
for (int j = 0; j < 8 && position < array.Length; j++, position++)
{
if (array[position])
{
bytes[i] = (byte)(bytes[i] | 1 << j);
}
}
}
return bytes;
}
/// <summary>
/// Converts the bits of this <see cref="BitArray" /> to a <see cref="string" /> containing a sequence of '0' or '1' characters.
/// </summary>
/// <returns>
/// An equivalent sequence of '0' or '1' characters representing this <see cref="BitArray" />.
/// </returns>
public string ToBitString()
{
Check.ArgumentNull(array);
char[] str = new char[array.Length];
for (int i = 0; i < str.Length; i++)
{
str[i] = array[i] ? '1' : '0';
}
return str.AsString();
}
/// <summary>
/// Compares the content of this <see cref="BitArray" /> agains another <see cref="BitArray" />. Returns <see langword="true" />, if both arrays contain the exact same set of data.
/// </summary>
/// <param name="otherArray">A <see cref="BitArray" /> to compare to this <see cref="BitArray" />.</param>
/// <returns>
/// <see langword="true" />, if both arrays contain the exact same set of data;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Compare([NotNullWhen(true)] BitArray? otherArray)
{
Check.ArgumentNull(array);
if (otherArray == null)
{
return false;
}
else if (array == otherArray)
{
return true;
}
else if (array.Length == otherArray.Length)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] != otherArray[i])
{
return false;
}
}
return true;
}
else
{
return false;
}
}
/// <summary>
/// Copies a specified number of bits from this <see cref="BitArray" /> and returns a new <see cref="BitArray" /> representing a fraction of the original <see cref="BitArray" />.
/// </summary>
/// <param name="index">A <see cref="int" /> value specifying the offset from which to start copying bits.</param>
/// <param name="count">A <see cref="int" /> value specifying the number of bits to copy.</param>
/// <returns>
/// A new <see cref="BitArray" /> representing a fraction of the original <see cref="BitArray" />.
/// </returns>
public BitArray GetBits(int index, int count)
{
Check.ArgumentNull(array);
Check.ArgumentOutOfRangeEx.GreaterEqual0(index);
Check.ArgumentOutOfRangeEx.GreaterEqual0(count);
Check.ArgumentEx.OffsetAndLengthOutOfBounds(index, count, array.Length);
BitArray copy = new(count);
for (int i = 0; i < count; i++)
{
copy[i] = array[index + i];
}
return copy;
}
/// <summary>
/// Sets all bits in the specified range in this <see cref="BitArray" /> to the specified value.
/// </summary>
/// <param name="offset">The zero-based index in this <see cref="BitArray" /> at which to begin writing values.</param>
/// <param name="count">The number of values to write.</param>
/// <param name="value">The <see cref="bool" /> value to assign to all bits in the specified range.</param>
public void SetAll(int offset, int count, bool value)
{
Check.ArgumentNull(array);
Check.ArgumentOutOfRangeEx.GreaterEqual0(offset);
Check.ArgumentOutOfRangeEx.GreaterEqual0(count);
Check.ArgumentEx.OffsetAndLengthOutOfBounds(offset, count, array.Length);
for (int i = offset; i < offset + count; i++)
{
array[i] = value;
}
}
/// <summary>
/// Sets the values of this <see cref="BitArray" /> to random values.
/// </summary>
public void SetRandomValues()
{
array.SetRandomValues(0, array.Length);
}
/// <summary>
/// Sets the values of this <see cref="BitArray" /> to random values.
/// </summary>
/// <param name="cryptographic"><see langword="true" /> to generate cryptographic random values.</param>
public void SetRandomValues(bool cryptographic)
{
array.SetRandomValues(0, array.Length, cryptographic);
}
/// <summary>
/// Sets the values of this <see cref="BitArray" /> to random values.
/// </summary>
/// <param name="offset">The zero-based index in this <see cref="BitArray" /> at which to begin writing values.</param>
/// <param name="count">The number of values to write.</param>
public void SetRandomValues(int offset, int count)
{
array.SetRandomValues(offset, count, false);
}
/// <summary>
/// Sets the values of this <see cref="BitArray" /> to random values.
/// </summary>
/// <param name="offset">The zero-based index in this <see cref="BitArray" /> at which to begin writing values.</param>
/// <param name="count">The number of values to write.</param>
/// <param name="cryptographic"><see langword="true" /> to generate cryptographic random values.</param>
public void SetRandomValues(int offset, int count, bool cryptographic)
{
Check.ArgumentNull(array);
Check.ArgumentOutOfRangeEx.GreaterEqual0(offset);
Check.ArgumentOutOfRangeEx.GreaterEqual0(count);
Check.ArgumentEx.OffsetAndLengthOutOfBounds(offset, count, array.Length);
byte[] buffer = new byte[16];
int bufferPosition = int.MaxValue;
if (cryptographic)
{
for (int i = offset; i < offset + count; i++)
{
if (bufferPosition >= buffer.Length << 3)
{
RandomNumberGenerator.Fill(buffer);
bufferPosition = 0;
}
array[i] = (buffer[bufferPosition >> 3] & 1 << (bufferPosition & 7)) > 0;
bufferPosition++;
}
}
else
{
for (int i = offset; i < offset + count; i++)
{
if (bufferPosition >= buffer.Length << 3)
{
Random.Shared.NextBytes(buffer);
bufferPosition = 0;
}
array[i] = (buffer[bufferPosition >> 3] & 1 << (bufferPosition & 7)) > 0;
bufferPosition++;
}
}
}
/// <summary>
/// Copies a specified number of elements from this <see cref="BitArray" /> to <paramref name="dest" />, beginning at the specified source offset, written to the specified destination offset.
/// </summary>
/// <param name="sourceOffset">The offset, at which to start reading from this <see cref="BitArray" />.</param>
/// <param name="dest">The <see cref="BitArray" /> that is written to.</param>
/// <param name="destOffset">The offset, at which to start writing to <paramref name="dest" />.</param>
/// <param name="count">The number of <see cref="bool" /> values to copy.</param>
public void CopyTo(int sourceOffset, BitArray dest, int destOffset, int count)
{
Check.ArgumentNull(array);
Check.ArgumentOutOfRangeEx.GreaterEqual0(sourceOffset);
Check.ArgumentNull(dest);
Check.ArgumentOutOfRangeEx.GreaterEqual0(destOffset);
Check.ArgumentOutOfRangeEx.GreaterEqual0(count);
Check.ArgumentEx.OffsetAndLengthOutOfBounds(sourceOffset, count, array.Length);
Check.ArgumentEx.OffsetAndLengthOutOfBounds(destOffset, count, dest.Length);
for (int i = 0; i < count; i++)
{
dest[i + destOffset] = array[i + sourceOffset];
}
}
/// <summary>
/// Merges all <see cref="BitArray" /> objects and returns a new <see cref="BitArray" />, where <paramref name="otherArrays" /> are concatenated after this array.
/// </summary>
/// <param name="otherArrays">An array of <see cref="BitArray" /> objects to append.</param>
/// <returns>
/// A new <see cref="BitArray" /> starting with this <see cref="BitArray" />, followed by all elements from <paramref name="otherArrays" />.
/// </returns>
public BitArray Concat(params BitArray[] otherArrays)
{
Check.ArgumentNull(array);
Check.ArgumentNull(otherArrays);
Check.ArgumentEx.ArrayValuesNotNull(otherArrays);
BitArray result = new(array.Length + otherArrays.Sum(other => other.Length));
array.CopyTo(0, result, 0, array.Length);
int offset = array.Length;
foreach (BitArray other in otherArrays)
{
other.CopyTo(0, result, offset, other.Length);
offset += other.Length;
}
return result;
}
}
}