pub struct IdHashMap<T, S = DefaultHashBuilder, A: Allocator = Global> { /* private fields */ }Expand description
A hash map where the key is part of the value.
The storage mechanism is a fast hash table of integer indexes to items, with these indexes stored in a hash table. This allows for efficient lookups by the key and prevents duplicates.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
// Define a struct with a key.
#[derive(Debug, PartialEq, Eq, Hash)]
struct MyItem {
id: String,
value: u32,
}
// Implement IdHashItem for the struct.
impl IdHashItem for MyItem {
// Keys can borrow from the item.
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
// Create an IdHashMap and insert items.
let mut map = IdHashMap::new();
map.insert_unique(MyItem { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(MyItem { id: "bar".to_string(), value: 20 }).unwrap();
// Look up items by their keys.
assert_eq!(map.get("foo").unwrap().value, 42);
assert_eq!(map.get("bar").unwrap().value, 20);
assert!(map.get("baz").is_none());Implementations§
Source§impl<T: IdHashItem> IdHashMap<T>
impl<T: IdHashItem> IdHashMap<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty IdHashMap.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let map: IdHashMap<Item> = IdHashMap::new();
assert!(map.is_empty());
assert_eq!(map.len(), 0);Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new IdHashMap with the given capacity.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let map: IdHashMap<Item> = IdHashMap::with_capacity(10);
assert!(map.capacity() >= 10);
assert!(map.is_empty());Source§impl<T: IdHashItem, S: BuildHasher> IdHashMap<T, S>
impl<T: IdHashItem, S: BuildHasher> IdHashMap<T, S>
Sourcepub const fn with_hasher(hasher: S) -> Self
pub const fn with_hasher(hasher: S) -> Self
Creates a new, empty IdHashMap with the given hasher.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
use std::collections::hash_map::RandomState;
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let hasher = RandomState::new();
let map: IdHashMap<Item, _> = IdHashMap::with_hasher(hasher);
assert!(map.is_empty());Sourcepub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
Creates a new IdHashMap with the given capacity and hasher.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
use std::collections::hash_map::RandomState;
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let hasher = RandomState::new();
let map: IdHashMap<Item, _> =
IdHashMap::with_capacity_and_hasher(10, hasher);
assert!(map.capacity() >= 10);
assert!(map.is_empty());Source§impl<T: IdHashItem, A: Clone + Allocator> IdHashMap<T, DefaultHashBuilder, A>
impl<T: IdHashItem, A: Clone + Allocator> IdHashMap<T, DefaultHashBuilder, A>
Sourcepub fn new_in(alloc: A) -> Self
pub fn new_in(alloc: A) -> Self
Creates a new empty IdHashMap using the given allocator.
Requires the allocator-api2 feature to be enabled.
§Examples
Using the bumpalo allocator:
use iddqd::{IdHashMap, IdHashItem, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> { &self.id }
id_upcast!();
}
// Define a new allocator.
let bump = bumpalo::Bump::new();
// Create a new IdHashMap using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> = IdHashMap::new_in(&bump);
assert!(map.is_empty());Sourcepub fn with_capacity_in(capacity: usize, alloc: A) -> Self
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self
Creates an empty IdHashMap with the specified capacity using the given
allocator.
Requires the allocator-api2 feature to be enabled.
§Examples
Using the bumpalo allocator:
use iddqd::{IdHashMap, IdHashItem, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> { &self.id }
id_upcast!();
}
// Define a new allocator.
let bump = bumpalo::Bump::new();
// Create a new IdHashMap with capacity using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> = IdHashMap::with_capacity_in(10, &bump);
assert!(map.capacity() >= 10);
assert!(map.is_empty());Source§impl<T: IdHashItem, S: BuildHasher, A: Clone + Allocator> IdHashMap<T, S, A>
impl<T: IdHashItem, S: BuildHasher, A: Clone + Allocator> IdHashMap<T, S, A>
Sourcepub fn with_hasher_in(hasher: S, alloc: A) -> Self
pub fn with_hasher_in(hasher: S, alloc: A) -> Self
Creates a new, empty IdHashMap with the given hasher and allocator.
Requires the allocator-api2 feature to be enabled.
§Examples
Using the bumpalo allocator:
use iddqd::{IdHashItem, IdHashMap, id_upcast};
use std::collections::hash_map::RandomState;
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
// Define a new allocator.
let bump = bumpalo::Bump::new();
let hasher = RandomState::new();
// Create a new IdHashMap with hasher using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> =
IdHashMap::with_hasher_in(hasher, &bump);
assert!(map.is_empty());Sourcepub fn with_capacity_and_hasher_in(capacity: usize, hasher: S, alloc: A) -> Self
pub fn with_capacity_and_hasher_in(capacity: usize, hasher: S, alloc: A) -> Self
Creates a new, empty IdHashMap with the given capacity, hasher, and
allocator.
Requires the allocator-api2 feature to be enabled.
§Examples
Using the bumpalo allocator:
use iddqd::{IdHashItem, IdHashMap, id_upcast};
use std::collections::hash_map::RandomState;
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
// Define a new allocator.
let bump = bumpalo::Bump::new();
let hasher = RandomState::new();
// Create a new IdHashMap with capacity and hasher using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> =
IdHashMap::with_capacity_and_hasher_in(10, hasher, &bump);
assert!(map.capacity() >= 10);
assert!(map.is_empty());Source§impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IdHashMap<T, S, A>
impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IdHashMap<T, S, A>
Sourcepub fn allocator(&self) -> &A
pub fn allocator(&self) -> &A
Returns the allocator.
Requires the allocator-api2 feature to be enabled.
§Examples
Using the bumpalo allocator:
use iddqd::{IdHashMap, IdHashItem, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> { &self.id }
id_upcast!();
}
// Define a new allocator.
let bump = bumpalo::Bump::new();
// Create a new IdHashMap using the allocator.
let map: IdHashMap<Item, _, &bumpalo::Bump> = IdHashMap::new_in(&bump);
let _allocator = map.allocator();Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the currently allocated capacity of the map.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let map: IdHashMap<Item> = IdHashMap::with_capacity(10);
assert!(map.capacity() >= 10);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map is empty.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
assert!(map.is_empty());
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
assert!(!map.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of items in the map.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
assert_eq!(map.len(), 0);
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
assert_eq!(map.len(), 1);
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
assert_eq!(map.len(), 2);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all items.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
assert_eq!(map.len(), 2);
map.clear();
assert!(map.is_empty());
assert_eq!(map.len(), 0);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 IdHashMap. The collection may reserve more space to
speculatively avoid frequent reallocations. After calling reserve,
capacity will be greater than or equal to self.len() + additional.
Does nothing if capacity is already sufficient.
§Panics
Panics if the new capacity overflows isize::MAX bytes, and
aborts the program in case of an allocation error. Use
try_reserve instead if you want to handle memory
allocation failure.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map: IdHashMap<Item> = IdHashMap::new();
map.reserve(100);
assert!(map.capacity() >= 100);Sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional more elements to be
inserted in the IdHashMap. The collection may reserve more space to
speculatively avoid frequent reallocations. After calling try_reserve,
capacity will be greater than or equal to self.len() + additional if
it returns Ok(()). Does nothing if capacity is already sufficient.
§Errors
If the capacity overflows, or the allocator reports a failure, then an error is returned.
§Notes
If reservation fails partway through, some internal structures may have already increased their capacity. The map remains in a valid state but may have uneven capacities across its internal structures.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map: IdHashMap<Item> = IdHashMap::new();
map.try_reserve(100).expect("allocation should succeed");
assert!(map.capacity() >= 100);Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map 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.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map: IdHashMap<Item> = IdHashMap::with_capacity(100);
map.insert_unique(Item { id: "foo".to_string(), value: 1 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 2 }).unwrap();
assert!(map.capacity() >= 100);
map.shrink_to_fit();
assert!(map.capacity() >= 2);Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the map with a lower limit. It will drop down no lower than the supplied limit while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
If the current capacity is less than the lower limit, this is a no-op.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map: IdHashMap<Item> = IdHashMap::with_capacity(100);
map.insert_unique(Item { id: "foo".to_string(), value: 1 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 2 }).unwrap();
assert!(map.capacity() >= 100);
map.shrink_to(10);
assert!(map.capacity() >= 10);
map.shrink_to(0);
assert!(map.capacity() >= 2);Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Iterates over the items in the map.
Similar to HashMap, the iteration order is arbitrary and not
guaranteed to be stable.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
let mut values: Vec<u32> = map.iter().map(|item| item.value).collect();
values.sort();
assert_eq!(values, vec![20, 42]);Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T, S, A> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T, S, A> ⓘ
Iterates over the items in the map, allowing for mutation.
Similar to HashMap, the iteration order is arbitrary and not
guaranteed to be stable.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
for mut item in map.iter_mut() {
item.value *= 2;
}
assert_eq!(map.get("foo").unwrap().value, 84);
assert_eq!(map.get("bar").unwrap().value, 40);Sourcepub fn insert_overwrite(&mut self, value: T) -> Option<T>
pub fn insert_overwrite(&mut self, value: T) -> Option<T>
Inserts a value into the map, removing and returning the conflicting item, if any.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
// First insertion returns None
let old = map.insert_overwrite(Item { id: "foo".to_string(), value: 42 });
assert!(old.is_none());
// Second insertion with same key returns the old value
let old = map.insert_overwrite(Item { id: "foo".to_string(), value: 100 });
assert_eq!(old.unwrap().value, 42);
assert_eq!(map.get("foo").unwrap().value, 100);Sourcepub fn insert_unique(&mut self, value: T) -> Result<(), DuplicateItem<T, &T>>
pub fn insert_unique(&mut self, value: T) -> Result<(), DuplicateItem<T, &T>>
Inserts a value into the set, returning an error if any duplicates were added.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
// First insertion succeeds
assert!(
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).is_ok()
);
// Second insertion with different key succeeds
assert!(
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).is_ok()
);
// Third insertion with duplicate key fails
assert!(
map.insert_unique(Item { id: "foo".to_string(), value: 100 }).is_err()
);Sourcepub fn contains_key<'a, Q>(&'a self, key1: &Q) -> bool
pub fn contains_key<'a, Q>(&'a self, key1: &Q) -> bool
Returns true if the map contains the given key.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
assert!(map.contains_key("foo"));
assert!(!map.contains_key("bar"));Sourcepub fn get<'a, Q>(&'a self, key: &Q) -> Option<&'a T>
pub fn get<'a, Q>(&'a self, key: &Q) -> Option<&'a T>
Gets a reference to the value associated with the given key.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
assert_eq!(map.get("foo").unwrap().value, 42);
assert!(map.get("bar").is_none());Sourcepub fn get_mut<'a, Q>(&'a mut self, key: &Q) -> Option<RefMut<'a, T, S>>
pub fn get_mut<'a, Q>(&'a mut self, key: &Q) -> Option<RefMut<'a, T, S>>
Gets a mutable reference to the value associated with the given key.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
if let Some(mut item) = map.get_mut("foo") {
item.value = 100;
}
assert_eq!(map.get("foo").unwrap().value, 100);
assert!(map.get_mut("bar").is_none());Sourcepub fn remove<'a, Q>(&'a mut self, key: &Q) -> Option<T>
pub fn remove<'a, Q>(&'a mut self, key: &Q) -> Option<T>
Removes an item from the map by its key.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
let removed = map.remove("foo");
assert_eq!(removed.unwrap().value, 42);
assert!(map.is_empty());
// Removing non-existent key returns None
assert!(map.remove("bar").is_none());Sourcepub fn entry<'a>(&'a mut self, key: T::Key<'_>) -> Entry<'a, T, S, A>
pub fn entry<'a>(&'a mut self, key: T::Key<'_>) -> Entry<'a, T, S, A>
Retrieves an entry by its key.
Due to borrow checker limitations, this always accepts an owned key rather than a borrowed form of it.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
// Use entry API for conditional insertion
map.entry("foo").or_insert(Item { id: "foo".to_string(), value: 42 });
map.entry("bar").or_insert(Item { id: "bar".to_string(), value: 20 });
assert_eq!(map.len(), 2);Sourcepub fn retain<'a, F>(&'a mut self, f: F)
pub fn retain<'a, F>(&'a mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all items T for which f(RefMut<T>) returns
false. The elements are visited in an arbitrary order.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
map.insert_unique(Item { id: "baz".to_string(), value: 99 }).unwrap();
// Retain only items where value is greater than 30
map.retain(|item| item.value > 30);
assert_eq!(map.len(), 2);
assert_eq!(map.get("foo").unwrap().value, 42);
assert_eq!(map.get("baz").unwrap().value, 99);
assert!(map.get("bar").is_none());Trait Implementations§
Source§impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> Extend<T> for IdHashMap<T, S, A>
The Extend implementation overwrites duplicates. In the future, there will
also be an extend_unique method that will return an error.
impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> Extend<T> for IdHashMap<T, S, A>
The Extend implementation overwrites duplicates. In the future, there will
also be an extend_unique method that will return an error.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
let new_items = vec![
Item { id: "foo".to_string(), value: 100 }, // overwrites existing
Item { id: "bar".to_string(), value: 20 }, // new item
];
map.extend(new_items);
assert_eq!(map.len(), 2);
assert_eq!(map.get("foo").unwrap().value, 100); // overwritten
assert_eq!(map.get("bar").unwrap().value, 20); // new
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
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)Source§impl<T: IdHashItem, S: Default + Clone + BuildHasher, A: Allocator + Default> FromIterator<T> for IdHashMap<T, S, A>
The FromIterator implementation for IdHashMap overwrites duplicate
items.
impl<T: IdHashItem, S: Default + Clone + BuildHasher, A: Allocator + Default> FromIterator<T> for IdHashMap<T, S, A>
The FromIterator implementation for IdHashMap overwrites duplicate
items.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let items = vec![
Item { id: "foo".to_string(), value: 42 },
Item { id: "bar".to_string(), value: 20 },
Item { id: "foo".to_string(), value: 100 }, // duplicate key, overwrites
];
let map: IdHashMap<Item> = items.into_iter().collect();
assert_eq!(map.len(), 2);
assert_eq!(map.get("foo").unwrap().value, 100); // last value wins
assert_eq!(map.get("bar").unwrap().value, 20);Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator for &'a IdHashMap<T, S, A>
impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator for &'a IdHashMap<T, S, A>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates an iterator over references to the items in the map.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
let mut values: Vec<u32> =
(&map).into_iter().map(|item| item.value).collect();
values.sort();
assert_eq!(values, vec![20, 42]);Source§impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator for &'a mut IdHashMap<T, S, A>
impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator for &'a mut IdHashMap<T, S, A>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates an iterator over mutable references to the items in the map.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
for mut item in &mut map {
item.value *= 2;
}
assert_eq!(map.get("foo").unwrap().value, 84);
assert_eq!(map.get("bar").unwrap().value, 40);Source§impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator for IdHashMap<T, S, A>
impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator for IdHashMap<T, S, A>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Consumes the map and creates an iterator over the owned items.
§Examples
use iddqd::{IdHashItem, IdHashMap, id_upcast};
#[derive(Debug, PartialEq, Eq, Hash)]
struct Item {
id: String,
value: u32,
}
impl IdHashItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdHashMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 20 }).unwrap();
let mut values: Vec<u32> = map.into_iter().map(|item| item.value).collect();
values.sort();
assert_eq!(values, vec![20, 42]);Source§impl<T: IdHashItem + PartialEq, S: Clone + BuildHasher, A: Allocator> PartialEq for IdHashMap<T, S, A>
impl<T: IdHashItem + PartialEq, S: Clone + BuildHasher, A: Allocator> PartialEq for IdHashMap<T, S, A>
impl<T: IdHashItem + Eq, S: Clone + BuildHasher, A: Allocator> Eq for IdHashMap<T, S, A>
Auto Trait Implementations§
impl<T, S, A> Freeze for IdHashMap<T, S, A>
impl<T, S, A> RefUnwindSafe for IdHashMap<T, S, A>
impl<T, S, A> Send for IdHashMap<T, S, A>
impl<T, S, A> Sync for IdHashMap<T, S, A>
impl<T, S, A> Unpin for IdHashMap<T, S, A>
impl<T, S, A> UnwindSafe for IdHashMap<T, S, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.