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>
impl<K, V> IntMap<K, V>
Sourcepub const fn new() -> Self
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>
impl<K: IntKey, V> IntMap<K, V>
Sourcepub fn with_capacity(capacity: usize) -> Self
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);
Sourcepub fn set_load_factor(&mut self, load_factor: f32)
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%
Sourcepub fn get_load_factor(&self) -> f32
pub fn get_load_factor(&self) -> f32
Returns the current load factor.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Ensures that the IntMap
has space for at least additional
more elements
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
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"));
Sourcepub fn insert_checked(&mut self, key: K, value: V) -> bool
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"));
Sourcepub fn get_mut(&mut self, key: K) -> Option<&mut V>
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);
Sourcepub fn contains_key(&self, key: K) -> bool
pub fn contains_key(&self, key: K) -> bool
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
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));
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
Returns an Iterator
over all key/value pairs with mutable value.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
Returns an Iterator
over all mutable values.
Sourcepub fn load_rate(&self) -> f64
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);
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V>
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<'de, K, V> Deserialize<'de> for IntMap<K, V>
impl<'de, K, V> Deserialize<'de> for IntMap<K, V>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<K: IntKey, V> Extend<(K, V)> for IntMap<K, V>
impl<K: IntKey, V> Extend<(K, V)> for IntMap<K, V>
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)