hashindexed

Struct HashIndexed

Source
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>
where E: KeyComparator<T, K>, K: Eq + Hash, IndexableValue<T, K, E>: Borrow<K>,

Source

pub fn new() -> HashIndexed<T, K, E>

Creates an empty HashIndexed collection.

Source

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.

Source

pub fn capacity(&self) -> usize

Returns the number of elements the collection can hold without reallocating.

Source

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.

Source

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.

Source

pub fn iter(&self) -> Iter<'_, T, K, E>

An iterator visiting all elements in arbitrary order.

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of elements in the collection.

Source

pub fn is_empty(&self) -> bool

Returns true if the collection contains no elements.

Source

pub fn clear(&mut self)

Clears the collection, removing all values.

Source

pub fn contains(&self, k: &K) -> bool

Returns true if the collection contains a value matching the given key.

Source

pub fn get(&self, k: &K) -> Option<&T>

Returns a reference to the value corresponding to the key.

Source

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.

Source

pub fn replace(&mut self, value: T) -> Option<T>

Adds a value to the set, replacing the existing value, if any, that is equal to the given one. Returns the replaced value.

Source

pub fn remove(&mut self, k: &K) -> Option<T>

Removes and returns the value in the collection, if any, that is equal to the given one.

Trait Implementations§

Source§

impl<T, K, E> Debug for HashIndexed<T, K, E>
where HashSet<IndexableValue<T, K, E>>: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T, K, E> IntoIterator for &'a HashIndexed<T, K, E>
where K: Eq + Hash, E: KeyComparator<T, K>, IndexableValue<T, K, E>: Borrow<K>,

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, K, E>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, T, K, E>

Creates an iterator from a value. Read more
Source§

impl<'a, T, K, E> IntoIterator for HashIndexed<T, K, E>
where K: Eq + Hash, E: KeyComparator<T, K>,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, K, E>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<T, K, E>

Creates an iterator from a value. Read more
Source§

impl<T, K, E> PartialEq for HashIndexed<T, K, E>
where HashSet<IndexableValue<T, K, E>>: PartialEq,

Source§

fn eq(&self, other: &HashIndexed<T, K, E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, K, E> Eq for HashIndexed<T, K, E>
where HashIndexed<T, K, E>: PartialEq,

Auto Trait Implementations§

§

impl<T, K, E> Freeze for HashIndexed<T, K, E>

§

impl<T, K, E> RefUnwindSafe for HashIndexed<T, K, E>

§

impl<T, K, E> Send for HashIndexed<T, K, E>
where T: Send, K: Send, E: Send,

§

impl<T, K, E> Sync for HashIndexed<T, K, E>
where T: Sync, K: Sync, E: Sync,

§

impl<T, K, E> Unpin for HashIndexed<T, K, E>
where T: Unpin, K: Unpin, E: Unpin,

§

impl<T, K, E> UnwindSafe for HashIndexed<T, K, E>
where T: UnwindSafe, K: UnwindSafe, E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.