MacroState.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package net.hkzlab.palanalisys;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. public class MacroState {
  5. public static final String MS_PRE_TAG = "MS_";
  6. public final String tag;
  7. public final boolean[] rpin_status;
  8. public final SubState[] substates;
  9. public final StateLink[] links;
  10. public MacroState(final String tag, final boolean[] rpin_status, final int outPins, final int inPins) {
  11. this.tag = tag;
  12. this.rpin_status = rpin_status;
  13. links = new StateLink[2 ^ inPins]; // Create space for the future links out of this
  14. substates = new SubState[3 ^ outPins]; // Create space for substates (each output pin is 3-state)
  15. }
  16. @Override
  17. public String toString() {
  18. final StringBuffer strBuf = new StringBuffer();
  19. strBuf.append(MS_PRE_TAG + tag + " - ");
  20. for (final boolean rpin : rpin_status)
  21. strBuf.append(rpin ? '1' : '0');
  22. return strBuf.toString();
  23. }
  24. @Override
  25. public int hashCode() {
  26. int hash = 0;
  27. for (int idx = 0; idx < rpin_status.length; idx++) {
  28. hash ^= ((rpin_status[idx] ? 1 : 0) << (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.tag.equals(this.tag)) return false;
  42. if(!Arrays.equals(ops.rpin_status, this.rpin_status)) 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. }