@Test(expected = IndexOutOfBoundsException.class) public void indexOutOutBoundsRight() throws Exception { Node<InvariantSpan> left2 = new Node<>(new InvariantSpan(100, 10, documentHash)); Node<InvariantSpan> right2 = new Node<>(new InvariantSpan(200, 5, documentHash)); Node<InvariantSpan> left1 = new Node.Builder<InvariantSpan>(10).left(left2).right(right2).build(); Node<InvariantSpan> root = new Node.Builder<InvariantSpan>(15).left(left1).build(); RopeUtils.index(20, root, 0); }
public static <T extends StreamElement> NodeIndex<T> index(long characterPosition, Node<T> x, long disp) { if (x == null) { throw new IllegalArgumentException("node is null"); } if (characterPosition < 1) { throw new IndexOutOfBoundsException("characterPosition must be greater than 0"); } if (x.isLeaf()) { return new NodeIndex<T>(x, disp); } if (x.weight < characterPosition) { if (x.right == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } if (x.weight > 0) { disp += x.weight; x.isRed = true; } return index(characterPosition - x.weight, x.right, disp); } else { if (x.left == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } return index(characterPosition, x.left, disp); } }
RopeUtils { public static <T extends StreamElement> NodeIndex<T> index(long characterPosition, Node<T> x, long disp) { if (x == null) { throw new IllegalArgumentException("node is null"); } if (characterPosition < 1) { throw new IndexOutOfBoundsException("characterPosition must be greater than 0"); } if (x.isLeaf()) { return new NodeIndex<T>(x, disp); } if (x.weight < characterPosition) { if (x.right == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } if (x.weight > 0) { disp += x.weight; x.isRed = true; } return index(characterPosition - x.weight, x.right, disp); } else { if (x.left == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } return index(characterPosition, x.left, disp); } } }
RopeUtils { public static <T extends StreamElement> NodeIndex<T> index(long characterPosition, Node<T> x, long disp) { if (x == null) { throw new IllegalArgumentException("node is null"); } if (characterPosition < 1) { throw new IndexOutOfBoundsException("characterPosition must be greater than 0"); } if (x.isLeaf()) { return new NodeIndex<T>(x, disp); } if (x.weight < characterPosition) { if (x.right == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } if (x.weight > 0) { disp += x.weight; x.isRed = true; } return index(characterPosition - x.weight, x.right, disp); } else { if (x.left == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } return index(characterPosition, x.left, disp); } }  }
RopeUtils { public static <T extends StreamElement> NodeIndex<T> index(long characterPosition, Node<T> x, long disp) { if (x == null) { throw new IllegalArgumentException("node is null"); } if (characterPosition < 1) { throw new IndexOutOfBoundsException("characterPosition must be greater than 0"); } if (x.isLeaf()) { return new NodeIndex<T>(x, disp); } if (x.weight < characterPosition) { if (x.right == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } if (x.weight > 0) { disp += x.weight; x.isRed = true; } return index(characterPosition - x.weight, x.right, disp); } else { if (x.left == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } return index(characterPosition, x.left, disp); } }  static long addWeightsOfRightLeaningChildNodes(Node<T> x); static long addWeightsOfRightLeaningParentNodes(Node<T> child); static void adjustWeightOfLeftLeaningParents(Node<T> startNode, long weight); static long characterCount(Node<T> x); static void collectLeafNodes(Node<T> x, Queue<Node<T>> queue); static Node<T> concat(List<Node<T>> orphans); static Node<T> concat(Node<T> left, Node<T> right); static void cutLeftNode(Node<T> x, List<Node<T>> orphans); static void cutRightNode(Node<T> x, List<Node<T>> orphans); static Node<T> findRoot(Node<T> child); static Node<T> findSearchNode(Node<T> x, long weight, Node<T> root); static NodeIndex<T> index(long characterPosition, Node<T> x, long disp); static boolean intersects(long start, long end, long start2, long end2); static Node<T> rebalance(Node<T> x); }
RopeUtils { public static <T extends StreamElement> NodeIndex<T> index(long characterPosition, Node<T> x, long disp) { if (x == null) { throw new IllegalArgumentException("node is null"); } if (characterPosition < 1) { throw new IndexOutOfBoundsException("characterPosition must be greater than 0"); } if (x.isLeaf()) { return new NodeIndex<T>(x, disp); } if (x.weight < characterPosition) { if (x.right == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } if (x.weight > 0) { disp += x.weight; x.isRed = true; } return index(characterPosition - x.weight, x.right, disp); } else { if (x.left == null) { throw new IndexOutOfBoundsException("Can't find node at position = " + (characterPosition + disp)); } return index(characterPosition, x.left, disp); } }  static long addWeightsOfRightLeaningChildNodes(Node<T> x); static long addWeightsOfRightLeaningParentNodes(Node<T> child); static void adjustWeightOfLeftLeaningParents(Node<T> startNode, long weight); static long characterCount(Node<T> x); static void collectLeafNodes(Node<T> x, Queue<Node<T>> queue); static Node<T> concat(List<Node<T>> orphans); static Node<T> concat(Node<T> left, Node<T> right); static void cutLeftNode(Node<T> x, List<Node<T>> orphans); static void cutRightNode(Node<T> x, List<Node<T>> orphans); static Node<T> findRoot(Node<T> child); static Node<T> findSearchNode(Node<T> x, long weight, Node<T> root); static NodeIndex<T> index(long characterPosition, Node<T> x, long disp); static boolean intersects(long start, long end, long start2, long end2); static Node<T> rebalance(Node<T> x);  }