Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Unified Diff: corelib/src/implementation/splay_tree.dart

Issue 9455051: Add getMinimum, getMaximum, getPreceding and getSucceeding to SplayTree. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/corelib/src/SplayTreeTest.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
+ }
}
« no previous file with comments | « no previous file | tests/corelib/src/SplayTreeTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698