Skip to content

Commit

Permalink
perf: improve speed of TreeSet.forEach
Browse files Browse the repository at this point in the history
  • Loading branch information
rbellens committed Feb 26, 2024
1 parent 7b90b36 commit cb98a5c
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions lib/src/treeset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,12 @@ class _Path<V> {
}

factory _Path.minimum(AvlNode<V> node, [_Path<V>? parent]) {
var p = parent;
while (node.left != null) {
p = _Path(node, p);
node = node.left!;
}
return _Path(node, p);
if (node.left == null) return _Path(node, parent);
return _Path.minimum(node.left!, _Path(node, parent));
}
Expand Down Expand Up @@ -801,15 +807,6 @@ class TreeCursor<V> extends BidirectionalIterator<V> {
throw ConcurrentModificationError(tree);
}
switch (_state) {
case _TreeCursorState.willBeOnAfterFirstMove:
_state = _TreeCursorState.on;
return true;

case _TreeCursorState.before:
if (_root == null) return false;
_path ??= _Path.minimum(_root!);
_state = _TreeCursorState.on;
return true;
case _TreeCursorState.on:
case _TreeCursorState.after:
case _TreeCursorState.willBeOnNextOrPreviousAfterFirstMove:
Expand All @@ -820,6 +817,15 @@ class TreeCursor<V> extends BidirectionalIterator<V> {
}
_state = _TreeCursorState.on;
return true;
case _TreeCursorState.willBeOnAfterFirstMove:
_state = _TreeCursorState.on;
return true;

case _TreeCursorState.before:
if (_root == null) return false;
_path ??= _Path.minimum(_root!);
_state = _TreeCursorState.on;
return true;
}
}

Expand All @@ -829,14 +835,6 @@ class TreeCursor<V> extends BidirectionalIterator<V> {
throw ConcurrentModificationError(tree);
}
switch (_state) {
case _TreeCursorState.willBeOnAfterFirstMove:
_state = _TreeCursorState.on;
return true;
case _TreeCursorState.after:
if (_root == null) return false;
_path ??= _Path.maximum(_root!);
_state = _TreeCursorState.on;
return true;
case _TreeCursorState.on:
case _TreeCursorState.before:
case _TreeCursorState.willBeOnNextOrPreviousAfterFirstMove:
Expand All @@ -847,6 +845,14 @@ class TreeCursor<V> extends BidirectionalIterator<V> {
}
_state = _TreeCursorState.on;
return true;
case _TreeCursorState.willBeOnAfterFirstMove:
_state = _TreeCursorState.on;
return true;
case _TreeCursorState.after:
if (_root == null) return false;
_path ??= _Path.maximum(_root!);
_state = _TreeCursorState.on;
return true;
}
}
}

0 comments on commit cb98a5c

Please sign in to comment.