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);
        }
示例#3
0
        public int ReadCounts(ByteBuffer sourceBuffer, int lengthInBytes, int maxIndex, Action <int, long> setCount)
        {
            var idx         = 0;
            int endPosition = sourceBuffer.Position + lengthInBytes;

            while (sourceBuffer.Position < endPosition && idx < maxIndex)
            {
                var item = ZigZagEncoding.GetLong(sourceBuffer);
                if (item < 0)
                {
                    var zeroCounts = -(item);
                    if (zeroCounts > int.MaxValue)
                    {
                        throw new ArgumentException("An encoded zero count of > int.MaxValue was encountered in the source");
                    }
                    idx += (int)zeroCounts;
                }
                else
                {
                    setCount(idx++, item);
                }
            }
            return(idx);
        }