123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- package me.km.snuviscript;
- import me.km.exception.GoHigherAtRootTreeException;
- import me.km.exception.NoChildTreeException;
- import it.unimi.dsi.fastutil.objects.ObjectArrayList;
- import java.util.Stack;
- public class Tree<T>
- {
- private final Node<T> root;
- private Node<T> currentNode;
- private final Stack<Integer> position;
- public Tree()
- {
- root = new Node<>();
- root.parent = null;
- root.data = null;
- root.children = new ObjectArrayList<>();
- root.selectedChild = -1;
-
- currentNode = root;
-
- position = new Stack<>();
- }
-
- public void clear()
- {
- root.children.clear();
- currentNode = root;
- position.clear();
- root.selectedChild = -1;
- }
- private static class Node<T>
- {
- private int actualCodeLine;
- private T data;
- private Node<T> parent;
- private ObjectArrayList<Node<T>> children;
- private int selectedChild;
- }
-
- public void addChild(T child, int actualCodeLine)
- {
- Node<T> node = new Node<>();
- node.data = child;
- node.parent = currentNode;
- node.children = new ObjectArrayList<>();
- node.selectedChild = -1;
- node.actualCodeLine = actualCodeLine;
- currentNode.children.add(node);
- }
-
- public int getActualCodeLine()
- {
- return currentNode.children.get(currentNode.selectedChild).actualCodeLine;
- }
-
- public void goHigher() throws GoHigherAtRootTreeException
- {
- currentNode.selectedChild = 0;
- currentNode = currentNode.parent;
- if(currentNode == null)
- {
- currentNode = root;
- throw new GoHigherAtRootTreeException();
- }
- position.pop();
- }
-
- public void goDeeper() throws NoChildTreeException
- {
- try
- {
- int i = currentNode.selectedChild;
- currentNode = currentNode.children.get(i);
- position.add(i);
- currentNode.selectedChild = -1;
- }
- catch(IndexOutOfBoundsException ex)
- {
- throw new NoChildTreeException();
- }
- }
-
- public void selectChild(int index) throws NoChildTreeException
- {
- if(index >= currentNode.children.size())
- {
- throw new NoChildTreeException();
- }
- currentNode.selectedChild = index;
- }
-
- public void selectNextChild() throws NoChildTreeException
- {
- currentNode.selectedChild++;
- if(currentNode.selectedChild >= currentNode.children.size())
- {
- currentNode.selectedChild--;
- throw new NoChildTreeException();
- }
- }
-
- public void selectPreviousChild()
- {
- currentNode.selectedChild--;
- }
-
- public void selectLastChild() throws NoChildTreeException
- {
- currentNode.selectedChild = currentNode.children.size() - 1;
- if(currentNode.selectedChild == -1)
- {
- throw new NoChildTreeException();
- }
- }
-
- public T getCurrentData()
- {
- return currentNode.data;
- }
-
- public T getCurrentChildData() throws NoChildTreeException
- {
- try
- {
- return currentNode.children.get(currentNode.selectedChild).data;
- }
- catch(IndexOutOfBoundsException ex)
- {
- throw new NoChildTreeException();
- }
- }
-
- public void goToRoot()
- {
- try
- {
- while(true)
- {
- goHigher();
- }
- }
- catch(GoHigherAtRootTreeException ex)
- {
- currentNode.selectedChild = -1;
- }
- }
-
- public void goToPosition(Integer... i) throws NoChildTreeException
- {
- goToRoot();
- for(int a = 0; a < i.length - 1; a++)
- {
- selectChild(i[a]);
- goDeeper();
- }
- selectChild(i[i.length - 1]);
- }
-
- public boolean isRootNodeSelected()
- {
- return currentNode.data == null;
- }
-
- public Integer[] getCurrentPosition()
- {
- Integer[] i = position.toArray(new Integer[position.size() + 1]);
- i[position.size()] = currentNode.selectedChild;
- return i;
- }
- }
|