8 stable releases

1.0.7 Feb 16, 2024
1.0.6 Jun 11, 2020
1.0.5 Nov 21, 2019
1.0.4 Dec 18, 2018
0.1.0 Jul 31, 2017

#191 in Data structures

Download history 1765/week @ 2024-07-23 1697/week @ 2024-07-30 1984/week @ 2024-08-06 1706/week @ 2024-08-13 2133/week @ 2024-08-20 1818/week @ 2024-08-27 1574/week @ 2024-09-03 1222/week @ 2024-09-10 1437/week @ 2024-09-17 1517/week @ 2024-09-24 1343/week @ 2024-10-01 1713/week @ 2024-10-08 2086/week @ 2024-10-15 1891/week @ 2024-10-22 2039/week @ 2024-10-29 1900/week @ 2024-11-05

8,387 downloads per month
Used in 14 crates (3 directly)

Apache-2.0/MIT

16KB
234 lines

Cactus

This library provides an immutable cactus stack (also called a spaghetti stack or parent pointer tree). A cactus stack is a (possibly empty) node with a (possibly null) pointer to a parent node. Any given node has a unique path back to the root node. Rather than mutably updating the stack, one creates and obtains access to immutable nodes (when a node becomes unreachable its memory is automatically reclaimed). A new child node pointing to a parent can be created via the child function (analogous to the normal push) and a parent can be retrieved via the parent function (analogous to the normal pop).

use cactus::Cactus;
let c = Cactus::new();
assert!(c.is_empty());
let c2 = c.child(1);
assert_eq!(c2.len(), 1);
assert_eq!(*c2.val().unwrap(), 1);
let c3 = c2.parent().unwrap();
assert!(c3.is_empty());

From a given node one can create multiple sub-stacks:

use cactus::Cactus;
let c = Cactus::new().child(1);
let c2 = c.child(2);
let c3 = c.child(3);
assert!(c2 != c3);
assert_eq!(c2.vals().cloned().collect::<Vec<_>>(), [2, 1]);
assert_eq!(c3.vals().cloned().collect::<Vec<_>>(), [3, 1]);

No runtime deps