Skip to content

Commit

Permalink
Refactored away clone
Browse files Browse the repository at this point in the history
  • Loading branch information
DavyLandman committed Sep 21, 2024
1 parent 1bc04ad commit f107012
Showing 1 changed file with 6 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ public AbstractString rotateLeftRight() {
public OfInt iterator() {
return new OfInt() {
final Deque<AbstractString> todo = new ArrayDeque<>(depth);
OfInt currentLeaf = leftmostLeafIterator(todo, LazyConcatString.this);
OfInt currentLeaf = leftmostLeaf(todo, LazyConcatString.this).iterator();

@Override
public boolean hasNext() {
Expand All @@ -1271,7 +1271,7 @@ public int nextInt() {
if (!currentLeaf.hasNext() && !todo.isEmpty()) {
// now we back track to the previous node we went left from,
// take the right branch and continue with its first leaf:
currentLeaf = leftmostLeafIterator(todo, todo.pop());
currentLeaf = leftmostLeaf(todo, todo.pop()).iterator();
}

assert currentLeaf.hasNext() || todo.isEmpty();
Expand All @@ -1284,7 +1284,7 @@ public int nextInt() {
public Iterator<CharBuffer> iterateParts() {
return new Iterator<> () {
final Deque<AbstractString> todo = new ArrayDeque<>(depth);
Iterator<CharBuffer> currentLeaf = leftmostLeafIteratorParts(todo, LazyConcatString.this);
Iterator<CharBuffer> currentLeaf = leftmostLeaf(todo, LazyConcatString.this).iterateParts();
@Override
public boolean hasNext() {
return currentLeaf.hasNext(); /* || !todo.isEmpty() is unnecessary due to post-condition of nextInt() */
Expand All @@ -1296,7 +1296,7 @@ public CharBuffer next() {
if (!currentLeaf.hasNext() && !todo.isEmpty()) {
// now we back track to the previous node we went left from,
// take the right branch and continue with its first leaf:
currentLeaf = leftmostLeafIteratorParts(todo, todo.pop());
currentLeaf = leftmostLeaf(todo, todo.pop()).iterateParts();
}

assert currentLeaf.hasNext() || todo.isEmpty();
Expand All @@ -1312,34 +1312,17 @@ public CharBuffer next() {
* the path of nodes to this leaf as a side-effect in the todo
* stack.
*/
private static Iterator<CharBuffer> leftmostLeafIteratorParts(Deque<AbstractString> todo, IStringTreeNode start) {
private static IStringTreeNode leftmostLeaf(Deque<AbstractString> todo, IStringTreeNode start) {
IStringTreeNode cur = start;

while (cur.depth() > 1) {
todo.push(cur.right());
cur = cur.left();
}

return cur.iterateParts();
return cur;
}

/**
* Static helper function for the iterator() method.
*
* It finds the left-most leaf of the tree, and collects
* the path of nodes to this leaf as a side-effect in the todo
* stack.
*/
private static OfInt leftmostLeafIterator(Deque<AbstractString> todo, IStringTreeNode start) {
IStringTreeNode cur = start;

while (cur.depth() > 1) {
todo.push(cur.right());
cur = cur.left();
}

return cur.iterator();
}
}

private static class IndentedString extends AbstractString {
Expand Down

0 comments on commit f107012

Please sign in to comment.