pub struct IdOrdMap<T> { /* private fields */ }Expand description
An ordered map where the keys are part of the values, based on a B-Tree.
The storage mechanism is a fast hash table of integer indexes to items, with the indexes stored in a B-Tree map.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
// Define a struct with a key.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct MyItem {
id: String,
value: u32,
}
// Implement IdOrdItem for the struct.
impl IdOrdItem for MyItem {
// Keys can borrow from the item.
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
// Create an IdOrdMap and insert items.
let mut map = IdOrdMap::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: IdOrdItem> IdOrdMap<T>
impl<T: IdOrdItem> IdOrdMap<T>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new, empty IdOrdMap.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let map: IdOrdMap<Item> = IdOrdMap::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 IdOrdMap with the given capacity.
The capacity will be used to initialize the underlying hash table.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let map: IdOrdMap<Item> = IdOrdMap::with_capacity(10);
assert!(map.capacity() >= 10);
assert!(map.is_empty());Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the currently allocated capacity of the map.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let map: IdOrdMap<Item> = IdOrdMap::with_capacity(10);
assert!(map.capacity() >= 10);Sourcepub fn from_iter_unique<I: IntoIterator<Item = T>>(
iter: I,
) -> Result<Self, DuplicateItem<T>>
pub fn from_iter_unique<I: IntoIterator<Item = T>>( iter: I, ) -> Result<Self, DuplicateItem<T>>
Constructs a new IdOrdMap from an iterator of values, rejecting
duplicates.
To overwrite duplicates instead, use IdOrdMap::from_iter.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem 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: 99 },
];
// Successful creation with unique keys
let map = IdOrdMap::from_iter_unique(items).unwrap();
assert_eq!(map.len(), 2);
assert_eq!(map.get("foo").unwrap().value, 42);
// Error with duplicate keys
let duplicate_items = vec![
Item { id: "foo".to_string(), value: 42 },
Item { id: "foo".to_string(), value: 99 },
];
assert!(IdOrdMap::from_iter_unique(duplicate_items).is_err());Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map is empty.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::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::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
assert_eq!(map.len(), 0);
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 99 }).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::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 99 }).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 IdOrdMap. 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.
Note: This only reserves capacity in the item storage. The internal
BTreeSet used for key-to-item mapping does not support capacity
reservation.
§Panics
Panics if the new capacity overflows isize::MAX bytes, and
aborts the program in case of an allocation error.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map: IdOrdMap<Item> = IdOrdMap::new();
map.reserve(100);
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.
Note: This only shrinks the item storage capacity. The internal
BTreeSet used for key-to-item mapping does not support capacity
control.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map: IdOrdMap<Item> = IdOrdMap::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.
Note: This only shrinks the item storage capacity. The internal
BTreeSet used for key-to-item mapping does not support capacity
control.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map: IdOrdMap<Item> = IdOrdMap::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 BTreeMap, the iteration is ordered by T::Key.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "charlie".to_string(), value: 30 }).unwrap();
map.insert_unique(Item { id: "alice".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bob".to_string(), value: 99 }).unwrap();
// Iteration is ordered by key
let mut iter = map.iter();
let item = iter.next().unwrap();
assert_eq!(item.id, "alice");
let item = iter.next().unwrap();
assert_eq!(item.id, "bob");
let item = iter.next().unwrap();
assert_eq!(item.id, "charlie");
assert!(iter.next().is_none());Sourcepub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> ⓘ
pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> ⓘ
Iterates over the items in the map, allowing for mutation.
Similar to BTreeMap, the iteration is ordered by T::Key.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bar".to_string(), value: 99 }).unwrap();
// Modify values through the mutable iterator
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, 198);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::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
// Successful insertion
assert!(
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).is_ok()
);
assert!(
map.insert_unique(Item { id: "bar".to_string(), value: 99 }).is_ok()
);
// Duplicate key
assert!(
map.insert_unique(Item { id: "foo".to_string(), value: 100 }).is_err()
);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::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
// First insertion - no conflict
let old = map.insert_overwrite(Item { id: "foo".to_string(), value: 42 });
assert!(old.is_none());
// Overwrite existing key - returns old value
let old = map.insert_overwrite(Item { id: "foo".to_string(), value: 99 });
assert!(old.is_some());
assert_eq!(old.unwrap().value, 42);
// Verify new value is in the map
assert_eq!(map.get("foo").unwrap().value, 99);Sourcepub fn contains_key<'a, Q>(&'a self, key: &Q) -> bool
pub fn contains_key<'a, Q>(&'a self, key: &Q) -> bool
Returns true if the map contains the given key.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::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::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::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>>
pub fn get_mut<'a, Q>(&'a mut self, key: &Q) -> Option<RefMut<'a, T>>
Gets a mutable reference to the item associated with the given key.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
if let Some(mut item) = map.get_mut("foo") {
item.value = 99;
}
assert_eq!(map.get("foo").unwrap().value, 99);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::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "foo".to_string(), value: 42 }).unwrap();
let removed = map.remove("foo");
assert!(removed.is_some());
assert_eq!(removed.unwrap().value, 42);
assert!(map.is_empty());
// Removing a non-existent key returns None
assert!(map.remove("bar").is_none());Sourcepub fn entry<'a>(&'a mut self, key: T::Key<'_>) -> Entry<'a, T>
pub fn entry<'a>(&'a mut self, key: T::Key<'_>) -> Entry<'a, T>
Retrieves an entry by its key.
Due to borrow checker limitations, this always accepts an owned key rather than a borrowed form.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_ord_map, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
// Insert via vacant entry
match map.entry("foo") {
id_ord_map::Entry::Vacant(entry) => {
entry.insert(Item { id: "foo".to_string(), value: 42 });
}
id_ord_map::Entry::Occupied(_) => {}
}
// Update via occupied entry
match map.entry("foo") {
id_ord_map::Entry::Occupied(mut entry) => {
entry.get_mut().value = 99;
}
id_ord_map::Entry::Vacant(_) => {}
}
assert_eq!(map.get("foo").unwrap().value, 99);Sourcepub fn first(&self) -> Option<&T>
pub fn first(&self) -> Option<&T>
Returns the first item in the map. The key of this item is the minimum key in the map.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "charlie".to_string(), value: 30 }).unwrap();
map.insert_unique(Item { id: "alice".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bob".to_string(), value: 99 }).unwrap();
// First item has the minimum key.
let first = map.first().unwrap();
assert_eq!(first.id, "alice");
assert_eq!(first.value, 42);
// Empty map returns None.
let empty_map: IdOrdMap<Item> = IdOrdMap::new();
assert!(empty_map.first().is_none());Sourcepub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, T>>
pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, T>>
Returns the first entry in the map for in-place manipulation. The key of this entry is the minimum key in the map.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "charlie".to_string(), value: 30 }).unwrap();
map.insert_unique(Item { id: "alice".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bob".to_string(), value: 99 }).unwrap();
// Modify the first entry.
if let Some(mut entry) = map.first_entry() {
entry.get_mut().value = 100;
}
assert_eq!(map.get("alice").unwrap().value, 100);Sourcepub fn pop_first(&mut self) -> Option<T>
pub fn pop_first(&mut self) -> Option<T>
Removes and returns the first element in the map. The key of this element is the minimum key in the map.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "charlie".to_string(), value: 30 }).unwrap();
map.insert_unique(Item { id: "alice".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bob".to_string(), value: 99 }).unwrap();
// Remove the first element.
let first = map.pop_first().unwrap();
assert_eq!(first.id, "alice");
assert_eq!(first.value, 42);
assert_eq!(map.len(), 2);
// Remove the next element.
let first = map.pop_first().unwrap();
assert_eq!(first.id, "bob");
// Empty map returns None.
map.pop_first();
assert!(map.pop_first().is_none());Sourcepub fn last(&self) -> Option<&T>
pub fn last(&self) -> Option<&T>
Returns the last item in the map. The key of this item is the maximum key in the map.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "charlie".to_string(), value: 30 }).unwrap();
map.insert_unique(Item { id: "alice".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bob".to_string(), value: 99 }).unwrap();
// Last item has the maximum key.
let last = map.last().unwrap();
assert_eq!(last.id, "charlie");
assert_eq!(last.value, 30);
// Empty map returns None.
let empty_map: IdOrdMap<Item> = IdOrdMap::new();
assert!(empty_map.last().is_none());Sourcepub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, T>>
pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, T>>
Returns the last entry in the map for in-place manipulation. The key of this entry is the maximum key in the map.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "charlie".to_string(), value: 30 }).unwrap();
map.insert_unique(Item { id: "alice".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bob".to_string(), value: 99 }).unwrap();
// Modify the last entry.
if let Some(mut entry) = map.last_entry() {
entry.get_mut().value = 200;
}
assert_eq!(map.get("charlie").unwrap().value, 200);Sourcepub fn pop_last(&mut self) -> Option<T>
pub fn pop_last(&mut self) -> Option<T>
Removes and returns the last element in the map. The key of this element is the maximum key in the map.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::new();
map.insert_unique(Item { id: "charlie".to_string(), value: 30 }).unwrap();
map.insert_unique(Item { id: "alice".to_string(), value: 42 }).unwrap();
map.insert_unique(Item { id: "bob".to_string(), value: 99 }).unwrap();
// Remove the last element.
let last = map.pop_last().unwrap();
assert_eq!(last.id, "charlie");
assert_eq!(last.value, 30);
assert_eq!(map.len(), 2);
// Remove the next element.
let last = map.pop_last().unwrap();
assert_eq!(last.id, "bob");
// Empty map returns None.
map.pop_last();
assert!(map.pop_last().is_none());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 ascending key order.
§Examples
use iddqd::{IdOrdItem, IdOrdMap, id_upcast};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Item {
id: String,
value: u32,
}
impl IdOrdItem for Item {
type Key<'a> = &'a str;
fn key(&self) -> Self::Key<'_> {
&self.id
}
id_upcast!();
}
let mut map = IdOrdMap::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: IdOrdItem> Extend<T> for IdOrdMap<T>
The Extend implementation overwrites duplicates. In the future, there will
also be an extend_unique method that will return an error.
impl<T: IdOrdItem> Extend<T> for IdOrdMap<T>
The Extend implementation overwrites duplicates. In the future, there will
also be an extend_unique method that will return an error.
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: IdOrdItem> FromIterator<T> for IdOrdMap<T>
The FromIterator implementation for IdOrdMap overwrites duplicate
items.
impl<T: IdOrdItem> FromIterator<T> for IdOrdMap<T>
The FromIterator implementation for IdOrdMap overwrites duplicate
items.
To reject duplicates, use IdOrdMap::from_iter_unique.
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: IdOrdItem> IntoIterator for &'a IdOrdMap<T>
impl<'a, T: IdOrdItem> IntoIterator for &'a IdOrdMap<T>
Source§impl<'a, T: IdOrdItem> IntoIterator for &'a mut IdOrdMap<T>
impl<'a, T: IdOrdItem> IntoIterator for &'a mut IdOrdMap<T>
Source§impl<T: IdOrdItem> IntoIterator for IdOrdMap<T>
impl<T: IdOrdItem> IntoIterator for IdOrdMap<T>
impl<T: IdOrdItem + Eq> Eq for IdOrdMap<T>
Auto Trait Implementations§
impl<T> Freeze for IdOrdMap<T>
impl<T> RefUnwindSafe for IdOrdMap<T>where
T: RefUnwindSafe,
impl<T> Send for IdOrdMap<T>where
T: Send,
impl<T> Sync for IdOrdMap<T>where
T: Sync,
impl<T> Unpin for IdOrdMap<T>where
T: Unpin,
impl<T> UnwindSafe for IdOrdMap<T>where
T: UnwindSafe,
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.