-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BoolListExtensions.cs
146 lines (125 loc) · 4.6 KB
/
BoolListExtensions.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
using System.Diagnostics.Contracts;
namespace X10D.Collections;
/// <summary>
/// Collection-related extension methods for <see cref="IReadOnlyList{T}" /> of <see cref="bool" />.
/// </summary>
public static class BoolListExtensions
{
/// <summary>
/// Packs a collection of booleans into a <see cref="byte" />.
/// </summary>
/// <param name="source">The collection of booleans to pack.</param>
/// <returns>An 8-bit unsigned integer containing the packed booleans.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="source" /> contains more than 8 elements.</exception>
/// <author>Alpha Anar</author>
[Pure]
public static byte PackByte(this IReadOnlyList<bool> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (source.Count > 8)
{
throw new ArgumentException("Source cannot contain more than than 8 elements.", nameof(source));
}
byte result = 0;
for (var i = 0; i < source.Count; i++)
{
result |= (byte)(source[i] ? 1 << i : 0);
}
return result;
}
/// <summary>
/// Packs a collection of booleans into a <see cref="short" />.
/// </summary>
/// <param name="source">The collection of booleans to pack.</param>
/// <returns>A 16-bit signed integer containing the packed booleans.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="source" /> contains more than 16 elements.</exception>
[Pure]
public static short PackInt16(this IReadOnlyList<bool> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (source.Count > 16)
{
throw new ArgumentException("Source cannot contain more than than 16 elements.", nameof(source));
}
short result = 0;
for (var i = 0; i < source.Count; i++)
{
result |= (short)(source[i] ? 1 << i : 0);
}
return result;
}
/// <summary>
/// Packs a collection of booleans into a <see cref="int" />.
/// </summary>
/// <param name="source">The collection of booleans to pack.</param>
/// <returns>A 32-bit signed integer containing the packed booleans.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="source" /> contains more than 32 elements.</exception>
[Pure]
public static int PackInt32(this IReadOnlyList<bool> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (source.Count > 32)
{
throw new ArgumentException("Source cannot contain more than than 32 elements.", nameof(source));
}
var result = 0;
for (var i = 0; i < source.Count; i++)
{
result |= source[i] ? 1 << i : 0;
}
return result;
}
/// <summary>
/// Packs a collection of booleans into a <see cref="long" />.
/// </summary>
/// <param name="source">The collection of booleans to pack.</param>
/// <returns>A 64-bit signed integer containing the packed booleans.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="source" /> contains more than 64 elements.</exception>
[Pure]
public static long PackInt64(this IReadOnlyList<bool> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (source.Count > 64)
{
throw new ArgumentException("Source cannot contain more than than 64 elements.", nameof(source));
}
var result = 0L;
for (var i = 0; i < source.Count; i++)
{
result |= source[i] ? 1L << i : 0;
}
return result;
}
}