- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/Physics/SimpleEntropyCollector.cs

    00001 // --------------------------------------------------------
    00002 // SimpleEntropyCollector.cs
    00003 // (A. Schiffler, 2009)
    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             // Initialize bitcount helper
    00074             bitCount = new BitCount();
    00075
    00076             // Initialize collector state
    00077             this.Reset();
    00078         }
    00079
    00083         public int BitsInPool
    00084         {
    00085             get { return bitsInPool; }
    00086         }
    00087
    00091         public void Reset()
    00092         {
    00093             // Reset pool and value trackers
    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                 // Different between values
    00110                 double delta = this.lastValue - value;
    00111
    00112                 // Translate the double into a 64 bit long.
    00113                 long deltaBits = BitConverter.DoubleToInt64Bits(delta);
    00114
    00115                 // Get bitcount
    00116                 int totalBits = this.bitCount.FastBitcount(deltaBits);
    00117
    00118                 // Generate this bit
    00119                 bool newBit = (bool)((totalBits & 1) == 1);
    00120
    00121                 // von Neumann corrector to unbias bitstream
    00122                 if (numBits == 1)
    00123                 {
    00124                     // Check for 10: add a 1
    00125                     if ((!newBit) && (this.lastBit))
    00126                     {
    00127                         // Use LSB of bitcount
    00128                         if (this.bitsInPool < 1024)
    00129                         {
    00130                             this.bitPool[this.bitsInPool] = true;
    00131                             this.bitsInPool++;
    00132                         }
    00133                     }
    00134                     // Check for 01: add a 0
    00135                     else if ((newBit) && (!this.lastBit))
    00136                     {
    00137                         // Use LSB of bitcount
    00138                         if (this.bitsInPool < 1024)
    00139                         {
    00140                             this.bitPool[this.bitsInPool] = false;
    00141                             this.bitsInPool++;
    00142                         }
    00143                     }
    00144                     else
    00145                     {
    00146                         // Discard 00 and 11 patterns
    00147                     }
    00148                     // Reset corrector counter
    00149                     this.numBits = 0;
    00150                 }
    00151                 else
    00152                 {
    00153                     // Store bit
    00154                     this.lastBit = newBit;
    00155                     this.numBits++;
    00156                 }
    00157
    00158                 // Reset value count to we cache the next submission
    00159                 this.numValues = 0;
    00160             }
    00161             else
    00162             {
    00163                 // Just cache value for next call
    00164                 this.lastValue = value;
    00165
    00166                 // Set value count so we process the next submission
    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 }