david_set/
lib.rs

1//! david-set contains a few collections that are optimized to scale
2//! in size well for small numbers of elements, while still scaling
3//! well in time (and size) for numbers of elements.  We have two set types:
4//!
5//! 1. `Set` is basically interchangeable with `HashSet`, although it
6//!    does require that its elements implement the `Copy` trait,
7//!    since otherwise I would have to learn to write correct `unsafe`
8//!    code, which would be scary.
9//!
10//! 2. `CastSet` is places a stronger requirement on its elements,
11//!     which must have trait `Cast`.  This is intended for elements
12//!     that are `Copy`, can be cheaply converted to `usize`, and are
13//!     sufficiently evenly distributed that they do not require real
14//!     hashing.  Basically, this is suitable if you want to store a
15//!     set of indices into an array.  All the basic integer types
16//!     should satisfy trait `Cast`.  Oh, and this set also requires
17//!     that one value of your type is "invalid".  For the unsigned
18//!     integer types, we take their maximum value to mean invalid.
19//!     This constraint allows us to save a bit more space.
20//!
21//! Both of these set types will do no heap allocation for small sets
22//! of small elements.  `CastSet` will store up to 16 bytes of
23//! elements before doing any heap allocation, while `Set` stores sets
24//! up to size 8 without allocation.  Both sets are typically faster
25//! than `HashSet` by a factor of around two, although for sets with
26//! more than 8 elements `Set` is in fact identical to `HashSet` in
27//! performance.
28//!
29//! # Examples
30//!
31//! ```
32//! use david_set::Set;
33//! let mut s: Set<usize> = Set::new();
34//! s.insert(1);
35//! assert!(s.contains(&1));
36//! ```
37//!
38//! ```
39//! use david_set::CastSet;
40//! let mut s: CastSet<usize> = CastSet::new();
41//! s.insert(1);
42//! assert!(s.contains(&1));
43//! ```
44
45#![deny(missing_docs)]
46
47mod vecset;
48pub use vecset::*;
49
50mod copyset;
51pub use copyset::*;
52
53mod castset;
54pub use castset::*;
55
56#[cfg(test)]
57extern crate rand;
58
59#[cfg(test)]
60#[macro_use]
61extern crate quickcheck;