1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
mod node; #[cfg(feature = "map")] pub mod rbmap; #[cfg(feature = "set")] pub mod rbtree; #[macro_use] #[cfg(feature = "queue")] pub mod rbqueue; mod helpers; #[cfg(feature = "map")] mod mapper; #[cfg(test)] mod rbtree_tests; #[cfg(test)] mod stress_test; #[cfg(feature = "map")] use mapper::Mapper; use node::Node; /// A map implemented using a red black tree to /// store key-value pairs. #[cfg(feature = "map")] #[allow(clippy::upper_case_acronyms)] #[derive(Clone)] pub struct RBMap<K: PartialOrd, V> { map: RBTree<Mapper<K, V>>, } /// A red black tree that can be used to store /// elements sorted by their PartialOrd provided /// ordering. #[cfg(feature = "set")] #[allow(clippy::upper_case_acronyms)] #[derive(Clone)] pub struct RBTree<T: PartialOrd> { root: Node<T>, contained: usize, } /// A priority queue implemented using a red black /// tree. The ordering supplied must satisfy the assymetry /// and transitivity rules as outlined by the dorumentation /// of std::cmp::PartialOrd. #[cfg(feature = "queue")] #[allow(clippy::upper_case_acronyms)] #[derive(Clone)] pub struct RBQueue<T, P> where P: Fn(&T, &T) -> std::cmp::Ordering, { root: Node<T>, contained: usize, cmp: P, } /// Returns an RBTree containing the items /// given separated by commas. /// # Example: /// ``` /// use rb_tree::{RBTree, new_set}; /// /// let t1 = new_set!('b', 'a', 'd', 'c'); /// let t2 = new_set!('d', 'f', 'e', 'c'); /// /// let mut in_both = t1.intersection(&t2); /// assert_eq!(in_both.next().unwrap(), &'c'); /// assert_eq!(in_both.next().unwrap(), &'d'); /// assert_eq!(in_both.next(), None); /// ``` #[cfg(feature = "set")] #[macro_export] macro_rules! new_set { ( $($v:expr),* ) => {{ let mut t = RBTree::new(); $( t.insert($v); )* t }}; } /// Returns an RBQueue that prioritises on given /// closure and contains the comma-separated /// elements following it. /// # Example: /// use rb_tree::{RBQueue, new_queue}; /// /// let mut q = new_queue!(|l, r| { /// match l - r { /// i32::MIN..=-1_i32 => Greater, /// 0 => Equal, /// 1_i32..=i32::MAX => Less /// } /// }; 1, 2, 3, 4); /// assert_eq!(q.pop().unwrap(), 4); /// assert_eq!(q.pop().unwrap(), 3); /// assert_eq!(q.pop().unwrap(), 2); /// assert_eq!(q.pop().unwrap(), 1); /// assert_eq!(q.pop(), None); /// ``` #[cfg(feature = "queue")] #[macro_export] macro_rules! new_queue { ($comp:expr; $($v:expr),*) => {{ let mut q = RBQueue::new($comp); $(q.insert($v);)* q }}; } /// Allows the creation of a queue using C-like /// comparison values. That is to say, `cmp` /// should return a value less than, equal to, /// or greater than 0 when `l` should be placed /// before, is equal to, or be placed after `r` /// respectively. /// /// `cmp` should be a function that takes two values /// from the queue and returns an integer (i8) /// providing the information as above. /// /// # Example: /// ``` /// # #[macro_use(new_c_queue)] /// # extern crate rb_tree; /// # use rb_tree::RBQueue; /// # fn main() { /// let mut q = new_c_queue!(|l: &i64, r| (r - l)); /// q.insert(1); /// q.insert(2); /// q.insert(3); /// assert_eq!(q.ordered(), [&3, &2, &1]); /// # } /// ``` /// /// # Example: /// ``` /// # #[macro_use(new_c_queue)] /// # extern crate rb_tree; /// # use rb_tree::RBQueue; /// # fn main() { /// let q = new_c_queue!(|l: &i64, r| (r - l); 1, 2, 3); /// assert_eq!(q.ordered(), [&3, &2, &1]); /// # } /// ``` #[cfg(feature = "queue")] #[macro_export] macro_rules! new_c_queue { ($cmp:expr) => { RBQueue::new(move |l, r| { let comp = Box::new($cmp); match comp(l, r) as i8 { -128i8 ..= -1 => std::cmp::Ordering::Less, 0 => std::cmp::Ordering::Equal, 1 ..= 127i8 => std::cmp::Ordering::Greater } }) }; ($cmp:expr; $($v:expr),*) => {{ let mut q = RBQueue::new(move |l, r| { let comp = Box::new($cmp); match comp(l, r) as i8 { -128i8 ..= -1 => std::cmp::Ordering::Less, 0 => std::cmp::Ordering::Equal, 1 ..= 127i8 => std::cmp::Ordering::Greater } }); $( q.insert($v); )* q }}; } /// Returns an RBMap containing the (key, value) /// pairs separated by commas. /// # Example: /// ``` /// use rb_tree::{RBMap, new_map}; /// /// let m = new_map!((1, 'a'), (2, 'b'), (3, 'c')); /// assert_eq!(m.get(&1).unwrap(), &'a'); /// assert_eq!(m.get(&2).unwrap(), &'b'); /// assert_eq!(m.get(&3).unwrap(), &'c'); /// ``` #[cfg(feature = "map")] #[macro_export] macro_rules! new_map { ( $(($k:expr, $v:expr)),* ) => {{ let mut m = RBMap::new(); $( m.insert($k, $v); )* m }}; }