public void CanRoundTrip() { var bufferLength = 20; var input = new long[] { 1234, 5678, 9101112, 13141516, 17181920, 21222324 }; var buffer = ByteBuffer.Allocate(bufferLength); foreach (var value in input) { ZigZagEncoding.PutLong(buffer, value); } buffer.Position = 0; var output = new long[input.Length]; for (int i = 0; i < output.Length; i++) { output[i] = ZigZagEncoding.GetLong(buffer); } CollectionAssert.AreEqual(input, output); }
private static int FillBufferFromCountsArray(ByteBuffer buffer, IRecordedData data) { int startPosition = buffer.Position; int srcIndex = 0; while (srcIndex < data.Counts.Length) { // V2 encoding format uses a ZigZag LEB128-64b9B encoded long. // Positive values are counts, while negative values indicate a repeat zero counts. i.e. -4 indicates 4 sequential buckets with 0 counts. long count = GetCountAtIndex(srcIndex++, data); if (count < 0) { throw new InvalidOperationException($"Cannot encode histogram containing negative counts ({count}) at index {srcIndex}"); } // Count trailing 0s (which follow this count): long zerosCount = 0; if (count == 0) { zerosCount = 1; while ((srcIndex < data.Counts.Length) && (GetCountAtIndex(srcIndex, data) == 0)) { zerosCount++; srcIndex++; } } if (zerosCount > 1) { ZigZagEncoding.PutLong(buffer, -zerosCount); } else { ZigZagEncoding.PutLong(buffer, count); } } return(buffer.Position - startPosition); }