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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
#![deny(missing_docs)] #![deny(missing_debug_implementations)] #![no_std] //! An intrusive, allocation-free [splay tree] implementation. //! //! [![](https://docs.rs/intrusive_splay_tree/badge.svg)](https://docs.rs/intrusive_splay_tree/) //! [![](https://img.shields.io/crates/v/intrusive_splay_tree.svg)](https://crates.io/crates/intrusive_splay_tree) //! [![](https://img.shields.io/crates/d/intrusive_splay_tree.svg)](https://crates.io/crates/intrusive_splay_tree) //! [![Travis CI Build Status](https://travis-ci.org/fitzgen/intrusive_splay_tree.svg?branch=master)](https://travis-ci.org/fitzgen/intrusive_splay_tree) //! //! Splay trees are self-adjusting, meaning that operating on an element (for //! example, doing a `find` or an `insert`) rebalances the tree in such a way //! that the element becomes the root. This means that subsequent operations on //! that element are *O(1)* as long as no other element is operated on in the //! meantime. //! //! ## Implementation and Goals //! //! * **Intrusive:** The space for the subtree pointers is stored *inside* the //! element type. In non-intrusive trees, we would have a node type that //! contains the subtree pointers and either a pointer to the element or we //! would move the element into the node. The intrusive design inverts the //! relationship, so that the elements hold the subtree pointers within //! themselves. //! //! * **Freedom from allocations and moves:** The intrusive design enables this //! implementation to fully avoid both allocations and moving elements in //! memory. Since the space for subtree pointers already exists in the element, //! no allocation is necessary, just a handful of pointer writes. Therefore, //! this implementation can be used in constrained environments that don't have //! access to an allocator (e.g. some embedded devices or within a signal //! handler) and with types that can't move in memory (e.g. `pthread_mutex_t`). //! //! * **Small code size:** This implementation is geared towards small code //! size, and uses trait objects internally to avoid the code bloat induced by //! monomorphization. This implementation is suitable for targeting WebAssembly, //! where code is downloaded over the network, and code bloat delays Web page //! loading. //! //! * **Nodes do not have parent pointers**: An intrusive node is only two words //! in size: left and right sub tree pointers. There are no parent pointers, //! which would require another word of overhead. To meet this goal, the //! implementation uses the "top-down" variant of splay trees. //! //! [splay tree]: https://en.wikipedia.org/wiki/Splay_tree //! [paper]: http://www.cs.cmu.edu/~sleator/papers/self-adjusting.pdf //! //! ## Constraints //! //! * **Elements within a tree must all have the same lifetime.** This means //! that you must use something like the [`bumpalo`][arena] crate for //! allocation, or be working with static data, etc. //! //! * **Elements in an intrusive collections are inherently shared.** They are //! always potentially aliased by the collection(s) they are in. In the other //! direction, a particular intrusive collection only has a shared reference to //! the element, since elements can both be in many intrusive collections at the //! same time. Therefore, you cannot get a unique, mutable reference to an //! element out of an intrusive splay tree. To work around this, you may need to //! liberally use interior mutability, for example by leveraging `Cell`, //! `RefCell`, and `Mutex`. //! //! [arena]: https://crates.io/crates/bumpalo //! //! ## Example //! //! This example defines a `Monster` type, where each of its instances live //! within two intrusive trees: one ordering monsters by their name, and the //! other ordering them by their health. //! //! ``` //! use intrusive_splay_tree::{impl_intrusive_node, SplayTree}; //! //! use std::cmp::Ordering; //! use std::marker::PhantomData; //! //! // We have a monster type, and we want to query monsters by both name and //! // health. //! #[derive(Debug)] //! struct Monster<'a> { //! name: String, //! health: u64, //! //! // An intrusive node so we can put monsters in a tree to query by name. //! by_name_node: intrusive_splay_tree::Node<'a>, //! //! // Another intrusive node so we can put monsters in a second tree (at //! // the same time!) and query them by health. //! by_health_node: intrusive_splay_tree::Node<'a>, //! } //! //! // Define a type for trees where monsters are ordered by name. //! struct MonstersByName; //! //! // Implement `IntrusiveNode` for the `MonstersByName` tree, where the //! // element type is `Monster` and the field in `Monster` that has this tree's //! // intrusive node is `by_name`. //! impl_intrusive_node! { //! impl<'a> IntrusiveNode<'a> for MonstersByName //! where //! type Elem = Monster<'a>, //! node = by_name_node; //! } //! //! // Define how to order `Monster`s within the `MonstersByName` tree by //! // implementing `TreeOrd`. //! impl<'a> intrusive_splay_tree::TreeOrd<'a, MonstersByName> for Monster<'a> { //! fn tree_cmp(&self, rhs: &Monster<'a>) -> Ordering { //! self.name.cmp(&rhs.name) //! } //! } //! //! // And do all the same things for trees where monsters are ordered by health... //! struct MonstersByHealth; //! impl_intrusive_node! { //! impl<'a> IntrusiveNode<'a> for MonstersByHealth //! where //! type Elem = Monster<'a>, //! node = by_health_node; //! } //! impl<'a> intrusive_splay_tree::TreeOrd<'a, MonstersByHealth> for Monster<'a> { //! fn tree_cmp(&self, rhs: &Monster<'a>) -> Ordering { //! self.health.cmp(&rhs.health) //! } //! } //! //! // We can also implement `TreeOrd` for other types, so that we can query the //! // tree by these types. For example, we want to query the `MonstersByHealth` //! // tree by some `u64` health value, and we want to query the `MonstersByName` //! // tree by some `&str` name value. //! //! impl<'a> intrusive_splay_tree::TreeOrd<'a, MonstersByHealth> for u64 { //! fn tree_cmp(&self, rhs: &Monster<'a>) -> Ordering { //! self.cmp(&rhs.health) //! } //! } //! //! impl<'a> intrusive_splay_tree::TreeOrd<'a, MonstersByName> for str { //! fn tree_cmp(&self, rhs: &Monster<'a>) -> Ordering { //! self.cmp(&rhs.name) //! } //! } //! //! impl<'a> Monster<'a> { //! /// The `Monster` constructor allocates `Monster`s in a bump arena, and //! /// inserts the new `Monster` in both trees. //! pub fn new( //! arena: &'a bumpalo::Bump, //! name: String, //! health: u64, //! by_name_tree: &mut SplayTree<'a, MonstersByName>, //! by_health_tree: &mut SplayTree<'a, MonstersByHealth> //! ) -> &'a Monster<'a> { //! let monster = arena.alloc(Monster { //! name, //! health, //! by_name_node: Default::default(), //! by_health_node: Default::default(), //! }); //! //! by_name_tree.insert(monster); //! by_health_tree.insert(monster); //! //! monster //! } //! } //! //! fn main() { //! // The arena that the monsters will live within. //! let mut arena = bumpalo::Bump::new(); //! //! // The splay trees ordered by name and health respectively. //! let mut by_name_tree = SplayTree::default(); //! let mut by_health_tree = SplayTree::default(); //! //! // Now let's create some monsters, inserting them into the trees! //! //! Monster::new( //! &arena, //! "Frankenstein's Monster".into(), //! 99, //! &mut by_name_tree, //! &mut by_health_tree, //! ); //! //! Monster::new( //! &arena, //! "Godzilla".into(), //! 2000, //! &mut by_name_tree, //! &mut by_health_tree, //! ); //! //! Monster::new( //! &arena, //! "Vegeta".into(), //! 9001, //! &mut by_name_tree, //! &mut by_health_tree, //! ); //! //! // Query the `MonstersByName` tree by a name. //! //! let godzilla = by_name_tree.find("Godzilla").unwrap(); //! assert_eq!(godzilla.name, "Godzilla"); //! //! assert!(by_name_tree.find("Gill-Man").is_none()); //! //! // Query the `MonstersByHealth` tree by a health. //! //! let vegeta = by_health_tree.find(&9001).unwrap(); //! assert_eq!(vegeta.name, "Vegeta"); //! //! assert!(by_health_tree.find(&0).is_none()); //! } //! ``` extern crate unreachable; mod internal; mod node; pub use node::Node; use core::cmp; use core::fmt; use core::iter; use core::marker::PhantomData; /// Defines how to get the intrusive node from a particular kind of /// `SplayTree`'s element type. /// /// Don't implement this by hand -- doing so is both boring and dangerous! /// Instead, use the `impl_intrusive_node!` macro. pub unsafe trait IntrusiveNode<'a> where Self: Sized, { /// The element struct type that contains a node for this tree. type Elem: TreeOrd<'a, Self>; /// Get the node for this tree from the given element. fn elem_to_node(elem: &'a Self::Elem) -> &'a Node<'a>; /// Get the element for this node (by essentially doing `offsetof` the /// node's field). /// /// ## Safety /// /// Given a node inside a different element type, or a node for a different /// tree within the same element type, this method will result in memory /// unsafety. unsafe fn node_to_elem(node: &'a Node<'a>) -> &'a Self::Elem; } /// Implement `IntrusiveNode` for a particular kind of `SplayTree` and its /// element type. #[macro_export] macro_rules! impl_intrusive_node { ( impl< $($typarams:tt),* > IntrusiveNode<$intrusive_node_lifetime:tt> for $tree:ty where type Elem = $elem:ty , node = $node:ident ; ) => { unsafe impl< $( $typarams )* > $crate::IntrusiveNode<$intrusive_node_lifetime> for $tree { type Elem = $elem; fn elem_to_node( elem: & $intrusive_node_lifetime Self::Elem ) -> & $intrusive_node_lifetime $crate::Node< $intrusive_node_lifetime > { &elem. $node } unsafe fn node_to_elem( node: & $intrusive_node_lifetime $crate::Node< $intrusive_node_lifetime > ) -> & $intrusive_node_lifetime Self::Elem { let offset = memoffset::offset_of!(Self::Elem, $node); let node = node as *const _ as *const u8; let elem = node.offset(-(offset as isize)) as *const Self::Elem; &*elem } } } } /// A total ordering between the `Self` type and the tree's element type /// `T::Elem`. /// /// Different from `Ord` in that it allows `Self` and `T::Elem` to be distinct /// types, so that you can query a splay tree without fully constructing its /// element type. pub trait TreeOrd<'a, T: IntrusiveNode<'a>> { /// What is the ordering relationship between `self` and the given tree /// element? fn tree_cmp(&self, elem: &'a T::Elem) -> cmp::Ordering; } struct Query<'a, 'b, K, T> where T: 'a + IntrusiveNode<'a>, K: 'b + ?Sized + TreeOrd<'a, T>, { key: &'b K, _phantom: PhantomData<&'a T>, } impl<'a, 'b, K, T> Query<'a, 'b, K, T> where T: IntrusiveNode<'a>, K: 'b + ?Sized + TreeOrd<'a, T>, { #[inline] fn new(key: &'b K) -> Query<'a, 'b, K, T> { Query { key, _phantom: PhantomData, } } } impl<'a, 'b, K, T> internal::CompareToNode<'a> for Query<'a, 'b, K, T> where T: 'a + IntrusiveNode<'a>, T::Elem: 'a, K: 'b + ?Sized + TreeOrd<'a, T>, { #[inline] unsafe fn compare_to_node(&self, node: &'a Node<'a>) -> cmp::Ordering { let val = T::node_to_elem(node); self.key.tree_cmp(val) } } /// An intrusive splay tree. /// /// The tree is parameterized by some marker type `T` whose `IntrusiveNode` /// implementation defines: /// /// * the element type contained in this tree: `T::Elem`, /// * how to get the intrusive node for this tree within an element, /// * and how to get the container element from a given intrusive node for this /// tree. pub struct SplayTree<'a, T> where T: IntrusiveNode<'a>, T::Elem: 'a, { tree: internal::SplayTree<'a>, _phantom: PhantomData<&'a T::Elem>, } impl<'a, T> Default for SplayTree<'a, T> where T: 'a + IntrusiveNode<'a>, T::Elem: 'a, { #[inline] fn default() -> SplayTree<'a, T> { SplayTree { tree: internal::SplayTree::default(), _phantom: PhantomData, } } } impl<'a, T> fmt::Debug for SplayTree<'a, T> where T: 'a + IntrusiveNode<'a>, T::Elem: 'a + fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let set = &mut f.debug_set(); self.walk(|x| { set.entry(x); }); set.finish() } } impl<'a, T> Extend<&'a T::Elem> for SplayTree<'a, T> where T: 'a + IntrusiveNode<'a>, { #[inline] fn extend<I: IntoIterator<Item = &'a T::Elem>>(&mut self, iter: I) { for x in iter { self.insert(x); } } } impl<'a, T> iter::FromIterator<&'a T::Elem> for SplayTree<'a, T> where T: 'a + IntrusiveNode<'a>, T::Elem: fmt::Debug, { #[inline] fn from_iter<I: IntoIterator<Item = &'a T::Elem>>(iter: I) -> Self { let mut me = SplayTree::default(); me.extend(iter); me } } impl<'a, T> SplayTree<'a, T> where T: 'a + IntrusiveNode<'a>, { /// Is this tree empty? #[inline] pub fn is_empty(&self) -> bool { self.tree.is_empty() } /// Get a reference to the root element, if any exists. pub fn root(&self) -> Option<&'a T::Elem> { self.tree.root().map(|r| unsafe { T::node_to_elem(r) }) } /// Find an element in the tree. /// /// This operation will splay the queried element to the root of the tree. /// /// The `key` must be of a type that implements `TreeOrd` for this tree's /// `T` type. The element type `T::Elem` must always implement `TreeOrd<T>`, /// so you can search the tree by element. You can also implement /// `TreeOrd<T>` for additional key types. This allows you to search the /// tree without constructing a full element. #[inline] pub fn find<K>(&mut self, key: &K) -> Option<&'a T::Elem> where K: ?Sized + TreeOrd<'a, T>, { unsafe { let query: Query<_, T> = Query::new(key); self.tree.find(&query).map(|node| T::node_to_elem(node)) } } /// Insert a new element into this tree. /// /// Returns `true` if the element was inserted into the tree. /// /// Returns `false` if there was already an element in the tree for which /// `TreeOrd` returned `Ordering::Equal`. In this case, the extant element /// is left in the tree, and `elem` is not inserted. /// /// This operation will splay the inserted element to the root of the tree. /// /// It is a logic error to insert an element that is already inserted in a /// `T` tree. /// /// ## Panics /// /// If `debug_assertions` are enabled, then this function may panic if /// `elem` is already in a `T` tree. If `debug_assertions` are not defined, /// the behavior is safe, but unspecified. #[inline] pub fn insert(&mut self, elem: &'a T::Elem) -> bool { unsafe { let query: Query<_, T> = Query::new(elem); let node = T::elem_to_node(elem); self.tree.insert(&query, node) } } /// Find and remove an element from the tree. /// /// If a matching element is found and removed, then `Some(removed_element)` /// is returned. Otherwise `None` is returned. /// /// The `key` must be of a type that implements `TreeOrd` for this tree's /// `T` type. The element type `T::Elem` must always implement `TreeOrd<T>`, /// so you can remove an element directly. You can also implement /// `TreeOrd<T>` for additional key types. This allows you to search the /// tree without constructing a full element, and remove the element that /// matches the given key, if any. #[inline] pub fn remove<K>(&mut self, key: &K) -> Option<&'a T::Elem> where K: ?Sized + TreeOrd<'a, T>, { unsafe { let query: Query<_, T> = Query::new(key); self.tree.remove(&query).map(|node| T::node_to_elem(node)) } } /// Walk the tree in order. /// /// The `C` type controls whether iteration should continue, or break and /// return a `C::Result` value. You can use `()` as `C`, and that always /// continues iteration. Using `Result<(), E>` as `C` allows you to halt /// iteration on error, and propagate the error value. Using `Option<T>` as /// `C` allows you to search for some value, halt iteration when its found, /// and return it. #[inline] pub fn walk<F, C>(&self, mut f: F) -> Option<C::Result> where F: FnMut(&'a T::Elem) -> C, C: WalkControl, { let mut result = None; self.tree.walk(&mut |node| unsafe { let elem = T::node_to_elem(node); result = f(elem).should_break(); result.is_none() }); result } } /// A trait that guides whether `SplayTree::walk` should continue or break, and /// what the return value is. pub trait WalkControl { /// The result type that is returned when we break. type Result; /// If iteration should halt, return `Some`. If iteration should continue, /// return `None`. fn should_break(self) -> Option<Self::Result>; } impl WalkControl for () { type Result = (); fn should_break(self) -> Option<()> { None } } impl<T> WalkControl for Option<T> { type Result = T; fn should_break(mut self) -> Option<T> { self.take() } } impl<E> WalkControl for Result<(), E> { type Result = E; fn should_break(self) -> Option<E> { self.err() } }