-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBitWriter.cs
More file actions
221 lines (177 loc) · 5.07 KB
/
Copy pathBitWriter.cs
File metadata and controls
221 lines (177 loc) · 5.07 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
using System.Runtime.CompilerServices;
namespace NetCode;
public sealed class BitWriter : IBitWriter
{
private const int DefaultCapacity = 1500;
private static readonly uint[] Masks;
private readonly ByteWriter _byteWriter;
private ulong _buffer;
private int _bitsInBuffer;
static BitWriter()
{
Masks = new uint[33];
for (int i = 1; i < Masks.Length - 1; i++)
{
var mask = (1u << i) - 1;
Masks[i] = mask;
}
Masks[32] = uint.MaxValue;
}
public BitWriter(int capacity = DefaultCapacity) : this(new ByteWriter(capacity))
{
}
public BitWriter(byte[] data) : this(new ByteWriter(data))
{
}
public BitWriter(ByteWriter byteWriter)
{
_byteWriter = byteWriter;
}
public int BitsCount => _byteWriter.Count * 8 + _bitsInBuffer;
public int BytesCount => _byteWriter.Count + Mathi.Ceiling(_bitsInBuffer, 8);
public (int Bytes, int Bits) Head
{
get
{
var (quotient, remainder) = Mathi.DivRem(_bitsInBuffer, 8);
return (_byteWriter.Count + quotient, remainder);
}
}
public (int Bytes, int Bits) Position
{
get
{
var (quotient, remainder) = Mathi.DivRem(_bitsInBuffer, 8);
return (_byteWriter.Count - _byteWriter.Start + quotient, remainder);
}
}
public int BitsPosition => (_byteWriter.Count - _byteWriter.Start) * 8 + _bitsInBuffer;
public int Capacity => _byteWriter.Capacity;
public byte[] Array => _bitsInBuffer == 0 ? _byteWriter.Array : throw new InvalidOperationException("Writer should be flushed first.");
public void SetArray(byte[] data) => SetArray(data, 0);
public void SetArray(byte[] data, int offset)
{
_byteWriter.SetArray(data, offset);
_bitsInBuffer = 0;
_buffer = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
_byteWriter.Clear();
_bitsInBuffer = 0;
_buffer = 0;
}
/// <summary>
/// Set bit at specific position. You can set only if you has written data to it position.
/// </summary>
public void SetAt(int bitPosition, bool value)
{
if (bitPosition < 0 || bitPosition >= _byteWriter.Capacity * 8)
throw new IndexOutOfRangeException();
if (bitPosition + 1 > BitsCount)
throw new NotSupportedException("You can't set bit before you write value to it position!");
var (byteIndex, bitIndex) = Mathi.DivRem(bitPosition, 8);
var bitPositionInBuffer = bitPosition - _byteWriter.Count * 8;
if (bitPositionInBuffer >= 0)
{
// set bit at the buffer
ulong mask = 1ul << bitPositionInBuffer;
if (value)
{
_buffer |= mask;
}
else
{
_buffer &= ~mask;
}
}
else
{
// set bit at the array
var @byte = _byteWriter.Array[byteIndex];
byte mask = (byte)(1 << bitIndex);
if (value)
{
@byte |= mask;
}
else
{
@byte = (byte)(@byte & ~mask);
}
_byteWriter.Array[byteIndex] = @byte;
}
}
public void WriteBits(int bitCount, uint value)
{
value &= Masks[bitCount];
_buffer |= (ulong)value << _bitsInBuffer;
_bitsInBuffer += bitCount;
if (_bitsInBuffer >= 32)
{
uint write = (uint)_buffer;
_byteWriter.Write(write);
_buffer >>= 32;
_bitsInBuffer -= 32;
}
}
public void Write(bool value) => WriteBits(1, value ? 1u : 0u);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(byte value)
{
if (_bitsInBuffer == 0)
{
_byteWriter.Write(value);
}
else
{
WriteBits(8, value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(ushort value)
{
if (_bitsInBuffer == 0)
{
_byteWriter.Write(value);
}
else
{
WriteBits(16, value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(short value)
{
Write((ushort)value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(uint value)
{
if (_bitsInBuffer == 0)
{
_byteWriter.Write(value);
}
else
{
WriteBits(32, value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write(int value)
{
Write((uint)value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Flush()
{
while (_bitsInBuffer > 0)
{
_byteWriter.Write((byte)_buffer);
_buffer >>= 8;
_bitsInBuffer -= 8;
}
_buffer = 0;
_bitsInBuffer = 0;
}
}