pub struct IdCache<I: Id, T> { /* private fields */ }
Expand description
A cache which generates sequentially-assigned ids for unique values.
§Example
use id_collections::id_type;
use id_cache::IdCache;
#[id_type]
struct WordId(u32);
let mut word_cache: IdCache<WordId, &str> = IdCache::new();
let foo_id = word_cache.make_id("foo");
let bar_id = word_cache.make_id("bar");
assert_eq!(word_cache[foo_id], "foo");
assert_eq!(word_cache[bar_id], "bar");
// ids for repeated values are reused:
assert_eq!(word_cache.make_id("foo"), foo_id);
§Serde Support
When the serde
Cargo feature is enabled, the IdCache<I, T>
type can be serialized and
deserialized using Serde. An IdCache<I, T>
is serialized as a sequence
consisting of the unique values in the cache, ordered by id:
use id_collections::id_type;
use id_cache::IdCache;
#[id_type]
struct WordId(u32);
let mut word_cache: IdCache<WordId, &str> = IdCache::new();
word_cache.make_id("foo");
word_cache.make_id("bar");
let serialized = serde_json::to_string(&word_cache).unwrap();
assert_eq!(&serialized, r#"["foo","bar"]"#);
Implementations§
Source§impl<I: Id, T> IdCache<I, T>
impl<I: Id, T> IdCache<I, T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new, empty IdCache<I, T>
.
§Examples
let cache: IdCache<u32, &str> = IdCache::new();
assert!(cache.is_empty());
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new, empty IdCache<I, T>
with space to hold at least capacity
unique
values.
§Examples
let mut cache: IdCache<u32, &str> = IdCache::with_capacity(100);
assert!(cache.is_empty());
Sourcepub fn count(&self) -> Count<I>
pub fn count(&self) -> Count<I>
Returns the total number of ids that have been assigned to unique values in the
IdCache<I, T>
.
§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
assert!(cache.count().is_empty());
cache.make_id("foo");
cache.make_id("bar");
assert_eq!(cache.count().to_value(), 2);
cache.make_id("foo"); // value already present, so does not assign a new id
assert_eq!(cache.count().to_value(), 2);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the total number of unique values in the IdCache<I, T>
.
§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
assert_eq!(cache.len(), 0);
cache.make_id("foo");
cache.make_id("bar");
assert_eq!(cache.len(), 2);
cache.make_id("foo"); // value already present, so does not increase the len
assert_eq!(cache.len(), 2);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the IdCache<I, T>
contains no values.
§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
assert!(cache.is_empty());
cache.make_id("foo");
assert!(!cache.is_empty());
Sourcepub fn make_id(&mut self, value: T) -> I
pub fn make_id(&mut self, value: T) -> I
Ensures value
has an id in the IdCache<I, T>
, and returns that id.
If value
is already present in the IdCache<I, T>
, then make_id
returns its existing
id. Otherwise, make_id
returns a new sequentally-assigned id.
§Panics
Panics if the number of ids in the IdCache<I, T>
overflows I
.
§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
assert_eq!(cache.make_id("foo"), 0);
assert_eq!(cache.make_id("bar"), 1);
assert_eq!(cache.make_id("foo"), 0);
Sourcepub fn get_id<U>(&self, value: &U) -> Option<I>
pub fn get_id<U>(&self, value: &U) -> Option<I>
Returns the id of a value in the IdCache<I, T>
, or None
if the value is not present.
§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
let foo_id = cache.make_id("foo");
assert_eq!(cache.get_id(&"foo"), Some(foo_id));
assert_eq!(cache.get_id(&"bar"), None);
Sourcepub fn get_value(&self, id: I) -> Option<&T>
pub fn get_value(&self, id: I) -> Option<&T>
Returns a reference to the value in the IdCache<I, T>
associated with a given id
, or
None
if the id has not been assigned.
§Examples
let mut cache: IdCache<u32, &str> = IdCache::new();
let foo_id = cache.make_id("foo");
assert_eq!(foo_id, 0);
assert_eq!(cache.get_value(foo_id), Some(&"foo"));
assert_eq!(cache.get_value(1), None);