Chromium Code Reviews| Index: corelib/src/implementation/splay_tree.dart |
| =================================================================== |
| --- corelib/src/implementation/splay_tree.dart (revision 4598) |
| +++ corelib/src/implementation/splay_tree.dart (working copy) |
| @@ -238,4 +238,62 @@ |
| String toString() { |
| return Maps.mapToString(this); |
| } |
| + |
| + /** |
| + * Get the first key in the map. Returns [ifEmpty] if the map is empty. |
| + */ |
| + K firstKey([K ifEmpty]) { |
|
kasperl
2012/02/27 06:50:03
Have you considered making ifEmpty a closure inste
Søren Gjesse
2012/02/27 07:53:16
Why not just return null if the splay tree is empt
kasperl
2012/02/27 08:53:17
I think we should go with Søren's proposal for now
Anders Johnsen
2012/02/27 09:06:51
If we return null, then map[] should support null
kasperl
2012/02/27 09:18:45
I actually find it hard to read the line:
var f
Anders Johnsen
2012/02/27 11:49:59
Yeah, I've turned them into a getter. Thank you!
|
| + if (_root === null) return ifEmpty; |
| + SplayTreeNode<K, V> node = _root; |
| + while (node.left !== null) { |
| + node = node.left; |
| + } |
| + splay_(node.key); |
| + return node.key; |
| + } |
| + |
| + /** |
| + * Get the last key in the map. Returns [ifEmpty] if the map is empty. |
| + */ |
| + V lastKey([K ifEmpty]) { |
| + if (_root === null) return ifEmpty; |
| + SplayTreeNode<K, V> node = _root; |
| + while (node.right !== null) { |
| + node = node.right; |
| + } |
| + splay_(node.key); |
| + return node.key; |
| + } |
| + |
| + /** |
| + * Get the last key in the map that is strictly smaller then [key]. Returns |
|
kasperl
2012/02/27 06:50:03
then -> than
Anders Johnsen
2012/02/27 09:06:51
Done.
|
| + * [ifInvalid] if [key] was not found. |
| + */ |
| + K keyBefore(K key, [K ifInvalid]) { |
|
kasperl
2012/02/27 06:50:03
lastKeyBefore?
Anders Johnsen
2012/02/27 09:06:51
Hmm, to me this sort of implies I could give any k
kasperl
2012/02/27 09:18:45
To me the term 'keyBefore' is almost the same as '
Anders Johnsen
2012/02/27 11:49:59
I agree, and it makes a lot more sense the more I
|
| + splay_(key); |
| + if (_root === null || key.compareTo(_root.key) != 0) return ifInvalid; |
| + SplayTreeNode<K, V> node = _root; |
| + if (node.left === null) return ifInvalid; |
| + node = node.left; |
| + while (node.right !== null) { |
| + node = node.right; |
| + } |
| + return node.key; |
| + } |
| + |
| + /** |
| + * Get the first key in the map that is strictly larger then [key]. Returns |
|
kasperl
2012/02/27 06:50:03
then -> than
Anders Johnsen
2012/02/27 09:06:51
Done.
|
| + * [ifInvalid] if [key] was not found. |
| + */ |
| + K keyAfter(K key, [K ifInvalid]) { |
|
kasperl
2012/02/27 06:50:03
firstKeyAfter?
Anders Johnsen
2012/02/27 09:06:51
Same as above.
|
| + splay_(key); |
| + if (_root === null || key.compareTo(_root.key) != 0) return ifInvalid; |
| + SplayTreeNode<K, V> node = _root; |
| + if (node.right === null) return ifInvalid; |
| + node = node.right; |
| + while (node.left !== null) { |
| + node = node.left; |
| + } |
| + return node.key; |
| + } |
| } |