title: Determine if two chess positions are equal
body: <p>I'm currently debugging my transposition table for a <strong>chess variant engine where pieces can be placed (ie. originally not on the board)</strong>. I need to know how often I'm hitting key collisions. I'm saving the piece list in each table index, along with the usual hash data. My simple solution for determining if two positions are equal is failing on transpositions because I'm linearly comparing the two piece lists.</p>
  
  <p><strong>Please do not suggest that I should be storing by board-centric instead of piece-centric</strong>. I have to store the piece list because of the unique nature of placable and captured pieces. Pieces in those states are like they are occupying an overlapping and position-less location. <strong>Please look at the description of how pieces are stored</strong>.</p>
  
  <pre><code>// [Piece List]
  // 
  // Contents: The location of the pieces.
  //           Values 0-63 are board indexes; -2 is dead; -1 is placeable
  // Structure: Black pieces are at indexes 0-15
  //            White pieces are at indexes 16-31
  //            Within each set of colors the pieces are arranged as following:
  //            8 Pawns, 2 Knights, 2 Bishops, 2 Rooks, 1 Queen, 1 King
  // Example: piece[15] = 6 means the black king is on board index 6
  //          piece[29] = -2 means the white rook is dead
  char piece[32];
  </code></pre>
  
  <p>A <strong>transposition happens</strong> when pieces are moved in a different order, but the end result is the same board position. For example the following positions are equal:</p>
  
  <pre><code>1) first rook on A1; second rook on D7
  2) first rook on D7; second rook on A1
  </code></pre>
  
  <p>The following is a <strong>non-optimised general algorithm</strong>; and the inner loop is similar to another <a href="https://stackoverflow.com/questions/3886986">general problem</a>, but with the added restraint that values in 0-63 will only happen once (ie. only one piece per square).</p>
  
  <pre><code>for each color:
      for each piece type:
          are all pieces in the same position, disregarding transpositions?
  </code></pre>
  
  <p>The following comparison does <strong>NOT work because of transpositions</strong>. What I need is a way to detect transpositions as equal and only report actually different positions.</p>
  
  <pre><code>bool operator==(const Position &amp;b)
  {
      for (int i = 0; i &lt; 32; i++)
          if (piece[i] != b.piece[i])
              return false;
      return true;
  }
  </code></pre>
  
  <p><strong>Performance/memory is a consideration</strong> because the table gets over 100K hits (where keys are equal) per turn and a typical table has 1 million items. Henceforth, I'm looking for something <strong>faster than copying and sorting the lists</strong>.</p>