Переглянути джерело

Restructure macro states to allow 2^in number of substates

Baglio Tabifata 5 роки тому
батько
коміт
3c965d987b

+ 5 - 2
src/net/hkzlab/palanalisys/MacroState.java

@@ -1,22 +1,25 @@
 package net.hkzlab.palanalisys;
 
-import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 
 public class MacroState {
     public static final String MS_PRE_TAG = "MS_";
 
     public final String tag;
     public final boolean[] rpin_status;
+
     public final SubState[] substates;
     public final StateLink[] links;
+    private final HashMap<Integer, SubState> ssMap;
 
     public MacroState(final String tag, final boolean[] rpin_status, final int outPins, final int inPins) {
         this.tag = tag;
         this.rpin_status = rpin_status;
 
         links = new StateLink[2 ^ inPins]; // Create space for the future links out of this
-        substates = new SubState[3 ^ outPins]; // Create space for substates (each output pin is 3-state)
+        substates = new SubState[2 ^ inPins]; // Create space for substates (each output pin is 3-state, but as they're triggered via input changes, we can have at most 2^inPins)
+        ssMap = new HashMap<>(); // Prepare the hashmap we'll use to avoid substate duplicates
     }
 
     @Override

+ 16 - 9
src/net/hkzlab/palanalisys/SubState.java

@@ -31,14 +31,7 @@ public class SubState {
 
     @Override
     public int hashCode() {
-        int hash = 0;
-
-        for(int idx = 0; idx < pin_status.length; idx++) {
-            int byte_idx = idx % 4;
-            hash ^= (pin_status[idx] & 0xFF) << (8 * byte_idx);
-        }
-
-        return hash ^ tag.hashCode();
+        return calculateSubStateKey(pin_status) ^ tag.hashCode();
     }
 
     @Override
@@ -54,7 +47,7 @@ public class SubState {
         return true;
     }
 
-    public static int calculateSubStateIndex(byte[] pinStatus) {
+    public static int calculateSubStateIndex(final byte[] pinStatus) {
         int index = 0;
 
         for(int idx = 0; idx < pinStatus.length; idx++) {
@@ -63,4 +56,18 @@ public class SubState {
 
         return index;
     }
+
+    /**
+     * 
+     */
+    public static int calculateSubStateKey(final byte[] in_comb) {
+        int hash = 0;
+
+        for(int idx = 0; idx < in_comb.length; idx++) {
+            int byte_idx = idx % 4;
+            hash ^= (in_comb[idx] & 0xFF) << (8 * byte_idx);
+        }
+
+        return hash;       
+    }
 }