Skip to content

Commit

Permalink
perf: improve speed of TreeSetView.elementAt
Browse files Browse the repository at this point in the history
  • Loading branch information
rbellens committed Feb 28, 2024
1 parent ade4553 commit a3b9c33
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/src/treeset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -916,20 +916,32 @@ class _Path<V> {
}

factory _Path.minimum(AvlNode<V> node, [_Path<V>? parent]) {
if (node.left == null) return _Path(node, parent);
return _Path.minimum(node.left!, _Path(node, parent));
var p = _Path(node, parent);
while (p.node.left != null) {
p = _Path(p.node.left!, p);
}
return p;
}

factory _Path.maximum(AvlNode<V> node, [_Path<V>? parent]) {
if (node.right == null) return _Path(node, parent);
return _Path.maximum(node.right!, _Path(node, parent));
var p = _Path(node, parent);
while (p.node.right != null) {
p = _Path(p.node.right!, p);
}
return p;
}

int get leftIndex =>
(parent?.leftIndex ?? 0) +
(node == parent?.node.right ? ((parent!.node.left?.length ?? 0) + 1) : 0);

int get index => leftIndex + (node.left?.length ?? 0);
int get index {
var v = node.left?.length ?? 0;
var p = this;
while (p.parent != null) {
if (p.node == p.parent!.node.right) {
v += (p.parent!.node.left?.length ?? 0) + 1;
}
p = p.parent!;
}
return v;
}

_Path<V>? next() {
var current = node;
Expand Down

0 comments on commit a3b9c33

Please sign in to comment.