Skip to content

Commit

Permalink
perf: improve speed of SortedMap.addAll
Browse files Browse the repository at this point in the history
  • Loading branch information
rbellens committed Feb 29, 2024
1 parent 8d75bbc commit d548773
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/src/treemap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class TreeMap<K extends Comparable, V> extends MapBase<K, V> {
@override
void operator []=(K key, V value) {
var p = MapEntry<K, V>(key, value);
_tree.remove(
p); // first remove otherwise not overwritten, could be improved?
// _tree.remove(
// p); // first remove otherwise not overwritten, could be improved?
_tree.add(p);
}

Expand Down
9 changes: 8 additions & 1 deletion lib/src/treeset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,14 @@ class AvlNode<V> {
AvlNode<V> add(Comparator<V> comparator, V element) {
var compare = comparator(element, object);
if (compare == 0) {
return this;
if (identical(object, element)) {
return this;
}
return AvlNode(
object: element,
left: left,
right: right,
);
} else if (compare < 0) {
var newLeft = left?.add(comparator, element) ?? AvlNode(object: element);
if (newLeft == left) return this;
Expand Down

0 comments on commit d548773

Please sign in to comment.