pub struct HashIndexed<T, K, E> { /* private fields */ }
Expand description
Stores a set of values indexed in a user-defined way.
Use like this:
use hashindexed::{HashIndexed, KeyComparator};
#[derive(Debug, Eq, PartialEq)]
struct MyType { num: i32, name: &'static str }
struct MyComparator;
impl KeyComparator<MyType, i32> for MyComparator {
fn extract_key(v: &MyType) -> &i32 { &v.num }
}
let mut container: HashIndexed<MyType, i32, MyComparator> =
HashIndexed::new();
container.insert(MyType { num: 1, name: "one" });
container.insert(MyType { num: 2, name: "two" });
container.insert(MyType { num: 3, name: "three" });
assert_eq!( container.remove(&1).unwrap().name, "one" );
assert_eq!( container.remove(&1), None );
assert!( container.contains(&2) );
assert_eq!( container.len(), 2 );
assert_eq!( container.get(&3).unwrap().name, "three" );
container.replace(MyType { num: 3, name: "THREE" });
assert_eq!( container.get(&3).unwrap().name, "THREE" );
Implementations§
Source§impl<T, K, E> HashIndexed<T, K, E>
impl<T, K, E> HashIndexed<T, K, E>
Sourcepub fn new() -> HashIndexed<T, K, E>
pub fn new() -> HashIndexed<T, K, E>
Creates an empty HashIndexed collection.
Sourcepub fn with_capacity(capacity: usize) -> HashIndexed<T, K, E>
pub fn with_capacity(capacity: usize) -> HashIndexed<T, K, E>
Creates an empty HashIndexed with space for at least capacity
elements in the hash table.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the collection can hold without reallocating.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted
in the collection. More spaces than this may be allocated to avoid
frequent reallocations.
§Panics
Panics if the new allocation size overflows usize
.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the collection as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
Sourcepub fn iter(&self) -> Iter<'_, T, K, E> ⓘ
pub fn iter(&self) -> Iter<'_, T, K, E> ⓘ
An iterator visiting all elements in arbitrary order.
Sourcepub fn into_iter(self) -> IntoIter<T, K, E> ⓘ
pub fn into_iter(self) -> IntoIter<T, K, E> ⓘ
Creates a consuming iterator, that is, one that moves each value out of the set in arbitrary order. The set cannot be used after calling this.
Sourcepub fn contains(&self, k: &K) -> bool
pub fn contains(&self, k: &K) -> bool
Returns true
if the collection contains a value matching the given
key.
Sourcepub fn get(&self, k: &K) -> Option<&T>
pub fn get(&self, k: &K) -> Option<&T>
Returns a reference to the value corresponding to the key.
Sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Adds a value to the set. Returns true if the value was not already present in the collection.