//============================================================== // Copyright (C) 2020 Chongdaoyang Inc. All rights reserved. // //============================================================== // Create by 种道洋 at 2020/6/21 13:05:15 . // Version 1.0 // CDYWORK //============================================================== using Cdy.Tag; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DBRuntime.His.Compress { /// /// /// public class FloatCompressBuffer { #region ... Variables ... private int index = 0; float[] mBuffer; private float mMaxValue; private int mPrecision; private bool mCanCompress = false; private float mLastValue; private long mPrecisionValue; #endregion ...Variables... #region ... Events ... #endregion ...Events... #region ... Constructor... /// /// /// /// public FloatCompressBuffer(int count) { mBuffer = new float[count]; } #endregion ...Constructor... #region ... Properties ... /// /// /// public int Precision { get { return mPrecision; } set { mPrecision = value; if (value > 0) { mPrecisionValue = (long)Math.Pow(10, value); mMaxValue = Int32.MaxValue / mPrecisionValue; } } } /// /// /// public MemoryBlock MemoryBlock { get; set; } /// /// /// public ProtoMemory VarintMemory { get; set; } #endregion ...Properties... #region ... Methods ... /// /// /// /// public void CheckAndResizeTo(int count) { if(count>mBuffer.Length) { mBuffer = new float[count]; } } /// /// /// /// /// private bool CanDecompress(float value) { return value < mMaxValue; } /// /// 将浮点数转换成整数 /// /// /// private int TranslateData(float value) { return (int)(value * mPrecisionValue); } /// /// /// /// public void Append(float value) { if (index == 0) { mBuffer[index] = value; mLastValue = value; } else { var val = (value - mLastValue); if(!CanDecompress(val)) { mCanCompress = false; } mBuffer[index] = val; mLastValue = value; } index++; } /// /// /// public void Reset() { index = 0; mCanCompress = true; } /// /// /// public void Compress() { if (mCanCompress && index>2) { MemoryBlock.Write((byte)this.Precision); MemoryBlock.Write(mBuffer[0]); for (int i = 1; i < index; i++) { VarintMemory.WriteSInt32(TranslateData(mBuffer[i])); } MemoryBlock.WriteBytes(9, VarintMemory.DataBuffer,0,(int)VarintMemory.WritePosition); } else { MemoryBlock.Write(byte.MaxValue); for (int i = 0; i < index; i++) { if (i == 0) { mLastValue = mBuffer[i]; } else { mLastValue += mBuffer[i]; } MemoryBlock.Write(mLastValue); } } } /// /// /// public static List Decompress(byte[] values) { var memory = new MemorySpan(values); var type = memory.ReadByte(); List re = new List(300); var dval = (float)Math.Pow(10, type); if(type == byte.MaxValue) { while(memory.Position