mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2026-04-29 06:48:41 +00:00
Gvwie updated
This commit is contained in:
@@ -6,17 +6,6 @@ namespace Esiur.Data.Gvwie;
|
||||
|
||||
public static class GroupInt16Codec
|
||||
{
|
||||
// Header layout:
|
||||
// 1 | cccccc | w
|
||||
//
|
||||
// MSB = 1 => grouped form
|
||||
// cccccc = 0..62 => short count = cccccc + 1 (1..63)
|
||||
// cccccc = 63 => extended count, followed by varint(count - 64)
|
||||
// w = 0 => width = 1 byte
|
||||
// w = 1 => width = 2 bytes
|
||||
//
|
||||
// MSB = 0 => literal fast path for ZigZag values in 7 bits
|
||||
|
||||
// ----------------- Encoder -----------------
|
||||
public static byte[] Encode(IList<short> values)
|
||||
{
|
||||
@@ -36,7 +25,7 @@ public static class GroupInt16Codec
|
||||
}
|
||||
|
||||
int start = i;
|
||||
int width = WidthFromZigZag(zz); // 1 or 2
|
||||
int width = WidthFromZigZag(zz); // 1..2
|
||||
int count = 1;
|
||||
|
||||
// Build a run of same-width non-literal values
|
||||
@@ -55,10 +44,11 @@ public static class GroupInt16Codec
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count <= 63)
|
||||
if (count <= 60)
|
||||
{
|
||||
// Short group:
|
||||
// Header: 1 | (count-1)[6 bits] | (width-1)[1 bit]
|
||||
// count field 000000..111011 => count 1..60
|
||||
byte header = 0x80;
|
||||
header |= (byte)(((count - 1) & 0x3F) << 1);
|
||||
header |= (byte)((width - 1) & 0x01);
|
||||
@@ -67,13 +57,33 @@ public static class GroupInt16Codec
|
||||
else
|
||||
{
|
||||
// Extended group:
|
||||
// Header: 1 | 111111 | (width-1)[1 bit]
|
||||
// Followed by varint(count - 64)
|
||||
// Header: 1 | g[6 bits] | (width-1)[1 bit]
|
||||
//
|
||||
// g = 111100 => LoL = 1 byte
|
||||
// g = 111101 => LoL = 2 bytes
|
||||
// g = 111110 => LoL = 3 bytes
|
||||
// g = 111111 => LoL = 4 bytes
|
||||
//
|
||||
// LoL stores (count - 61) in little-endian form.
|
||||
|
||||
uint extra = checked((uint)(count - 61));
|
||||
int lol = LengthOfLength(extra);
|
||||
|
||||
byte groupBits = lol switch
|
||||
{
|
||||
1 => 0b111100,
|
||||
2 => 0b111101,
|
||||
3 => 0b111110,
|
||||
4 => 0b111111,
|
||||
_ => throw new InvalidOperationException("Invalid LoL.")
|
||||
};
|
||||
|
||||
byte header = 0x80;
|
||||
header |= 0x7E; // count bits = 111111
|
||||
header |= (byte)(groupBits << 1);
|
||||
header |= (byte)((width - 1) & 0x01);
|
||||
dst.Add(header);
|
||||
WriteVarUInt32(dst, (uint)(count - 64));
|
||||
|
||||
WriteLE(dst, extra, lol);
|
||||
}
|
||||
|
||||
// Payload: 'count' zigzag values, LE, 'width' bytes each
|
||||
@@ -98,24 +108,33 @@ public static class GroupInt16Codec
|
||||
|
||||
if ((h & 0x80) == 0)
|
||||
{
|
||||
// Fast path: 7-bit ZigZag in low bits
|
||||
// Fast path: 7-bit zigzag in low bits
|
||||
ushort zz7 = (ushort)(h & 0x7F);
|
||||
result.Add(UnZigZag16(zz7));
|
||||
continue;
|
||||
}
|
||||
|
||||
int countField = (h >> 1) & 0x3F;
|
||||
int width = (h & 0x01) + 1; // 1 or 2
|
||||
int width = (h & 0x01) + 1;
|
||||
|
||||
int count;
|
||||
if (countField == 63)
|
||||
|
||||
if (countField <= 59)
|
||||
{
|
||||
uint extra = ReadVarUInt32(src, ref pos);
|
||||
count = checked(64 + (int)extra);
|
||||
// Short group: 0..59 => count 1..60
|
||||
count = countField + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
count = countField + 1;
|
||||
// Extended group:
|
||||
// 60 => LoL=1
|
||||
// 61 => LoL=2
|
||||
// 62 => LoL=3
|
||||
// 63 => LoL=4
|
||||
int lol = countField - 59;
|
||||
|
||||
uint extra = checked((uint)ReadLE(src, ref pos, lol));
|
||||
count = checked(61 + (int)extra);
|
||||
}
|
||||
|
||||
for (int j = 0; j < count; j++)
|
||||
@@ -142,6 +161,15 @@ public static class GroupInt16Codec
|
||||
return z <= 0xFF ? 1 : 2;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int LengthOfLength(uint value)
|
||||
{
|
||||
if (value <= 0xFFu) return 1;
|
||||
if (value <= 0xFFFFu) return 2;
|
||||
if (value <= 0xFFFFFFu) return 3;
|
||||
return 4;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void WriteLE(List<byte> dst, ushort value, int width)
|
||||
{
|
||||
@@ -149,50 +177,23 @@ public static class GroupInt16Codec
|
||||
dst.Add((byte)((value >> (8 * i)) & 0xFF));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void WriteLE(List<byte> dst, uint value, int width)
|
||||
{
|
||||
for (int i = 0; i < width; i++)
|
||||
dst.Add((byte)((value >> (8 * i)) & 0xFF));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint ReadLE(ReadOnlySpan<byte> src, ref int pos, int width)
|
||||
{
|
||||
if ((uint)(pos + width) > (uint)src.Length)
|
||||
throw new ArgumentException("Buffer underflow while reading group payload.");
|
||||
throw new ArgumentException("Buffer underflow while reading payload.");
|
||||
|
||||
uint v = 0;
|
||||
for (int i = 0; i < width; i++)
|
||||
v |= (uint)src[pos++] << (8 * i);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void WriteVarUInt32(List<byte> dst, uint value)
|
||||
{
|
||||
while (value >= 0x80)
|
||||
{
|
||||
dst.Add((byte)((value & 0x7F) | 0x80));
|
||||
value >>= 7;
|
||||
}
|
||||
|
||||
dst.Add((byte)value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint ReadVarUInt32(ReadOnlySpan<byte> src, ref int pos)
|
||||
{
|
||||
uint result = 0;
|
||||
int shift = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (pos >= src.Length)
|
||||
throw new ArgumentException("Buffer underflow while reading varint.");
|
||||
|
||||
byte b = src[pos++];
|
||||
result |= (uint)(b & 0x7F) << shift;
|
||||
|
||||
if ((b & 0x80) == 0)
|
||||
return result;
|
||||
|
||||
shift += 7;
|
||||
if (shift >= 35)
|
||||
throw new ArgumentException("Varint is too long for UInt32.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user