Skip to content

Commit

Permalink
perf: Bring back custom iterator implementation of the iterator method (
Browse files Browse the repository at this point in the history
#46)

* chore: Add benchmarking [WIP]

* perf: Bring back custom iterator implementation of the iterator method

---------

Co-authored-by: Lukas Klingsbo <[email protected]>
  • Loading branch information
luanpotter and spydon authored Sep 8, 2024
1 parent ce95910 commit eef82f1
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/ordered_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class OrderedSet<E> extends IterableMixin<E> {

@override
Iterator<E> get iterator {
return _backingSet.expand<E>((es) => es).iterator;
return _OrderedSetIterator<E>(this);
}

/// The tree's elements in reversed order, cached when possible.
Expand Down Expand Up @@ -179,3 +179,30 @@ class OrderedSet<E> extends IterableMixin<E> {
_length = 0;
}
}

class _OrderedSetIterator<E> implements Iterator<E> {
final Iterator<Set<E>> _iterator;
Iterator<E>? _innerIterator;

_OrderedSetIterator(OrderedSet<E> orderedSet)
: _iterator = orderedSet._backingSet.iterator;

@override
E get current => _innerIterator!.current;

@pragma('vm:prefer-inline')
@pragma('wasm:prefer-inline')
@override
bool moveNext() {
if (_innerIterator?.moveNext() != true) {
final result = _iterator.moveNext();

if (result) {
_innerIterator = _iterator.current.iterator..moveNext();
}

return result;
}
return true;
}
}

0 comments on commit eef82f1

Please sign in to comment.