Skip to content

Commit

Permalink
perf: TreeSet.elementAt
Browse files Browse the repository at this point in the history
  • Loading branch information
rbellens committed Feb 22, 2024
1 parent 84dca02 commit 331fd7a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/src/treeset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class AvlTreeSet<V> extends TreeSet<V> {
@override
int get length => _root?.length ?? 0;

@override
V elementAt(int index) {
RangeError.checkNotNegative(index, 'index');
if (index >= length) {
throw IndexError.withLength(index, length,
indexable: this, name: 'index');
}
return _root!.elementAt(index);
}

@override
bool add(V element) {
if (_root == null) {
Expand Down Expand Up @@ -540,6 +550,17 @@ class AvlNode<V> {
}
return this;
}

V elementAt(int index) {
var l = left?.length ?? 0;
if (index < l) {
return left!.elementAt(index);
}
if (index == l) {
return this.object;
}
return right!.elementAt(index - l - 1);
}
}

abstract class BidirectionalIterator<E> implements Iterator<E> {
Expand Down

0 comments on commit 331fd7a

Please sign in to comment.