Advertisement
Guest User

CompressedTable

a guest
Feb 17th, 2012
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Piglet.Common
  6. {
  7.     internal class CompressedTable : ITable2D
  8.     {
  9.         private readonly short[] displacement;
  10.         private readonly short[] data;
  11.  
  12.         public CompressedTable(short[,] uncompressed)
  13.         {
  14.             // Create a displacement table
  15.             var numStates = uncompressed.GetUpperBound(0) + 1;
  16.             displacement = new short[numStates];
  17.  
  18.             var table = new List<short>();
  19.  
  20.             // Add the first range straight away.
  21.             table.AddRange(StateActions(0, uncompressed));
  22.             displacement[0] = 0;
  23.  
  24.             // For each additional state, try to match as best as possible with the existing list
  25.             for (int state = 1; state < numStates; ++state)
  26.             {
  27.                 // Need to run *past* the table in order to add wholly incompatible matches
  28.                 // this will not index out of the table, so there is no need to worry.
  29.                 for (short displacementIndex = 0; displacementIndex <= table.Count(); ++displacementIndex)
  30.                 {
  31.                     bool spotFound = true;
  32.                     int offset = displacementIndex;
  33.                     foreach (var stateAction in StateActions(state, uncompressed))
  34.                     {
  35.                         if (offset >= table.Count())
  36.                         {
  37.                             // Run out of table to check, but is still OK.
  38.                             break;
  39.                         }
  40.                         if (stateAction != table[offset])
  41.                         {
  42.                             // Not found
  43.                             spotFound = false;
  44.                             break;
  45.                         }
  46.                         ++offset;
  47.                     }
  48.  
  49.                     // Exiting the loop, if a spot is found add the correct displacement index
  50.                     if (spotFound)
  51.                     {
  52.                         displacement[state] = displacementIndex;
  53.  
  54.                         // Add to the state table as much as is needed.
  55.                         table.AddRange(StateActions(state, uncompressed).Skip(offset - displacementIndex));
  56.  
  57.                         // Break loop to process next state.
  58.                         break;
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             data = table.ToArray();
  64.         }
  65.  
  66.         private IEnumerable<short> StateActions(int state, short[,] uncompressed)
  67.         {
  68.             for (int i = 0; i <= uncompressed.GetUpperBound(1); ++i)
  69.             {
  70.                 yield return uncompressed[state, i];
  71.             }
  72.         }
  73.  
  74.         public int this[int state, int input]
  75.         {
  76.             get { return data[displacement[state] + input]; }
  77.             set { throw new NotImplementedException(); }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement