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);
        }
Example #2
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);
        }