symbol_map/lib.rs
1//! Provides fast mapping of arbitrary values to symbolic identifiers.
2//!
3//! The mapping to symbols is stored in the [Table](struct.Table.html) type,
4//! which retains ownership of the values being mapped. Any type that implements
5//! [SymbolId](trait.SymbolId.html) may be used as a symbol. Impls are provided
6//! for Rust's default unsigned integer types.
7//!
8//! Fast bidirectional lookup on top of a Table is provided by the
9//! [indexing](indexing/index.html) package, through the
10//! [Indexing](indexing/trait.Indexing.html) trait. For convenience, a
11//! HashMap-backed index is provided in
12//! [HashIndexing](indexing/struct.HashIndexing.html).
13//!
14//! # Example
15//!
16//! ```
17//! use symbol_map::indexing::{HashIndexing,Indexing};
18//! use std::str::FromStr;
19//!
20//! let mut pos_index = HashIndexing::<String, usize>::default();
21//! let s1 = String::from_str("NNP").unwrap();
22//! let s2 = String::from_str("VBD").unwrap();
23//! let s3 = String::from_str("NNP").unwrap();
24//!
25//! // We lose ownership of values passed to get_or_insert, so we pass in
26//! // clones of our data.
27//! {
28//! // The value returned by get_or_inset tells us whether a new association
29//! // was inserted, but we just unwrap it here. The resulting association
30//! // has a borrow of the index and its underlying symbol table, so we
31//! // restrict assoc to this inner scope in order to make additional
32//! // insertions below.
33//! let assoc = pos_index.get_or_insert(s1.clone()).unwrap();
34//! assert!(*assoc.id() == 0);
35//! assert!(assoc.data() == &s1);
36//! assert!(assoc.data() == &s3);
37//! }
38//! pos_index.get_or_insert(s2.clone());
39//! pos_index.get_or_insert(s3.clone());
40//! // Look up the values we just inserted.
41//! let assoc1 = pos_index.get(&s1).unwrap();
42//! let assoc2 = pos_index.get(&s2).unwrap();
43//! let assoc3 = pos_index.get(&s3).unwrap();
44//! assert!(assoc1.data() == &s1);
45//! assert!(assoc1.data() == &s3);
46//! assert!(*assoc1.id() == 0);
47//! assert!(*assoc2.id() == 1);
48//! assert!(*assoc3.id() == 0);
49//! assert!(assoc1 != assoc2);
50//! assert!(assoc1 == assoc3);
51//! ```
52
53pub mod indexing;
54mod table; // Not pub because all pub symbols re-exported.
55
56#[cfg(test)] extern crate crossbeam;
57
58pub use self::table::{Symbol, SymbolId, Table, TableIntoIter, TableIter};