Skip to content

Commit

Permalink
[Truffle] Iterative implementations for getByteSlow
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Fish committed Oct 3, 2016
1 parent 5c97ec0 commit edada29
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 10 additions & 3 deletions truffle/src/main/java/org/jruby/truffle/core/rope/ConcatRope.java
Expand Up @@ -48,11 +48,18 @@ public Rope withEncoding(Encoding newEncoding, CodeRange newCodeRange) {
@Override
@TruffleBoundary
public byte getByteSlow(int index) {
if (index < left.byteLength()) {
return left.getByteSlow(index);
Rope nextLeft = left;
Rope nextRight = right;
while (index < nextLeft.byteLength()) {
if (nextLeft instanceof ConcatRope) {
nextRight = ((ConcatRope) nextLeft).getRight();
nextLeft = ((ConcatRope) nextLeft).getLeft();
} else {
return nextLeft.getByteSlow(index);
}
}

return right.getByteSlow(index - left.byteLength());
return nextRight.getByteSlow(index - nextLeft.byteLength());
}

public Rope getLeft() {
Expand Down
Expand Up @@ -62,7 +62,13 @@ protected byte[] getBytesSlow() {

@Override
public byte getByteSlow(int index) {
return child.getByteSlow(index + offset);
int idx = index + offset;
Rope nextChild = child;
while (nextChild instanceof SubstringRope) {
idx += ((SubstringRope) nextChild).getOffset();
nextChild = ((SubstringRope) nextChild).getChild();
}
return nextChild.getByteSlow(idx);
}

public Rope getChild() {
Expand Down

0 comments on commit edada29

Please sign in to comment.