# scapegoat


Ordered set and map data structures via an arena-based [scapegoat tree](https://people.csail.mit.edu/rivest/pubs/GR93.pdf) (memory-efficient, self-balancing binary search tree).
This library is `!#[no_std]` compatible by default and strictly `#![forbid(unsafe_code)]`.
### About
Three APIs:
* Ordered Set API ([`SGSet`](crate::SGSet))
* Ordered Map API ([`SGMap`](crate::SGMap))
* Binary Tree API ([`SGTree`](crate::SGTree))
Strives for two properties:
* **Maximal safety:** strong [memory safety](https://tiemoko.com/blog/blue-team-rust/) guarantees.
* **Compile-time safety:** no `unsafe` (no raw pointer dereference, etc.).
* **Debug-time safety:** `debug_assert!` for logical invariants exercised in testing.
* **Runtime safety:** no interior mutability (e.g. no need for `Rc<RefCell<T>>`'s runtime check).
* **Minimal footprint:** small binary with low resource use.
* **Memory-efficient:** nodes have only child index metadata, node memory is re-used.
* **Recursion-free:** all operations are iterative, so stack use and runtime are both minimized.
* **Zero-copy:** rebuild/removal re-point in-place, nodes are never copied or cloned.
Other features:
* **Generic:** map keys and set elements can be any type that implements the `Ord` trait.
* **Arbitrarily mutable:** elements can be insert and removed, map values can be mutated.
### Usage
`SGMap` non-exhaustive API example (would work identically for `std::collections::BTreeMap`):
```rust
use scapegoat::SGMap;
let mut example = SGMap::new();
example.insert(3, String::from("the"));
example.insert(2, String::from("don't blame"));
example.insert(1, String::from("Please"));
example.insert(4, String::from("borrow checker"));
assert_eq!(
let please_tuple = example.pop_first().unwrap();
assert_eq!(please_tuple, (1, String::from("Please")));
example.insert(5, String::from("! :P"));
let dont_blame = example.get_mut(&2).unwrap();
dont_blame.remove(0);
dont_blame.insert(0, 'D');
assert_eq!(
* If the `SG_MAX_STACK_ELEMS` environment variable is not set, it will default to `1024`.
* For embedded systems without dynamic (heap) memory: `SG_MAX_STACK_ELEMS` is a hard maximum - attempting to insert beyond this limit will cause a panic.
* For any system with dynamic memory: the first `SG_MAX_STACK_ELEMS` elements are stack-allocated and the remainder will be automatically heap-allocated (no panic).
### Trusted Dependencies
This library has two dependencies, each of which have no dependencies of their own (e.g. exactly two total dependencies).
Both dependencies were carefully chosen.
* [`smallvec`](https://crates.io/crates/smallvec) - `!#[no_std]` compatible `Vector` alternative. Used in Mozilla's Servo browser engine.
* [`libm`](https://crates.io/crates/libm) - `!#[no_std]` compatible math operations. Maintained by the Rust Language Team.
### Note
This project is an exercise in safe data structure design. It's not as mature, fast, or memory efficient as the [standard library's `BTreeMap`/`BTreeSet`](http://cglab.ca/~abeinges/blah/rust-btree-case/).