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