MacroState.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package net.hkzlab.palanalisys;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. public class MacroState {
  5. public static final String MS_PRE_TAG = "MS_";
  6. public final String tag;
  7. public final int rpins;
  8. public final int rpin_status;
  9. public final SubState[] substates;
  10. public final StateLink[] links;
  11. public final HashMap<Integer, SubState> ssMap;
  12. public MacroState(final String tag, final int rpin_status, final int rpins, final int inPins) {
  13. this.tag = tag;
  14. this.rpin_status = rpin_status;
  15. this.rpins = rpins;
  16. links = new StateLink[1 << inPins]; // Create space for the future links out of this
  17. substates = new SubState[1 << 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)
  18. ssMap = new HashMap<>(); // Prepare the hashmap we'll use to avoid substate duplicates
  19. }
  20. @Override
  21. public String toString() {
  22. return MS_PRE_TAG + tag + " - " + String.format("%02X", rpin_status);
  23. }
  24. @Override
  25. public int hashCode() {
  26. int hash = 0;
  27. for (int idx = 0; idx < (1 << rpins); idx++) {
  28. hash ^= (((rpin_status >> idx) & 0x01) << (idx % 32));
  29. }
  30. return hash ^ tag.hashCode();
  31. }
  32. @Override
  33. public boolean equals(final Object o) {
  34. if (this == o)
  35. return true;
  36. if (o == null)
  37. return false;
  38. if (this.getClass() != o.getClass())
  39. return false;
  40. final MacroState ops = (MacroState) o;
  41. if(ops.rpin_status != this.rpin_status) return false;
  42. if(!ops.tag.equals(this.tag)) return false;
  43. return true;
  44. }
  45. public static int calculateMacroStateIndex(boolean[] rpinStatus) {
  46. int index = 0;
  47. for(int idx = 0; idx < rpinStatus.length; idx++) {
  48. index |= ((rpinStatus[idx] ? 1:0) << idx);
  49. }
  50. return index;
  51. }
  52. }