substack 1.1.1

Stackbound iterable linked list for heap-free recursive algorithms
Documentation
  • Coverage
  • 100%
    22 out of 22 items documented1 out of 17 items with examples
  • Size
  • Source code size: 11.43 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.92 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • lbfalvy/substack
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • lbfalvy

A linked list on the stack to avoid heap allocations in recursive algorithms

use substack::Substack;

/// Walk a disjoint set and find the representative of an element, or return None if the
/// set contains a loop
fn find_value(alias_map: &[usize], idx: usize, prev: Substack<usize>) -> Option<usize> {
  match () {
    () if alias_map[idx] == idx => Some(idx),
    () if prev.iter().any(|i| *i == idx) => None,
    () => find_value(alias_map, alias_map[idx], prev.push(idx)),
  }
}

const map: &[usize] = &[2, 4, 1, 5, 1, 5];

assert_eq!(find_value(map, 0, Substack::Bottom), None);
assert_eq!(find_value(map, 3, Substack::Bottom), Some(5));