Skip to content

Commit 584914b

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[nnbd/corelib] Remove unnecessary polymorphism in SplayTreeNode
Fixes #41064 Change-Id: I144265410ee92ba5df93cc0cf9ad1692da165b17 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139748 Reviewed-by: Lasse R.H. Nielsen <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent 6a5b87f commit 584914b

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

sdk_nnbd/lib/collection/splay_tree.dart

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,38 @@ part of dart.collection;
66

77
typedef _Predicate<T> = bool Function(T value);
88

9-
class _SplayTreeNodeBase<K> {
10-
_SplayTreeNode<K>? left;
11-
_SplayTreeNode<K>? right;
12-
}
13-
149
/// A node in a splay tree. It holds the sorting key and the left
1510
/// and right children in the tree.
16-
class _SplayTreeNode<K> extends _SplayTreeNodeBase<K> {
17-
final K key;
11+
class _SplayTreeNode<K> {
12+
// The key is nullable to be able to create a dummy node.
13+
final K? _key;
1814

19-
_SplayTreeNode(this.key);
20-
}
15+
_SplayTreeNode<K>? left;
16+
_SplayTreeNode<K>? right;
17+
18+
_SplayTreeNode(this._key);
2119

22-
class _DummySplayTreeNode<K> extends _SplayTreeNodeBase<K>
23-
implements _SplayTreeNode<K> {
24-
K get key => throw UnsupportedError("");
20+
K get key {
21+
// TODO(dartbug.com/40892): replace with '_key as K'
22+
K? localKey = _key;
23+
return (localKey != null) ? localKey : localKey as K;
24+
}
2525
}
2626

2727
/// A node in a splay tree based map.
2828
///
2929
/// A [_SplayTreeNode] that also contains a value
3030
class _SplayTreeMapNode<K, V> extends _SplayTreeNode<K> {
31-
V value;
32-
_SplayTreeMapNode(K key, this.value) : super(key);
33-
}
31+
// The value is nullable to be able to create a dummy node.
32+
V? _value;
33+
34+
_SplayTreeMapNode(K? key, this._value) : super(key);
3435

35-
class _DummySplayTreeMapNode<K, V> extends _DummySplayTreeNode<K>
36-
implements _SplayTreeMapNode<K, V> {
37-
V get value => throw UnsupportedError("");
38-
set value(V v) => throw UnsupportedError("");
36+
V get value {
37+
// TODO(dartbug.com/40892): replace with '_value as V'
38+
V? localValue = _value;
39+
return (localValue != null) ? localValue : localValue as V;
40+
}
3941
}
4042

4143
/// A splay tree is a self-balancing binary search tree.
@@ -264,7 +266,7 @@ Comparator<K> _defaultCompare<K>() {
264266
class SplayTreeMap<K, V> extends _SplayTree<K, _SplayTreeMapNode<K, V>>
265267
with MapMixin<K, V> {
266268
_SplayTreeMapNode<K, V>? _root;
267-
final _SplayTreeMapNode<K, V> _dummy = _DummySplayTreeMapNode<K, V>();
269+
final _SplayTreeMapNode<K, V> _dummy = _SplayTreeMapNode<K, V>(null, null);
268270

269271
Comparator<K> _comparator;
270272
_Predicate _validKey;
@@ -359,7 +361,7 @@ class SplayTreeMap<K, V> extends _SplayTree<K, _SplayTreeMapNode<K, V>>
359361
// the key to the root of the tree.
360362
int comp = _splay(key);
361363
if (comp == 0) {
362-
_root!.value = value;
364+
_root!._value = value;
363365
return;
364366
}
365367
_addNewRoot(_SplayTreeMapNode(key, value), comp);
@@ -652,7 +654,7 @@ class _SplayTreeNodeIterator<K>
652654
class SplayTreeSet<E> extends _SplayTree<E, _SplayTreeNode<E>>
653655
with IterableMixin<E>, SetMixin<E> {
654656
_SplayTreeNode<E>? _root;
655-
final _SplayTreeNode<E> _dummy = _DummySplayTreeNode<E>();
657+
final _SplayTreeNode<E> _dummy = _SplayTreeNode<E>(null);
656658

657659
Comparator<E> _comparator;
658660
_Predicate _validKey;

0 commit comments

Comments
 (0)