Advertisement
Guest User

State.java

a guest
Feb 13th, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.53 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class State implements Comparable <State> {
  4.    
  5.     private static int activeCellsCount, arraySize;
  6.     private static short [] netrepValues;
  7.     private static int   [] netrepIndices, lastEqual1, lastEqual2;
  8.     private static SortedSet <int []> flagsCollection;
  9.     private static List <State> stateList;
  10.    
  11.     private short porterCell, move, moveCount, pushCount;
  12.     private int [] cellFlags;
  13.     private State previousState;
  14.    
  15.     public static void init (MapHandler .NetworkRep netrep) {
  16.        
  17.         activeCellsCount = netrep .activeCellsCount ();
  18.         arraySize = (activeCellsCount >> 5) + (0 == (activeCellsCount & 31) ? 0 : 1);
  19.         netrepValues  = netrep .values ();
  20.         netrepIndices = netrep .indices ();
  21.         lastEqual1 = null;
  22.         lastEqual2 = null;
  23.         flagsCollection = new TreeSet   <> (new ArrayComparator ());
  24.         stateList       = new ArrayList <> ();
  25.     }
  26.    
  27.     public State (short [] stateCells) {
  28.         int k, size, cell;
  29.        
  30.         size = stateCells .length - 1;
  31.         porterCell = stateCells [size];
  32.         move = -1;
  33.         moveCount = 0;
  34.         pushCount = 0;
  35.         cellFlags = new int [arraySize];
  36.        
  37.         for (k = 0; size > k; ++k) {
  38.             cell = stateCells [k];
  39.             cellFlags [cell >> 5] |= 1 << (cell & 31);
  40.         }
  41.         if (flagsCollection .isEmpty ()) {
  42.             flagsCollection .add (cellFlags);
  43.         } else {
  44.             cellFlags = checkMasksCollection (cellFlags);
  45.         }
  46.         previousState = null;
  47.     }
  48.    
  49.     public State previousState () {
  50.         return previousState;
  51.     }
  52.    
  53.     public short move () {
  54.         return move;
  55.     }
  56.    
  57.     public boolean isSamePlacementAs (State state) {
  58.         return cellFlags == state .cellFlags;
  59.     }
  60.    
  61.     public static int placementCount () {
  62.         return flagsCollection .size ();
  63.     }
  64.    
  65.     public short moveCount () {
  66.         return moveCount;
  67.     }
  68.    
  69.     public short pushCount () {
  70.         return pushCount;
  71.     }
  72.    
  73.     public short [] stateCells () {
  74.         int k, value, mask;
  75.         short cell;
  76.         ArrayList <Short> cellsList = new ArrayList <> ();
  77.         short [] result;
  78.        
  79.         cell = 0;
  80.         for (k = 0; arraySize > k; ++k) {
  81.             value = cellFlags [k];
  82.             for (mask = 1; 0 != mask; mask <<= 1, ++cell) {
  83.                 if (0 != (value & mask)) {
  84.                     cellsList .add (cell);
  85.                 }
  86.             }
  87.         }
  88.         cellsList .add (porterCell);
  89.        
  90.         value = cellsList .size ();
  91.         result = new short [value];
  92.        
  93.         for (k = 0; value > k; ++k) {
  94.             result [k] = cellsList .get (k);
  95.         }
  96.        
  97.         return result;
  98.     }
  99.    
  100.     private State (short porterCell, short move, short moveCount, short pushCount, int [] cellFlags, State previousState) {
  101.         this .porterCell    = porterCell;
  102.         this .move          = move;
  103.         this .moveCount     = moveCount;
  104.         this .pushCount     = pushCount;
  105.         this .cellFlags     = cellFlags;
  106.         this .previousState = previousState;
  107.     }
  108.    
  109.     public State [] expandForth () {
  110.         int positionIndex, indexLimit;
  111.         int maskAdress_1, cllFlgMask_1, flagsValue_1;
  112.         int maskAdress_2, cllFlgMask_2, flagsValue_2;
  113.         short move, cell_1, cell_2;
  114.         int [] newCellFlags;
  115.        
  116.         stateList .clear ();
  117.         positionIndex = netrepIndices [porterCell];
  118.         indexLimit    = netrepIndices [porterCell + 1];
  119.        
  120.         while (indexLimit > positionIndex) {
  121.             move   = netrepValues [positionIndex];
  122.             ++positionIndex;
  123.             cell_1 = netrepValues [positionIndex];
  124.             maskAdress_1 = cell_1 >> 5;
  125.             cllFlgMask_1 = 1 << (cell_1 & 31);
  126.             flagsValue_1 = cellFlags [maskAdress_1];
  127.             if (0 == (flagsValue_1 & cllFlgMask_1)) {
  128.                 positionIndex += 3;
  129.                 stateList .add (new State (
  130.                         cell_1, move,
  131.                         (short) (moveCount + 1), pushCount, cellFlags, this));
  132.                 continue;
  133.             }
  134.             ++positionIndex;
  135.             cell_2 = netrepValues [positionIndex];
  136.             positionIndex += 2;
  137.             if (0 > cell_2) {
  138.                 continue;
  139.             }
  140.             maskAdress_2 = cell_2 >> 5;
  141.             cllFlgMask_2   = 1 << (cell_2 & 31);
  142.             if (maskAdress_1 == maskAdress_2) {
  143.                 if (0 != (flagsValue_1 & cllFlgMask_2)) {
  144.                     continue;
  145.                 }
  146.                 newCellFlags = Arrays .copyOf (cellFlags, arraySize);
  147.                 newCellFlags [maskAdress_1] = flagsValue_1 ^ cllFlgMask_1 ^ cllFlgMask_2;
  148.             } else {
  149.                 flagsValue_2 = cellFlags [maskAdress_2];
  150.                 if (0 != (flagsValue_2 & cllFlgMask_2)) {
  151.                     continue;
  152.                 }
  153.                 newCellFlags = Arrays .copyOf (cellFlags, arraySize);
  154.                 newCellFlags [maskAdress_1] = flagsValue_1 ^ cllFlgMask_1;
  155.                 newCellFlags [maskAdress_2] = flagsValue_2 ^ cllFlgMask_2;
  156.             }
  157.             stateList .add (new State (
  158.                     cell_1, move,
  159.                     (short) (moveCount + 1),
  160.                     (short) (pushCount + 1), checkMasksCollection (newCellFlags), this));
  161.         }
  162.        
  163.         return stateList .toArray (new State [stateList .size ()]);
  164.     }
  165.    
  166.     public State [] expandBack () {
  167.         int positionIndex, indexLimit;
  168.         int maskAdress_1, cllFlgMask_1, flagsValue_1;
  169.         int maskAdress_2, cllFlgMask_2, flagsValue_2;
  170.         int maskAdress_3, cllFlgMask_3;
  171.         short move, cell_1, cell_2;
  172.         int [] newCellFlags;
  173.        
  174.         stateList .clear ();
  175.         positionIndex = netrepIndices [porterCell];
  176.         indexLimit    = netrepIndices [porterCell + 1];
  177.        
  178.         while (indexLimit > positionIndex) {
  179.             move   = netrepValues [positionIndex];
  180.             ++positionIndex;
  181.             cell_1 = netrepValues [positionIndex];
  182.             maskAdress_1 = cell_1 >> 5;
  183.             cllFlgMask_1 = 1 << (cell_1 & 31);
  184.             flagsValue_1 = cellFlags [maskAdress_1];
  185.             if (0 != (flagsValue_1 & cllFlgMask_1)) {
  186.                 positionIndex += 3;
  187.                 continue;
  188.             }
  189.             stateList .add (new State (
  190.                     cell_1, move,
  191.                     (short) (moveCount + 1), pushCount, cellFlags, this));
  192.             positionIndex += 2;
  193.             cell_2 = netrepValues [positionIndex];
  194.             ++positionIndex;
  195.             if (0 > cell_2) {
  196.                 continue;
  197.             }
  198.             maskAdress_2 = cell_2 >> 5;
  199.             cllFlgMask_2 = 1 << (cell_2 & 31);
  200.             if (maskAdress_1 == maskAdress_2) {
  201.                 flagsValue_2 = flagsValue_1;
  202.             } else {
  203.                 flagsValue_2 = cellFlags [maskAdress_2];
  204.             }
  205.             if (0 == (flagsValue_2 & cllFlgMask_2)) {
  206.                 continue;
  207.             }
  208.             newCellFlags = Arrays .copyOf (cellFlags, arraySize);
  209.             maskAdress_3 = porterCell >> 5;
  210.             cllFlgMask_3 = 1 << (porterCell & 31);
  211.             if (maskAdress_2 == maskAdress_3) {
  212.                 newCellFlags [maskAdress_2] = flagsValue_2 ^ cllFlgMask_2 ^ cllFlgMask_3;
  213.             } else {
  214.                 newCellFlags [maskAdress_2] = flagsValue_2 ^ cllFlgMask_2;
  215.                 newCellFlags [maskAdress_3] ^=               cllFlgMask_3;
  216.             }
  217.             stateList .add (new State (
  218.                     cell_1, (short) (-move - 1),
  219.                     (short) (moveCount + 1),
  220.                     (short) (pushCount + 1), checkMasksCollection (newCellFlags), this));
  221.         }
  222.        
  223.         return stateList .toArray (new State [stateList .size ()]);
  224.     }
  225.    
  226.     @Override
  227.     public int compareTo (State state) {
  228.         int k, value1, value2;
  229.        
  230.         if (cellFlags != state .cellFlags) {
  231.             for (k = 0; arraySize > k; ++k) {
  232.                 value1 =        cellFlags [k];
  233.                 value2 = state .cellFlags [k];
  234.                 if (value1 > value2) {
  235.                     return 1;
  236.                 }
  237.                 if (value1 < value2) {
  238.                     return -1;
  239.                 }
  240.             }
  241.         }
  242.         if (porterCell > state .porterCell) {
  243.             return 1;
  244.         }
  245.         if (porterCell < state .porterCell) {
  246.             return -1;
  247.         }
  248.         return 0;
  249.     }
  250.    
  251.     private static int [] checkMasksCollection (int [] newMask) {
  252.        
  253.         if (flagsCollection .contains (newMask)) {
  254.             if (newMask == lastEqual1) {
  255.                 return lastEqual2;
  256.             }
  257.             return lastEqual1;
  258.         }
  259.         flagsCollection .add (newMask);
  260.         return newMask;
  261.     }
  262.    
  263.     private static class ArrayComparator implements Comparator <int []> {
  264.  
  265.         @Override
  266.         public int compare (int [] array1, int [] array2) {
  267.             int k, value1, value2;
  268.            
  269.             for (k = 0; arraySize > k; ++k) {
  270.                 value1 = array1 [k];
  271.                 value2 = array2 [k];
  272.                 if (value1 > value2) {
  273.                     return 1;
  274.                 }
  275.                 if (value1 < value2) {
  276.                     return -1;
  277.                 }
  278.             }
  279.             lastEqual1 = array1;
  280.             lastEqual2 = array2;
  281.             return 0;
  282.         }
  283.     }
  284. }
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement