示例#1
0
        public void TestEncodeDecodeInt32()
        {
            var values = new int[32 * 4];

            for (var i = 1; i < 32; i++)
            {
                var value = (uint)Math.Pow(2, i);
                values[i * 4]     = (int)value - 1;
                values[i * 4 + 1] = (int)value;
                values[i * 4 + 2] = -values[i * 4];
                values[i * 4 + 3] = -values[i * 4 + 1];
            }

            var buffer = stackalloc byte[5];

            for (var i = 0; i < values.Length; i++)
            {
                var p = buffer;
                Codec.EncodeInt32(values[i], ref p);

                p = buffer;
                var value = Codec.DecodeInt32(ref p);
                Assert.AreEqual(values[i], value, "At idx: " + i);
            }
        }
示例#2
0
        public static void EncodeMinDelta32(byte *src, int len, int skip, ref byte *dst)
        {
            var start = src;
            var end   = src + len;
            var next  = sizeof(int) + skip;

            var maxNegDelta = int.MinValue;
            var minPosDelta = int.MaxValue;

            src += next;
            while (src < end)
            {
                var delta = *(int *)src - *(int *)(src - next);
                if (delta < 0)
                {
                    maxNegDelta = Math.Max(maxNegDelta, delta);
                }
                else
                {
                    minPosDelta = Math.Min(minPosDelta, delta);
                }

                src += next;
            }

            src         = start;
            start       = dst;
            maxNegDelta = -maxNegDelta;

            dst += sizeof(int);
            Codec.EncodeInt32(*(int *)src, ref dst);
            Codec.EncodeInt32(maxNegDelta, ref dst);
            Codec.EncodeInt32(minPosDelta, ref dst);

            src += next;
            while (src < end)
            {
                var delta = *(int *)src - *(int *)(src - next);
                if (delta < 0)
                {
                    Codec.EncodeInt32(true, -delta - maxNegDelta, ref dst);
                }
                else
                {
                    Codec.EncodeInt32(false, delta - minPosDelta, ref dst);
                }

                src += next;
            }

            *(int *)start = (int)(dst - start);
        }
示例#3
0
        public int Encode(ref byte *src, int lSrc, ref byte *dst, int lDst)
        {
            var data  = (Int32Entry *)src;
            var count = lSrc / sizeof(Int32Entry) * sizeof(Int32Entry);

            var minTicks    = long.MaxValue;
            var maxNegValue = int.MinValue;
            var minPosValue = int.MaxValue;

            for (var i = 0; i < count; i++)
            {
                minTicks = Math.Min(minTicks, (data + i)->ticks);
                if ((data + i)->value < 0)
                {
                    maxNegValue = Math.Max(maxNegValue, (data + i)->value);
                }
                else
                {
                    minPosValue = Math.Min(minPosValue, (data + i)->value);
                }
            }

            maxNegValue = -maxNegValue;
            var start = dst;

            // Encode header
            Codec.EncodeInt64(minTicks, ref dst);
            Codec.EncodeInt32(maxNegValue, ref dst);
            Codec.EncodeInt32(minPosValue, ref dst);

            for (var i = 0; i < count; i++)
            {
                Codec.EncodeInt64((data + i)->ticks - minTicks, ref dst);

                if ((data + i)->value < 0)
                {
                    Codec.EncodeInt32(true, -(data + i)->value - maxNegValue, ref dst);
                }
                else
                {
                    Codec.EncodeInt32(false, (data + i)->value - minPosValue, ref dst);
                }
            }

            return((int)(dst - start));
        }