mirror of
				https://github.com/esiur/esiur-dotnet.git
				synced 2025-10-31 16:01:34 +00:00 
			
		
		
		
	Pull Stream
This commit is contained in:
		
							
								
								
									
										137
									
								
								Esiur/Data/GVWIE/GroupInt16Codec.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										137
									
								
								Esiur/Data/GVWIE/GroupInt16Codec.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,137 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Runtime.CompilerServices; | ||||
|  | ||||
| namespace Esiur.Data.GVWIE; | ||||
|  | ||||
| public static class GroupInt16Codec | ||||
| { | ||||
|     // ----------------- Encoder ----------------- | ||||
|     public static byte[] Encode(IList<short> values) | ||||
|     { | ||||
|         var dst = new List<byte>(values.Count); // close lower bound | ||||
|         int i = 0; | ||||
|  | ||||
|         while (i < values.Count) | ||||
|         { | ||||
|             ushort zz = ZigZag16(values[i]); | ||||
|  | ||||
|             // Fast path: single byte with 7-bit ZigZag | ||||
|             if (zz <= 0x7Fu) | ||||
|             { | ||||
|                 dst.Add((byte)zz); // MSB=0 implicitly | ||||
|                 i++; | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             // Group path: up to 64 items sharing width (1 or 2 bytes) | ||||
|             int start = i; | ||||
|             int count = 1; | ||||
|             int width = (zz <= 0xFFu) ? 1 : 2; | ||||
|  | ||||
|             while (count < 64 && (i + count) < values.Count) | ||||
|             { | ||||
|                 ushort z2 = ZigZag16(values[i + count]); | ||||
|                 int w2 = (z2 <= 0xFFu) ? 1 : 2; | ||||
|                 if (w2 > width) width = w2; // widen as needed | ||||
|                 count++; | ||||
|             } | ||||
|  | ||||
|             // Header: 1 | (count-1)[6 bits] | (width-1)[1 bit] | ||||
|             byte header = 0x80; | ||||
|             header |= (byte)(((count - 1) & 0x3F) << 1); | ||||
|             header |= (byte)((width - 1) & 0x01); | ||||
|             dst.Add(header); | ||||
|  | ||||
|             // Payload: count ZigZag magnitudes, LE, 'width' bytes each | ||||
|             for (int k = 0; k < count; k++) | ||||
|             { | ||||
|                 ushort z = ZigZag16(values[start + k]); | ||||
|                 WriteLE(dst, z, width); | ||||
|             } | ||||
|  | ||||
|             i += count; | ||||
|         } | ||||
|  | ||||
|         return dst.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Decoder ----------------- | ||||
|     public static short[] Decode(ReadOnlySpan<byte> src) | ||||
|     { | ||||
|         var result = new List<short>(); | ||||
|         int pos = 0; | ||||
|  | ||||
|         while (pos < src.Length) | ||||
|         { | ||||
|             byte h = src[pos++]; | ||||
|  | ||||
|             if ((h & 0x80) == 0) | ||||
|             { | ||||
|                 // Fast path: 7-bit ZigZag | ||||
|                 ushort zz7 = (ushort)(h & 0x7F); | ||||
|                 result.Add(UnZigZag16(zz7)); | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             int count = ((h >> 1) & 0x3F) + 1; // 1..64 | ||||
|             int width = (h & 0x01) + 1;        // 1..2 | ||||
|  | ||||
|             for (int j = 0; j < count; j++) | ||||
|             { | ||||
|                 uint raw = ReadLE(src, ref pos, width); | ||||
|                 if (width > 2 && (raw >> 16) != 0) | ||||
|                     throw new OverflowException("Decoded ZigZag value exceeds 16-bit range."); | ||||
|  | ||||
|                 ushort u = (ushort)raw; | ||||
|                 short val = UnZigZag16(u); | ||||
|                 result.Add(val); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return result.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Helpers ----------------- | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static ushort ZigZag16(short v) | ||||
|     { | ||||
|         // (v << 1) ^ (v >> 15), result as unsigned 16-bit | ||||
|         return (ushort)(((uint)(ushort)v << 1) ^ (uint)((int)v >> 15)); | ||||
|     } | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static short UnZigZag16(ushort u) | ||||
|     { | ||||
|         // (u >> 1) ^ -(u & 1), narrowed to 16-bit signed | ||||
|         return (short)((u >> 1) ^ (ushort)-(short)(u & 1)); | ||||
|     } | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static void WriteLE(List<byte> dst, ushort value, int width) | ||||
|     { | ||||
|         // width is 1 or 2 | ||||
|         dst.Add((byte)(value & 0xFF)); | ||||
|         if (width == 2) dst.Add((byte)(value >> 8)); | ||||
|     } | ||||
|  | ||||
|     [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."); | ||||
|  | ||||
|         uint v = src[pos++]; | ||||
|         if (width == 2) | ||||
|         { | ||||
|             v |= (uint)src[pos++] << 8; | ||||
|         } | ||||
|         return v; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										129
									
								
								Esiur/Data/GVWIE/GroupInt32Codec.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										129
									
								
								Esiur/Data/GVWIE/GroupInt32Codec.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,129 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| using System.Runtime.CompilerServices; | ||||
| using System.Collections; | ||||
|  | ||||
| namespace Esiur.Data.GVWIE; | ||||
|  | ||||
| public static class GroupInt32Codec | ||||
| { | ||||
|     // ----------------- Encoder ----------------- | ||||
|     public static byte[] Encode(IList<int> values) | ||||
|     { | ||||
|         //var values = value as int[]; | ||||
|  | ||||
|         var dst = new List<byte>(values.Count * 2); | ||||
|         int i = 0; | ||||
|  | ||||
|         while (i < values.Count) | ||||
|         { | ||||
|             uint zz = ZigZag32(values[i]); | ||||
|  | ||||
|             // Fast path: single byte (MSB=0) when zigzag fits in 7 bits | ||||
|             if (zz <= 0x7Fu) | ||||
|             { | ||||
|                 dst.Add((byte)zz); | ||||
|                 i++; | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             // Group: up to 32 items sharing a common width (1..4 bytes) | ||||
|             int start = i; | ||||
|             int count = 1; | ||||
|             int width = WidthFromZigZag(zz); | ||||
|  | ||||
|             while (count < 32 && (i + count) < values.Count) | ||||
|             { | ||||
|                 uint z2 = ZigZag32(values[i + count]); | ||||
|                 int w2 = WidthFromZigZag(z2); | ||||
|                 width = Math.Max(width, w2); // widen as needed | ||||
|                 count++; | ||||
|             } | ||||
|  | ||||
|             // Header: 1 | (count-1)[5 bits] | (width-1)[2 bits] | ||||
|             byte header = 0x80; | ||||
|             header |= (byte)(((count - 1) & 0x1F) << 2); | ||||
|             header |= (byte)((width - 1) & 0x03); | ||||
|             dst.Add(header); | ||||
|  | ||||
|             // Payload: 'count' zigzag values, LE, 'width' bytes each | ||||
|             for (int k = 0; k < count; k++) | ||||
|                 WriteLE(dst, ZigZag32(values[start + k]), width); | ||||
|  | ||||
|             i += count; | ||||
|         } | ||||
|  | ||||
|         return dst.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Decoder ----------------- | ||||
|     public static int[] Decode(ReadOnlySpan<byte> src) | ||||
|     { | ||||
|         var result = new List<int>(); | ||||
|         int pos = 0; | ||||
|  | ||||
|         while (pos < src.Length) | ||||
|         { | ||||
|             byte h = src[pos++]; | ||||
|  | ||||
|             if ((h & 0x80) == 0) | ||||
|             { | ||||
|                 // Fast path: 7-bit ZigZag in low bits | ||||
|                 uint zz7 = (uint)(h & 0x7F); | ||||
|                 result.Add(UnZigZag32(zz7)); | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             int count = ((h >> 2) & 0x1F) + 1; // 1..32 | ||||
|             int width = (h & 0x03) + 1;        // 1..4 | ||||
|  | ||||
|             for (int j = 0; j < count; j++) | ||||
|             { | ||||
|                 uint raw = (uint)ReadLE(src, ref pos, width); | ||||
|                 int val = UnZigZag32(raw); | ||||
|                 result.Add(val); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return result.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Helpers ----------------- | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static uint ZigZag32(int v) => (uint)((v << 1) ^ (v >> 31)); | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static int UnZigZag32(uint u) => (int)((u >> 1) ^ (uint)-(int)(u & 1)); | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static int WidthFromZigZag(uint z) | ||||
|     { | ||||
|         if (z <= 0xFFu) return 1; | ||||
|         if (z <= 0xFFFFu) return 2; | ||||
|         if (z <= 0xFFFFFFu) return 3; | ||||
|         return 4; | ||||
|     } | ||||
|  | ||||
|     [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 ulong 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."); | ||||
|  | ||||
|         ulong v = 0; | ||||
|         for (int i = 0; i < width; i++) | ||||
|             v |= (ulong)src[pos++] << (8 * i); | ||||
|         return v; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										135
									
								
								Esiur/Data/GVWIE/GroupInt64Codec.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										135
									
								
								Esiur/Data/GVWIE/GroupInt64Codec.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,135 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Runtime.CompilerServices; | ||||
|  | ||||
| namespace Esiur.Data.GVWIE; | ||||
|  | ||||
| public static class GroupInt64Codec | ||||
| { | ||||
|     // ----------------- Encoder ----------------- | ||||
|     public static byte[] Encode(IList<long> values) | ||||
|     { | ||||
|         var dst = new List<byte>(values.Count * 2); | ||||
|         int i = 0; | ||||
|  | ||||
|         while (i < values.Count) | ||||
|         { | ||||
|             ulong zz = ZigZag64(values[i]); | ||||
|  | ||||
|             // Fast path: 1 byte when ZigZag fits in 7 bits | ||||
|             if (zz <= 0x7Ful) | ||||
|             { | ||||
|                 dst.Add((byte)zz); // MSB = 0 implicitly | ||||
|                 i++; | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             // Group path: up to 16 items sharing a common width (1..8 bytes) | ||||
|             int start = i; | ||||
|             int count = 1; | ||||
|             int width = WidthFromZigZag(zz); | ||||
|  | ||||
|             while (count < 16 && (i + count) < values.Count) | ||||
|             { | ||||
|                 ulong z2 = ZigZag64(values[i + count]); | ||||
|                 int w2 = WidthFromZigZag(z2); | ||||
|                 width = Math.Max(width, w2);   // widen as needed | ||||
|                 count++; | ||||
|             } | ||||
|  | ||||
|             // Header: 1 | (count-1)[4 bits] | (width-1)[3 bits] | ||||
|             byte header = 0x80; | ||||
|             header |= (byte)(((count - 1) & 0x0F) << 3); | ||||
|             header |= (byte)((width - 1) & 0x07); | ||||
|             dst.Add(header); | ||||
|  | ||||
|             // Payload: 'count' ZigZag values, LE, 'width' bytes each | ||||
|             for (int k = 0; k < count; k++) | ||||
|             { | ||||
|                 ulong z = ZigZag64(values[start + k]); | ||||
|                 WriteLE(dst, z, width); | ||||
|             } | ||||
|  | ||||
|             i += count; | ||||
|         } | ||||
|  | ||||
|         return dst.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Decoder ----------------- | ||||
|     public static long[] Decode(ReadOnlySpan<byte> src) | ||||
|     { | ||||
|         var result = new List<long>(); | ||||
|         int pos = 0; | ||||
|  | ||||
|         while (pos < src.Length) | ||||
|         { | ||||
|             byte h = src[pos++]; | ||||
|  | ||||
|             if ((h & 0x80) == 0) | ||||
|             { | ||||
|                 // Fast path: 7-bit ZigZag | ||||
|                 ulong zz7 = (ulong)(h & 0x7F); | ||||
|                 result.Add(UnZigZag64(zz7)); | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             int count = ((h >> 3) & 0x0F) + 1; // 1..16 | ||||
|             int width = (h & 0x07) + 1;        // 1..8 | ||||
|  | ||||
|             for (int j = 0; j < count; j++) | ||||
|             { | ||||
|                 ulong raw = ReadLE(src, ref pos, width); | ||||
|                 long val = UnZigZag64(raw); | ||||
|                 result.Add(val); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return result.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Helpers ----------------- | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static ulong ZigZag64(long v) => (ulong)((v << 1) ^ (v >> 63)); | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static long UnZigZag64(ulong u) => (long)((u >> 1) ^ (ulong)-(long)(u & 1)); | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static int WidthFromZigZag(ulong z) | ||||
|     { | ||||
|         if (z <= 0xFFUL) return 1; | ||||
|         if (z <= 0xFFFFUL) return 2; | ||||
|         if (z <= 0xFFFFFFUL) return 3; | ||||
|         if (z <= 0xFFFFFFFFUL) return 4; | ||||
|         if (z <= 0xFFFFFFFFFFUL) return 5; | ||||
|         if (z <= 0xFFFFFFFFFFFFUL) return 6; | ||||
|         if (z <= 0xFFFFFFFFFFFFFFUL) return 7; | ||||
|         return 8; | ||||
|     } | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static void WriteLE(List<byte> dst, ulong value, int width) | ||||
|     { | ||||
|         for (int i = 0; i < width; i++) | ||||
|             dst.Add((byte)((value >> (8 * i)) & 0xFF)); | ||||
|     } | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static ulong 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."); | ||||
|  | ||||
|         ulong v = 0; | ||||
|         for (int i = 0; i < width; i++) | ||||
|             v |= (ulong)src[pos++] << (8 * i); | ||||
|         return v; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										121
									
								
								Esiur/Data/GVWIE/GroupUInt16Codec.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								Esiur/Data/GVWIE/GroupUInt16Codec.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,121 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| using System.Runtime.CompilerServices; | ||||
|  | ||||
| namespace Esiur.Data.GVWIE; | ||||
|  | ||||
|  | ||||
| public static class GroupUInt16Codec | ||||
| { | ||||
|     // ----------------- Encoder ----------------- | ||||
|     public static byte[] Encode(IList<ushort> values) | ||||
|     { | ||||
|         if (values is null) throw new ArgumentNullException(nameof(values)); | ||||
|  | ||||
|         var dst = new List<byte>(values.Count * 2); | ||||
|         int i = 0; | ||||
|  | ||||
|         while (i < values.Count) | ||||
|         { | ||||
|             ushort v = values[i]; | ||||
|  | ||||
|             // Fast path: single byte for 0..127 | ||||
|             if (v <= 0x7F) | ||||
|             { | ||||
|                 dst.Add((byte)v); // MSB=0 implicitly | ||||
|                 i++; | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             // Group path: up to 16 items sharing a common width (1..2 bytes for uint16) | ||||
|             int start = i; | ||||
|             int count = 1; | ||||
|             int width = WidthFromUnsigned(v); | ||||
|  | ||||
|             while (count < 16 && (i + count) < values.Count) | ||||
|             { | ||||
|                 ushort v2 = values[i + count]; | ||||
|                 int w2 = WidthFromUnsigned(v2); | ||||
|                 if (w2 > width) width = w2; // widen group if needed | ||||
|                 count++; | ||||
|             } | ||||
|  | ||||
|             // Header: 1 | (count-1)[4b] | (width-1)[3b] | ||||
|             byte header = 0x80; | ||||
|             header |= (byte)(((count - 1) & 0xF) << 3); | ||||
|             header |= (byte)((width - 1) & 0x7); | ||||
|             dst.Add(header); | ||||
|  | ||||
|             // Payload | ||||
|             for (int k = 0; k < count; k++) | ||||
|             { | ||||
|                 WriteLE(dst, values[start + k], width); | ||||
|             } | ||||
|  | ||||
|             i += count; | ||||
|         } | ||||
|  | ||||
|         return dst.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Decoder ----------------- | ||||
|     public static ushort[] Decode(ReadOnlySpan<byte> src) | ||||
|     { | ||||
|         var result = new List<ushort>(); | ||||
|         int pos = 0; | ||||
|  | ||||
|         while (pos < src.Length) | ||||
|         { | ||||
|             byte h = src[pos++]; | ||||
|  | ||||
|             if ((h & 0x80) == 0) | ||||
|             { | ||||
|                 // Fast path byte (0..127) | ||||
|                 result.Add(h); | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             int count = ((h >> 3) & 0xF) + 1;  // 1..16 | ||||
|             int width = (h & 0x7) + 1;        // 1..8 (expect 1..2) | ||||
|  | ||||
|             if (width > 2) | ||||
|                 throw new NotSupportedException($"Width {width} bytes exceeds uint16 capacity."); | ||||
|  | ||||
|             for (int j = 0; j < count; j++) | ||||
|             { | ||||
|                 uint val = (uint)ReadLE(src, ref pos, width); | ||||
|                 if (val > 0xFFFFu) | ||||
|                     throw new OverflowException("Decoded value exceeds UInt16 range."); | ||||
|                 result.Add((ushort)val); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return result.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Helpers ----------------- | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static int WidthFromUnsigned(ushort v) => (v <= 0xFF) ? 1 : 2; | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static void WriteLE(List<byte> dst, ushort value, int width) | ||||
|     { | ||||
|         // width is 1 or 2 | ||||
|         dst.Add((byte)(value & 0xFF)); | ||||
|         if (width == 2) dst.Add((byte)(value >> 8)); | ||||
|     } | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static ulong ReadLE(ReadOnlySpan<byte> src, ref int pos, int width) | ||||
|     { | ||||
|         if (pos + width > src.Length) | ||||
|             throw new ArgumentException("Buffer underflow while reading payload."); | ||||
|  | ||||
|         ulong v = src[pos++]; // first byte (LSB) | ||||
|         if (width == 2) v |= (ulong)src[pos++] << 8; | ||||
|         return v; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										125
									
								
								Esiur/Data/GVWIE/GroupUInt32Codec.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										125
									
								
								Esiur/Data/GVWIE/GroupUInt32Codec.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,125 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| using System.Runtime.CompilerServices; | ||||
|  | ||||
| namespace Esiur.Data.GVWIE; | ||||
|  | ||||
| public static class GroupUInt32Codec | ||||
| { | ||||
|     // ----------------- Encoder ----------------- | ||||
|     public static byte[] Encode(IList<uint> values) | ||||
|     { | ||||
|         if (values is null) throw new ArgumentNullException(nameof(values)); | ||||
|  | ||||
|         var dst = new List<byte>(values.Count * 2); | ||||
|         int i = 0; | ||||
|  | ||||
|         while (i < values.Count) | ||||
|         { | ||||
|             uint v = values[i]; | ||||
|  | ||||
|             // Fast path: single byte for 0..127 | ||||
|             if (v <= 0x7Fu) | ||||
|             { | ||||
|                 dst.Add((byte)v); // MSB=0 implicitly | ||||
|                 i++; | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             // Group path: up to 16 items sharing a common width (1..4 bytes for uint32) | ||||
|             int start = i; | ||||
|             int count = 1; | ||||
|             int width = WidthFromUnsigned(v); | ||||
|  | ||||
|             while (count < 16 && (i + count) < values.Count) | ||||
|             { | ||||
|                 uint v2 = values[i + count]; | ||||
|                 int w2 = WidthFromUnsigned(v2); | ||||
|                 if (w2 > width) width = w2; | ||||
|                 count++; | ||||
|             } | ||||
|  | ||||
|             // Header: 1 | (count-1)[4b] | (width-1)[3b] | ||||
|             byte header = 0x80; | ||||
|             header |= (byte)(((count - 1) & 0xF) << 3); | ||||
|             header |= (byte)((width - 1) & 0x7); | ||||
|             dst.Add(header); | ||||
|  | ||||
|             // Payload | ||||
|             for (int k = 0; k < count; k++) | ||||
|             { | ||||
|                 WriteLE(dst, values[start + k], width); | ||||
|             } | ||||
|  | ||||
|             i += count; | ||||
|         } | ||||
|  | ||||
|         return dst.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Decoder ----------------- | ||||
|     public static uint[] Decode(ReadOnlySpan<byte> src) | ||||
|     { | ||||
|         var result = new List<uint>(); | ||||
|         int pos = 0; | ||||
|  | ||||
|         while (pos < src.Length) | ||||
|         { | ||||
|             byte h = src[pos++]; | ||||
|  | ||||
|             if ((h & 0x80) == 0) | ||||
|             { | ||||
|                 // Fast path byte (0..127) | ||||
|                 result.Add(h); | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             int count = ((h >> 3) & 0xF) + 1;  // 1..16 | ||||
|             int width = (h & 0x7) + 1;        // 1..8 (we expect 1..4) | ||||
|  | ||||
|             if (width > 4) | ||||
|                 throw new NotSupportedException($"Width {width} bytes exceeds uint32 capacity."); | ||||
|  | ||||
|             for (int j = 0; j < count; j++) | ||||
|             { | ||||
|                 uint val = (uint)ReadLE(src, ref pos, width); | ||||
|                 result.Add(val); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return result.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Helpers ----------------- | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static int WidthFromUnsigned(uint v) | ||||
|     { | ||||
|         if (v <= 0xFFu) return 1; | ||||
|         if (v <= 0xFFFFu) return 2; | ||||
|         if (v <= 0xFFFFFFu) return 3; | ||||
|         return 4; | ||||
|     } | ||||
|  | ||||
|     [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 ulong ReadLE(ReadOnlySpan<byte> src, ref int pos, int width) | ||||
|     { | ||||
|         if (pos + width > src.Length) | ||||
|             throw new ArgumentException("Buffer underflow while reading payload."); | ||||
|  | ||||
|         ulong v = 0; | ||||
|         for (int i = 0; i < width; i++) | ||||
|             v |= (ulong)src[pos++] << (8 * i); | ||||
|  | ||||
|         return v; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										132
									
								
								Esiur/Data/GVWIE/GroupUInt64Codec.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								Esiur/Data/GVWIE/GroupUInt64Codec.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,132 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Runtime.CompilerServices; | ||||
|  | ||||
| namespace Esiur.Data.GVWIE; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Runtime.CompilerServices; | ||||
|  | ||||
| public static class GroupUInt64Codec | ||||
| { | ||||
|     // ----------------- Encoder ----------------- | ||||
|     public static byte[] Encode(IList<ulong> values) | ||||
|     { | ||||
|         if (values is null) throw new ArgumentNullException(nameof(values)); | ||||
|  | ||||
|         var dst = new List<byte>(values.Count * 2); | ||||
|         int i = 0; | ||||
|  | ||||
|         while (i < values.Count) | ||||
|         { | ||||
|             ulong v = values[i]; | ||||
|  | ||||
|             // Fast path: single byte for 0..127 | ||||
|             if (v <= 0x7FUL) | ||||
|             { | ||||
|                 dst.Add((byte)v); // MSB = 0 implicitly | ||||
|                 i++; | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             // Group path: up to 16 items sharing max width (1..8 bytes) | ||||
|             int start = i; | ||||
|             int count = 1; | ||||
|             int width = WidthFromUnsigned(v); | ||||
|  | ||||
|             while (count < 16 && (i + count) < values.Count) | ||||
|             { | ||||
|                 ulong v2 = values[i + count]; | ||||
|                 int w2 = WidthFromUnsigned(v2); | ||||
|                 if (w2 > width) width = w2; | ||||
|                 count++; | ||||
|             } | ||||
|  | ||||
|             // Header: 1 | (count-1)[4b] | (width-1)[3b] | ||||
|             byte header = 0x80; | ||||
|             header |= (byte)(((count - 1) & 0xF) << 3); | ||||
|             header |= (byte)((width - 1) & 0x7); | ||||
|             dst.Add(header); | ||||
|  | ||||
|             // Payload | ||||
|             for (int k = 0; k < count; k++) | ||||
|                 WriteLE(dst, values[start + k], width); | ||||
|  | ||||
|             i += count; | ||||
|         } | ||||
|  | ||||
|         return dst.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Decoder ----------------- | ||||
|     public static ulong[] Decode(ReadOnlySpan<byte> src) | ||||
|     { | ||||
|         var result = new List<ulong>(); | ||||
|         int pos = 0; | ||||
|  | ||||
|         while (pos < src.Length) | ||||
|         { | ||||
|             byte h = src[pos++]; | ||||
|  | ||||
|             if ((h & 0x80) == 0) | ||||
|             { | ||||
|                 // Fast path byte (0..127) | ||||
|                 result.Add(h); | ||||
|                 continue; | ||||
|             } | ||||
|  | ||||
|             int count = ((h >> 3) & 0xF) + 1;  // 1..16 | ||||
|             int width = (h & 0x7) + 1;        // 1..8 | ||||
|  | ||||
|             if (width < 1 || width > 8) | ||||
|                 throw new NotSupportedException($"Invalid width {width} in header."); | ||||
|  | ||||
|             for (int j = 0; j < count; j++) | ||||
|             { | ||||
|                 ulong val = ReadLE(src, ref pos, width); | ||||
|                 result.Add(val); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return result.ToArray(); | ||||
|     } | ||||
|  | ||||
|     // ----------------- Helpers ----------------- | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static int WidthFromUnsigned(ulong v) | ||||
|     { | ||||
|         if (v <= 0xFFUL) return 1; | ||||
|         if (v <= 0xFFFFUL) return 2; | ||||
|         if (v <= 0xFFFFFFUL) return 3; | ||||
|         if (v <= 0xFFFFFFFFUL) return 4; | ||||
|         if (v <= 0xFFFFFFFFFFUL) return 5; | ||||
|         if (v <= 0xFFFFFFFFFFFFUL) return 6; | ||||
|         if (v <= 0xFFFFFFFFFFFFFFUL) return 7; | ||||
|         return 8; | ||||
|     } | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static void WriteLE(List<byte> dst, ulong value, int width) | ||||
|     { | ||||
|         for (int i = 0; i < width; i++) | ||||
|             dst.Add((byte)((value >> (8 * i)) & 0xFF)); | ||||
|     } | ||||
|  | ||||
|     [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||||
|     private static ulong ReadLE(ReadOnlySpan<byte> src, ref int pos, int width) | ||||
|     { | ||||
|         if (pos + width > src.Length) | ||||
|             throw new ArgumentException("Buffer underflow while reading payload."); | ||||
|  | ||||
|         ulong v = 0; | ||||
|         for (int i = 0; i < width; i++) | ||||
|             v |= (ulong)src[pos++] << (8 * i); | ||||
|  | ||||
|         return v; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user