Struct IntMap

Source
pub struct IntMap<K, V> { /* private fields */ }
Expand description

A hashmap that maps an integer based K to V.

Implementations§

Source§

impl<K, V> IntMap<K, V>

Source

pub const fn new() -> Self

Creates a new IntMap.

The IntMap is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

The IntMap is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::new();
assert_eq!(map, IntMap::default());
Source§

impl<K: IntKey, V> IntMap<K, V>

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new IntMap with at least the given capacity.

If the capacity is 0, the IntMap will not allocate. Otherwise the capacity is rounded to the next power of two and space for elements is allocated accordingly.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::with_capacity(20);
Source

pub fn set_load_factor(&mut self, load_factor: f32)

Sets the load factor of the IntMap rounded to the first decimal point.

A load factor between 0.0 and 1.0 will reduce hash collisions but use more space. A load factor above 1.0 will tolerate hash collisions and use less space.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::with_capacity(20);
map.set_load_factor(0.909); // Sets load factor to 90.9%
Source

pub fn get_load_factor(&self) -> f32

Returns the current load factor.

Source

pub fn reserve(&mut self, additional: usize)

Ensures that the IntMap has space for at least additional more elements

Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Inserts a key/value pair into the IntMap.

This function returns the previous value if any otherwise None.

§Examples
use intmap::IntMap;

let mut map: IntMap::<u64, _> = IntMap::new();
assert_eq!(map.insert(21, "Eat my shorts"), None);
assert_eq!(map.insert(21, "Ay, caramba"), Some("Eat my shorts"));
assert_eq!(map.get(21), Some(&"Ay, caramba"));
Source

pub fn insert_checked(&mut self, key: K, value: V) -> bool

Insert a key/value pair into the IntMap if the key is not yet inserted.

This function returns true if key/value were inserted and false otherwise.

§Examples
use intmap::IntMap;

let mut map: IntMap::<u64, _> = IntMap::new();
assert!(map.insert_checked(21, "Eat my shorts"));
assert!(!map.insert_checked(21, "Ay, caramba"));
assert_eq!(map.get(21), Some(&"Eat my shorts"));
Source

pub fn get(&self, key: K) -> Option<&V>

Gets the value for the given key from the IntMap.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::new();
map.insert(21, 42);
let val = map.get(21);
assert!(val.is_some());
assert_eq!(*val.unwrap(), 42);
assert!(map.contains_key(21));
Source

pub fn get_mut(&mut self, key: K) -> Option<&mut V>

Gets the mutable value for the given key from the IntMap.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::new();
map.insert(21, 42);

assert_eq!(*map.get(21).unwrap(), 42);
assert!(map.contains_key(21));

{
    let mut val = map.get_mut(21).unwrap();
    *val+=1;
}
    assert_eq!(*map.get(21).unwrap(), 43);
Source

pub fn remove(&mut self, key: K) -> Option<V>

Removes the value for given key from the IntMap and returns it.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::new();
map.insert(21, 42);
let val = map.remove(21);
assert!(val.is_some());
assert_eq!(val.unwrap(), 42);
assert!(!map.contains_key(21));
Source

pub fn contains_key(&self, key: K) -> bool

Returns true if the key is present in the IntMap.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::new();
map.insert(21, 42);
assert!(map.contains_key(21));
Source

pub fn clear(&mut self)

Removes all elements from the IntMap.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::new();
map.insert(21, 42);
map.clear();
assert_eq!(map.len(), 0);
Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(K, &V) -> bool,

Retains only the key/value pairs specified by the predicate.

In other words, remove all elements such that f(key, &value) returns false.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::new();
map.insert(1, 11);
map.insert(2, 12);
map.insert(4, 13);

// retain only the odd values
map.retain(|k, v| *v % 2 == 1);

assert_eq!(map.len(), 2);
assert!(map.contains_key(1));
assert!(map.contains_key(4));
Source

pub fn is_empty(&self) -> bool

Returns true if the IntMap is empty

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::new();
map.insert(21, 42);
assert!(!map.is_empty());
map.remove(21);
assert!(map.is_empty());
Source

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

Returns an Iterator over all key/value pairs.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

Returns an Iterator over all key/value pairs with mutable value.

Source

pub fn keys(&self) -> Keys<'_, K, V>

Returns an Iterator over all keys.

Source

pub fn values(&self) -> Values<'_, K, V>

Returns an Iterator over all values.

Source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>

Returns an Iterator over all mutable values.

Source

pub fn drain(&mut self) -> Drain<'_, K, V>

Returns an Iterator over all key/value pairs that removes the pairs from the IntMap during iteration.

If the Iterator is droppend then all remaining key/value pairs will be removed from the IntMap.

Source

pub fn len(&self) -> usize

Returns the number of key/value pairs in the IntMap.

Source

pub fn load(&self) -> u64

Returns the number of filled slots.

Source

pub fn load_rate(&self) -> f64

Returns the ratio between key/value pairs and available slots as percentage.

§Examples
use intmap::IntMap;

let mut map: IntMap<u64, u64> = IntMap::with_capacity(2);
map.set_load_factor(2.0);
assert_eq!(map.load_rate(), 0.0);
map.insert(1, 42);
assert_eq!(map.load_rate(), 50.0);
map.insert(2, 42);
assert_eq!(map.load_rate(), 100.0);
map.insert(3, 42);
assert_eq!(map.load_rate(), 150.0);
Source

pub fn capacity(&self) -> usize

Returns the total number of available slots.

Source

pub fn entry(&mut self, key: K) -> Entry<'_, K, V>

Gets the Entry that corresponds to the given key.

§Examples
use intmap::{IntMap, Entry};

let mut counters = IntMap::new();

for number in [10, 30, 10, 40, 50, 50, 60, 50] {
    let counter = match counters.entry(number) {
        Entry::Occupied(entry) => entry.into_mut(),
        Entry::Vacant(entry) => entry.insert(0),
    };
    *counter += 1;
}

assert_eq!(counters.get(10), Some(&2));
assert_eq!(counters.get(20), None);
assert_eq!(counters.get(30), Some(&1));
assert_eq!(counters.get(40), Some(&1));
assert_eq!(counters.get(50), Some(&3));
assert_eq!(counters.get(60), Some(&1));

Trait Implementations§

Source§

impl<K: Clone, V: Clone> Clone for IntMap<K, V>

Source§

fn clone(&self) -> IntMap<K, V>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, V> Debug for IntMap<K, V>
where K: IntKey + Debug, V: Debug,

Source§

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

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

impl<K, V> Default for IntMap<K, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, K, V> Deserialize<'de> for IntMap<K, V>
where K: IntKey + Deserialize<'de>, V: Deserialize<'de>,

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K: IntKey, V> Extend<(K, V)> for IntMap<K, V>

Source§

fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K: IntKey, V> FromIterator<(K, V)> for IntMap<K, V>

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<K: IntKey, V> IntoIterator for IntMap<K, V>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V> PartialEq for IntMap<K, V>
where K: IntKey, V: PartialEq,

Source§

fn eq(&self, other: &IntMap<K, V>) -> 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<K, V> Serialize for IntMap<K, V>
where K: IntKey + Serialize, V: Serialize,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<K: IntKey, V: Eq> Eq for IntMap<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for IntMap<K, V>

§

impl<K, V> RefUnwindSafe for IntMap<K, V>

§

impl<K, V> Send for IntMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for IntMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for IntMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for IntMap<K, V>
where K: UnwindSafe, V: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,