pub struct IndexMap<T> { /* private fields */ }
Expand description
A map of usize
to value, which allows efficient O(1) inserts, O(1) indexing and O(1) removal.
See crate level documentation for more information.
Implementations§
Source§impl<T> IndexMap<T>
impl<T> IndexMap<T>
Sourcepub fn keys(&self) -> Keys<'_, T> ⓘ
pub fn keys(&self) -> Keys<'_, T> ⓘ
An iterator visiting all keys in ascending order.
The iterator element type is usize
.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
map.insert("a");
map.insert("b");
map.insert("c");
for key in map.keys() {
println!("{}", key);
}
Sourcepub fn values(&self) -> Values<'_, T> ⓘ
pub fn values(&self) -> Values<'_, T> ⓘ
An iterator visiting all values in ascending order of their keys.
The iterator element type is &T
.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
map.insert("a");
map.insert("b");
map.insert("c");
for val in map.values() {
println!("{}", val);
}
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, T> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, T> ⓘ
An iterator visiting all values mutably in ascending order of their keys.
The iterator element type is &mut T
.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
map.insert(2);
map.insert(4);
map.insert(6);
for val in map.values_mut() {
*val *= 2;
}
for val in map.values() {
println!("{}", val);
}
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
An iterator visiting all key-value pairs in ascending order of keys.
The iterator element type is (usize, &T)
.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
map.insert("a");
map.insert("b");
map.insert("c");
for (key, val) in map.iter() {
println!("key: {} val: {}", key, val);
}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
An iterator visiting all key-value pairs in ascending order of keys, with mutable references
to the values.
The iterator element type is (usize, &mut T)
.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
map.insert(2);
map.insert(4);
map.insert(6);
// Update all values
for (_, val) in map.iter_mut() {
*val *= 2;
}
for (key, val) in map.iter() {
println!("key: {} val: {}", key, val);
}
Sourcepub fn drain(&mut self) -> Drain<'_, T> ⓘ
pub fn drain(&mut self) -> Drain<'_, T> ⓘ
Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
§Examples
use index_map::IndexMap;
let mut a = IndexMap::new();
a.insert("a");
a.insert("b");
let mut iter = a.drain();
assert_eq!(iter.next(), Some((0, "a")));
assert_eq!(iter.next(), Some((1, "b")));
assert_eq!(iter.next(), None);
assert!(a.is_empty());
Source§impl<T> IndexMap<T>
impl<T> IndexMap<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new IndexMap
.
It initially has a capacity of 0, and won’t allocate until first inserted into.
§Examples
use index_map::IndexMap;
let mut map: IndexMap<&str> = IndexMap::new();
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty IndexMap
with the specified capacity.
The map will be able to hold at least capacity elements without reallocating. If capacity is 0, the map will not allocate.
§Examples
use index_map::IndexMap;
let mut map: IndexMap<&str> = IndexMap::with_capacity(10);
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements map can hold without reallocating.
§Examples
use index_map::IndexMap;
let mut map: IndexMap<&str> = IndexMap::with_capacity(10);
assert!(map.capacity() >= 10);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements present in the map.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
assert_eq!(map.len(), 0);
map.insert("a");
assert_eq!(map.len(), 1);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the map is empty.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
assert!(map.is_empty());
map.insert("a");
assert!(!map.is_empty());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, dropping all key-value pairs. Keeps the allocated memory for reuse.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
map.insert("a");
map.clear();
assert!(map.is_empty());
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 IndexMap
The collection may reserve more space to avoid frequent reallocations.
§Panics
Panics if the new capacity exceeds isize::MAX
bytes.
§Examples
use index_map::IndexMap;
let mut map: IndexMap<&str> = IndexMap::new();
map.reserve(10);
assert!(map.capacity() >= 10);
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 to keep keys valid.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::with_capacity(100);
map.insert("a");
map.insert("b");
assert!(map.capacity() >= 100);
map.shrink_to_fit();
assert!(map.capacity() >= 2);
Sourcepub fn contains_key(&self, index: usize) -> bool
pub fn contains_key(&self, index: usize) -> bool
Returns true
if the map contains a value for the specified key.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
map.insert("a");
assert_eq!(map.contains_key(0), true);
assert_eq!(map.contains_key(1), false);
Sourcepub fn insert(&mut self, value: T) -> usize
pub fn insert(&mut self, value: T) -> usize
Inserts a value into the map, returning the generated key, for it.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
assert_eq!(map.insert("a"), 0);
assert_eq!(map.is_empty(), false);
let b = map.insert("b");
assert_eq!(map[b], "b");
Sourcepub fn remove(&mut self, index: usize) -> Option<T>
pub fn remove(&mut self, index: usize) -> Option<T>
Removes a key from the map, returning the value at the key if the key was previously in the map.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
let a = map.insert("a");
assert_eq!(map.remove(a), Some("a"));
assert_eq!(map.remove(a), None);
Sourcepub fn remove_entry(&mut self, index: usize) -> Option<(usize, T)>
pub fn remove_entry(&mut self, index: usize) -> Option<(usize, T)>
Removes a key from the map, returning the key and value if the key was previously in the map.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
let a = map.insert("a");
assert_eq!(map.remove_entry(a), Some((0, "a")));
assert_eq!(map.remove(a), None);
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the value corresponding to the key.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
map.insert("a");
assert_eq!(map.get(0), Some(&"a"));
assert_eq!(map.get(1), None);
Sourcepub fn get_key_value(&self, index: usize) -> Option<(usize, &T)>
pub fn get_key_value(&self, index: usize) -> Option<(usize, &T)>
Returns the key-value pair corresponding to the key.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
map.insert("a");
assert_eq!(map.get_key_value(0), Some((0, &"a")));
assert_eq!(map.get_key_value(1), None);
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the value corresponding to the key.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
let a = map.insert("a");
if let Some(x) = map.get_mut(a) {
*x = "b";
}
assert_eq!(map[a], "b");
Sourcepub fn retain<P>(&mut self, predicate: P)
pub fn retain<P>(&mut self, predicate: P)
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v)
such that f(k, &mut v)
returns false
.
§Examples
use index_map::IndexMap;
let mut map = IndexMap::new();
for i in 0..6 {
map.insert(i*2);
}
map.retain(|k, _| k % 2 == 0);
assert_eq!(map.len(), 3);