diff --git a/src/libcollections/treemap.rs b/src/libcollections/treemap.rs index 02246c33317f1..20c8da485cea7 100644 --- a/src/libcollections/treemap.rs +++ b/src/libcollections/treemap.rs @@ -44,21 +44,21 @@ use vec::Vec; #[allow(missing_doc)] #[deriving(Clone)] -pub struct TreeMap { - root: Option>>, +pub struct TreeMap { + root: Option>>, length: uint } -impl PartialEq for TreeMap { - fn eq(&self, other: &TreeMap) -> bool { +impl PartialEq for TreeMap { + fn eq(&self, other: &TreeMap) -> bool { self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a == b) } } // Lexicographical comparison -fn lt(a: &TreeMap, - b: &TreeMap) -> bool { +fn lt(a: &TreeMap, + b: &TreeMap) -> bool { // the Zip iterator is as long as the shortest of a and b. for ((key_a, value_a), (key_b, value_b)) in a.iter().zip(b.iter()) { if *key_a < *key_b { return true; } @@ -70,12 +70,12 @@ fn lt(a: &TreeMap, a.len() < b.len() } -impl PartialOrd for TreeMap { +impl PartialOrd for TreeMap { #[inline] - fn lt(&self, other: &TreeMap) -> bool { lt(self, other) } + fn lt(&self, other: &TreeMap) -> bool { lt(self, other) } } -impl Show for TreeMap { +impl Show for TreeMap { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { try!(write!(f, "{{")); @@ -88,20 +88,20 @@ impl Show for TreeMap { } } -impl Collection for TreeMap { +impl Collection for TreeMap { fn len(&self) -> uint { self.length } } -impl Mutable for TreeMap { +impl Mutable for TreeMap { fn clear(&mut self) { self.root = None; self.length = 0 } } -impl Map for TreeMap { - fn find<'a>(&'a self, key: &K) -> Option<&'a V> { - let mut current: &'a Option>> = &self.root; +impl Map for TreeMap { + fn find<'a>(&'a self, key: &Key) -> Option<&'a Value> { + let mut current: &'a Option>> = &self.root; loop { match *current { Some(ref r) => { @@ -117,37 +117,37 @@ impl Map for TreeMap { } } -impl MutableMap for TreeMap { +impl MutableMap for TreeMap { #[inline] - fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V> { + fn find_mut<'a>(&'a mut self, key: &Key) -> Option<&'a mut Value> { find_mut(&mut self.root, key) } - fn swap(&mut self, key: K, value: V) -> Option { + fn swap(&mut self, key: Key, value: Value) -> Option { let ret = insert(&mut self.root, key, value); if ret.is_none() { self.length += 1 } ret } - fn pop(&mut self, key: &K) -> Option { + fn pop(&mut self, key: &Key) -> Option { let ret = remove(&mut self.root, key); if ret.is_some() { self.length -= 1 } ret } } -impl Default for TreeMap { +impl Default for TreeMap { #[inline] - fn default() -> TreeMap { TreeMap::new() } + fn default() -> TreeMap { TreeMap::new() } } -impl TreeMap { +impl TreeMap { /// Create an empty TreeMap - pub fn new() -> TreeMap { TreeMap{root: None, length: 0} } + pub fn new() -> TreeMap { TreeMap{root: None, length: 0} } /// Get a lazy iterator over the key-value pairs in the map. /// Requires that it be frozen (immutable). - pub fn iter<'a>(&'a self) -> Entries<'a, K, V> { + pub fn iter<'a>(&'a self) -> Entries<'a, Key, Value> { Entries { stack: vec!(), node: deref(&self.root), @@ -158,13 +158,13 @@ impl TreeMap { /// Get a lazy reverse iterator over the key-value pairs in the map. /// Requires that it be frozen (immutable). - pub fn rev_iter<'a>(&'a self) -> RevEntries<'a, K, V> { + pub fn rev_iter<'a>(&'a self) -> RevEntries<'a, Key, Value> { RevEntries{iter: self.iter()} } /// Get a lazy forward iterator over the key-value pairs in the /// map, with the values being mutable. - pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> { + pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, Key, Value> { MutEntries { stack: vec!(), node: mut_deref(&mut self.root), @@ -174,13 +174,13 @@ impl TreeMap { } /// Get a lazy reverse iterator over the key-value pairs in the /// map, with the values being mutable. - pub fn mut_rev_iter<'a>(&'a mut self) -> RevMutEntries<'a, K, V> { + pub fn mut_rev_iter<'a>(&'a mut self) -> RevMutEntries<'a, Key, Value> { RevMutEntries{iter: self.mut_iter()} } /// Get a lazy iterator that consumes the treemap. - pub fn move_iter(self) -> MoveEntries { + pub fn move_iter(self) -> MoveEntries { let TreeMap { root: root, length: length } = self; let stk = match root { None => vec!(), @@ -227,10 +227,10 @@ macro_rules! bound_setup { } -impl TreeMap { +impl TreeMap { /// Get a lazy iterator that should be initialized using /// `traverse_left`/`traverse_right`/`traverse_complete`. - fn iter_for_traversal<'a>(&'a self) -> Entries<'a, K, V> { + fn iter_for_traversal<'a>(&'a self) -> Entries<'a, Key, Value> { Entries { stack: vec!(), node: deref(&self.root), @@ -241,19 +241,19 @@ impl TreeMap { /// Return a lazy iterator to the first key-value pair whose key is not less than `k` /// If all keys in map are less than `k` an empty iterator is returned. - pub fn lower_bound<'a>(&'a self, k: &K) -> Entries<'a, K, V> { + pub fn lower_bound<'a>(&'a self, k: &Key) -> Entries<'a, Key, Value> { bound_setup!(self.iter_for_traversal(), true) } /// Return a lazy iterator to the first key-value pair whose key is greater than `k` /// If all keys in map are not greater than `k` an empty iterator is returned. - pub fn upper_bound<'a>(&'a self, k: &K) -> Entries<'a, K, V> { + pub fn upper_bound<'a>(&'a self, k: &Key) -> Entries<'a, Key, Value> { bound_setup!(self.iter_for_traversal(), false) } /// Get a lazy iterator that should be initialized using /// `traverse_left`/`traverse_right`/`traverse_complete`. - fn mut_iter_for_traversal<'a>(&'a mut self) -> MutEntries<'a, K, V> { + fn mut_iter_for_traversal<'a>(&'a mut self) -> MutEntries<'a, Key, Value> { MutEntries { stack: vec!(), node: mut_deref(&mut self.root), @@ -267,7 +267,7 @@ impl TreeMap { /// /// If all keys in map are less than `k` an empty iterator is /// returned. - pub fn mut_lower_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> { + pub fn mut_lower_bound<'a>(&'a mut self, k: &Key) -> MutEntries<'a, Key, Value> { bound_setup!(self.mut_iter_for_traversal(), true) } @@ -276,31 +276,31 @@ impl TreeMap { /// /// If all keys in map are not greater than `k` an empty iterator /// is returned. - pub fn mut_upper_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> { + pub fn mut_upper_bound<'a>(&'a mut self, k: &Key) -> MutEntries<'a, Key, Value> { bound_setup!(self.mut_iter_for_traversal(), false) } } /// Lazy forward iterator over a map -pub struct Entries<'a, K, V> { - stack: Vec<&'a TreeNode>, +pub struct Entries<'a, Key, Value> { + stack: Vec<&'a TreeNode>, // See the comment on MutEntries; this is just to allow // code-sharing (for this immutable-values iterator it *could* very - // well be Option<&'a TreeNode>). - node: *TreeNode, + // well be Option<&'a TreeNode>). + node: *TreeNode, remaining_min: uint, remaining_max: uint } /// Lazy backward iterator over a map -pub struct RevEntries<'a, K, V> { - iter: Entries<'a, K, V>, +pub struct RevEntries<'a, Key, Value> { + iter: Entries<'a, Key, Value>, } /// Lazy forward iterator over a map that allows for the mutation of /// the values. -pub struct MutEntries<'a, K, V> { - stack: Vec<&'a mut TreeNode>, +pub struct MutEntries<'a, Key, Value> { + stack: Vec<&'a mut TreeNode>, // Unfortunately, we require some unsafe-ness to get around the // fact that we would be storing a reference *into* one of the // nodes in the stack. @@ -320,14 +320,14 @@ pub struct MutEntries<'a, K, V> { // it under control. // // (This field can legitimately be null.) - node: *mut TreeNode, + node: *mut TreeNode, remaining_min: uint, remaining_max: uint } /// Lazy backward iterator over a map -pub struct RevMutEntries<'a, K, V> { - iter: MutEntries<'a, K, V>, +pub struct RevMutEntries<'a, Key, Value> { + iter: MutEntries<'a, Key, Value>, } @@ -353,9 +353,9 @@ macro_rules! define_iterator { ) => { // private methods on the forward iterator (item!() for the // addr_mut in the next_ return value) - item!(impl<'a, K, V> $name<'a, K, V> { + item!(impl<'a, Key, Value> $name<'a, Key, Value> { #[inline(always)] - fn next_(&mut self, forward: bool) -> Option<(&'a K, &'a $($addr_mut)* V)> { + fn next_(&mut self, forward: bool) -> Option<(&'a Key, &'a $($addr_mut)* Value)> { while !self.stack.is_empty() || !self.node.is_null() { if !self.node.is_null() { let node = unsafe {addr!(& $($addr_mut)* *self.node)}; @@ -424,11 +424,12 @@ macro_rules! define_iterator { }) // the forward Iterator impl. - item!(impl<'a, K, V> Iterator<(&'a K, &'a $($addr_mut)* V)> for $name<'a, K, V> { + item!(impl<'a, Key, Value> Iterator<(&'a Key, &'a $($addr_mut)* Value)> + for $name<'a, Key, Value> { /// Advance the iterator to the next node (in order) and return a /// tuple with a reference to the key and value. If there are no /// more nodes, return `None`. - fn next(&mut self) -> Option<(&'a K, &'a $($addr_mut)* V)> { + fn next(&mut self) -> Option<(&'a Key, &'a $($addr_mut)* Value)> { self.next_(true) } @@ -439,8 +440,9 @@ macro_rules! define_iterator { }) // the reverse Iterator impl. - item!(impl<'a, K, V> Iterator<(&'a K, &'a $($addr_mut)* V)> for $rev_name<'a, K, V> { - fn next(&mut self) -> Option<(&'a K, &'a $($addr_mut)* V)> { + item!(impl<'a, Key, Value> Iterator<(&'a Key, &'a $($addr_mut)* Value)> + for $rev_name<'a, Key, Value> { + fn next(&mut self) -> Option<(&'a Key, &'a $($addr_mut)* Value)> { self.iter.next_(false) } @@ -468,22 +470,22 @@ define_iterator! { addr_mut = mut } -fn deref<'a, K, V>(node: &'a Option>>) -> *TreeNode { +fn deref<'a, Key, Value>(node: &'a Option>>) -> *TreeNode { match *node { Some(ref n) => { - let n: &TreeNode = *n; - n as *TreeNode + let n: &TreeNode = *n; + n as *TreeNode } None => ptr::null() } } -fn mut_deref(x: &mut Option>>) - -> *mut TreeNode { +fn mut_deref(x: &mut Option>>) + -> *mut TreeNode { match *x { Some(ref mut n) => { - let n: &mut TreeNode = *n; - n as *mut TreeNode + let n: &mut TreeNode = *n; + n as *mut TreeNode } None => ptr::mut_null() } @@ -492,14 +494,14 @@ fn mut_deref(x: &mut Option>>) /// Lazy forward iterator over a map that consumes the map while iterating -pub struct MoveEntries { - stack: Vec>, +pub struct MoveEntries { + stack: Vec>, remaining: uint } -impl Iterator<(K, V)> for MoveEntries { +impl Iterator<(Key, Value)> for MoveEntries { #[inline] - fn next(&mut self) -> Option<(K, V)> { + fn next(&mut self) -> Option<(Key, Value)> { while !self.stack.is_empty() { let TreeNode { key: key, @@ -811,24 +813,24 @@ impl<'a, T: Ord> Iterator<&'a T> for UnionItems<'a, T> { // Nodes keep track of their level in the tree, starting at 1 in the // leaves and with a red child sharing the level of the parent. #[deriving(Clone)] -struct TreeNode { - key: K, - value: V, - left: Option>>, - right: Option>>, +struct TreeNode { + key: Key, + value: Value, + left: Option>>, + right: Option>>, level: uint } -impl TreeNode { +impl TreeNode { /// Creates a new tree node. #[inline] - pub fn new(key: K, value: V) -> TreeNode { + pub fn new(key: Key, value: Value) -> TreeNode { TreeNode{key: key, value: value, left: None, right: None, level: 1} } } // Remove left horizontal link by rotating right -fn skew(node: &mut Box>) { +fn skew(node: &mut Box>) { if node.left.as_ref().map_or(false, |x| x.level == node.level) { let mut save = node.left.take_unwrap(); swap(&mut node.left, &mut save.right); // save.right now None @@ -839,7 +841,7 @@ fn skew(node: &mut Box>) { // Remove dual horizontal link by rotating left and increasing level of // the parent -fn split(node: &mut Box>) { +fn split(node: &mut Box>) { if node.right.as_ref().map_or(false, |x| x.right.as_ref().map_or(false, |y| y.level == node.level)) { let mut save = node.right.take_unwrap(); @@ -850,9 +852,9 @@ fn split(node: &mut Box>) { } } -fn find_mut<'r, K: Ord, V>(node: &'r mut Option>>, - key: &K) - -> Option<&'r mut V> { +fn find_mut<'r, Key: Ord, Value>(node: &'r mut Option>>, + key: &Key) + -> Option<&'r mut Value> { match *node { Some(ref mut x) => { match key.cmp(&x.key) { @@ -865,8 +867,8 @@ fn find_mut<'r, K: Ord, V>(node: &'r mut Option>>, } } -fn insert(node: &mut Option>>, - key: K, value: V) -> Option { +fn insert(node: &mut Option>>, + key: Key, value: Value) -> Option { match *node { Some(ref mut save) => { match key.cmp(&save.key) { @@ -895,10 +897,10 @@ fn insert(node: &mut Option>>, } } -fn remove(node: &mut Option>>, - key: &K) -> Option { - fn heir_swap(node: &mut Box>, - child: &mut Option>>) { +fn remove(node: &mut Option>>, + key: &Key) -> Option { + fn heir_swap(node: &mut Box>, + child: &mut Option>>) { // *could* be done without recursion, but it won't borrow check for x in child.mut_iter() { if x.right.is_some() { @@ -978,17 +980,17 @@ fn remove(node: &mut Option>>, }; } -impl FromIterator<(K, V)> for TreeMap { - fn from_iter>(iter: T) -> TreeMap { +impl FromIterator<(Key, Value)> for TreeMap { + fn from_iter>(iter: T) -> TreeMap { let mut map = TreeMap::new(); map.extend(iter); map } } -impl Extendable<(K, V)> for TreeMap { +impl Extendable<(Key, Value)> for TreeMap { #[inline] - fn extend>(&mut self, mut iter: T) { + fn extend>(&mut self, mut iter: T) { for (k, v) in iter { self.insert(k, v); } @@ -1088,8 +1090,8 @@ mod test_treemap { assert_eq!(m.find(&k1), Some(&v1)); } - fn check_equal(ctrl: &[(K, V)], - map: &TreeMap) { + fn check_equal(ctrl: &[(Key, Value)], + map: &TreeMap) { assert_eq!(ctrl.is_empty(), map.is_empty()); for x in ctrl.iter() { let &(ref k, ref v) = x; @@ -1109,8 +1111,8 @@ mod test_treemap { } } - fn check_left(node: &Option>>, - parent: &Box>) { + fn check_left(node: &Option>>, + parent: &Box>) { match *node { Some(ref r) => { assert_eq!(r.key.cmp(&parent.key), Less); @@ -1122,8 +1124,8 @@ mod test_treemap { } } - fn check_right(node: &Option>>, - parent: &Box>, + fn check_right(node: &Option>>, + parent: &Box>, parent_red: bool) { match *node { Some(ref r) => { @@ -1139,7 +1141,7 @@ mod test_treemap { } } - fn check_structure(map: &TreeMap) { + fn check_structure(map: &TreeMap) { match map.root { Some(ref r) => { check_left(&r.left, r);