Tree.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package me.km.snuviscript;
  2. import me.km.exception.GoHigherAtRootTreeException;
  3. import me.km.exception.NoChildTreeException;
  4. import it.unimi.dsi.fastutil.objects.ObjectArrayList;
  5. import java.util.Stack;
  6. public class Tree<T>
  7. {
  8. private final Node<T> root;
  9. private Node<T> currentNode;
  10. private final Stack<Integer> position;
  11. public Tree()
  12. {
  13. root = new Node<>();
  14. root.parent = null;
  15. root.data = null;
  16. root.children = new ObjectArrayList<>();
  17. root.selectedChild = -1;
  18. currentNode = root;
  19. position = new Stack<>();
  20. }
  21. public void clear()
  22. {
  23. root.children.clear();
  24. currentNode = root;
  25. position.clear();
  26. root.selectedChild = -1;
  27. }
  28. private static class Node<T>
  29. {
  30. private int actualCodeLine;
  31. private T data;
  32. private Node<T> parent;
  33. private ObjectArrayList<Node<T>> children;
  34. private int selectedChild;
  35. }
  36. public void addChild(T child, int actualCodeLine)
  37. {
  38. Node<T> node = new Node<>();
  39. node.data = child;
  40. node.parent = currentNode;
  41. node.children = new ObjectArrayList<>();
  42. node.selectedChild = -1;
  43. node.actualCodeLine = actualCodeLine;
  44. currentNode.children.add(node);
  45. }
  46. public int getActualCodeLine()
  47. {
  48. return currentNode.children.get(currentNode.selectedChild).actualCodeLine;
  49. }
  50. public void goHigher() throws GoHigherAtRootTreeException
  51. {
  52. currentNode.selectedChild = 0;
  53. currentNode = currentNode.parent;
  54. if(currentNode == null)
  55. {
  56. currentNode = root;
  57. throw new GoHigherAtRootTreeException();
  58. }
  59. position.pop();
  60. }
  61. public void goDeeper() throws NoChildTreeException
  62. {
  63. try
  64. {
  65. int i = currentNode.selectedChild;
  66. currentNode = currentNode.children.get(i);
  67. position.add(i);
  68. currentNode.selectedChild = -1;
  69. }
  70. catch(IndexOutOfBoundsException ex)
  71. {
  72. throw new NoChildTreeException();
  73. }
  74. }
  75. public void selectChild(int index) throws NoChildTreeException
  76. {
  77. if(index >= currentNode.children.size())
  78. {
  79. throw new NoChildTreeException();
  80. }
  81. currentNode.selectedChild = index;
  82. }
  83. public void selectNextChild() throws NoChildTreeException
  84. {
  85. currentNode.selectedChild++;
  86. if(currentNode.selectedChild >= currentNode.children.size())
  87. {
  88. currentNode.selectedChild--;
  89. throw new NoChildTreeException();
  90. }
  91. }
  92. public void selectPreviousChild()
  93. {
  94. currentNode.selectedChild--;
  95. }
  96. public void selectLastChild() throws NoChildTreeException
  97. {
  98. currentNode.selectedChild = currentNode.children.size() - 1;
  99. if(currentNode.selectedChild == -1)
  100. {
  101. throw new NoChildTreeException();
  102. }
  103. }
  104. public T getCurrentData()
  105. {
  106. return currentNode.data;
  107. }
  108. public T getCurrentChildData() throws NoChildTreeException
  109. {
  110. try
  111. {
  112. return currentNode.children.get(currentNode.selectedChild).data;
  113. }
  114. catch(IndexOutOfBoundsException ex)
  115. {
  116. throw new NoChildTreeException();
  117. }
  118. }
  119. public void goToRoot()
  120. {
  121. try
  122. {
  123. while(true)
  124. {
  125. goHigher();
  126. }
  127. }
  128. catch(GoHigherAtRootTreeException ex)
  129. {
  130. currentNode.selectedChild = -1;
  131. }
  132. }
  133. public void goToPosition(Integer... i) throws NoChildTreeException
  134. {
  135. goToRoot();
  136. for(int a = 0; a < i.length - 1; a++)
  137. {
  138. selectChild(i[a]);
  139. goDeeper();
  140. }
  141. selectChild(i[i.length - 1]);
  142. }
  143. public boolean isRootNodeSelected()
  144. {
  145. return currentNode.data == null;
  146. }
  147. public Integer[] getCurrentPosition()
  148. {
  149. Integer[] i = position.toArray(new Integer[position.size() + 1]);
  150. i[position.size()] = currentNode.selectedChild;
  151. return i;
  152. }
  153. }