1*e0dfd4d5Shanjiezhou // Copyright 2017-2018 By tickdream125@hotmail.com. 2*e0dfd4d5Shanjiezhou // 3*e0dfd4d5Shanjiezhou // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 4*e0dfd4d5Shanjiezhou // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 5*e0dfd4d5Shanjiezhou // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 6*e0dfd4d5Shanjiezhou // option. This file may not be copied, modified, or distributed 7*e0dfd4d5Shanjiezhou // except according to those terms. 8*e0dfd4d5Shanjiezhou 9*e0dfd4d5Shanjiezhou // This file brings from https://github.com/tickbh/rbtree-rs/blob/master/src/lib.rs 10*e0dfd4d5Shanjiezhou // Thanks to the author tickbh 11*e0dfd4d5Shanjiezhou 12*e0dfd4d5Shanjiezhou #![allow(dead_code)] 13*e0dfd4d5Shanjiezhou 14*e0dfd4d5Shanjiezhou use core::cmp::Ord; 15*e0dfd4d5Shanjiezhou use core::fmt::{self, Debug}; 16*e0dfd4d5Shanjiezhou use core::cmp::Ordering; 17*e0dfd4d5Shanjiezhou use core::ptr; 18*e0dfd4d5Shanjiezhou use core::iter::{IntoIterator, FromIterator}; 19*e0dfd4d5Shanjiezhou use core::marker; 20*e0dfd4d5Shanjiezhou use core::mem; 21*e0dfd4d5Shanjiezhou use core::ops::Index; 22*e0dfd4d5Shanjiezhou 23*e0dfd4d5Shanjiezhou use alloc::boxed::Box; 24*e0dfd4d5Shanjiezhou 25*e0dfd4d5Shanjiezhou use crate::kdebug; 26*e0dfd4d5Shanjiezhou 27*e0dfd4d5Shanjiezhou #[derive(Debug, Copy, Clone, PartialEq, Eq)] 28*e0dfd4d5Shanjiezhou enum Color { 29*e0dfd4d5Shanjiezhou Red, 30*e0dfd4d5Shanjiezhou Black, 31*e0dfd4d5Shanjiezhou } 32*e0dfd4d5Shanjiezhou 33*e0dfd4d5Shanjiezhou /*****************RBTreeNode***************************/ 34*e0dfd4d5Shanjiezhou struct RBTreeNode<K: Ord, V> { 35*e0dfd4d5Shanjiezhou color: Color, 36*e0dfd4d5Shanjiezhou left: NodePtr<K, V>, 37*e0dfd4d5Shanjiezhou right: NodePtr<K, V>, 38*e0dfd4d5Shanjiezhou parent: NodePtr<K, V>, 39*e0dfd4d5Shanjiezhou key: K, 40*e0dfd4d5Shanjiezhou value: V, 41*e0dfd4d5Shanjiezhou } 42*e0dfd4d5Shanjiezhou 43*e0dfd4d5Shanjiezhou impl<K: Ord, V> RBTreeNode<K, V> { 44*e0dfd4d5Shanjiezhou #[inline] 45*e0dfd4d5Shanjiezhou fn pair(self) -> (K, V) { 46*e0dfd4d5Shanjiezhou (self.key, self.value) 47*e0dfd4d5Shanjiezhou } 48*e0dfd4d5Shanjiezhou } 49*e0dfd4d5Shanjiezhou 50*e0dfd4d5Shanjiezhou impl<K, V> Debug for RBTreeNode<K, V> 51*e0dfd4d5Shanjiezhou where 52*e0dfd4d5Shanjiezhou K: Ord + Debug, 53*e0dfd4d5Shanjiezhou V: Debug, 54*e0dfd4d5Shanjiezhou { 55*e0dfd4d5Shanjiezhou fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 56*e0dfd4d5Shanjiezhou write!(f, "k:{:?} v:{:?} c:{:?}", self.key, self.value, self.color) 57*e0dfd4d5Shanjiezhou } 58*e0dfd4d5Shanjiezhou } 59*e0dfd4d5Shanjiezhou 60*e0dfd4d5Shanjiezhou /*****************NodePtr***************************/ 61*e0dfd4d5Shanjiezhou #[derive(Debug)] 62*e0dfd4d5Shanjiezhou struct NodePtr<K: Ord, V>(*mut RBTreeNode<K, V>); 63*e0dfd4d5Shanjiezhou 64*e0dfd4d5Shanjiezhou impl<K: Ord, V> Clone for NodePtr<K, V> { 65*e0dfd4d5Shanjiezhou fn clone(&self) -> NodePtr<K, V> { 66*e0dfd4d5Shanjiezhou NodePtr(self.0) 67*e0dfd4d5Shanjiezhou } 68*e0dfd4d5Shanjiezhou } 69*e0dfd4d5Shanjiezhou 70*e0dfd4d5Shanjiezhou impl<K: Ord, V> Copy for NodePtr<K, V> {} 71*e0dfd4d5Shanjiezhou 72*e0dfd4d5Shanjiezhou impl<K: Ord, V> Ord for NodePtr<K, V> { 73*e0dfd4d5Shanjiezhou fn cmp(&self, other: &NodePtr<K, V>) -> Ordering { 74*e0dfd4d5Shanjiezhou unsafe { (*self.0).key.cmp(&(*other.0).key) } 75*e0dfd4d5Shanjiezhou } 76*e0dfd4d5Shanjiezhou } 77*e0dfd4d5Shanjiezhou 78*e0dfd4d5Shanjiezhou impl<K: Ord, V> PartialOrd for NodePtr<K, V> { 79*e0dfd4d5Shanjiezhou fn partial_cmp(&self, other: &NodePtr<K, V>) -> Option<Ordering> { 80*e0dfd4d5Shanjiezhou unsafe { Some((*self.0).key.cmp(&(*other.0).key)) } 81*e0dfd4d5Shanjiezhou } 82*e0dfd4d5Shanjiezhou } 83*e0dfd4d5Shanjiezhou 84*e0dfd4d5Shanjiezhou impl<K: Ord, V> PartialEq for NodePtr<K, V> { 85*e0dfd4d5Shanjiezhou fn eq(&self, other: &NodePtr<K, V>) -> bool { 86*e0dfd4d5Shanjiezhou self.0 == other.0 87*e0dfd4d5Shanjiezhou } 88*e0dfd4d5Shanjiezhou } 89*e0dfd4d5Shanjiezhou 90*e0dfd4d5Shanjiezhou impl<K: Ord, V> Eq for NodePtr<K, V> {} 91*e0dfd4d5Shanjiezhou 92*e0dfd4d5Shanjiezhou impl<K: Ord, V> NodePtr<K, V> { 93*e0dfd4d5Shanjiezhou fn new(k: K, v: V) -> NodePtr<K, V> { 94*e0dfd4d5Shanjiezhou let node = RBTreeNode { 95*e0dfd4d5Shanjiezhou color: Color::Black, 96*e0dfd4d5Shanjiezhou left: NodePtr::null(), 97*e0dfd4d5Shanjiezhou right: NodePtr::null(), 98*e0dfd4d5Shanjiezhou parent: NodePtr::null(), 99*e0dfd4d5Shanjiezhou key: k, 100*e0dfd4d5Shanjiezhou value: v, 101*e0dfd4d5Shanjiezhou }; 102*e0dfd4d5Shanjiezhou NodePtr(Box::into_raw(Box::new(node))) 103*e0dfd4d5Shanjiezhou } 104*e0dfd4d5Shanjiezhou 105*e0dfd4d5Shanjiezhou #[inline] 106*e0dfd4d5Shanjiezhou fn set_color(&mut self, color: Color) { 107*e0dfd4d5Shanjiezhou if self.is_null() { 108*e0dfd4d5Shanjiezhou return; 109*e0dfd4d5Shanjiezhou } 110*e0dfd4d5Shanjiezhou unsafe { 111*e0dfd4d5Shanjiezhou (*self.0).color = color; 112*e0dfd4d5Shanjiezhou } 113*e0dfd4d5Shanjiezhou } 114*e0dfd4d5Shanjiezhou 115*e0dfd4d5Shanjiezhou #[inline] 116*e0dfd4d5Shanjiezhou fn set_red_color(&mut self) { 117*e0dfd4d5Shanjiezhou self.set_color(Color::Red); 118*e0dfd4d5Shanjiezhou } 119*e0dfd4d5Shanjiezhou 120*e0dfd4d5Shanjiezhou #[inline] 121*e0dfd4d5Shanjiezhou fn set_black_color(&mut self) { 122*e0dfd4d5Shanjiezhou self.set_color(Color::Black); 123*e0dfd4d5Shanjiezhou } 124*e0dfd4d5Shanjiezhou 125*e0dfd4d5Shanjiezhou #[inline] 126*e0dfd4d5Shanjiezhou fn get_color(&self) -> Color { 127*e0dfd4d5Shanjiezhou if self.is_null() { 128*e0dfd4d5Shanjiezhou return Color::Black; 129*e0dfd4d5Shanjiezhou } 130*e0dfd4d5Shanjiezhou unsafe { (*self.0).color } 131*e0dfd4d5Shanjiezhou } 132*e0dfd4d5Shanjiezhou 133*e0dfd4d5Shanjiezhou #[inline] 134*e0dfd4d5Shanjiezhou fn is_red_color(&self) -> bool { 135*e0dfd4d5Shanjiezhou if self.is_null() { 136*e0dfd4d5Shanjiezhou return false; 137*e0dfd4d5Shanjiezhou } 138*e0dfd4d5Shanjiezhou unsafe { (*self.0).color == Color::Red } 139*e0dfd4d5Shanjiezhou } 140*e0dfd4d5Shanjiezhou 141*e0dfd4d5Shanjiezhou #[inline] 142*e0dfd4d5Shanjiezhou fn is_black_color(&self) -> bool { 143*e0dfd4d5Shanjiezhou if self.is_null() { 144*e0dfd4d5Shanjiezhou return true; 145*e0dfd4d5Shanjiezhou } 146*e0dfd4d5Shanjiezhou unsafe { (*self.0).color == Color::Black } 147*e0dfd4d5Shanjiezhou } 148*e0dfd4d5Shanjiezhou 149*e0dfd4d5Shanjiezhou #[inline] 150*e0dfd4d5Shanjiezhou fn is_left_child(&self) -> bool { 151*e0dfd4d5Shanjiezhou self.parent().left() == *self 152*e0dfd4d5Shanjiezhou } 153*e0dfd4d5Shanjiezhou 154*e0dfd4d5Shanjiezhou #[inline] 155*e0dfd4d5Shanjiezhou fn is_right_child(&self) -> bool { 156*e0dfd4d5Shanjiezhou self.parent().right() == *self 157*e0dfd4d5Shanjiezhou } 158*e0dfd4d5Shanjiezhou 159*e0dfd4d5Shanjiezhou #[inline] 160*e0dfd4d5Shanjiezhou fn min_node(self) -> NodePtr<K, V> { 161*e0dfd4d5Shanjiezhou let mut temp = self.clone(); 162*e0dfd4d5Shanjiezhou while !temp.left().is_null() { 163*e0dfd4d5Shanjiezhou temp = temp.left(); 164*e0dfd4d5Shanjiezhou } 165*e0dfd4d5Shanjiezhou return temp; 166*e0dfd4d5Shanjiezhou } 167*e0dfd4d5Shanjiezhou 168*e0dfd4d5Shanjiezhou #[inline] 169*e0dfd4d5Shanjiezhou fn max_node(self) -> NodePtr<K, V> { 170*e0dfd4d5Shanjiezhou let mut temp = self.clone(); 171*e0dfd4d5Shanjiezhou while !temp.right().is_null() { 172*e0dfd4d5Shanjiezhou temp = temp.right(); 173*e0dfd4d5Shanjiezhou } 174*e0dfd4d5Shanjiezhou return temp; 175*e0dfd4d5Shanjiezhou } 176*e0dfd4d5Shanjiezhou 177*e0dfd4d5Shanjiezhou #[inline] 178*e0dfd4d5Shanjiezhou fn next(self) -> NodePtr<K, V> { 179*e0dfd4d5Shanjiezhou if !self.right().is_null() { 180*e0dfd4d5Shanjiezhou self.right().min_node() 181*e0dfd4d5Shanjiezhou } else { 182*e0dfd4d5Shanjiezhou let mut temp = self; 183*e0dfd4d5Shanjiezhou loop { 184*e0dfd4d5Shanjiezhou if temp.parent().is_null() { 185*e0dfd4d5Shanjiezhou return NodePtr::null(); 186*e0dfd4d5Shanjiezhou } 187*e0dfd4d5Shanjiezhou if temp.is_left_child() { 188*e0dfd4d5Shanjiezhou return temp.parent(); 189*e0dfd4d5Shanjiezhou } 190*e0dfd4d5Shanjiezhou temp = temp.parent(); 191*e0dfd4d5Shanjiezhou } 192*e0dfd4d5Shanjiezhou } 193*e0dfd4d5Shanjiezhou } 194*e0dfd4d5Shanjiezhou 195*e0dfd4d5Shanjiezhou #[inline] 196*e0dfd4d5Shanjiezhou fn prev(self) -> NodePtr<K, V> { 197*e0dfd4d5Shanjiezhou if !self.left().is_null() { 198*e0dfd4d5Shanjiezhou self.left().max_node() 199*e0dfd4d5Shanjiezhou } else { 200*e0dfd4d5Shanjiezhou let mut temp = self; 201*e0dfd4d5Shanjiezhou loop { 202*e0dfd4d5Shanjiezhou if temp.parent().is_null() { 203*e0dfd4d5Shanjiezhou return NodePtr::null(); 204*e0dfd4d5Shanjiezhou } 205*e0dfd4d5Shanjiezhou if temp.is_right_child() { 206*e0dfd4d5Shanjiezhou return temp.parent(); 207*e0dfd4d5Shanjiezhou } 208*e0dfd4d5Shanjiezhou temp = temp.parent(); 209*e0dfd4d5Shanjiezhou } 210*e0dfd4d5Shanjiezhou } 211*e0dfd4d5Shanjiezhou } 212*e0dfd4d5Shanjiezhou 213*e0dfd4d5Shanjiezhou #[inline] 214*e0dfd4d5Shanjiezhou fn set_parent(&mut self, parent: NodePtr<K, V>) { 215*e0dfd4d5Shanjiezhou if self.is_null() { 216*e0dfd4d5Shanjiezhou return; 217*e0dfd4d5Shanjiezhou } 218*e0dfd4d5Shanjiezhou unsafe { (*self.0).parent = parent } 219*e0dfd4d5Shanjiezhou } 220*e0dfd4d5Shanjiezhou 221*e0dfd4d5Shanjiezhou #[inline] 222*e0dfd4d5Shanjiezhou fn set_left(&mut self, left: NodePtr<K, V>) { 223*e0dfd4d5Shanjiezhou if self.is_null() { 224*e0dfd4d5Shanjiezhou return; 225*e0dfd4d5Shanjiezhou } 226*e0dfd4d5Shanjiezhou unsafe { (*self.0).left = left } 227*e0dfd4d5Shanjiezhou } 228*e0dfd4d5Shanjiezhou 229*e0dfd4d5Shanjiezhou #[inline] 230*e0dfd4d5Shanjiezhou fn set_right(&mut self, right: NodePtr<K, V>) { 231*e0dfd4d5Shanjiezhou if self.is_null() { 232*e0dfd4d5Shanjiezhou return; 233*e0dfd4d5Shanjiezhou } 234*e0dfd4d5Shanjiezhou unsafe { (*self.0).right = right } 235*e0dfd4d5Shanjiezhou } 236*e0dfd4d5Shanjiezhou 237*e0dfd4d5Shanjiezhou 238*e0dfd4d5Shanjiezhou #[inline] 239*e0dfd4d5Shanjiezhou fn parent(&self) -> NodePtr<K, V> { 240*e0dfd4d5Shanjiezhou if self.is_null() { 241*e0dfd4d5Shanjiezhou return NodePtr::null(); 242*e0dfd4d5Shanjiezhou } 243*e0dfd4d5Shanjiezhou unsafe { (*self.0).parent.clone() } 244*e0dfd4d5Shanjiezhou } 245*e0dfd4d5Shanjiezhou 246*e0dfd4d5Shanjiezhou #[inline] 247*e0dfd4d5Shanjiezhou fn left(&self) -> NodePtr<K, V> { 248*e0dfd4d5Shanjiezhou if self.is_null() { 249*e0dfd4d5Shanjiezhou return NodePtr::null(); 250*e0dfd4d5Shanjiezhou } 251*e0dfd4d5Shanjiezhou unsafe { (*self.0).left.clone() } 252*e0dfd4d5Shanjiezhou } 253*e0dfd4d5Shanjiezhou 254*e0dfd4d5Shanjiezhou #[inline] 255*e0dfd4d5Shanjiezhou fn right(&self) -> NodePtr<K, V> { 256*e0dfd4d5Shanjiezhou if self.is_null() { 257*e0dfd4d5Shanjiezhou return NodePtr::null(); 258*e0dfd4d5Shanjiezhou } 259*e0dfd4d5Shanjiezhou unsafe { (*self.0).right.clone() } 260*e0dfd4d5Shanjiezhou } 261*e0dfd4d5Shanjiezhou 262*e0dfd4d5Shanjiezhou #[inline] 263*e0dfd4d5Shanjiezhou fn null() -> NodePtr<K, V> { 264*e0dfd4d5Shanjiezhou NodePtr(ptr::null_mut()) 265*e0dfd4d5Shanjiezhou } 266*e0dfd4d5Shanjiezhou 267*e0dfd4d5Shanjiezhou #[inline] 268*e0dfd4d5Shanjiezhou fn is_null(&self) -> bool { 269*e0dfd4d5Shanjiezhou self.0.is_null() 270*e0dfd4d5Shanjiezhou } 271*e0dfd4d5Shanjiezhou } 272*e0dfd4d5Shanjiezhou 273*e0dfd4d5Shanjiezhou impl<K: Ord + Clone, V: Clone> NodePtr<K, V> { 274*e0dfd4d5Shanjiezhou unsafe fn deep_clone(&self) -> NodePtr<K, V> { 275*e0dfd4d5Shanjiezhou let mut node = NodePtr::new((*self.0).key.clone(), (*self.0).value.clone()); 276*e0dfd4d5Shanjiezhou if !self.left().is_null() { 277*e0dfd4d5Shanjiezhou node.set_left(self.left().deep_clone()); 278*e0dfd4d5Shanjiezhou node.left().set_parent(node); 279*e0dfd4d5Shanjiezhou } 280*e0dfd4d5Shanjiezhou if !self.right().is_null() { 281*e0dfd4d5Shanjiezhou node.set_right(self.right().deep_clone()); 282*e0dfd4d5Shanjiezhou node.right().set_parent(node); 283*e0dfd4d5Shanjiezhou } 284*e0dfd4d5Shanjiezhou node 285*e0dfd4d5Shanjiezhou } 286*e0dfd4d5Shanjiezhou } 287*e0dfd4d5Shanjiezhou 288*e0dfd4d5Shanjiezhou /// A red black tree implemented with Rust 289*e0dfd4d5Shanjiezhou /// It is required that the keys implement the [`Ord`] traits. 290*e0dfd4d5Shanjiezhou 291*e0dfd4d5Shanjiezhou /// # Examples 292*e0dfd4d5Shanjiezhou /// ```rust 293*e0dfd4d5Shanjiezhou /// use rbtree::RBTree; 294*e0dfd4d5Shanjiezhou /// // type inference lets us omit an explicit type signature (which 295*e0dfd4d5Shanjiezhou /// // would be `RBTree<&str, &str>` in this example). 296*e0dfd4d5Shanjiezhou /// let mut book_reviews = RBTree::new(); 297*e0dfd4d5Shanjiezhou /// 298*e0dfd4d5Shanjiezhou /// // review some books. 299*e0dfd4d5Shanjiezhou /// book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book."); 300*e0dfd4d5Shanjiezhou /// book_reviews.insert("Grimms' Fairy Tales", "Masterpiece."); 301*e0dfd4d5Shanjiezhou /// book_reviews.insert("Pride and Prejudice", "Very enjoyable."); 302*e0dfd4d5Shanjiezhou /// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot."); 303*e0dfd4d5Shanjiezhou /// 304*e0dfd4d5Shanjiezhou /// // check for a specific one. 305*e0dfd4d5Shanjiezhou /// if !book_reviews.contains_key(&"Les Misérables") { 306*e0dfd4d5Shanjiezhou /// kdebug!( 307*e0dfd4d5Shanjiezhou /// "We've got {} reviews, but Les Misérables ain't one.", 308*e0dfd4d5Shanjiezhou /// book_reviews.len() 309*e0dfd4d5Shanjiezhou /// ); 310*e0dfd4d5Shanjiezhou /// } 311*e0dfd4d5Shanjiezhou /// 312*e0dfd4d5Shanjiezhou /// // oops, this review has a lot of spelling mistakes, let's delete it. 313*e0dfd4d5Shanjiezhou /// book_reviews.remove(&"The Adventures of Sherlock Holmes"); 314*e0dfd4d5Shanjiezhou /// 315*e0dfd4d5Shanjiezhou /// // look up the values associated with some keys. 316*e0dfd4d5Shanjiezhou /// let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"]; 317*e0dfd4d5Shanjiezhou /// for book in &to_find { 318*e0dfd4d5Shanjiezhou /// match book_reviews.get(book) { 319*e0dfd4d5Shanjiezhou /// Some(review) => kdebug!("{}: {}", book, review), 320*e0dfd4d5Shanjiezhou /// None => kdebug!("{} is unreviewed.", book), 321*e0dfd4d5Shanjiezhou /// } 322*e0dfd4d5Shanjiezhou /// } 323*e0dfd4d5Shanjiezhou /// 324*e0dfd4d5Shanjiezhou /// // iterate over everything. 325*e0dfd4d5Shanjiezhou /// for (book, review) in book_reviews.iter() { 326*e0dfd4d5Shanjiezhou /// kdebug!("{}: \"{}\"", book, review); 327*e0dfd4d5Shanjiezhou /// } 328*e0dfd4d5Shanjiezhou /// 329*e0dfd4d5Shanjiezhou /// book_reviews.print_tree(); 330*e0dfd4d5Shanjiezhou /// ``` 331*e0dfd4d5Shanjiezhou /// 332*e0dfd4d5Shanjiezhou /// // A `RBTree` with fixed list of elements can be initialized from an array: 333*e0dfd4d5Shanjiezhou /// ``` 334*e0dfd4d5Shanjiezhou /// use rbtree::RBTree; 335*e0dfd4d5Shanjiezhou /// let timber_resources: RBTree<&str, i32> = 336*e0dfd4d5Shanjiezhou /// [("Norway", 100), 337*e0dfd4d5Shanjiezhou /// ("Denmark", 50), 338*e0dfd4d5Shanjiezhou /// ("Iceland", 10)] 339*e0dfd4d5Shanjiezhou /// .iter().cloned().collect(); 340*e0dfd4d5Shanjiezhou /// // use the values stored in rbtree 341*e0dfd4d5Shanjiezhou /// ``` 342*e0dfd4d5Shanjiezhou pub struct RBTree<K: Ord, V> { 343*e0dfd4d5Shanjiezhou root: NodePtr<K, V>, 344*e0dfd4d5Shanjiezhou len: usize, 345*e0dfd4d5Shanjiezhou } 346*e0dfd4d5Shanjiezhou 347*e0dfd4d5Shanjiezhou // Drop all owned pointers if the tree is dropped 348*e0dfd4d5Shanjiezhou impl<K: Ord, V> Drop for RBTree<K, V> { 349*e0dfd4d5Shanjiezhou #[inline] 350*e0dfd4d5Shanjiezhou fn drop(&mut self) { 351*e0dfd4d5Shanjiezhou self.clear(); 352*e0dfd4d5Shanjiezhou } 353*e0dfd4d5Shanjiezhou } 354*e0dfd4d5Shanjiezhou 355*e0dfd4d5Shanjiezhou /// If key and value are both impl Clone, we can call clone to get a copy. 356*e0dfd4d5Shanjiezhou impl<K: Ord + Clone, V: Clone> Clone for RBTree<K, V> { 357*e0dfd4d5Shanjiezhou fn clone(&self) -> RBTree<K, V> { 358*e0dfd4d5Shanjiezhou unsafe { 359*e0dfd4d5Shanjiezhou let mut new = RBTree::new(); 360*e0dfd4d5Shanjiezhou new.root = self.root.deep_clone(); 361*e0dfd4d5Shanjiezhou new.len = self.len; 362*e0dfd4d5Shanjiezhou new 363*e0dfd4d5Shanjiezhou } 364*e0dfd4d5Shanjiezhou } 365*e0dfd4d5Shanjiezhou } 366*e0dfd4d5Shanjiezhou 367*e0dfd4d5Shanjiezhou impl<K, V> Debug for RBTree<K, V> 368*e0dfd4d5Shanjiezhou where 369*e0dfd4d5Shanjiezhou K: Ord + Debug, 370*e0dfd4d5Shanjiezhou V: Debug, 371*e0dfd4d5Shanjiezhou { 372*e0dfd4d5Shanjiezhou fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 373*e0dfd4d5Shanjiezhou f.debug_map().entries(self.iter()).finish() 374*e0dfd4d5Shanjiezhou } 375*e0dfd4d5Shanjiezhou } 376*e0dfd4d5Shanjiezhou 377*e0dfd4d5Shanjiezhou /// This is a method to help us to get inner struct. 378*e0dfd4d5Shanjiezhou impl<K: Ord + Debug, V: Debug> RBTree<K, V> { 379*e0dfd4d5Shanjiezhou fn tree_print(&self, node: NodePtr<K, V>, direction: i32) { 380*e0dfd4d5Shanjiezhou if node.is_null() { 381*e0dfd4d5Shanjiezhou return; 382*e0dfd4d5Shanjiezhou } 383*e0dfd4d5Shanjiezhou if direction == 0 { 384*e0dfd4d5Shanjiezhou unsafe { 385*e0dfd4d5Shanjiezhou kdebug!("'{:?}' is root node", (*node.0)); 386*e0dfd4d5Shanjiezhou } 387*e0dfd4d5Shanjiezhou } else { 388*e0dfd4d5Shanjiezhou let direct = if direction == -1 { "left" } else { "right" }; 389*e0dfd4d5Shanjiezhou unsafe { 390*e0dfd4d5Shanjiezhou kdebug!( 391*e0dfd4d5Shanjiezhou "{:?} is {:?}'s {:?} child ", 392*e0dfd4d5Shanjiezhou (*node.0), 393*e0dfd4d5Shanjiezhou *node.parent().0, 394*e0dfd4d5Shanjiezhou direct 395*e0dfd4d5Shanjiezhou ); 396*e0dfd4d5Shanjiezhou } 397*e0dfd4d5Shanjiezhou } 398*e0dfd4d5Shanjiezhou self.tree_print(node.left(), -1); 399*e0dfd4d5Shanjiezhou self.tree_print(node.right(), 1); 400*e0dfd4d5Shanjiezhou } 401*e0dfd4d5Shanjiezhou 402*e0dfd4d5Shanjiezhou pub fn print_tree(&self) { 403*e0dfd4d5Shanjiezhou if self.root.is_null() { 404*e0dfd4d5Shanjiezhou kdebug!("This is a empty tree"); 405*e0dfd4d5Shanjiezhou return; 406*e0dfd4d5Shanjiezhou } 407*e0dfd4d5Shanjiezhou kdebug!("This tree size = {:?}, begin:-------------", self.len()); 408*e0dfd4d5Shanjiezhou self.tree_print(self.root, 0); 409*e0dfd4d5Shanjiezhou kdebug!("end--------------------------"); 410*e0dfd4d5Shanjiezhou } 411*e0dfd4d5Shanjiezhou } 412*e0dfd4d5Shanjiezhou 413*e0dfd4d5Shanjiezhou /// all key be same, but it has multi key, if has multi key, it perhaps no correct 414*e0dfd4d5Shanjiezhou impl<K, V> PartialEq for RBTree<K, V> 415*e0dfd4d5Shanjiezhou where 416*e0dfd4d5Shanjiezhou K: Eq + Ord, 417*e0dfd4d5Shanjiezhou V: PartialEq, 418*e0dfd4d5Shanjiezhou { 419*e0dfd4d5Shanjiezhou fn eq(&self, other: &RBTree<K, V>) -> bool { 420*e0dfd4d5Shanjiezhou if self.len() != other.len() { 421*e0dfd4d5Shanjiezhou return false; 422*e0dfd4d5Shanjiezhou } 423*e0dfd4d5Shanjiezhou 424*e0dfd4d5Shanjiezhou self.iter().all(|(key, value)| { 425*e0dfd4d5Shanjiezhou other.get(key).map_or(false, |v| *value == *v) 426*e0dfd4d5Shanjiezhou }) 427*e0dfd4d5Shanjiezhou } 428*e0dfd4d5Shanjiezhou } 429*e0dfd4d5Shanjiezhou 430*e0dfd4d5Shanjiezhou impl<K, V> Eq for RBTree<K, V> 431*e0dfd4d5Shanjiezhou where 432*e0dfd4d5Shanjiezhou K: Eq + Ord, 433*e0dfd4d5Shanjiezhou V: Eq, 434*e0dfd4d5Shanjiezhou { 435*e0dfd4d5Shanjiezhou } 436*e0dfd4d5Shanjiezhou 437*e0dfd4d5Shanjiezhou impl<'a, K, V> Index<&'a K> for RBTree<K, V> 438*e0dfd4d5Shanjiezhou where 439*e0dfd4d5Shanjiezhou K: Ord, 440*e0dfd4d5Shanjiezhou { 441*e0dfd4d5Shanjiezhou type Output = V; 442*e0dfd4d5Shanjiezhou 443*e0dfd4d5Shanjiezhou #[inline] 444*e0dfd4d5Shanjiezhou fn index(&self, index: &K) -> &V { 445*e0dfd4d5Shanjiezhou self.get(index).expect("no entry found for key") 446*e0dfd4d5Shanjiezhou } 447*e0dfd4d5Shanjiezhou } 448*e0dfd4d5Shanjiezhou 449*e0dfd4d5Shanjiezhou 450*e0dfd4d5Shanjiezhou impl<K: Ord, V> FromIterator<(K, V)> for RBTree<K, V> { 451*e0dfd4d5Shanjiezhou fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> RBTree<K, V> { 452*e0dfd4d5Shanjiezhou let mut tree = RBTree::new(); 453*e0dfd4d5Shanjiezhou tree.extend(iter); 454*e0dfd4d5Shanjiezhou tree 455*e0dfd4d5Shanjiezhou } 456*e0dfd4d5Shanjiezhou } 457*e0dfd4d5Shanjiezhou 458*e0dfd4d5Shanjiezhou /// RBTree into iter 459*e0dfd4d5Shanjiezhou impl<K: Ord, V> Extend<(K, V)> for RBTree<K, V> { 460*e0dfd4d5Shanjiezhou fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) { 461*e0dfd4d5Shanjiezhou let iter = iter.into_iter(); 462*e0dfd4d5Shanjiezhou for (k, v) in iter { 463*e0dfd4d5Shanjiezhou self.insert(k, v); 464*e0dfd4d5Shanjiezhou } 465*e0dfd4d5Shanjiezhou } 466*e0dfd4d5Shanjiezhou } 467*e0dfd4d5Shanjiezhou 468*e0dfd4d5Shanjiezhou /// provide the rbtree all keys 469*e0dfd4d5Shanjiezhou /// # Examples 470*e0dfd4d5Shanjiezhou /// ``` 471*e0dfd4d5Shanjiezhou /// use rbtree::RBTree; 472*e0dfd4d5Shanjiezhou /// let mut m = RBTree::new(); 473*e0dfd4d5Shanjiezhou /// for i in 1..6 { 474*e0dfd4d5Shanjiezhou /// m.insert(i, i); 475*e0dfd4d5Shanjiezhou /// } 476*e0dfd4d5Shanjiezhou /// let vec = vec![1, 2, 3, 4, 5]; 477*e0dfd4d5Shanjiezhou /// let key_vec: Vec<_> = m.keys().cloned().collect(); 478*e0dfd4d5Shanjiezhou /// assert_eq!(vec, key_vec); 479*e0dfd4d5Shanjiezhou /// ``` 480*e0dfd4d5Shanjiezhou pub struct Keys<'a, K: Ord + 'a, V: 'a> { 481*e0dfd4d5Shanjiezhou inner: Iter<'a, K, V>, 482*e0dfd4d5Shanjiezhou } 483*e0dfd4d5Shanjiezhou 484*e0dfd4d5Shanjiezhou impl<'a, K: Ord, V> Clone for Keys<'a, K, V> { 485*e0dfd4d5Shanjiezhou fn clone(&self) -> Keys<'a, K, V> { 486*e0dfd4d5Shanjiezhou Keys { inner: self.inner.clone() } 487*e0dfd4d5Shanjiezhou } 488*e0dfd4d5Shanjiezhou } 489*e0dfd4d5Shanjiezhou 490*e0dfd4d5Shanjiezhou impl<'a, K: Ord + Debug, V> fmt::Debug for Keys<'a, K, V> { 491*e0dfd4d5Shanjiezhou fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 492*e0dfd4d5Shanjiezhou f.debug_list().entries(self.clone()).finish() 493*e0dfd4d5Shanjiezhou } 494*e0dfd4d5Shanjiezhou } 495*e0dfd4d5Shanjiezhou 496*e0dfd4d5Shanjiezhou impl<'a, K: Ord, V> Iterator for Keys<'a, K, V> { 497*e0dfd4d5Shanjiezhou type Item = &'a K; 498*e0dfd4d5Shanjiezhou 499*e0dfd4d5Shanjiezhou #[inline] 500*e0dfd4d5Shanjiezhou fn next(&mut self) -> Option<&'a K> { 501*e0dfd4d5Shanjiezhou self.inner.next().map(|(k, _)| k) 502*e0dfd4d5Shanjiezhou } 503*e0dfd4d5Shanjiezhou 504*e0dfd4d5Shanjiezhou #[inline] 505*e0dfd4d5Shanjiezhou fn size_hint(&self) -> (usize, Option<usize>) { 506*e0dfd4d5Shanjiezhou self.inner.size_hint() 507*e0dfd4d5Shanjiezhou } 508*e0dfd4d5Shanjiezhou } 509*e0dfd4d5Shanjiezhou 510*e0dfd4d5Shanjiezhou /// provide the rbtree all values order by key 511*e0dfd4d5Shanjiezhou /// # Examples 512*e0dfd4d5Shanjiezhou /// ``` 513*e0dfd4d5Shanjiezhou /// use rbtree::RBTree; 514*e0dfd4d5Shanjiezhou /// let mut m = RBTree::new(); 515*e0dfd4d5Shanjiezhou /// m.insert(2, 5); 516*e0dfd4d5Shanjiezhou /// m.insert(1, 6); 517*e0dfd4d5Shanjiezhou /// m.insert(3, 8); 518*e0dfd4d5Shanjiezhou /// m.insert(4, 9); 519*e0dfd4d5Shanjiezhou /// let vec = vec![6, 5, 8, 9]; 520*e0dfd4d5Shanjiezhou /// let key_vec: Vec<_> = m.values().cloned().collect(); 521*e0dfd4d5Shanjiezhou /// assert_eq!(vec, key_vec); 522*e0dfd4d5Shanjiezhou /// ``` 523*e0dfd4d5Shanjiezhou pub struct Values<'a, K: 'a + Ord, V: 'a> { 524*e0dfd4d5Shanjiezhou inner: Iter<'a, K, V>, 525*e0dfd4d5Shanjiezhou } 526*e0dfd4d5Shanjiezhou 527*e0dfd4d5Shanjiezhou impl<'a, K: Ord, V> Clone for Values<'a, K, V> { 528*e0dfd4d5Shanjiezhou fn clone(&self) -> Values<'a, K, V> { 529*e0dfd4d5Shanjiezhou Values { inner: self.inner.clone() } 530*e0dfd4d5Shanjiezhou } 531*e0dfd4d5Shanjiezhou } 532*e0dfd4d5Shanjiezhou 533*e0dfd4d5Shanjiezhou impl<'a, K: Ord + Debug, V: Debug> fmt::Debug for Values<'a, K, V> { 534*e0dfd4d5Shanjiezhou fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 535*e0dfd4d5Shanjiezhou f.debug_list().entries(self.clone()).finish() 536*e0dfd4d5Shanjiezhou } 537*e0dfd4d5Shanjiezhou } 538*e0dfd4d5Shanjiezhou 539*e0dfd4d5Shanjiezhou 540*e0dfd4d5Shanjiezhou impl<'a, K: Ord, V> Iterator for Values<'a, K, V> { 541*e0dfd4d5Shanjiezhou type Item = &'a V; 542*e0dfd4d5Shanjiezhou 543*e0dfd4d5Shanjiezhou #[inline] 544*e0dfd4d5Shanjiezhou fn next(&mut self) -> Option<&'a V> { 545*e0dfd4d5Shanjiezhou self.inner.next().map(|(_, v)| v) 546*e0dfd4d5Shanjiezhou } 547*e0dfd4d5Shanjiezhou 548*e0dfd4d5Shanjiezhou #[inline] 549*e0dfd4d5Shanjiezhou fn size_hint(&self) -> (usize, Option<usize>) { 550*e0dfd4d5Shanjiezhou self.inner.size_hint() 551*e0dfd4d5Shanjiezhou } 552*e0dfd4d5Shanjiezhou } 553*e0dfd4d5Shanjiezhou 554*e0dfd4d5Shanjiezhou /// provide the rbtree all values and it can be modify 555*e0dfd4d5Shanjiezhou /// # Examples 556*e0dfd4d5Shanjiezhou /// ``` 557*e0dfd4d5Shanjiezhou /// use rbtree::RBTree; 558*e0dfd4d5Shanjiezhou /// let mut m = RBTree::new(); 559*e0dfd4d5Shanjiezhou /// for i in 0..32 { 560*e0dfd4d5Shanjiezhou /// m.insert(i, i); 561*e0dfd4d5Shanjiezhou /// } 562*e0dfd4d5Shanjiezhou /// assert_eq!(m.len(), 32); 563*e0dfd4d5Shanjiezhou /// for v in m.values_mut() { 564*e0dfd4d5Shanjiezhou /// *v *= 2; 565*e0dfd4d5Shanjiezhou /// } 566*e0dfd4d5Shanjiezhou /// for i in 0..32 { 567*e0dfd4d5Shanjiezhou /// assert_eq!(m.get(&i).unwrap(), &(i * 2)); 568*e0dfd4d5Shanjiezhou /// } 569*e0dfd4d5Shanjiezhou /// ``` 570*e0dfd4d5Shanjiezhou pub struct ValuesMut<'a, K: 'a + Ord, V: 'a> { 571*e0dfd4d5Shanjiezhou inner: IterMut<'a, K, V>, 572*e0dfd4d5Shanjiezhou } 573*e0dfd4d5Shanjiezhou 574*e0dfd4d5Shanjiezhou impl<'a, K: Ord, V> Clone for ValuesMut<'a, K, V> { 575*e0dfd4d5Shanjiezhou fn clone(&self) -> ValuesMut<'a, K, V> { 576*e0dfd4d5Shanjiezhou ValuesMut { inner: self.inner.clone() } 577*e0dfd4d5Shanjiezhou } 578*e0dfd4d5Shanjiezhou } 579*e0dfd4d5Shanjiezhou 580*e0dfd4d5Shanjiezhou impl<'a, K: Ord + Debug, V: Debug> fmt::Debug for ValuesMut<'a, K, V> { 581*e0dfd4d5Shanjiezhou fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 582*e0dfd4d5Shanjiezhou f.debug_list().entries(self.clone()).finish() 583*e0dfd4d5Shanjiezhou } 584*e0dfd4d5Shanjiezhou } 585*e0dfd4d5Shanjiezhou 586*e0dfd4d5Shanjiezhou impl<'a, K: Ord, V> Iterator for ValuesMut<'a, K, V> { 587*e0dfd4d5Shanjiezhou type Item = &'a mut V; 588*e0dfd4d5Shanjiezhou 589*e0dfd4d5Shanjiezhou #[inline] 590*e0dfd4d5Shanjiezhou fn next(&mut self) -> Option<&'a mut V> { 591*e0dfd4d5Shanjiezhou self.inner.next().map(|(_, v)| v) 592*e0dfd4d5Shanjiezhou } 593*e0dfd4d5Shanjiezhou 594*e0dfd4d5Shanjiezhou #[inline] 595*e0dfd4d5Shanjiezhou fn size_hint(&self) -> (usize, Option<usize>) { 596*e0dfd4d5Shanjiezhou self.inner.size_hint() 597*e0dfd4d5Shanjiezhou } 598*e0dfd4d5Shanjiezhou } 599*e0dfd4d5Shanjiezhou 600*e0dfd4d5Shanjiezhou /// Convert RBTree to iter, move out the tree. 601*e0dfd4d5Shanjiezhou pub struct IntoIter<K: Ord, V> { 602*e0dfd4d5Shanjiezhou head: NodePtr<K, V>, 603*e0dfd4d5Shanjiezhou tail: NodePtr<K, V>, 604*e0dfd4d5Shanjiezhou len: usize, 605*e0dfd4d5Shanjiezhou } 606*e0dfd4d5Shanjiezhou 607*e0dfd4d5Shanjiezhou // Drop all owned pointers if the collection is dropped 608*e0dfd4d5Shanjiezhou impl<K: Ord, V> Drop for IntoIter<K, V> { 609*e0dfd4d5Shanjiezhou #[inline] 610*e0dfd4d5Shanjiezhou fn drop(&mut self) { 611*e0dfd4d5Shanjiezhou for (_, _) in self {} 612*e0dfd4d5Shanjiezhou } 613*e0dfd4d5Shanjiezhou } 614*e0dfd4d5Shanjiezhou 615*e0dfd4d5Shanjiezhou impl<K: Ord, V> Iterator for IntoIter<K, V> { 616*e0dfd4d5Shanjiezhou type Item = (K, V); 617*e0dfd4d5Shanjiezhou 618*e0dfd4d5Shanjiezhou fn next(&mut self) -> Option<(K, V)> { 619*e0dfd4d5Shanjiezhou if self.len == 0 { 620*e0dfd4d5Shanjiezhou return None; 621*e0dfd4d5Shanjiezhou } 622*e0dfd4d5Shanjiezhou 623*e0dfd4d5Shanjiezhou if self.head.is_null() { 624*e0dfd4d5Shanjiezhou return None; 625*e0dfd4d5Shanjiezhou } 626*e0dfd4d5Shanjiezhou 627*e0dfd4d5Shanjiezhou let next = self.head.next(); 628*e0dfd4d5Shanjiezhou let (k, v) = unsafe { 629*e0dfd4d5Shanjiezhou ( 630*e0dfd4d5Shanjiezhou core::ptr::read(&(*self.head.0).key), 631*e0dfd4d5Shanjiezhou core::ptr::read(&(*self.head.0).value), 632*e0dfd4d5Shanjiezhou ) 633*e0dfd4d5Shanjiezhou }; 634*e0dfd4d5Shanjiezhou self.head = next; 635*e0dfd4d5Shanjiezhou self.len -= 1; 636*e0dfd4d5Shanjiezhou Some((k, v)) 637*e0dfd4d5Shanjiezhou } 638*e0dfd4d5Shanjiezhou 639*e0dfd4d5Shanjiezhou fn size_hint(&self) -> (usize, Option<usize>) { 640*e0dfd4d5Shanjiezhou (self.len, Some(self.len)) 641*e0dfd4d5Shanjiezhou } 642*e0dfd4d5Shanjiezhou } 643*e0dfd4d5Shanjiezhou 644*e0dfd4d5Shanjiezhou impl<K: Ord, V> DoubleEndedIterator for IntoIter<K, V> { 645*e0dfd4d5Shanjiezhou #[inline] 646*e0dfd4d5Shanjiezhou fn next_back(&mut self) -> Option<(K, V)> { 647*e0dfd4d5Shanjiezhou if self.len == 0 { 648*e0dfd4d5Shanjiezhou return None; 649*e0dfd4d5Shanjiezhou } 650*e0dfd4d5Shanjiezhou 651*e0dfd4d5Shanjiezhou if self.tail.is_null() { 652*e0dfd4d5Shanjiezhou return None; 653*e0dfd4d5Shanjiezhou } 654*e0dfd4d5Shanjiezhou 655*e0dfd4d5Shanjiezhou let prev = self.tail.prev(); 656*e0dfd4d5Shanjiezhou let obj = unsafe { Box::from_raw(self.tail.0) }; 657*e0dfd4d5Shanjiezhou let (k, v) = obj.pair(); 658*e0dfd4d5Shanjiezhou self.tail = prev; 659*e0dfd4d5Shanjiezhou self.len -= 1; 660*e0dfd4d5Shanjiezhou Some((k, v)) 661*e0dfd4d5Shanjiezhou } 662*e0dfd4d5Shanjiezhou } 663*e0dfd4d5Shanjiezhou 664*e0dfd4d5Shanjiezhou /// provide iter ref for RBTree 665*e0dfd4d5Shanjiezhou /// # Examples 666*e0dfd4d5Shanjiezhou /// ``` 667*e0dfd4d5Shanjiezhou /// use rbtree::RBTree; 668*e0dfd4d5Shanjiezhou /// let mut m = RBTree::new(); 669*e0dfd4d5Shanjiezhou /// for i in 0..32 { 670*e0dfd4d5Shanjiezhou /// m.insert(i, i * 2); 671*e0dfd4d5Shanjiezhou /// } 672*e0dfd4d5Shanjiezhou /// assert_eq!(m.len(), 32); 673*e0dfd4d5Shanjiezhou /// let mut observed: u32 = 0; 674*e0dfd4d5Shanjiezhou /// for (k, v) in m.iter() { 675*e0dfd4d5Shanjiezhou /// assert_eq!(*v, *k * 2); 676*e0dfd4d5Shanjiezhou /// observed |= 1 << *k; 677*e0dfd4d5Shanjiezhou /// } 678*e0dfd4d5Shanjiezhou /// assert_eq!(observed, 0xFFFF_FFFF); 679*e0dfd4d5Shanjiezhou /// ``` 680*e0dfd4d5Shanjiezhou pub struct Iter<'a, K: Ord + 'a, V: 'a> { 681*e0dfd4d5Shanjiezhou head: NodePtr<K, V>, 682*e0dfd4d5Shanjiezhou tail: NodePtr<K, V>, 683*e0dfd4d5Shanjiezhou len: usize, 684*e0dfd4d5Shanjiezhou _marker: marker::PhantomData<&'a ()>, 685*e0dfd4d5Shanjiezhou } 686*e0dfd4d5Shanjiezhou 687*e0dfd4d5Shanjiezhou impl<'a, K: Ord + 'a, V: 'a> Clone for Iter<'a, K, V> { 688*e0dfd4d5Shanjiezhou fn clone(&self) -> Iter<'a, K, V> { 689*e0dfd4d5Shanjiezhou Iter { 690*e0dfd4d5Shanjiezhou head: self.head, 691*e0dfd4d5Shanjiezhou tail: self.tail, 692*e0dfd4d5Shanjiezhou len: self.len, 693*e0dfd4d5Shanjiezhou _marker: self._marker, 694*e0dfd4d5Shanjiezhou } 695*e0dfd4d5Shanjiezhou } 696*e0dfd4d5Shanjiezhou } 697*e0dfd4d5Shanjiezhou 698*e0dfd4d5Shanjiezhou impl<'a, K: Ord + 'a, V: 'a> Iterator for Iter<'a, K, V> { 699*e0dfd4d5Shanjiezhou type Item = (&'a K, &'a V); 700*e0dfd4d5Shanjiezhou 701*e0dfd4d5Shanjiezhou fn next(&mut self) -> Option<(&'a K, &'a V)> { 702*e0dfd4d5Shanjiezhou if self.len == 0 { 703*e0dfd4d5Shanjiezhou return None; 704*e0dfd4d5Shanjiezhou } 705*e0dfd4d5Shanjiezhou 706*e0dfd4d5Shanjiezhou if self.head.is_null() { 707*e0dfd4d5Shanjiezhou return None; 708*e0dfd4d5Shanjiezhou } 709*e0dfd4d5Shanjiezhou 710*e0dfd4d5Shanjiezhou let (k, v) = unsafe { (&(*self.head.0).key, &(*self.head.0).value) }; 711*e0dfd4d5Shanjiezhou self.head = self.head.next(); 712*e0dfd4d5Shanjiezhou self.len -= 1; 713*e0dfd4d5Shanjiezhou Some((k, v)) 714*e0dfd4d5Shanjiezhou } 715*e0dfd4d5Shanjiezhou 716*e0dfd4d5Shanjiezhou fn size_hint(&self) -> (usize, Option<usize>) { 717*e0dfd4d5Shanjiezhou (self.len, Some(self.len)) 718*e0dfd4d5Shanjiezhou } 719*e0dfd4d5Shanjiezhou } 720*e0dfd4d5Shanjiezhou 721*e0dfd4d5Shanjiezhou impl<'a, K: Ord + 'a, V: 'a> DoubleEndedIterator for Iter<'a, K, V> { 722*e0dfd4d5Shanjiezhou #[inline] 723*e0dfd4d5Shanjiezhou fn next_back(&mut self) -> Option<(&'a K, &'a V)> { 724*e0dfd4d5Shanjiezhou // kdebug!("len = {:?}", self.len); 725*e0dfd4d5Shanjiezhou if self.len == 0 { 726*e0dfd4d5Shanjiezhou return None; 727*e0dfd4d5Shanjiezhou } 728*e0dfd4d5Shanjiezhou 729*e0dfd4d5Shanjiezhou if self.tail == self.head { 730*e0dfd4d5Shanjiezhou return None; 731*e0dfd4d5Shanjiezhou } 732*e0dfd4d5Shanjiezhou 733*e0dfd4d5Shanjiezhou let (k, v) = unsafe { (&(*self.tail.0).key, &(*self.tail.0).value) }; 734*e0dfd4d5Shanjiezhou self.tail = self.tail.prev(); 735*e0dfd4d5Shanjiezhou self.len -= 1; 736*e0dfd4d5Shanjiezhou Some((k, v)) 737*e0dfd4d5Shanjiezhou } 738*e0dfd4d5Shanjiezhou } 739*e0dfd4d5Shanjiezhou 740*e0dfd4d5Shanjiezhou /// provide iter mut ref for RBTree 741*e0dfd4d5Shanjiezhou /// # Examples 742*e0dfd4d5Shanjiezhou /// ``` 743*e0dfd4d5Shanjiezhou /// use rbtree::RBTree; 744*e0dfd4d5Shanjiezhou /// let mut m = RBTree::new(); 745*e0dfd4d5Shanjiezhou /// for i in 0..32 { 746*e0dfd4d5Shanjiezhou /// m.insert(i, i); 747*e0dfd4d5Shanjiezhou /// } 748*e0dfd4d5Shanjiezhou /// assert_eq!(m.len(), 32); 749*e0dfd4d5Shanjiezhou /// for (_, v) in m.iter_mut() { 750*e0dfd4d5Shanjiezhou /// *v *= 2; 751*e0dfd4d5Shanjiezhou /// } 752*e0dfd4d5Shanjiezhou /// for i in 0..32 { 753*e0dfd4d5Shanjiezhou /// assert_eq!(m.get(&i).unwrap(), &(i * 2)); 754*e0dfd4d5Shanjiezhou /// } 755*e0dfd4d5Shanjiezhou /// ``` 756*e0dfd4d5Shanjiezhou pub struct IterMut<'a, K: Ord + 'a, V: 'a> { 757*e0dfd4d5Shanjiezhou head: NodePtr<K, V>, 758*e0dfd4d5Shanjiezhou tail: NodePtr<K, V>, 759*e0dfd4d5Shanjiezhou len: usize, 760*e0dfd4d5Shanjiezhou _marker: marker::PhantomData<&'a ()>, 761*e0dfd4d5Shanjiezhou } 762*e0dfd4d5Shanjiezhou 763*e0dfd4d5Shanjiezhou impl<'a, K: Ord + 'a, V: 'a> Clone for IterMut<'a, K, V> { 764*e0dfd4d5Shanjiezhou fn clone(&self) -> IterMut<'a, K, V> { 765*e0dfd4d5Shanjiezhou IterMut { 766*e0dfd4d5Shanjiezhou head: self.head, 767*e0dfd4d5Shanjiezhou tail: self.tail, 768*e0dfd4d5Shanjiezhou len: self.len, 769*e0dfd4d5Shanjiezhou _marker: self._marker, 770*e0dfd4d5Shanjiezhou } 771*e0dfd4d5Shanjiezhou } 772*e0dfd4d5Shanjiezhou } 773*e0dfd4d5Shanjiezhou 774*e0dfd4d5Shanjiezhou impl<'a, K: Ord + 'a, V: 'a> Iterator for IterMut<'a, K, V> { 775*e0dfd4d5Shanjiezhou type Item = (&'a K, &'a mut V); 776*e0dfd4d5Shanjiezhou 777*e0dfd4d5Shanjiezhou fn next(&mut self) -> Option<(&'a K, &'a mut V)> { 778*e0dfd4d5Shanjiezhou if self.len == 0 { 779*e0dfd4d5Shanjiezhou return None; 780*e0dfd4d5Shanjiezhou } 781*e0dfd4d5Shanjiezhou 782*e0dfd4d5Shanjiezhou if self.head.is_null() { 783*e0dfd4d5Shanjiezhou return None; 784*e0dfd4d5Shanjiezhou } 785*e0dfd4d5Shanjiezhou 786*e0dfd4d5Shanjiezhou let (k, v) = unsafe { (&(*self.head.0).key, &mut (*self.head.0).value) }; 787*e0dfd4d5Shanjiezhou self.head = self.head.next(); 788*e0dfd4d5Shanjiezhou self.len -= 1; 789*e0dfd4d5Shanjiezhou Some((k, v)) 790*e0dfd4d5Shanjiezhou } 791*e0dfd4d5Shanjiezhou 792*e0dfd4d5Shanjiezhou fn size_hint(&self) -> (usize, Option<usize>) { 793*e0dfd4d5Shanjiezhou (self.len, Some(self.len)) 794*e0dfd4d5Shanjiezhou } 795*e0dfd4d5Shanjiezhou } 796*e0dfd4d5Shanjiezhou 797*e0dfd4d5Shanjiezhou impl<'a, K: Ord + 'a, V: 'a> DoubleEndedIterator for IterMut<'a, K, V> { 798*e0dfd4d5Shanjiezhou #[inline] 799*e0dfd4d5Shanjiezhou fn next_back(&mut self) -> Option<(&'a K, &'a mut V)> { 800*e0dfd4d5Shanjiezhou if self.len == 0 { 801*e0dfd4d5Shanjiezhou return None; 802*e0dfd4d5Shanjiezhou } 803*e0dfd4d5Shanjiezhou 804*e0dfd4d5Shanjiezhou if self.tail == self.head { 805*e0dfd4d5Shanjiezhou return None; 806*e0dfd4d5Shanjiezhou } 807*e0dfd4d5Shanjiezhou 808*e0dfd4d5Shanjiezhou let (k, v) = unsafe { (&(*self.tail.0).key, &mut (*self.tail.0).value) }; 809*e0dfd4d5Shanjiezhou self.tail = self.tail.prev(); 810*e0dfd4d5Shanjiezhou self.len -= 1; 811*e0dfd4d5Shanjiezhou Some((k, v)) 812*e0dfd4d5Shanjiezhou } 813*e0dfd4d5Shanjiezhou } 814*e0dfd4d5Shanjiezhou 815*e0dfd4d5Shanjiezhou 816*e0dfd4d5Shanjiezhou impl<K: Ord, V> IntoIterator for RBTree<K, V> { 817*e0dfd4d5Shanjiezhou type Item = (K, V); 818*e0dfd4d5Shanjiezhou type IntoIter = IntoIter<K, V>; 819*e0dfd4d5Shanjiezhou 820*e0dfd4d5Shanjiezhou #[inline] 821*e0dfd4d5Shanjiezhou fn into_iter(mut self) -> IntoIter<K, V> { 822*e0dfd4d5Shanjiezhou let iter = if self.root.is_null() { 823*e0dfd4d5Shanjiezhou IntoIter { 824*e0dfd4d5Shanjiezhou head: NodePtr::null(), 825*e0dfd4d5Shanjiezhou tail: NodePtr::null(), 826*e0dfd4d5Shanjiezhou len: self.len, 827*e0dfd4d5Shanjiezhou } 828*e0dfd4d5Shanjiezhou } else { 829*e0dfd4d5Shanjiezhou IntoIter { 830*e0dfd4d5Shanjiezhou head: self.first_child(), 831*e0dfd4d5Shanjiezhou tail: self.last_child(), 832*e0dfd4d5Shanjiezhou len: self.len, 833*e0dfd4d5Shanjiezhou } 834*e0dfd4d5Shanjiezhou }; 835*e0dfd4d5Shanjiezhou self.fast_clear(); 836*e0dfd4d5Shanjiezhou iter 837*e0dfd4d5Shanjiezhou } 838*e0dfd4d5Shanjiezhou } 839*e0dfd4d5Shanjiezhou 840*e0dfd4d5Shanjiezhou impl<K: Ord, V> RBTree<K, V> { 841*e0dfd4d5Shanjiezhou /// Creates an empty `RBTree`. 842*e0dfd4d5Shanjiezhou pub fn new() -> RBTree<K, V> { 843*e0dfd4d5Shanjiezhou RBTree { 844*e0dfd4d5Shanjiezhou root: NodePtr::null(), 845*e0dfd4d5Shanjiezhou len: 0, 846*e0dfd4d5Shanjiezhou } 847*e0dfd4d5Shanjiezhou } 848*e0dfd4d5Shanjiezhou 849*e0dfd4d5Shanjiezhou /// Returns the len of `RBTree`. 850*e0dfd4d5Shanjiezhou #[inline] 851*e0dfd4d5Shanjiezhou pub fn len(&self) -> usize { 852*e0dfd4d5Shanjiezhou self.len 853*e0dfd4d5Shanjiezhou } 854*e0dfd4d5Shanjiezhou 855*e0dfd4d5Shanjiezhou /// Returns `true` if the `RBTree` is empty. 856*e0dfd4d5Shanjiezhou #[inline] 857*e0dfd4d5Shanjiezhou pub fn is_empty(&self) -> bool { 858*e0dfd4d5Shanjiezhou self.root.is_null() 859*e0dfd4d5Shanjiezhou } 860*e0dfd4d5Shanjiezhou 861*e0dfd4d5Shanjiezhou /* 862*e0dfd4d5Shanjiezhou * 对红黑树的节点(x)进行左旋转 863*e0dfd4d5Shanjiezhou * 864*e0dfd4d5Shanjiezhou * 左旋示意图(对节点x进行左旋): 865*e0dfd4d5Shanjiezhou * px px 866*e0dfd4d5Shanjiezhou * / / 867*e0dfd4d5Shanjiezhou * x y 868*e0dfd4d5Shanjiezhou * / \ --(左旋)--> / \ # 869*e0dfd4d5Shanjiezhou * lx y x ry 870*e0dfd4d5Shanjiezhou * / \ / \ 871*e0dfd4d5Shanjiezhou * ly ry lx ly 872*e0dfd4d5Shanjiezhou * 873*e0dfd4d5Shanjiezhou * 874*e0dfd4d5Shanjiezhou */ 875*e0dfd4d5Shanjiezhou #[inline] 876*e0dfd4d5Shanjiezhou unsafe fn left_rotate(&mut self, mut node: NodePtr<K, V>) { 877*e0dfd4d5Shanjiezhou let mut temp = node.right(); 878*e0dfd4d5Shanjiezhou node.set_right(temp.left()); 879*e0dfd4d5Shanjiezhou 880*e0dfd4d5Shanjiezhou if !temp.left().is_null() { 881*e0dfd4d5Shanjiezhou temp.left().set_parent(node.clone()); 882*e0dfd4d5Shanjiezhou } 883*e0dfd4d5Shanjiezhou 884*e0dfd4d5Shanjiezhou temp.set_parent(node.parent()); 885*e0dfd4d5Shanjiezhou if node == self.root { 886*e0dfd4d5Shanjiezhou self.root = temp.clone(); 887*e0dfd4d5Shanjiezhou } else if node == node.parent().left() { 888*e0dfd4d5Shanjiezhou node.parent().set_left(temp.clone()); 889*e0dfd4d5Shanjiezhou } else { 890*e0dfd4d5Shanjiezhou node.parent().set_right(temp.clone()); 891*e0dfd4d5Shanjiezhou } 892*e0dfd4d5Shanjiezhou 893*e0dfd4d5Shanjiezhou temp.set_left(node.clone()); 894*e0dfd4d5Shanjiezhou node.set_parent(temp.clone()); 895*e0dfd4d5Shanjiezhou } 896*e0dfd4d5Shanjiezhou 897*e0dfd4d5Shanjiezhou /* 898*e0dfd4d5Shanjiezhou * 对红黑树的节点(y)进行右旋转 899*e0dfd4d5Shanjiezhou * 900*e0dfd4d5Shanjiezhou * 右旋示意图(对节点y进行左旋): 901*e0dfd4d5Shanjiezhou * py py 902*e0dfd4d5Shanjiezhou * / / 903*e0dfd4d5Shanjiezhou * y x 904*e0dfd4d5Shanjiezhou * / \ --(右旋)--> / \ # 905*e0dfd4d5Shanjiezhou * x ry lx y 906*e0dfd4d5Shanjiezhou * / \ / \ # 907*e0dfd4d5Shanjiezhou * lx rx rx ry 908*e0dfd4d5Shanjiezhou * 909*e0dfd4d5Shanjiezhou */ 910*e0dfd4d5Shanjiezhou #[inline] 911*e0dfd4d5Shanjiezhou unsafe fn right_rotate(&mut self, mut node: NodePtr<K, V>) { 912*e0dfd4d5Shanjiezhou let mut temp = node.left(); 913*e0dfd4d5Shanjiezhou node.set_left(temp.right()); 914*e0dfd4d5Shanjiezhou 915*e0dfd4d5Shanjiezhou if !temp.right().is_null() { 916*e0dfd4d5Shanjiezhou temp.right().set_parent(node.clone()); 917*e0dfd4d5Shanjiezhou } 918*e0dfd4d5Shanjiezhou 919*e0dfd4d5Shanjiezhou temp.set_parent(node.parent()); 920*e0dfd4d5Shanjiezhou if node == self.root { 921*e0dfd4d5Shanjiezhou self.root = temp.clone(); 922*e0dfd4d5Shanjiezhou } else if node == node.parent().right() { 923*e0dfd4d5Shanjiezhou node.parent().set_right(temp.clone()); 924*e0dfd4d5Shanjiezhou } else { 925*e0dfd4d5Shanjiezhou node.parent().set_left(temp.clone()); 926*e0dfd4d5Shanjiezhou } 927*e0dfd4d5Shanjiezhou 928*e0dfd4d5Shanjiezhou temp.set_right(node.clone()); 929*e0dfd4d5Shanjiezhou node.set_parent(temp.clone()); 930*e0dfd4d5Shanjiezhou } 931*e0dfd4d5Shanjiezhou 932*e0dfd4d5Shanjiezhou /// replace value if key exist, if not exist insert it. 933*e0dfd4d5Shanjiezhou /// # Examples 934*e0dfd4d5Shanjiezhou /// ``` 935*e0dfd4d5Shanjiezhou /// use rbtree::RBTree; 936*e0dfd4d5Shanjiezhou /// let mut m = RBTree::new(); 937*e0dfd4d5Shanjiezhou /// assert_eq!(m.len(), 0); 938*e0dfd4d5Shanjiezhou /// m.insert(2, 4); 939*e0dfd4d5Shanjiezhou /// assert_eq!(m.len(), 1); 940*e0dfd4d5Shanjiezhou /// assert_eq!(m.replace_or_insert(2, 6).unwrap(), 4); 941*e0dfd4d5Shanjiezhou /// assert_eq!(m.len(), 1); 942*e0dfd4d5Shanjiezhou /// assert_eq!(*m.get(&2).unwrap(), 6); 943*e0dfd4d5Shanjiezhou /// ``` 944*e0dfd4d5Shanjiezhou #[inline] 945*e0dfd4d5Shanjiezhou pub fn replace_or_insert(&mut self, k: K, mut v: V) -> Option<V> { 946*e0dfd4d5Shanjiezhou let node = self.find_node(&k); 947*e0dfd4d5Shanjiezhou if node.is_null() { 948*e0dfd4d5Shanjiezhou self.insert(k, v); 949*e0dfd4d5Shanjiezhou return None; 950*e0dfd4d5Shanjiezhou } 951*e0dfd4d5Shanjiezhou 952*e0dfd4d5Shanjiezhou unsafe { 953*e0dfd4d5Shanjiezhou mem::swap(&mut v, &mut (*node.0).value); 954*e0dfd4d5Shanjiezhou } 955*e0dfd4d5Shanjiezhou 956*e0dfd4d5Shanjiezhou Some(v) 957*e0dfd4d5Shanjiezhou } 958*e0dfd4d5Shanjiezhou 959*e0dfd4d5Shanjiezhou #[inline] 960*e0dfd4d5Shanjiezhou unsafe fn insert_fixup(&mut self, mut node: NodePtr<K, V>) { 961*e0dfd4d5Shanjiezhou let mut parent; 962*e0dfd4d5Shanjiezhou let mut gparent; 963*e0dfd4d5Shanjiezhou 964*e0dfd4d5Shanjiezhou while node.parent().is_red_color() { 965*e0dfd4d5Shanjiezhou parent = node.parent(); 966*e0dfd4d5Shanjiezhou gparent = parent.parent(); 967*e0dfd4d5Shanjiezhou //若“父节点”是“祖父节点的左孩子” 968*e0dfd4d5Shanjiezhou if parent == gparent.left() { 969*e0dfd4d5Shanjiezhou // Case 1条件:叔叔节点是红色 970*e0dfd4d5Shanjiezhou let mut uncle = gparent.right(); 971*e0dfd4d5Shanjiezhou if !uncle.is_null() && uncle.is_red_color() { 972*e0dfd4d5Shanjiezhou uncle.set_black_color(); 973*e0dfd4d5Shanjiezhou parent.set_black_color(); 974*e0dfd4d5Shanjiezhou gparent.set_red_color(); 975*e0dfd4d5Shanjiezhou node = gparent; 976*e0dfd4d5Shanjiezhou continue; 977*e0dfd4d5Shanjiezhou } 978*e0dfd4d5Shanjiezhou 979*e0dfd4d5Shanjiezhou // Case 2条件:叔叔是黑色,且当前节点是右孩子 980*e0dfd4d5Shanjiezhou if parent.right() == node { 981*e0dfd4d5Shanjiezhou self.left_rotate(parent); 982*e0dfd4d5Shanjiezhou let temp = parent; 983*e0dfd4d5Shanjiezhou parent = node; 984*e0dfd4d5Shanjiezhou node = temp; 985*e0dfd4d5Shanjiezhou } 986*e0dfd4d5Shanjiezhou 987*e0dfd4d5Shanjiezhou // Case 3条件:叔叔是黑色,且当前节点是左孩子。 988*e0dfd4d5Shanjiezhou parent.set_black_color(); 989*e0dfd4d5Shanjiezhou gparent.set_red_color(); 990*e0dfd4d5Shanjiezhou self.right_rotate(gparent); 991*e0dfd4d5Shanjiezhou } else { 992*e0dfd4d5Shanjiezhou // Case 1条件:叔叔节点是红色 993*e0dfd4d5Shanjiezhou let mut uncle = gparent.left(); 994*e0dfd4d5Shanjiezhou if !uncle.is_null() && uncle.is_red_color() { 995*e0dfd4d5Shanjiezhou uncle.set_black_color(); 996*e0dfd4d5Shanjiezhou parent.set_black_color(); 997*e0dfd4d5Shanjiezhou gparent.set_red_color(); 998*e0dfd4d5Shanjiezhou node = gparent; 999*e0dfd4d5Shanjiezhou continue; 1000*e0dfd4d5Shanjiezhou } 1001*e0dfd4d5Shanjiezhou 1002*e0dfd4d5Shanjiezhou // Case 2条件:叔叔是黑色,且当前节点是右孩子 1003*e0dfd4d5Shanjiezhou if parent.left() == node { 1004*e0dfd4d5Shanjiezhou self.right_rotate(parent); 1005*e0dfd4d5Shanjiezhou let temp = parent; 1006*e0dfd4d5Shanjiezhou parent = node; 1007*e0dfd4d5Shanjiezhou node = temp; 1008*e0dfd4d5Shanjiezhou } 1009*e0dfd4d5Shanjiezhou 1010*e0dfd4d5Shanjiezhou // Case 3条件:叔叔是黑色,且当前节点是左孩子。 1011*e0dfd4d5Shanjiezhou parent.set_black_color(); 1012*e0dfd4d5Shanjiezhou gparent.set_red_color(); 1013*e0dfd4d5Shanjiezhou self.left_rotate(gparent); 1014*e0dfd4d5Shanjiezhou } 1015*e0dfd4d5Shanjiezhou } 1016*e0dfd4d5Shanjiezhou self.root.set_black_color(); 1017*e0dfd4d5Shanjiezhou } 1018*e0dfd4d5Shanjiezhou 1019*e0dfd4d5Shanjiezhou #[inline] 1020*e0dfd4d5Shanjiezhou pub fn insert(&mut self, k: K, v: V) { 1021*e0dfd4d5Shanjiezhou self.len += 1; 1022*e0dfd4d5Shanjiezhou let mut node = NodePtr::new(k, v); 1023*e0dfd4d5Shanjiezhou let mut y = NodePtr::null(); 1024*e0dfd4d5Shanjiezhou let mut x = self.root; 1025*e0dfd4d5Shanjiezhou 1026*e0dfd4d5Shanjiezhou while !x.is_null() { 1027*e0dfd4d5Shanjiezhou y = x; 1028*e0dfd4d5Shanjiezhou match node.cmp(&&mut x) { 1029*e0dfd4d5Shanjiezhou Ordering::Less => { 1030*e0dfd4d5Shanjiezhou x = x.left(); 1031*e0dfd4d5Shanjiezhou } 1032*e0dfd4d5Shanjiezhou _ => { 1033*e0dfd4d5Shanjiezhou x = x.right(); 1034*e0dfd4d5Shanjiezhou } 1035*e0dfd4d5Shanjiezhou }; 1036*e0dfd4d5Shanjiezhou } 1037*e0dfd4d5Shanjiezhou node.set_parent(y); 1038*e0dfd4d5Shanjiezhou 1039*e0dfd4d5Shanjiezhou if y.is_null() { 1040*e0dfd4d5Shanjiezhou self.root = node; 1041*e0dfd4d5Shanjiezhou } else { 1042*e0dfd4d5Shanjiezhou match node.cmp(&&mut y) { 1043*e0dfd4d5Shanjiezhou Ordering::Less => { 1044*e0dfd4d5Shanjiezhou y.set_left(node); 1045*e0dfd4d5Shanjiezhou } 1046*e0dfd4d5Shanjiezhou _ => { 1047*e0dfd4d5Shanjiezhou y.set_right(node); 1048*e0dfd4d5Shanjiezhou } 1049*e0dfd4d5Shanjiezhou }; 1050*e0dfd4d5Shanjiezhou } 1051*e0dfd4d5Shanjiezhou 1052*e0dfd4d5Shanjiezhou node.set_red_color(); 1053*e0dfd4d5Shanjiezhou unsafe { 1054*e0dfd4d5Shanjiezhou self.insert_fixup(node); 1055*e0dfd4d5Shanjiezhou } 1056*e0dfd4d5Shanjiezhou } 1057*e0dfd4d5Shanjiezhou 1058*e0dfd4d5Shanjiezhou #[inline] 1059*e0dfd4d5Shanjiezhou fn find_node(&self, k: &K) -> NodePtr<K, V> { 1060*e0dfd4d5Shanjiezhou if self.root.is_null() { 1061*e0dfd4d5Shanjiezhou return NodePtr::null(); 1062*e0dfd4d5Shanjiezhou } 1063*e0dfd4d5Shanjiezhou let mut temp = &self.root; 1064*e0dfd4d5Shanjiezhou unsafe { 1065*e0dfd4d5Shanjiezhou loop { 1066*e0dfd4d5Shanjiezhou let next = match k.cmp(&(*temp.0).key) { 1067*e0dfd4d5Shanjiezhou Ordering::Less => &mut (*temp.0).left, 1068*e0dfd4d5Shanjiezhou Ordering::Greater => &mut (*temp.0).right, 1069*e0dfd4d5Shanjiezhou Ordering::Equal => return *temp, 1070*e0dfd4d5Shanjiezhou }; 1071*e0dfd4d5Shanjiezhou if next.is_null() { 1072*e0dfd4d5Shanjiezhou break; 1073*e0dfd4d5Shanjiezhou } 1074*e0dfd4d5Shanjiezhou temp = next; 1075*e0dfd4d5Shanjiezhou } 1076*e0dfd4d5Shanjiezhou } 1077*e0dfd4d5Shanjiezhou NodePtr::null() 1078*e0dfd4d5Shanjiezhou } 1079*e0dfd4d5Shanjiezhou 1080*e0dfd4d5Shanjiezhou #[inline] 1081*e0dfd4d5Shanjiezhou fn first_child(&self) -> NodePtr<K, V> { 1082*e0dfd4d5Shanjiezhou if self.root.is_null() { 1083*e0dfd4d5Shanjiezhou NodePtr::null() 1084*e0dfd4d5Shanjiezhou } else { 1085*e0dfd4d5Shanjiezhou let mut temp = self.root; 1086*e0dfd4d5Shanjiezhou while !temp.left().is_null() { 1087*e0dfd4d5Shanjiezhou temp = temp.left(); 1088*e0dfd4d5Shanjiezhou } 1089*e0dfd4d5Shanjiezhou return temp; 1090*e0dfd4d5Shanjiezhou } 1091*e0dfd4d5Shanjiezhou } 1092*e0dfd4d5Shanjiezhou 1093*e0dfd4d5Shanjiezhou #[inline] 1094*e0dfd4d5Shanjiezhou fn last_child(&self) -> NodePtr<K, V> { 1095*e0dfd4d5Shanjiezhou if self.root.is_null() { 1096*e0dfd4d5Shanjiezhou NodePtr::null() 1097*e0dfd4d5Shanjiezhou } else { 1098*e0dfd4d5Shanjiezhou let mut temp = self.root; 1099*e0dfd4d5Shanjiezhou while !temp.right().is_null() { 1100*e0dfd4d5Shanjiezhou temp = temp.right(); 1101*e0dfd4d5Shanjiezhou } 1102*e0dfd4d5Shanjiezhou return temp; 1103*e0dfd4d5Shanjiezhou } 1104*e0dfd4d5Shanjiezhou } 1105*e0dfd4d5Shanjiezhou 1106*e0dfd4d5Shanjiezhou #[inline] 1107*e0dfd4d5Shanjiezhou pub fn get_first(&self) -> Option<(&K, &V)> { 1108*e0dfd4d5Shanjiezhou let first = self.first_child(); 1109*e0dfd4d5Shanjiezhou if first.is_null() { 1110*e0dfd4d5Shanjiezhou return None; 1111*e0dfd4d5Shanjiezhou } 1112*e0dfd4d5Shanjiezhou unsafe { Some((&(*first.0).key, &(*first.0).value)) } 1113*e0dfd4d5Shanjiezhou } 1114*e0dfd4d5Shanjiezhou 1115*e0dfd4d5Shanjiezhou #[inline] 1116*e0dfd4d5Shanjiezhou pub fn get_last(&self) -> Option<(&K, &V)> { 1117*e0dfd4d5Shanjiezhou let last = self.last_child(); 1118*e0dfd4d5Shanjiezhou if last.is_null() { 1119*e0dfd4d5Shanjiezhou return None; 1120*e0dfd4d5Shanjiezhou } 1121*e0dfd4d5Shanjiezhou unsafe { Some((&(*last.0).key, &(*last.0).value)) } 1122*e0dfd4d5Shanjiezhou } 1123*e0dfd4d5Shanjiezhou 1124*e0dfd4d5Shanjiezhou #[inline] 1125*e0dfd4d5Shanjiezhou pub fn pop_first(&mut self) -> Option<(K, V)> { 1126*e0dfd4d5Shanjiezhou let first = self.first_child(); 1127*e0dfd4d5Shanjiezhou if first.is_null() { 1128*e0dfd4d5Shanjiezhou return None; 1129*e0dfd4d5Shanjiezhou } 1130*e0dfd4d5Shanjiezhou unsafe { Some(self.delete(first)) } 1131*e0dfd4d5Shanjiezhou } 1132*e0dfd4d5Shanjiezhou 1133*e0dfd4d5Shanjiezhou #[inline] 1134*e0dfd4d5Shanjiezhou pub fn pop_last(&mut self) -> Option<(K, V)> { 1135*e0dfd4d5Shanjiezhou let last = self.last_child(); 1136*e0dfd4d5Shanjiezhou if last.is_null() { 1137*e0dfd4d5Shanjiezhou return None; 1138*e0dfd4d5Shanjiezhou } 1139*e0dfd4d5Shanjiezhou unsafe { Some(self.delete(last)) } 1140*e0dfd4d5Shanjiezhou } 1141*e0dfd4d5Shanjiezhou 1142*e0dfd4d5Shanjiezhou #[inline] 1143*e0dfd4d5Shanjiezhou pub fn get_first_mut(&mut self) -> Option<(&K, &mut V)> { 1144*e0dfd4d5Shanjiezhou let first = self.first_child(); 1145*e0dfd4d5Shanjiezhou if first.is_null() { 1146*e0dfd4d5Shanjiezhou return None; 1147*e0dfd4d5Shanjiezhou } 1148*e0dfd4d5Shanjiezhou unsafe { Some((&(*first.0).key, &mut (*first.0).value)) } 1149*e0dfd4d5Shanjiezhou } 1150*e0dfd4d5Shanjiezhou 1151*e0dfd4d5Shanjiezhou #[inline] 1152*e0dfd4d5Shanjiezhou pub fn get_last_mut(&mut self) -> Option<(&K, &mut V)> { 1153*e0dfd4d5Shanjiezhou let last = self.last_child(); 1154*e0dfd4d5Shanjiezhou if last.is_null() { 1155*e0dfd4d5Shanjiezhou return None; 1156*e0dfd4d5Shanjiezhou } 1157*e0dfd4d5Shanjiezhou unsafe { Some((&(*last.0).key, &mut (*last.0).value)) } 1158*e0dfd4d5Shanjiezhou } 1159*e0dfd4d5Shanjiezhou 1160*e0dfd4d5Shanjiezhou #[inline] 1161*e0dfd4d5Shanjiezhou pub fn get(&self, k: &K) -> Option<&V> { 1162*e0dfd4d5Shanjiezhou let node = self.find_node(k); 1163*e0dfd4d5Shanjiezhou if node.is_null() { 1164*e0dfd4d5Shanjiezhou return None; 1165*e0dfd4d5Shanjiezhou } 1166*e0dfd4d5Shanjiezhou 1167*e0dfd4d5Shanjiezhou unsafe { Some(&(*node.0).value) } 1168*e0dfd4d5Shanjiezhou } 1169*e0dfd4d5Shanjiezhou 1170*e0dfd4d5Shanjiezhou #[inline] 1171*e0dfd4d5Shanjiezhou pub fn get_mut(&mut self, k: &K) -> Option<&mut V> { 1172*e0dfd4d5Shanjiezhou let node = self.find_node(k); 1173*e0dfd4d5Shanjiezhou if node.is_null() { 1174*e0dfd4d5Shanjiezhou return None; 1175*e0dfd4d5Shanjiezhou } 1176*e0dfd4d5Shanjiezhou 1177*e0dfd4d5Shanjiezhou unsafe { Some(&mut (*node.0).value) } 1178*e0dfd4d5Shanjiezhou } 1179*e0dfd4d5Shanjiezhou 1180*e0dfd4d5Shanjiezhou #[inline] 1181*e0dfd4d5Shanjiezhou pub fn contains_key(&self, k: &K) -> bool { 1182*e0dfd4d5Shanjiezhou let node = self.find_node(k); 1183*e0dfd4d5Shanjiezhou if node.is_null() { 1184*e0dfd4d5Shanjiezhou return false; 1185*e0dfd4d5Shanjiezhou } 1186*e0dfd4d5Shanjiezhou true 1187*e0dfd4d5Shanjiezhou } 1188*e0dfd4d5Shanjiezhou 1189*e0dfd4d5Shanjiezhou #[inline] 1190*e0dfd4d5Shanjiezhou fn clear_recurse(&mut self, current: NodePtr<K, V>) { 1191*e0dfd4d5Shanjiezhou if !current.is_null() { 1192*e0dfd4d5Shanjiezhou unsafe { 1193*e0dfd4d5Shanjiezhou self.clear_recurse(current.left()); 1194*e0dfd4d5Shanjiezhou self.clear_recurse(current.right()); 1195*e0dfd4d5Shanjiezhou drop(Box::from_raw(current.0)); 1196*e0dfd4d5Shanjiezhou } 1197*e0dfd4d5Shanjiezhou } 1198*e0dfd4d5Shanjiezhou } 1199*e0dfd4d5Shanjiezhou 1200*e0dfd4d5Shanjiezhou #[inline] 1201*e0dfd4d5Shanjiezhou pub fn clear(&mut self) { 1202*e0dfd4d5Shanjiezhou let root = self.root; 1203*e0dfd4d5Shanjiezhou self.root = NodePtr::null(); 1204*e0dfd4d5Shanjiezhou self.clear_recurse(root); 1205*e0dfd4d5Shanjiezhou } 1206*e0dfd4d5Shanjiezhou 1207*e0dfd4d5Shanjiezhou /// Empties the `RBTree` without freeing objects in it. 1208*e0dfd4d5Shanjiezhou #[inline] 1209*e0dfd4d5Shanjiezhou fn fast_clear(&mut self) { 1210*e0dfd4d5Shanjiezhou self.root = NodePtr::null(); 1211*e0dfd4d5Shanjiezhou } 1212*e0dfd4d5Shanjiezhou 1213*e0dfd4d5Shanjiezhou #[inline] 1214*e0dfd4d5Shanjiezhou pub fn remove(&mut self, k: &K) -> Option<V> { 1215*e0dfd4d5Shanjiezhou let node = self.find_node(k); 1216*e0dfd4d5Shanjiezhou if node.is_null() { 1217*e0dfd4d5Shanjiezhou return None; 1218*e0dfd4d5Shanjiezhou } 1219*e0dfd4d5Shanjiezhou unsafe { Some(self.delete(node).1) } 1220*e0dfd4d5Shanjiezhou } 1221*e0dfd4d5Shanjiezhou 1222*e0dfd4d5Shanjiezhou #[inline] 1223*e0dfd4d5Shanjiezhou unsafe fn delete_fixup(&mut self, mut node: NodePtr<K, V>, mut parent: NodePtr<K, V>) { 1224*e0dfd4d5Shanjiezhou let mut other; 1225*e0dfd4d5Shanjiezhou while node != self.root && node.is_black_color() { 1226*e0dfd4d5Shanjiezhou if parent.left() == node { 1227*e0dfd4d5Shanjiezhou other = parent.right(); 1228*e0dfd4d5Shanjiezhou //x的兄弟w是红色的 1229*e0dfd4d5Shanjiezhou if other.is_red_color() { 1230*e0dfd4d5Shanjiezhou other.set_black_color(); 1231*e0dfd4d5Shanjiezhou parent.set_red_color(); 1232*e0dfd4d5Shanjiezhou self.left_rotate(parent); 1233*e0dfd4d5Shanjiezhou other = parent.right(); 1234*e0dfd4d5Shanjiezhou } 1235*e0dfd4d5Shanjiezhou 1236*e0dfd4d5Shanjiezhou //x的兄弟w是黑色,且w的俩个孩子也都是黑色的 1237*e0dfd4d5Shanjiezhou if other.left().is_black_color() && other.right().is_black_color() { 1238*e0dfd4d5Shanjiezhou other.set_red_color(); 1239*e0dfd4d5Shanjiezhou node = parent; 1240*e0dfd4d5Shanjiezhou parent = node.parent(); 1241*e0dfd4d5Shanjiezhou } else { 1242*e0dfd4d5Shanjiezhou //x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。 1243*e0dfd4d5Shanjiezhou if other.right().is_black_color() { 1244*e0dfd4d5Shanjiezhou other.left().set_black_color(); 1245*e0dfd4d5Shanjiezhou other.set_red_color(); 1246*e0dfd4d5Shanjiezhou self.right_rotate(other); 1247*e0dfd4d5Shanjiezhou other = parent.right(); 1248*e0dfd4d5Shanjiezhou } 1249*e0dfd4d5Shanjiezhou //x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。 1250*e0dfd4d5Shanjiezhou other.set_color(parent.get_color()); 1251*e0dfd4d5Shanjiezhou parent.set_black_color(); 1252*e0dfd4d5Shanjiezhou other.right().set_black_color(); 1253*e0dfd4d5Shanjiezhou self.left_rotate(parent); 1254*e0dfd4d5Shanjiezhou node = self.root; 1255*e0dfd4d5Shanjiezhou break; 1256*e0dfd4d5Shanjiezhou } 1257*e0dfd4d5Shanjiezhou } else { 1258*e0dfd4d5Shanjiezhou other = parent.left(); 1259*e0dfd4d5Shanjiezhou //x的兄弟w是红色的 1260*e0dfd4d5Shanjiezhou if other.is_red_color() { 1261*e0dfd4d5Shanjiezhou other.set_black_color(); 1262*e0dfd4d5Shanjiezhou parent.set_red_color(); 1263*e0dfd4d5Shanjiezhou self.right_rotate(parent); 1264*e0dfd4d5Shanjiezhou other = parent.left(); 1265*e0dfd4d5Shanjiezhou } 1266*e0dfd4d5Shanjiezhou 1267*e0dfd4d5Shanjiezhou //x的兄弟w是黑色,且w的俩个孩子也都是黑色的 1268*e0dfd4d5Shanjiezhou if other.left().is_black_color() && other.right().is_black_color() { 1269*e0dfd4d5Shanjiezhou other.set_red_color(); 1270*e0dfd4d5Shanjiezhou node = parent; 1271*e0dfd4d5Shanjiezhou parent = node.parent(); 1272*e0dfd4d5Shanjiezhou } else { 1273*e0dfd4d5Shanjiezhou //x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。 1274*e0dfd4d5Shanjiezhou if other.left().is_black_color() { 1275*e0dfd4d5Shanjiezhou other.right().set_black_color(); 1276*e0dfd4d5Shanjiezhou other.set_red_color(); 1277*e0dfd4d5Shanjiezhou self.left_rotate(other); 1278*e0dfd4d5Shanjiezhou other = parent.left(); 1279*e0dfd4d5Shanjiezhou } 1280*e0dfd4d5Shanjiezhou //x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。 1281*e0dfd4d5Shanjiezhou other.set_color(parent.get_color()); 1282*e0dfd4d5Shanjiezhou parent.set_black_color(); 1283*e0dfd4d5Shanjiezhou other.left().set_black_color(); 1284*e0dfd4d5Shanjiezhou self.right_rotate(parent); 1285*e0dfd4d5Shanjiezhou node = self.root; 1286*e0dfd4d5Shanjiezhou break; 1287*e0dfd4d5Shanjiezhou } 1288*e0dfd4d5Shanjiezhou } 1289*e0dfd4d5Shanjiezhou } 1290*e0dfd4d5Shanjiezhou 1291*e0dfd4d5Shanjiezhou node.set_black_color(); 1292*e0dfd4d5Shanjiezhou } 1293*e0dfd4d5Shanjiezhou 1294*e0dfd4d5Shanjiezhou #[inline] 1295*e0dfd4d5Shanjiezhou unsafe fn delete(&mut self, node: NodePtr<K, V>) -> (K, V) { 1296*e0dfd4d5Shanjiezhou let mut child; 1297*e0dfd4d5Shanjiezhou let mut parent; 1298*e0dfd4d5Shanjiezhou let color; 1299*e0dfd4d5Shanjiezhou 1300*e0dfd4d5Shanjiezhou self.len -= 1; 1301*e0dfd4d5Shanjiezhou // 被删除节点的"左右孩子都不为空"的情况。 1302*e0dfd4d5Shanjiezhou if !node.left().is_null() && !node.right().is_null() { 1303*e0dfd4d5Shanjiezhou // 被删节点的后继节点。(称为"取代节点") 1304*e0dfd4d5Shanjiezhou // 用它来取代"被删节点"的位置,然后再将"被删节点"去掉。 1305*e0dfd4d5Shanjiezhou let mut replace = node.right().min_node(); 1306*e0dfd4d5Shanjiezhou if node == self.root { 1307*e0dfd4d5Shanjiezhou self.root = replace; 1308*e0dfd4d5Shanjiezhou } else { 1309*e0dfd4d5Shanjiezhou if node.parent().left() == node { 1310*e0dfd4d5Shanjiezhou node.parent().set_left(replace); 1311*e0dfd4d5Shanjiezhou } else { 1312*e0dfd4d5Shanjiezhou node.parent().set_right(replace); 1313*e0dfd4d5Shanjiezhou } 1314*e0dfd4d5Shanjiezhou } 1315*e0dfd4d5Shanjiezhou 1316*e0dfd4d5Shanjiezhou // child是"取代节点"的右孩子,也是需要"调整的节点"。 1317*e0dfd4d5Shanjiezhou // "取代节点"肯定不存在左孩子!因为它是一个后继节点。 1318*e0dfd4d5Shanjiezhou child = replace.right(); 1319*e0dfd4d5Shanjiezhou parent = replace.parent(); 1320*e0dfd4d5Shanjiezhou color = replace.get_color(); 1321*e0dfd4d5Shanjiezhou if parent == node { 1322*e0dfd4d5Shanjiezhou parent = replace; 1323*e0dfd4d5Shanjiezhou } else { 1324*e0dfd4d5Shanjiezhou if !child.is_null() { 1325*e0dfd4d5Shanjiezhou child.set_parent(parent); 1326*e0dfd4d5Shanjiezhou } 1327*e0dfd4d5Shanjiezhou parent.set_left(child); 1328*e0dfd4d5Shanjiezhou replace.set_right(node.right()); 1329*e0dfd4d5Shanjiezhou node.right().set_parent(replace); 1330*e0dfd4d5Shanjiezhou } 1331*e0dfd4d5Shanjiezhou 1332*e0dfd4d5Shanjiezhou replace.set_parent(node.parent()); 1333*e0dfd4d5Shanjiezhou replace.set_color(node.get_color()); 1334*e0dfd4d5Shanjiezhou replace.set_left(node.left()); 1335*e0dfd4d5Shanjiezhou node.left().set_parent(replace); 1336*e0dfd4d5Shanjiezhou 1337*e0dfd4d5Shanjiezhou if color == Color::Black { 1338*e0dfd4d5Shanjiezhou self.delete_fixup(child, parent); 1339*e0dfd4d5Shanjiezhou } 1340*e0dfd4d5Shanjiezhou 1341*e0dfd4d5Shanjiezhou let obj = Box::from_raw(node.0); 1342*e0dfd4d5Shanjiezhou return obj.pair(); 1343*e0dfd4d5Shanjiezhou } 1344*e0dfd4d5Shanjiezhou 1345*e0dfd4d5Shanjiezhou if !node.left().is_null() { 1346*e0dfd4d5Shanjiezhou child = node.left(); 1347*e0dfd4d5Shanjiezhou } else { 1348*e0dfd4d5Shanjiezhou child = node.right(); 1349*e0dfd4d5Shanjiezhou } 1350*e0dfd4d5Shanjiezhou 1351*e0dfd4d5Shanjiezhou parent = node.parent(); 1352*e0dfd4d5Shanjiezhou color = node.get_color(); 1353*e0dfd4d5Shanjiezhou if !child.is_null() { 1354*e0dfd4d5Shanjiezhou child.set_parent(parent); 1355*e0dfd4d5Shanjiezhou } 1356*e0dfd4d5Shanjiezhou 1357*e0dfd4d5Shanjiezhou if self.root == node { 1358*e0dfd4d5Shanjiezhou self.root = child 1359*e0dfd4d5Shanjiezhou } else { 1360*e0dfd4d5Shanjiezhou if parent.left() == node { 1361*e0dfd4d5Shanjiezhou parent.set_left(child); 1362*e0dfd4d5Shanjiezhou } else { 1363*e0dfd4d5Shanjiezhou parent.set_right(child); 1364*e0dfd4d5Shanjiezhou } 1365*e0dfd4d5Shanjiezhou } 1366*e0dfd4d5Shanjiezhou 1367*e0dfd4d5Shanjiezhou if color == Color::Black { 1368*e0dfd4d5Shanjiezhou self.delete_fixup(child, parent); 1369*e0dfd4d5Shanjiezhou } 1370*e0dfd4d5Shanjiezhou 1371*e0dfd4d5Shanjiezhou let obj = Box::from_raw(node.0); 1372*e0dfd4d5Shanjiezhou return obj.pair(); 1373*e0dfd4d5Shanjiezhou } 1374*e0dfd4d5Shanjiezhou 1375*e0dfd4d5Shanjiezhou /// Return the keys iter 1376*e0dfd4d5Shanjiezhou #[inline] 1377*e0dfd4d5Shanjiezhou pub fn keys(&self) -> Keys<K, V> { 1378*e0dfd4d5Shanjiezhou Keys { inner: self.iter() } 1379*e0dfd4d5Shanjiezhou } 1380*e0dfd4d5Shanjiezhou 1381*e0dfd4d5Shanjiezhou /// Return the value iter 1382*e0dfd4d5Shanjiezhou #[inline] 1383*e0dfd4d5Shanjiezhou pub fn values(&self) -> Values<K, V> { 1384*e0dfd4d5Shanjiezhou Values { inner: self.iter() } 1385*e0dfd4d5Shanjiezhou } 1386*e0dfd4d5Shanjiezhou 1387*e0dfd4d5Shanjiezhou /// Return the value iter mut 1388*e0dfd4d5Shanjiezhou #[inline] 1389*e0dfd4d5Shanjiezhou pub fn values_mut(&mut self) -> ValuesMut<K, V> { 1390*e0dfd4d5Shanjiezhou ValuesMut { inner: self.iter_mut() } 1391*e0dfd4d5Shanjiezhou } 1392*e0dfd4d5Shanjiezhou 1393*e0dfd4d5Shanjiezhou /// Return the key and value iter 1394*e0dfd4d5Shanjiezhou #[inline] 1395*e0dfd4d5Shanjiezhou pub fn iter(&self) -> Iter<K, V> { 1396*e0dfd4d5Shanjiezhou Iter { 1397*e0dfd4d5Shanjiezhou head: self.first_child(), 1398*e0dfd4d5Shanjiezhou tail: self.last_child(), 1399*e0dfd4d5Shanjiezhou len: self.len, 1400*e0dfd4d5Shanjiezhou _marker: marker::PhantomData, 1401*e0dfd4d5Shanjiezhou } 1402*e0dfd4d5Shanjiezhou } 1403*e0dfd4d5Shanjiezhou 1404*e0dfd4d5Shanjiezhou /// Return the key and mut value iter 1405*e0dfd4d5Shanjiezhou #[inline] 1406*e0dfd4d5Shanjiezhou pub fn iter_mut(&mut self) -> IterMut<K, V> { 1407*e0dfd4d5Shanjiezhou IterMut { 1408*e0dfd4d5Shanjiezhou head: self.first_child(), 1409*e0dfd4d5Shanjiezhou tail: self.last_child(), 1410*e0dfd4d5Shanjiezhou len: self.len, 1411*e0dfd4d5Shanjiezhou _marker: marker::PhantomData, 1412*e0dfd4d5Shanjiezhou } 1413*e0dfd4d5Shanjiezhou } 1414*e0dfd4d5Shanjiezhou } 1415*e0dfd4d5Shanjiezhou 1416*e0dfd4d5Shanjiezhou mod tests { 1417*e0dfd4d5Shanjiezhou 1418*e0dfd4d5Shanjiezhou #[test] 1419*e0dfd4d5Shanjiezhou fn test_insert() { 1420*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1421*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 0); 1422*e0dfd4d5Shanjiezhou m.insert(1, 2); 1423*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 1); 1424*e0dfd4d5Shanjiezhou m.insert(2, 4); 1425*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 2); 1426*e0dfd4d5Shanjiezhou m.insert(2, 6); 1427*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 3); 1428*e0dfd4d5Shanjiezhou assert_eq!(*m.get(&1).unwrap(), 2); 1429*e0dfd4d5Shanjiezhou assert_eq!(*m.get(&2).unwrap(), 4); 1430*e0dfd4d5Shanjiezhou assert_eq!(*m.get(&2).unwrap(), 4); 1431*e0dfd4d5Shanjiezhou } 1432*e0dfd4d5Shanjiezhou 1433*e0dfd4d5Shanjiezhou #[test] 1434*e0dfd4d5Shanjiezhou fn test_replace() { 1435*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1436*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 0); 1437*e0dfd4d5Shanjiezhou m.insert(2, 4); 1438*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 1); 1439*e0dfd4d5Shanjiezhou assert_eq!(m.replace_or_insert(2, 6).unwrap(), 4); 1440*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 1); 1441*e0dfd4d5Shanjiezhou assert_eq!(*m.get(&2).unwrap(), 6); 1442*e0dfd4d5Shanjiezhou } 1443*e0dfd4d5Shanjiezhou 1444*e0dfd4d5Shanjiezhou 1445*e0dfd4d5Shanjiezhou #[test] 1446*e0dfd4d5Shanjiezhou fn test_clone() { 1447*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1448*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 0); 1449*e0dfd4d5Shanjiezhou m.insert(1, 2); 1450*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 1); 1451*e0dfd4d5Shanjiezhou m.insert(2, 4); 1452*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 2); 1453*e0dfd4d5Shanjiezhou let m2 = m.clone(); 1454*e0dfd4d5Shanjiezhou m.clear(); 1455*e0dfd4d5Shanjiezhou assert_eq!(*m2.get(&1).unwrap(), 2); 1456*e0dfd4d5Shanjiezhou assert_eq!(*m2.get(&2).unwrap(), 4); 1457*e0dfd4d5Shanjiezhou assert_eq!(m2.len(), 2); 1458*e0dfd4d5Shanjiezhou } 1459*e0dfd4d5Shanjiezhou 1460*e0dfd4d5Shanjiezhou #[test] 1461*e0dfd4d5Shanjiezhou fn test_empty_remove() { 1462*e0dfd4d5Shanjiezhou let mut m: RBTree<isize, bool> = RBTree::new(); 1463*e0dfd4d5Shanjiezhou assert_eq!(m.remove(&0), None); 1464*e0dfd4d5Shanjiezhou } 1465*e0dfd4d5Shanjiezhou 1466*e0dfd4d5Shanjiezhou #[test] 1467*e0dfd4d5Shanjiezhou fn test_empty_iter() { 1468*e0dfd4d5Shanjiezhou let mut m: RBTree<isize, bool> = RBTree::new(); 1469*e0dfd4d5Shanjiezhou assert_eq!(m.iter().next(), None); 1470*e0dfd4d5Shanjiezhou assert_eq!(m.iter_mut().next(), None); 1471*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 0); 1472*e0dfd4d5Shanjiezhou assert!(m.is_empty()); 1473*e0dfd4d5Shanjiezhou assert_eq!(m.into_iter().next(), None); 1474*e0dfd4d5Shanjiezhou } 1475*e0dfd4d5Shanjiezhou 1476*e0dfd4d5Shanjiezhou #[test] 1477*e0dfd4d5Shanjiezhou fn test_lots_of_insertions() { 1478*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1479*e0dfd4d5Shanjiezhou 1480*e0dfd4d5Shanjiezhou // Try this a few times to make sure we never screw up the hashmap's 1481*e0dfd4d5Shanjiezhou // internal state. 1482*e0dfd4d5Shanjiezhou for _ in 0..10 { 1483*e0dfd4d5Shanjiezhou assert!(m.is_empty()); 1484*e0dfd4d5Shanjiezhou 1485*e0dfd4d5Shanjiezhou for i in 1..101 { 1486*e0dfd4d5Shanjiezhou m.insert(i, i); 1487*e0dfd4d5Shanjiezhou 1488*e0dfd4d5Shanjiezhou for j in 1..i + 1 { 1489*e0dfd4d5Shanjiezhou let r = m.get(&j); 1490*e0dfd4d5Shanjiezhou assert_eq!(r, Some(&j)); 1491*e0dfd4d5Shanjiezhou } 1492*e0dfd4d5Shanjiezhou 1493*e0dfd4d5Shanjiezhou for j in i + 1..101 { 1494*e0dfd4d5Shanjiezhou let r = m.get(&j); 1495*e0dfd4d5Shanjiezhou assert_eq!(r, None); 1496*e0dfd4d5Shanjiezhou } 1497*e0dfd4d5Shanjiezhou } 1498*e0dfd4d5Shanjiezhou 1499*e0dfd4d5Shanjiezhou for i in 101..201 { 1500*e0dfd4d5Shanjiezhou assert!(!m.contains_key(&i)); 1501*e0dfd4d5Shanjiezhou } 1502*e0dfd4d5Shanjiezhou 1503*e0dfd4d5Shanjiezhou // remove forwards 1504*e0dfd4d5Shanjiezhou for i in 1..101 { 1505*e0dfd4d5Shanjiezhou assert!(m.remove(&i).is_some()); 1506*e0dfd4d5Shanjiezhou 1507*e0dfd4d5Shanjiezhou for j in 1..i + 1 { 1508*e0dfd4d5Shanjiezhou assert!(!m.contains_key(&j)); 1509*e0dfd4d5Shanjiezhou } 1510*e0dfd4d5Shanjiezhou 1511*e0dfd4d5Shanjiezhou for j in i + 1..101 { 1512*e0dfd4d5Shanjiezhou assert!(m.contains_key(&j)); 1513*e0dfd4d5Shanjiezhou } 1514*e0dfd4d5Shanjiezhou } 1515*e0dfd4d5Shanjiezhou 1516*e0dfd4d5Shanjiezhou for i in 1..101 { 1517*e0dfd4d5Shanjiezhou assert!(!m.contains_key(&i)); 1518*e0dfd4d5Shanjiezhou } 1519*e0dfd4d5Shanjiezhou 1520*e0dfd4d5Shanjiezhou for i in 1..101 { 1521*e0dfd4d5Shanjiezhou m.insert(i, i); 1522*e0dfd4d5Shanjiezhou } 1523*e0dfd4d5Shanjiezhou 1524*e0dfd4d5Shanjiezhou // remove backwards 1525*e0dfd4d5Shanjiezhou for i in (1..101).rev() { 1526*e0dfd4d5Shanjiezhou assert!(m.remove(&i).is_some()); 1527*e0dfd4d5Shanjiezhou 1528*e0dfd4d5Shanjiezhou for j in i..101 { 1529*e0dfd4d5Shanjiezhou assert!(!m.contains_key(&j)); 1530*e0dfd4d5Shanjiezhou } 1531*e0dfd4d5Shanjiezhou 1532*e0dfd4d5Shanjiezhou for j in 1..i { 1533*e0dfd4d5Shanjiezhou assert!(m.contains_key(&j)); 1534*e0dfd4d5Shanjiezhou } 1535*e0dfd4d5Shanjiezhou } 1536*e0dfd4d5Shanjiezhou } 1537*e0dfd4d5Shanjiezhou } 1538*e0dfd4d5Shanjiezhou 1539*e0dfd4d5Shanjiezhou #[test] 1540*e0dfd4d5Shanjiezhou fn test_find_mut() { 1541*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1542*e0dfd4d5Shanjiezhou m.insert(1, 12); 1543*e0dfd4d5Shanjiezhou m.insert(2, 8); 1544*e0dfd4d5Shanjiezhou m.insert(5, 14); 1545*e0dfd4d5Shanjiezhou let new = 100; 1546*e0dfd4d5Shanjiezhou match m.get_mut(&5) { 1547*e0dfd4d5Shanjiezhou None => panic!(), 1548*e0dfd4d5Shanjiezhou Some(x) => *x = new, 1549*e0dfd4d5Shanjiezhou } 1550*e0dfd4d5Shanjiezhou assert_eq!(m.get(&5), Some(&new)); 1551*e0dfd4d5Shanjiezhou } 1552*e0dfd4d5Shanjiezhou 1553*e0dfd4d5Shanjiezhou #[test] 1554*e0dfd4d5Shanjiezhou fn test_remove() { 1555*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1556*e0dfd4d5Shanjiezhou m.insert(1, 2); 1557*e0dfd4d5Shanjiezhou assert_eq!(*m.get(&1).unwrap(), 2); 1558*e0dfd4d5Shanjiezhou m.insert(5, 3); 1559*e0dfd4d5Shanjiezhou assert_eq!(*m.get(&5).unwrap(), 3); 1560*e0dfd4d5Shanjiezhou m.insert(9, 4); 1561*e0dfd4d5Shanjiezhou assert_eq!(*m.get(&1).unwrap(), 2); 1562*e0dfd4d5Shanjiezhou assert_eq!(*m.get(&5).unwrap(), 3); 1563*e0dfd4d5Shanjiezhou assert_eq!(*m.get(&9).unwrap(), 4); 1564*e0dfd4d5Shanjiezhou assert_eq!(m.remove(&1).unwrap(), 2); 1565*e0dfd4d5Shanjiezhou assert_eq!(m.remove(&5).unwrap(), 3); 1566*e0dfd4d5Shanjiezhou assert_eq!(m.remove(&9).unwrap(), 4); 1567*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 0); 1568*e0dfd4d5Shanjiezhou } 1569*e0dfd4d5Shanjiezhou 1570*e0dfd4d5Shanjiezhou #[test] 1571*e0dfd4d5Shanjiezhou fn test_is_empty() { 1572*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1573*e0dfd4d5Shanjiezhou m.insert(1, 2); 1574*e0dfd4d5Shanjiezhou assert!(!m.is_empty()); 1575*e0dfd4d5Shanjiezhou assert!(m.remove(&1).is_some()); 1576*e0dfd4d5Shanjiezhou assert!(m.is_empty()); 1577*e0dfd4d5Shanjiezhou } 1578*e0dfd4d5Shanjiezhou 1579*e0dfd4d5Shanjiezhou #[test] 1580*e0dfd4d5Shanjiezhou fn test_pop() { 1581*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1582*e0dfd4d5Shanjiezhou m.insert(2, 4); 1583*e0dfd4d5Shanjiezhou m.insert(1, 2); 1584*e0dfd4d5Shanjiezhou m.insert(3, 6); 1585*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 3); 1586*e0dfd4d5Shanjiezhou assert_eq!(m.pop_first(), Some((1, 2))); 1587*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 2); 1588*e0dfd4d5Shanjiezhou assert_eq!(m.pop_last(), Some((3, 6))); 1589*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 1); 1590*e0dfd4d5Shanjiezhou assert_eq!(m.get_first(), Some((&2, &4))); 1591*e0dfd4d5Shanjiezhou assert_eq!(m.get_last(), Some((&2, &4))); 1592*e0dfd4d5Shanjiezhou } 1593*e0dfd4d5Shanjiezhou 1594*e0dfd4d5Shanjiezhou #[test] 1595*e0dfd4d5Shanjiezhou fn test_iterate() { 1596*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1597*e0dfd4d5Shanjiezhou for i in 0..32 { 1598*e0dfd4d5Shanjiezhou m.insert(i, i * 2); 1599*e0dfd4d5Shanjiezhou } 1600*e0dfd4d5Shanjiezhou assert_eq!(m.len(), 32); 1601*e0dfd4d5Shanjiezhou 1602*e0dfd4d5Shanjiezhou let mut observed: u32 = 0; 1603*e0dfd4d5Shanjiezhou 1604*e0dfd4d5Shanjiezhou for (k, v) in m.iter() { 1605*e0dfd4d5Shanjiezhou assert_eq!(*v, *k * 2); 1606*e0dfd4d5Shanjiezhou observed |= 1 << *k; 1607*e0dfd4d5Shanjiezhou } 1608*e0dfd4d5Shanjiezhou assert_eq!(observed, 0xFFFF_FFFF); 1609*e0dfd4d5Shanjiezhou } 1610*e0dfd4d5Shanjiezhou 1611*e0dfd4d5Shanjiezhou #[test] 1612*e0dfd4d5Shanjiezhou fn test_keys() { 1613*e0dfd4d5Shanjiezhou let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; 1614*e0dfd4d5Shanjiezhou let map: RBTree<_, _> = vec.into_iter().collect(); 1615*e0dfd4d5Shanjiezhou let keys: Vec<_> = map.keys().cloned().collect(); 1616*e0dfd4d5Shanjiezhou assert_eq!(keys.len(), 3); 1617*e0dfd4d5Shanjiezhou assert!(keys.contains(&1)); 1618*e0dfd4d5Shanjiezhou assert!(keys.contains(&2)); 1619*e0dfd4d5Shanjiezhou assert!(keys.contains(&3)); 1620*e0dfd4d5Shanjiezhou } 1621*e0dfd4d5Shanjiezhou 1622*e0dfd4d5Shanjiezhou #[test] 1623*e0dfd4d5Shanjiezhou fn test_values() { 1624*e0dfd4d5Shanjiezhou let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; 1625*e0dfd4d5Shanjiezhou let map: RBTree<_, _> = vec.into_iter().collect(); 1626*e0dfd4d5Shanjiezhou let values: Vec<_> = map.values().cloned().collect(); 1627*e0dfd4d5Shanjiezhou assert_eq!(values.len(), 3); 1628*e0dfd4d5Shanjiezhou assert!(values.contains(&'a')); 1629*e0dfd4d5Shanjiezhou assert!(values.contains(&'b')); 1630*e0dfd4d5Shanjiezhou assert!(values.contains(&'c')); 1631*e0dfd4d5Shanjiezhou } 1632*e0dfd4d5Shanjiezhou 1633*e0dfd4d5Shanjiezhou #[test] 1634*e0dfd4d5Shanjiezhou fn test_values_mut() { 1635*e0dfd4d5Shanjiezhou let vec = vec![(1, 1), (2, 2), (3, 3)]; 1636*e0dfd4d5Shanjiezhou let mut map: RBTree<_, _> = vec.into_iter().collect(); 1637*e0dfd4d5Shanjiezhou for value in map.values_mut() { 1638*e0dfd4d5Shanjiezhou *value = (*value) * 2 1639*e0dfd4d5Shanjiezhou } 1640*e0dfd4d5Shanjiezhou let values: Vec<_> = map.values().cloned().collect(); 1641*e0dfd4d5Shanjiezhou assert_eq!(values.len(), 3); 1642*e0dfd4d5Shanjiezhou assert!(values.contains(&2)); 1643*e0dfd4d5Shanjiezhou assert!(values.contains(&4)); 1644*e0dfd4d5Shanjiezhou assert!(values.contains(&6)); 1645*e0dfd4d5Shanjiezhou } 1646*e0dfd4d5Shanjiezhou 1647*e0dfd4d5Shanjiezhou #[test] 1648*e0dfd4d5Shanjiezhou fn test_find() { 1649*e0dfd4d5Shanjiezhou let mut m = RBTree::new(); 1650*e0dfd4d5Shanjiezhou assert!(m.get(&1).is_none()); 1651*e0dfd4d5Shanjiezhou m.insert(1, 2); 1652*e0dfd4d5Shanjiezhou match m.get(&1) { 1653*e0dfd4d5Shanjiezhou None => panic!(), 1654*e0dfd4d5Shanjiezhou Some(v) => assert_eq!(*v, 2), 1655*e0dfd4d5Shanjiezhou } 1656*e0dfd4d5Shanjiezhou } 1657*e0dfd4d5Shanjiezhou 1658*e0dfd4d5Shanjiezhou #[test] 1659*e0dfd4d5Shanjiezhou fn test_eq() { 1660*e0dfd4d5Shanjiezhou let mut m1 = RBTree::new(); 1661*e0dfd4d5Shanjiezhou m1.insert(1, 2); 1662*e0dfd4d5Shanjiezhou m1.insert(2, 3); 1663*e0dfd4d5Shanjiezhou m1.insert(3, 4); 1664*e0dfd4d5Shanjiezhou 1665*e0dfd4d5Shanjiezhou let mut m2 = RBTree::new(); 1666*e0dfd4d5Shanjiezhou m2.insert(1, 2); 1667*e0dfd4d5Shanjiezhou m2.insert(2, 3); 1668*e0dfd4d5Shanjiezhou 1669*e0dfd4d5Shanjiezhou assert!(m1 != m2); 1670*e0dfd4d5Shanjiezhou 1671*e0dfd4d5Shanjiezhou m2.insert(3, 4); 1672*e0dfd4d5Shanjiezhou 1673*e0dfd4d5Shanjiezhou assert_eq!(m1, m2); 1674*e0dfd4d5Shanjiezhou } 1675*e0dfd4d5Shanjiezhou 1676*e0dfd4d5Shanjiezhou #[test] 1677*e0dfd4d5Shanjiezhou fn test_show() { 1678*e0dfd4d5Shanjiezhou let mut map = RBTree::new(); 1679*e0dfd4d5Shanjiezhou let empty: RBTree<i32, i32> = RBTree::new(); 1680*e0dfd4d5Shanjiezhou 1681*e0dfd4d5Shanjiezhou map.insert(1, 2); 1682*e0dfd4d5Shanjiezhou map.insert(3, 4); 1683*e0dfd4d5Shanjiezhou 1684*e0dfd4d5Shanjiezhou let map_str = format!("{:?}", map); 1685*e0dfd4d5Shanjiezhou 1686*e0dfd4d5Shanjiezhou assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}"); 1687*e0dfd4d5Shanjiezhou assert_eq!(format!("{:?}", empty), "{}"); 1688*e0dfd4d5Shanjiezhou } 1689*e0dfd4d5Shanjiezhou 1690*e0dfd4d5Shanjiezhou #[test] 1691*e0dfd4d5Shanjiezhou fn test_from_iter() { 1692*e0dfd4d5Shanjiezhou let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; 1693*e0dfd4d5Shanjiezhou 1694*e0dfd4d5Shanjiezhou let map: RBTree<_, _> = xs.iter().cloned().collect(); 1695*e0dfd4d5Shanjiezhou 1696*e0dfd4d5Shanjiezhou for &(k, v) in &xs { 1697*e0dfd4d5Shanjiezhou assert_eq!(map.get(&k), Some(&v)); 1698*e0dfd4d5Shanjiezhou } 1699*e0dfd4d5Shanjiezhou } 1700*e0dfd4d5Shanjiezhou 1701*e0dfd4d5Shanjiezhou #[test] 1702*e0dfd4d5Shanjiezhou fn test_size_hint() { 1703*e0dfd4d5Shanjiezhou let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; 1704*e0dfd4d5Shanjiezhou 1705*e0dfd4d5Shanjiezhou let map: RBTree<_, _> = xs.iter().cloned().collect(); 1706*e0dfd4d5Shanjiezhou 1707*e0dfd4d5Shanjiezhou let mut iter = map.iter(); 1708*e0dfd4d5Shanjiezhou 1709*e0dfd4d5Shanjiezhou for _ in iter.by_ref().take(3) {} 1710*e0dfd4d5Shanjiezhou 1711*e0dfd4d5Shanjiezhou assert_eq!(iter.size_hint(), (3, Some(3))); 1712*e0dfd4d5Shanjiezhou } 1713*e0dfd4d5Shanjiezhou 1714*e0dfd4d5Shanjiezhou #[test] 1715*e0dfd4d5Shanjiezhou fn test_iter_len() { 1716*e0dfd4d5Shanjiezhou let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; 1717*e0dfd4d5Shanjiezhou 1718*e0dfd4d5Shanjiezhou let map: RBTree<_, _> = xs.iter().cloned().collect(); 1719*e0dfd4d5Shanjiezhou 1720*e0dfd4d5Shanjiezhou let mut iter = map.iter(); 1721*e0dfd4d5Shanjiezhou 1722*e0dfd4d5Shanjiezhou for _ in iter.by_ref().take(3) {} 1723*e0dfd4d5Shanjiezhou 1724*e0dfd4d5Shanjiezhou assert_eq!(iter.count(), 3); 1725*e0dfd4d5Shanjiezhou } 1726*e0dfd4d5Shanjiezhou 1727*e0dfd4d5Shanjiezhou #[test] 1728*e0dfd4d5Shanjiezhou fn test_mut_size_hint() { 1729*e0dfd4d5Shanjiezhou let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; 1730*e0dfd4d5Shanjiezhou 1731*e0dfd4d5Shanjiezhou let mut map: RBTree<_, _> = xs.iter().cloned().collect(); 1732*e0dfd4d5Shanjiezhou 1733*e0dfd4d5Shanjiezhou let mut iter = map.iter_mut(); 1734*e0dfd4d5Shanjiezhou 1735*e0dfd4d5Shanjiezhou for _ in iter.by_ref().take(3) {} 1736*e0dfd4d5Shanjiezhou 1737*e0dfd4d5Shanjiezhou assert_eq!(iter.size_hint(), (3, Some(3))); 1738*e0dfd4d5Shanjiezhou } 1739*e0dfd4d5Shanjiezhou 1740*e0dfd4d5Shanjiezhou #[test] 1741*e0dfd4d5Shanjiezhou fn test_iter_mut_len() { 1742*e0dfd4d5Shanjiezhou let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]; 1743*e0dfd4d5Shanjiezhou 1744*e0dfd4d5Shanjiezhou let mut map: RBTree<_, _> = xs.iter().cloned().collect(); 1745*e0dfd4d5Shanjiezhou 1746*e0dfd4d5Shanjiezhou let mut iter = map.iter_mut(); 1747*e0dfd4d5Shanjiezhou 1748*e0dfd4d5Shanjiezhou for _ in iter.by_ref().take(3) {} 1749*e0dfd4d5Shanjiezhou 1750*e0dfd4d5Shanjiezhou assert_eq!(iter.count(), 3); 1751*e0dfd4d5Shanjiezhou } 1752*e0dfd4d5Shanjiezhou 1753*e0dfd4d5Shanjiezhou #[test] 1754*e0dfd4d5Shanjiezhou fn test_index() { 1755*e0dfd4d5Shanjiezhou let mut map = RBTree::new(); 1756*e0dfd4d5Shanjiezhou 1757*e0dfd4d5Shanjiezhou map.insert(1, 2); 1758*e0dfd4d5Shanjiezhou map.insert(2, 1); 1759*e0dfd4d5Shanjiezhou map.insert(3, 4); 1760*e0dfd4d5Shanjiezhou 1761*e0dfd4d5Shanjiezhou assert_eq!(map[&2], 1); 1762*e0dfd4d5Shanjiezhou } 1763*e0dfd4d5Shanjiezhou 1764*e0dfd4d5Shanjiezhou #[test] 1765*e0dfd4d5Shanjiezhou #[should_panic] 1766*e0dfd4d5Shanjiezhou fn test_index_nonexistent() { 1767*e0dfd4d5Shanjiezhou let mut map = RBTree::new(); 1768*e0dfd4d5Shanjiezhou 1769*e0dfd4d5Shanjiezhou map.insert(1, 2); 1770*e0dfd4d5Shanjiezhou map.insert(2, 1); 1771*e0dfd4d5Shanjiezhou map.insert(3, 4); 1772*e0dfd4d5Shanjiezhou 1773*e0dfd4d5Shanjiezhou map[&4]; 1774*e0dfd4d5Shanjiezhou } 1775*e0dfd4d5Shanjiezhou 1776*e0dfd4d5Shanjiezhou #[test] 1777*e0dfd4d5Shanjiezhou fn test_extend_iter() { 1778*e0dfd4d5Shanjiezhou let mut a = RBTree::new(); 1779*e0dfd4d5Shanjiezhou a.insert(1, "one"); 1780*e0dfd4d5Shanjiezhou let mut b = RBTree::new(); 1781*e0dfd4d5Shanjiezhou b.insert(2, "two"); 1782*e0dfd4d5Shanjiezhou b.insert(3, "three"); 1783*e0dfd4d5Shanjiezhou 1784*e0dfd4d5Shanjiezhou a.extend(b.into_iter()); 1785*e0dfd4d5Shanjiezhou 1786*e0dfd4d5Shanjiezhou assert_eq!(a.len(), 3); 1787*e0dfd4d5Shanjiezhou assert_eq!(a[&1], "one"); 1788*e0dfd4d5Shanjiezhou assert_eq!(a[&2], "two"); 1789*e0dfd4d5Shanjiezhou assert_eq!(a[&3], "three"); 1790*e0dfd4d5Shanjiezhou } 1791*e0dfd4d5Shanjiezhou }