SubState.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package net.hkzlab.palanalisys;
  2. import java.util.Arrays;
  3. public class SubState {
  4. public static final String SS_PRE_TAG = "SS_";
  5. public final String tag;
  6. public final MacroState macroState;
  7. public final Byte[] pin_status;
  8. public SubState(final String tag, final MacroState macroState, final Byte[] pin_status) {
  9. this.tag = tag;
  10. this.macroState = macroState;
  11. this.pin_status = pin_status;
  12. }
  13. @Override
  14. public String toString() {
  15. StringBuffer strBuf = new StringBuffer();
  16. strBuf.append(SS_PRE_TAG+tag+"-");
  17. for(byte pin : pin_status) {
  18. if(pin < 0) strBuf.append('x');
  19. else if (pin == 0) strBuf.append(0);
  20. else strBuf.append(1);
  21. }
  22. return strBuf.toString();
  23. }
  24. @Override
  25. public int hashCode() {
  26. return calculateSubStateKey(pin_status) ^ tag.hashCode();
  27. }
  28. @Override
  29. public boolean equals(Object o) {
  30. if(this == o) return true;
  31. if(o == null) return false;
  32. if(this.getClass() != o.getClass()) return false;
  33. SubState ops = (SubState)o;
  34. if(!ops.tag.equals(this.tag)) return false;
  35. if(!Arrays.equals(ops.pin_status, this.pin_status)) return false;
  36. return true;
  37. }
  38. public static int calculateSubStateIndex(final boolean[] inputs) {
  39. int index = 0;
  40. for(int idx = 0; idx < inputs.length; idx++) {
  41. index += ((inputs[idx] ? 1 : 0) << idx);
  42. }
  43. return index;
  44. }
  45. /**
  46. *
  47. */
  48. public static int calculateSubStateKey(final Byte[] out_comb) {
  49. int hash = 0;
  50. for(int idx = 0; idx < out_comb.length; idx++) {
  51. int byte_idx = idx % 4;
  52. hash ^= (out_comb[idx] & 0xFF) << (8 * byte_idx);
  53. }
  54. return hash;
  55. }
  56. }