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