-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBitReader.cs
More file actions
207 lines (169 loc) · 5.05 KB
/
BitReader.cs
File metadata and controls
207 lines (169 loc) · 5.05 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
using System.Runtime.CompilerServices;
namespace NetCode;
public sealed class BitReader : IBitReader
{
private static readonly uint[] Masks;
private ByteReader _byteReader;
private ulong _buffer;
private int _bitsInBuffer;
static BitReader()
{
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 BitReader() : this(new ByteReader())
{
}
public BitReader(byte[] data) : this(new ByteReader(data))
{
}
public BitReader(ByteReader byteReader)
{
_byteReader = byteReader;
}
public int Start => _byteReader.Start;
public int End => _byteReader.End;
/// <summary>
/// Returns the number of bytes and bits that can be read from this instance.
/// Example. We read 3 bits from 2 bytes array: 0b_00001111_00001111. Tuple (Bytes: 1, Bits: 5) will be returned.
/// ^
/// </summary>
public (int Bytes, int Bits) RemainingToRead
{
get
{
var (quotient, remainder) = Mathi.DivRem(_bitsInBuffer, 8);
return (_byteReader.RemainingToRead + quotient, remainder);
}
}
/// <summary>
/// Returns the pointer with current position of reading value.
/// Example. We read 3 bits from 2 bytes array: 0b_00001111_00001111. Tuple (Bytes: 0, Bits: 3) will be returned.
/// ^
/// </summary>
public (int Bytes, int Bits) Head
{
get
{
var (quotient, remainder) = Mathi.DivRem(_bitsInBuffer, 8);
if (remainder == 0)
{
return (_byteReader.Head - quotient, 0);
}
return (_byteReader.Head - quotient - 1, 8 - remainder);
}
}
/// <summary>
/// Returns current position.
/// Example. We had offset 1 and read 3 bits from 2 bytes array: 0b_00001111_00001111. Tuple (Bytes: 0, Bits: 3) will be returned.
/// ^
/// </summary>
public (int Bytes, int Bits) Position
{
get
{
var (quotient, remainder) = Mathi.DivRem(_bitsInBuffer, 8);
if (remainder == 0)
{
return (_byteReader.Head - _byteReader.Start - quotient, 0);
}
return (_byteReader.Head - _byteReader.Start - quotient - 1, 8 - remainder);
}
}
/// <summary>
/// Returns Position.Bytes * 8 + Position.Bits.
/// </summary>
public int BitsPosition => (_byteReader.Head - _byteReader.Start) * 8 - _bitsInBuffer;
public void SetArray(byte[] data) => SetArray(data, 0);
public void SetArray(byte[] data, int offset) => SetArray(data, offset, data.Length - offset);
public void SetArray(byte[] data, int start, int length)
{
_byteReader.SetArray(data, start, length);
_buffer = 0;
_bitsInBuffer = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset()
{
_byteReader.Reset();
_buffer = 0;
_bitsInBuffer = 0;
}
public uint ReadBits(int bitCount)
{
if (bitCount > _bitsInBuffer)
{
FillBuffer(bitCount);
}
uint value = (uint)_buffer & Masks[bitCount];
_buffer >>= bitCount;
_bitsInBuffer -= bitCount;
return value;
}
public bool ReadBool()
{
var bits = ReadBits(1);
return bits == 1;
}
private void FillBuffer(int bitCount)
{
(ulong temp, int readBytes) = _byteReader.TryReadUInt();
temp <<= _bitsInBuffer;
_buffer |= temp;
_bitsInBuffer += readBytes * 8;
if (bitCount > _bitsInBuffer)
{
ThrowHelper.ThrowIndexOutOfRangeException();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte ReadByte()
{
if (_bitsInBuffer == 0)
{
return _byteReader.ReadByte();
}
return (byte)ReadBits(8);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort ReadUShort()
{
if (_bitsInBuffer == 0)
{
return _byteReader.ReadUShort();
}
return (ushort)ReadBits(16);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short ReadShort()
{
if (_bitsInBuffer == 0)
{
return _byteReader.ReadShort();
}
return (short)ReadBits(16);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ReadUInt()
{
if (_bitsInBuffer == 0)
{
return _byteReader.ReadUInt();
}
return ReadBits(32);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ReadInt()
{
if (_bitsInBuffer == 0)
{
return _byteReader.ReadInt();
}
return (int)ReadBits(32);
}
}