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