public int Encode(Int64Entry *src, int count, byte *dst) { if (count <= 0) { return(count); } var minDeltaTicks = long.MaxValue; var maxNegDeltaValue = long.MinValue; var minPosDeltaValue = long.MaxValue; for (var i = 1; i < count; i++) { minDeltaTicks = Math.Min(minDeltaTicks, (src + i)->ticks - (src + i - 1)->ticks); var delta = (src + i)->value - (src + i - 1)->value; if (delta < 0) { maxNegDeltaValue = Math.Max(maxNegDeltaValue, delta); } else { minPosDeltaValue = Math.Min(minPosDeltaValue, delta); } } var start = dst; maxNegDeltaValue = -maxNegDeltaValue; // Encodes header Codec.EncodeInt64(src->ticks, ref dst); Codec.EncodeInt64(minDeltaTicks, ref dst); Codec.EncodeInt64(src->value, ref dst); Codec.EncodeInt64(maxNegDeltaValue, ref dst); Codec.EncodeInt64(minPosDeltaValue, ref dst); // Encodes data block for (var i = 1; i < count; i++) { Codec.EncodeInt64(((src + i)->ticks - (src + i - 1)->ticks) - minDeltaTicks, ref dst); var delta = (src + i)->value - (src + i - 1)->value; if (delta < 0) { Codec.EncodeInt64(true, -delta - maxNegDeltaValue, ref dst); } else { Codec.EncodeInt64(false, delta - minPosDeltaValue, ref dst); } } return((int)(dst - start)); }
public int Decode(byte *src, int len, Int64Entry *dst) { if (len <= 0) { return(len); } var end = src + len; // Decodes header dst->ticks = Codec.DecodeInt64(ref src); var minDeltaTicks = Codec.DecodeInt64(ref src); dst->value = Codec.DecodeInt64(ref src); var maxNegDeltaValue = Codec.DecodeInt64(ref src); var minPosDeltaValue = Codec.DecodeInt64(ref src); // Decodes data block var start = dst; dst++; while (src < end) { dst->ticks = (dst - 1)->ticks + Codec.DecodeInt64(ref src) + minDeltaTicks; var delta = Codec.DecodeInt64(ref src, out var isNegative); if (isNegative) { dst->value = (dst - 1)->value - (delta + maxNegDeltaValue); } else { dst->value = (dst - 1)->value + delta + minPosDeltaValue; } dst++; } return((int)(dst - start)); }