00001
00002
00003
00004
00005
00006 namespace NewGamePhysics.Physics
00007 {
00008 using System;
00009 using System.Text;
00010
00011 using NewGamePhysics.Utilities;
00012
00024 public class SimpleEntropyCollector
00025 {
00029 private int poolSize;
00030
00034 private bool[] bitPool;
00035
00039 private int bitsInPool;
00040
00045 private int numValues;
00046
00050 private double lastValue;
00051
00056 private int numBits;
00057
00061 private bool lastBit;
00062
00066 private BitCount bitCount;
00067
00071 public SimpleEntropyCollector()
00072 {
00073
00074 bitCount = new BitCount();
00075
00076
00077 this.Reset();
00078 }
00079
00083 public int BitsInPool
00084 {
00085 get { return bitsInPool; }
00086 }
00087
00091 public void Reset()
00092 {
00093
00094 this.poolSize = 1024;
00095 this.bitsInPool = 0;
00096 this.bitPool = new bool[poolSize];
00097 this.numValues = 0;
00098 this.numBits = 0;
00099 }
00100
00105 public void AddValue(double value)
00106 {
00107 if (numValues == 1)
00108 {
00109
00110 double delta = this.lastValue - value;
00111
00112
00113 long deltaBits = BitConverter.DoubleToInt64Bits(delta);
00114
00115
00116 int totalBits = this.bitCount.FastBitcount(deltaBits);
00117
00118
00119 bool newBit = (bool)((totalBits & 1) == 1);
00120
00121
00122 if (numBits == 1)
00123 {
00124
00125 if ((!newBit) && (this.lastBit))
00126 {
00127
00128 if (this.bitsInPool < 1024)
00129 {
00130 this.bitPool[this.bitsInPool] = true;
00131 this.bitsInPool++;
00132 }
00133 }
00134
00135 else if ((newBit) && (!this.lastBit))
00136 {
00137
00138 if (this.bitsInPool < 1024)
00139 {
00140 this.bitPool[this.bitsInPool] = false;
00141 this.bitsInPool++;
00142 }
00143 }
00144 else
00145 {
00146
00147 }
00148
00149 this.numBits = 0;
00150 }
00151 else
00152 {
00153
00154 this.lastBit = newBit;
00155 this.numBits++;
00156 }
00157
00158
00159 this.numValues = 0;
00160 }
00161 else
00162 {
00163
00164 this.lastValue = value;
00165
00166
00167 this.numValues = 1;
00168 }
00169 }
00170
00176 public override string ToString()
00177 {
00178 StringBuilder sb = new StringBuilder();
00179
00180 if (this.bitsInPool > 0)
00181 {
00182 for (int i = bitsInPool - 1; i >= 0; i--)
00183 {
00184 if (bitPool[i])
00185 {
00186 sb.Append("1");
00187 }
00188 else
00189 {
00190 sb.Append("0");
00191 }
00192 }
00193 }
00194
00195 return sb.ToString();
00196 }
00197
00198 }
00199 }