-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSevenBitInteger.cs
More file actions
146 lines (129 loc) · 3.95 KB
/
SevenBitInteger.cs
File metadata and controls
146 lines (129 loc) · 3.95 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
namespace BytecodeApi.Mathematics;
/// <summary>
/// Class that encodes integers to and from a 7-bit variable length format.
/// </summary>
public static class SevenBitInteger
{
/// <summary>
/// Converts a 32-bit integer into a compressed format.
/// </summary>
/// <param name="value">The <see cref="int" /> value to convert.</param>
/// <returns>
/// A new <see cref="byte" />[] that represents the specified 32-bit integer in compressed format.
/// </returns>
public static byte[] Encode(int value)
{
List<byte> bytes = new(5);
uint remaining = (uint)value;
while (remaining > 127)
{
bytes.Add((byte)(remaining | 128));
remaining >>= 7;
}
bytes.Add((byte)remaining);
return bytes.ToArray();
}
/// <summary>
/// Converts a 64-bit integer into a compressed format.
/// </summary>
/// <param name="value">The <see cref="long" /> value to convert.</param>
/// <returns>
/// A new <see cref="byte" />[] that represents the specified 64-bit integer in compressed format.
/// </returns>
public static byte[] Encode(long value)
{
List<byte> bytes = new(9);
ulong remaining = (ulong)value;
while (remaining > 127)
{
bytes.Add((byte)(remaining | 128));
remaining >>= 7;
}
bytes.Add((byte)remaining);
return bytes.ToArray();
}
/// <summary>
/// Calculates the number of bytes produced by encoding the specified 32-bit integer.
/// </summary>
/// <param name="value">The <see cref="int" /> value to convert.</param>
/// <returns>
/// The number of bytes the <see cref="Encode(int)" /> method will produce for the specified number.
/// </returns>
public static int GetByteCount(int value)
{
int count = 1;
uint remaining = (uint)value;
while (remaining > 127)
{
remaining >>= 7;
count++;
}
return count;
}
/// <summary>
/// Calculates the number of bytes produced by encoding the specified 64-bit integer.
/// </summary>
/// <param name="value">The <see cref="long" /> value to convert.</param>
/// <returns>
/// The number of bytes the <see cref="Encode(long)" /> method will produce for the specified number.
/// </returns>
public static int GetByteCount(long value)
{
int count = 1;
ulong remaining = (ulong)value;
while (remaining > 127)
{
remaining >>= 7;
count++;
}
return count;
}
/// <summary>
/// Converts a compressed 32-bit integer into a <see cref="int" /> value.
/// </summary>
/// <param name="value">The <see cref="byte" />[] value to convert with up to 5 bytes capacity.</param>
/// <returns>
/// A <see cref="int" /> value that was converted from the specified 32-bit integer in binary format.
/// </returns>
public static int Decode(byte[] value)
{
Check.ArgumentNull(value);
Check.Argument(value.Length is > 0 and <= 5, nameof(value), "A 7-bit encoded integer has between 1 and 5 bytes.");
int returnValue = 0;
int bitIndex = 0;
foreach (byte b in value)
{
returnValue |= (b & 127) << bitIndex;
bitIndex += 7;
if ((b & 128) == 0)
{
return returnValue;
}
}
throw Throw.Format("The value format is invalid.");
}
/// <summary>
/// Converts a compressed 64-bit integer into a <see cref="long" /> value.
/// </summary>
/// <param name="value">The <see cref="byte" />[] value to convert with up to 9 bytes capacity.</param>
/// <returns>
/// A <see cref="long" /> value that was converted from the specified 64-bit integer in binary format.
/// </returns>
public static long DecodeLong(byte[] value)
{
Check.ArgumentNull(value);
Check.Argument(value.Length is > 0 and <= 9, nameof(value), "A 7-bit encoded integer has between 1 and 9 bytes.");
long returnValue = 0;
int bitIndex = 0;
foreach (byte b in value)
{
returnValue |= (long)(b & 127) << bitIndex;
bitIndex += 7;
if ((b & 128) == 0)
{
return returnValue;
}
}
throw Throw.Format("The value format is invalid.");
}
}