Expand description
This crate exposes macros that generate data structures
on Ord
keys that provide faster lookups than regular Vec
s (O(log(n))
vs O(n)
)
and is simpler and more memory efficient than hashmaps. It is ideal for small
lookup tables where insertions and deletions are infrequent.
§Example
use sortedvec::sortedvec;
sortedvec! {
struct SortedVec {
fn derive_key(x: &u32) -> u32 { *x }
}
}
let unsorted = vec![3, 5, 0, 10, 7, 1];
let sorted = SortedVec::from(unsorted.clone());
// linear search (slow!)
let unsorted_contains_six: Option<_> = unsorted.iter().find(|&x| *x == 6);
assert!(unsorted_contains_six.is_none());
// binary search (fast!)
let sorted_contains_six: Option<_> = sorted.find(&6);
assert!(sorted_contains_six.is_none());
Modules§
- example
- An example of a data structure defined using the
sortedvec!
macro. The structure is generated by the following code.
Macros§
- sortedvec
- A macro that defines a sorted vector data structure.
- sortedvec_
slicekey - A macro that defines a specialized sorted vector data structure on slice keys.