Repository navigation
Expand file tree
/
Copy pathBitSet.cs
More file actions
220 lines (170 loc) · 5.78 KB
/
Copy pathBitSet.cs
File metadata and controls
220 lines (170 loc) · 5.78 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
using System;
using System.Runtime.CompilerServices;
namespace softh.Collections
{
public sealed class BitSet
{
private const MethodImplOptions INLINE = MethodImplOptions.AggressiveInlining;
private const int MASK_SIZE = 32;
private const int LOG2_MASK_SIZE = 5;
/// <summary>
/// Create a new bitset of the given length. All bits are initially false.
/// Length will be rounded up to a multiple of mask size (32).
/// </summary>
public BitSet( int length ) => _bits = IsValidLength( ref length ) ? new uint[ length ] : Array.Empty<uint>();
private uint[] _bits;
/// <summary>
/// Returns the number of bits actually used by this bitset.
/// </summary>
public int Length
{
[MethodImpl( INLINE )]
get => _bits.Length << LOG2_MASK_SIZE;
}
[MethodImpl( INLINE )]
private static bool IsValidLength( ref int length ) => ( length = ( ( length - 1 ) >> LOG2_MASK_SIZE ) + 1 ) > 0;
#region Get/Set
public Bit this[ int offset ]
{
[MethodImpl( INLINE )]
get => Bit.GetFrom( _bits[ offset >> LOG2_MASK_SIZE ], offset );
}
[MethodImpl( INLINE )]
public void SetTrue( int offset ) => _bits[ offset >> LOG2_MASK_SIZE ] |= 1u << offset;
[MethodImpl( INLINE )]
public void SetFalse( int offset ) => _bits[ offset >> LOG2_MASK_SIZE ] &= ~( 1u << offset );
/// <summary>
/// Sets the bits in the given range from (inclusive) and to (exclusive) to true.
/// </summary>
[MethodImpl( INLINE )]
public void SetTrue( int from, int to )
{
if( from >= to )
{
return;
}
( int start, int end ) = ( from >> LOG2_MASK_SIZE, to - 1 >> LOG2_MASK_SIZE );
to = MASK_SIZE - to;
if( start == end )
{
_bits[ start ] |= ( ~0u >> to ) & ( ~0u << from );
return;
}
_bits[ start ] |= ~0u << from;
_bits[ end ] |= ~0u >> to;
for( start++; start < end; start++ )
{
_bits[ start ] = ~0u;
}
}
/// <summary>
/// Sets the bits in the given range from (inclusive) and to (exclusive) to false.
/// </summary>
[MethodImpl( INLINE )]
public void SetFalse( int from, int to )
{
if( from >= to )
{
return;
}
( int start, int end ) = ( from >> LOG2_MASK_SIZE, to - 1 >> LOG2_MASK_SIZE );
to = MASK_SIZE - to;
if( start == end )
{
_bits[ start ] &= ~( ( ~0u >> to ) & ( ~0u << from ) );
return;
}
_bits[ start ] &= ~( ~0u << from );
_bits[ end ] &= ~( ~0u >> to );
for( start++; start < end; start++ )
{
_bits[ start ] = 0u;
}
}
/// <summary>
/// Sets all bits in the bitset to true.
/// </summary>
[MethodImpl( INLINE )]
public void SetTrue()
{
var bits = _bits;
for( int i = 0; i < bits.Length; i++ )
{
bits[ i ] = ~0u;
}
}
/// <summary>
/// Sets all bits in the bitset to false.
/// </summary>
[MethodImpl( INLINE )]
public void SetFalse()
{
var bits = _bits;
for( int i = 0; i < bits.Length; i++ )
{
bits[ i ] = 0u;
}
}
#endregion
#region Resize
/// <summary>
/// Changes the number of bits of this bitset to the specified new size.
/// </summary>
[MethodImpl( INLINE )]
public void Resize( int newSize )
{
if ( !IsValidLength( ref newSize ) )
{
_bits = Array.Empty<uint>();
return;
}
if( newSize == _bits.Length )
{
return;
}
var newArray = new uint[ newSize ];
Array.Copy( _bits, 0, newArray, 0, _bits.Length >= newSize ? newSize : _bits.Length );
_bits = newArray;
}
#endregion
#region PopCount
/// <summary>
/// Returns the population count (number of bits set) in the given range from (inclusive) and to (exclusive).
/// </summary>
[MethodImpl( INLINE )]
public int PopCount( int from, int to )
{
if( from >= to )
{
return 0;
}
( int start, int end ) = ( from >> LOG2_MASK_SIZE, to - 1 >> LOG2_MASK_SIZE );
to = MASK_SIZE - to;
if( start == end )
{
return Bit.PopCount( _bits[ start ] & ( ( ~0u << from ) & ( ~0u >> to ) ) );
}
int count = Bit.PopCount( ( _bits[ start ] & ( ~0u << from ) )
| ( (ulong)( _bits[ end ] & ( ~0u >> to ) ) << MASK_SIZE ) );
for( start++; start < end; start++ )
{
count += Bit.PopCount( _bits[ start ] );
}
return count;
}
/// <summary>
/// Returns the population count (number of bits set) of this set.
/// </summary>
[MethodImpl( INLINE )]
public int PopCount()
{
int count = 0;
for ( int i = 0; i < Length; i++ )
{
count += Bit.PopCount( _bits[ i ] );
}
return count;
}
#endregion
}
}