Struct KeyedVecSet

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

A vector-based map implementation which retains the order of inserted entries.

Internally it is represented as a Vec<(K, V)> to support keys that are neither Hash nor Ord.

Implementations§

Source§

impl<K, V> KeyedVecSet<K, V>

Source

pub const fn new() -> Self

Create a new map. (Does not allocate.)

§Examples
use vecset::KeyedVecSet;

let mut map: KeyedVecSet<i32, &str> = KeyedVecSet::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Create a new map with capacity for capacity key-value pairs. (Does not allocate if capacity is zero.)

§Examples
use vecset::KeyedVecSet;

let mut map: KeyedVecSet<i32, &str> = KeyedVecSet::with_capacity(10);
assert_eq!(map.len(), 0);
assert!(map.capacity() >= 10);
Source

pub fn capacity(&self) -> usize

Returns the number of entries the map can hold without reallocating.

§Examples
use vecset::KeyedVecSet;

let mut map: KeyedVecSet<i32, (i32, &str)> = KeyedVecSet::with_capacity(10);
assert_eq!(map.capacity(), 10);
Source

pub fn len(&self) -> usize

Returns the number of entries in the map, also referred to as its ‘length’.

§Examples
use vecset::KeyedVecSet;

let mut a = KeyedVecSet::<u32, (u32, &str)>::new();
assert_eq!(a.len(), 0);
a.insert((1, "a"));
assert_eq!(a.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no entries.

§Examples
use vecset::KeyedVecSet;

let mut a = KeyedVecSet::<i32, (i32, &str)>::new();
assert!(a.is_empty());
a.insert((1, "a"));
assert!(!a.is_empty());
Source

pub fn clear(&mut self)

Clears the map, removing all entries.

§Examples
use vecset::KeyedVecSet;

let mut a = KeyedVecSet::<i32, (i32, &str)>::new();
a.insert((1, "a"));
a.clear();
assert!(a.is_empty());
Source

pub fn truncate(&mut self, len: usize)

Shortens the map, keeping the first len key-value pairs and dropping the rest.

If len is greater than the map’s current length, this has no effect.

§Examples

Truncating a four element map to two elements:

use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2), ("c", 3), ("d", 4)]);
map.truncate(2);
assert_eq!(map, KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2)]));

No truncation occurs when len is greater than the map’s current length:

use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2), ("c", 3), ("d", 4)]);
map.truncate(8);
assert_eq!(map, KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2), ("c", 3), ("d", 4)]));
Source

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

Reserves capacity for at least additional more elements to be inserted in the given KeyedVecSet<K, V>. 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 exceeds isize::MAX bytes.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1)]);
map.reserve(10);
assert!(map.capacity() >= 11);
Source

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

Reserves the minimum capacity for at least additional more elements to be inserted in the given KeyedVecSet<K, V>. Does nothing if the capacity is already sufficient.

Note that the allocator may give the collection more space than it requests. Therefore, capacity can not be relied upon to be precisely minimal. Prefer Self::reserve if future insertions are expected.

§Panics

Panics if the new capacity exceeds isize::MAX bytes.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1)]);
map.reserve_exact(10);
assert!(map.capacity() >= 11);
Source

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

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) for which f(&k, &mut v) returns false.

§Examples
use vecset::KeyedVecSet;

let mut map: KeyedVecSet<i32, (i32, i32)> = (0..8).map(|x| (x, x*10)).collect();
map.retain(|&v| v.0 % 2 == 0);
assert_eq!(map.len(), 4);
Source

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 vecset::KeyedVecSet;

let mut map: KeyedVecSet<i32, (i32, i32)> = KeyedVecSet::with_capacity(100);
map.insert((1, 2));
map.insert((3, 4));
assert!(map.capacity() >= 100);
map.shrink_to_fit();
assert!(map.capacity() >= 2);
Source

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 vecset::KeyedVecSet;

let mut map: KeyedVecSet<i32, (i32, i32)> = KeyedVecSet::with_capacity(100);
map.insert((1, 2));
map.insert((3, 4));
assert!(map.capacity() >= 100);
map.shrink_to(10);
assert!(map.capacity() >= 10);
map.shrink_to(0);
assert!(map.capacity() >= 2);
Source

pub fn split_off(&mut self, at: usize) -> KeyedVecSet<K, V>

Splits the map into two at the given index.

Returns a newly allocated map containing the key-value pairs in the range [at, len). After the call, the original map will be left containing the key-value pairs [0, at) with its previous capacity unchanged.

§Panics

Panics if at > len.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2), ("c", 3)]);
let map2 = map.split_off(1);
assert_eq!(map, KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1)]));
assert_eq!(map2, KeyedVecSet::<&str, (&str, u32)>::from_iter([("b", 2), ("c", 3)]));
Source

pub fn drain<R>(&mut self, range: R) -> Drain<'_, V>
where R: RangeBounds<usize>,

Removes the specified range from the vector in bulk, returning all removed elements as an iterator. If the iterator is dropped before being fully consumed, it drops the remaining removed elements.

The returned iterator keeps a mutable borrow on the vector to optimize its implementation.

§Panics

Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.

§Examples
use vecset::KeyedVecSet;

let mut v = KeyedVecSet::<&str, (&str, i32)>::from_iter([("a", 1), ("b", 2), ("c", 3)]);
let u: KeyedVecSet<_, _> = v.drain(1..).collect();
assert_eq!(v, KeyedVecSet::<&str, (&str, i32)>::from([("a", 1)]));
assert_eq!(u, KeyedVecSet::<&str, (&str, i32)>::from([("b", 2), ("c", 3)]));

// A full range clears the vector, like `clear()` does
v.drain(..);
assert_eq!(v, KeyedVecSet::new());
Source

pub fn as_slice(&self) -> &[V]

Extracts a slice containing the map entries.

use vecset::KeyedVecSet;

let map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("b", 2), ("a", 1), ("c", 3)]);
let slice = map.as_slice();
assert_eq!(slice, [("a", 1), ("b", 2), ("c", 3)]);
Source

pub unsafe fn as_mut_slice(&mut self) -> &mut [V]

Extracts a slice containing the map entries.

§Safety

Changing key may cause the map to be unsorted or have duplicate keys. Those states will make KeyedVecSet working unspecified way.

use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("b", 2), ("a", 1), ("c", 3)]);
let slice = unsafe {
    let slice = map.as_mut_slice();
    slice[0].1 = 10;
    &*slice
};
assert_eq!(slice, [("a", 10), ("b", 2), ("c", 3)]);
Source

pub fn to_vec(&self) -> Vec<V>
where V: Clone,

Copies the map entries into a new Vec<(K, V)>.

use vecset::KeyedVecSet;

let map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("b", 2), ("a", 1), ("c", 3)]);
let vec = map.to_vec();
assert_eq!(vec, [("a", 1), ("b", 2), ("c", 3)]);
// Here, `map` and `vec` can be modified independently.
Source

pub fn into_vec(self) -> Vec<V>

Takes ownership of the map and returns its entries as a Vec<(K, V)>.

use vecset::KeyedVecSet;

let map = KeyedVecSet::<&str, (&str, i32)>::from([("b", 2), ("a", 1), ("c", 3)]);
let vec = map.into_vec();
assert_eq!(vec, [("a", 1), ("b", 2), ("c", 3)]);
Source

pub unsafe fn from_sorted_vec(base: Vec<V>) -> Self

Takes ownership of provided vector and converts it into KeyedVecSet.

§Safety

The vector must be sorted and have no duplicate keys. One way to guarantee it is to sort the vector (e.g. by using [T]::sort_by_key) and then drop duplicate keys (e.g. by using Vec::dedup_by_key).

§Example
use vecset::KeyedVecSet;

let mut vec = vec![("b", 2), ("a", 1), ("c", 3), ("b", 4)];
vec.sort_by_key(|slot| slot.0);
vec.dedup_by_key(|slot| slot.0);
// SAFETY: We've just deduplicated the vector.
let map = unsafe { KeyedVecSet::from_sorted_vec(vec) };

assert_eq!(map, KeyedVecSet::<&str, (&str, i32)>::from([("b", 2), ("a", 1), ("c", 3)]));
Source§

impl<K, V> KeyedVecSet<K, V>

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q> + Ord, Q: Ord + ?Sized, V: Keyed<K>,

Return true if an equivalent to key exists in the map.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<i32, (i32, &str)>::new();
map.insert((1, "a"));
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
Source

pub fn first(&self) -> Option<&V>

Get the first key-value pair.

use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2)]);
assert_eq!(map.first(), Some(&("a", 1)));
Source

pub unsafe fn first_mut(&mut self) -> Option<&mut V>

Get the first key-value pair, with mutable access to the value.

§Safety

Changing key may cause the map to be unsorted or have duplicate keys. Those states will make KeyedVecSet working unspecified way.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2)]);

unsafe {
    if let Some(v) = map.first_mut() {
        v.1 = v.1 + 10;
    }
}
assert_eq!(map.first(), Some(&("a", 11)));
Source

pub fn last(&self) -> Option<&V>

Get the last key-value pair.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2)]);
assert_eq!(map.last(), Some(&("b", 2)));
map.pop();
map.pop();
assert_eq!(map.last(), None);
Source

pub unsafe fn last_mut(&mut self) -> Option<&mut V>

Get the last key-value pair, with mutable access to the value.

§Safety

Changing key may cause the map to be unsorted or have duplicate keys. Those states will make KeyedVecSet working unspecified way.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2)]);

unsafe {
    if let Some(v) = map.last_mut() {
        v.1 += 10;
    }
}
assert_eq!(map.last(), Some(&("b", 12)));
Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q> + Ord, Q: Ord + ?Sized, V: Keyed<K>,

Return a reference to the value stored for key, if it is present, else None.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<i32, (i32, &str)>::new();
map.insert((1, "a"));
assert_eq!(map.get(&1), Some(&(1, "a")));
assert_eq!(map.get(&2), None);
Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q> + Ord, Q: Ord + ?Sized, V: Keyed<K>,

Return a mutable reference to the value stored for key, if it is present, else None.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<i32, (i32, &str)>::new();
map.insert((1, "a"));
if let Some(x) = map.get_mut(&1) {
    x.1 = "b";
}
assert_eq!(map[&1], (1, "b"));
Source

pub fn get_index(&self, index: usize) -> Option<&V>

Return references to the key-value pair stored at index, if it is present, else None.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<i32, (i32, &str)>::new();
map.insert((1, "a"));
assert_eq!(map.get_index(0), Some(&(1, "a")));
assert_eq!(map.get_index(1), None);
Source

pub unsafe fn get_index_mut(&mut self, index: usize) -> Option<&mut V>

Return a reference to the key and a mutable reference to the value stored at index, if it is present, else None.

§Safety

Changing key may cause the map to be unsorted or have duplicate keys. Those states will make KeyedVecSet working unspecified way.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<i32, (i32, &str)>::new();
map.insert((1, "a"));
unsafe {
    if let Some((_, v)) = map.get_index_mut(0) {
        *v = "b";
    }
}
assert_eq!(map[0].1, "b");
Source

pub unsafe fn get_unchecked(&self, index: usize) -> &V

Returns a reference to an element or subslice, without doing bounds checking.

For a safe alternative see get_index.

§Safety

Calling this method with an out-of-bounds index is [undefined behavior] even if the resulting reference is not used.

Source

pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut V

Returns a mutable reference to an element or subslice, without doing bounds checking.

For a safe alternative see get_index_mut.

§Safety

Calling this method with an out-of-bounds index is [undefined behavior] even if the resulting reference is not used.

Source

pub fn get_full<Q>(&self, key: &Q) -> Result<(usize, &V), usize>
where K: Borrow<Q> + Ord, Q: Ord + ?Sized, V: Keyed<K>,

Return the index and references to the key-value pair stored for key, if it is present, else None.

§Errors

Returns the index where the key would be inserted if it doesn’t exist.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<i32, (i32, &str)>::new();
map.insert((1, "a"));
assert_eq!(map.get_full(&1), Ok((0, &(1, "a"))));
assert_eq!(map.get_full(&2), Err(1));
Source

pub unsafe fn get_full_mut<Q>( &mut self, key: &Q, ) -> Result<(usize, &mut V), usize>
where K: Borrow<Q> + Ord, Q: Ord + ?Sized, V: Keyed<K>,

Return the index, a reference to the key and a mutable reference to the value stored for key, if it is present, else None.

§Safety

Changing key may cause the map to be unsorted or have duplicate keys. Those states will make KeyedVecSet working unspecified way.

§Errors

Returns the index where the key would be inserted if it doesn’t exist.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<i32, (i32, &str)>::new();
map.insert((1, "a"));

unsafe {
    if let Ok((_, v)) = map.get_full_mut(&1) {
        v.1 = "b";
    }
}
assert_eq!(map.get(&1), Some(&(1, "b")));

Return item index, if it exists in the map.

§Errors

Returns the index where the key would be inserted if it doesn’t exist.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, i32)>::new();
map.insert(("a", 10));
map.insert(("b", 20));
assert_eq!(map.binary_search("a"), Ok(0));
assert_eq!(map.binary_search("b"), Ok(1));
assert_eq!(map.binary_search("c"), Err(2));
Source§

impl<K, V> KeyedVecSet<K, V>

Source

pub fn pop(&mut self) -> Option<V>

Removes the last element from the map and returns it, or None if it is empty.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2)]);
assert_eq!(map.pop(), Some(("b", 2)));
assert_eq!(map.pop(), Some(("a", 1)));
assert!(map.is_empty());
assert_eq!(map.pop(), None);
Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q> + Ord, Q: Ord + ?Sized, V: Keyed<K>,

Remove the key-value pair equivalent to key and return its value.

Like Vec::remove, the pair is removed by shifting all of the elements that follow it, preserving their relative order. This perturbs the index of all of those elements!

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<u32, (u32, &str)>::from_iter([(1, "a"), (2, "b"), (3, "c"), (4, "d")]);
assert_eq!(map.remove(&2), Some((2, "b")));
assert_eq!(map.remove(&2), None);
assert_eq!(map, KeyedVecSet::<u32, (u32, &str)>::from([(1, "a"), (3, "c"), (4, "d")]));
Source

pub fn remove_index(&mut self, index: usize) -> V

Removes and returns the key-value pair at position index within the map, shifting all elements after it to the left.

§Panics

Panics if index is out of bounds.

§Examples
use vecset::KeyedVecSet;

let mut v = KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("b", 2), ("c", 3)]);
assert_eq!(v.remove_index(1), ("b", 2));
assert_eq!(v, KeyedVecSet::<&str, (&str, u32)>::from_iter([("a", 1), ("c", 3)]));
Source§

impl<K, V> KeyedVecSet<K, V>
where K: Ord, V: Keyed<K>,

Source

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

Insert a key-value pair in the map.

If an equivalent key already exists in the map: the key remains and retains in its place in the order, its corresponding value is updated with value and the older value is returned inside Some(_).

If no equivalent key existed in the map: the new key-value pair is inserted, last in order, and None is returned.

See also entry if you want to insert or modify or if you need to get the index of the corresponding key-value pair.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<i32, (i32, &str)>::new();
assert_eq!(map.insert((37, "a")), None);
assert_eq!(map.is_empty(), false);

map.insert((37, "b"));
assert_eq!(map.insert((37, "c")), Some((37, "b")));
assert_eq!(map[&37].1, "c");
Source

pub fn insert_index(&mut self, index: usize, value: V)

Insert a key-value pair at the given index.

§Panics

Panics if:

  • index > len()
  • The key already exists at a different position
  • The insertion would break the sorted order
§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, i32)>::new();
map.insert_index(0, ("b", 2));
map.insert_index(1, ("c", 3));
map.insert_index(0, ("a", 1));
assert_eq!(map["a"].1, 1);
assert_eq!(map["b"].1, 2);
assert_eq!(map["c"].1, 3);
Source

pub unsafe fn insert_index_unchecked(&mut self, index: usize, value: V)

Insert a key-value pair in the map without sorting validation.

The given index must be vacant.

See also replace_index_unchecked if you want to replace existing slot.

§Safety

The caller must ensure that inserting the value at the given index maintains the sorted order. Failing to do so may result in a map with unsorted keys or duplicate keys.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, i32)>::new();
unsafe {
    map.insert_index_unchecked(0, ("b", 2));
    map.insert_index_unchecked(1, ("c", 3));
    map.insert_index_unchecked(0, ("a", 1));
}
assert_eq!(map["b"].1, 2);
Source

pub fn replace_index(&mut self, index: usize, value: V) -> V

Replace a key-value pair at the given index.

§Panics

Panics if:

  • index >= len()
  • The new key is different from the existing key at that index
  • The replacement would break the sorted order
§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, i32)>::from([("a", 10), ("b", 20), ("c", 30)]);
let replaced = map.replace_index(1, ("b", 15));
assert_eq!(replaced.1, 20);
assert_eq!(map["b"].1, 15);
Source

pub unsafe fn replace_index_unchecked(&mut self, index: usize, value: V) -> V

Replace a key-value pair in the map without sorting validation.

The given index must be occupied.

See also insert_index_unchecked if you want to insert a new key-value pair.

§Safety

The caller must ensure that the new value has the same key as the existing value at the given index and that the replacement maintains the sorted order. Failing to do so may result in a map with unsorted keys or duplicate keys.

§Examples
use vecset::KeyedVecSet;

let mut map = unsafe { KeyedVecSet::<&str, (&str, i32)>::from_sorted_vec(vec![("a", 10), ("b", 20), ("c", 30)]) };
let replaced = unsafe { map.replace_index_unchecked(1, ("b", 15)) };
assert_eq!(replaced.1, 20);
assert_eq!(map["b"].1, 15);
Source

pub fn insert_full(&mut self, value: V) -> (usize, Option<V>)

Insert a key-value pair in the map, and get their index.

If an equivalent key already exists in the map: the key remains and retains in its place in the order, its corresponding value is updated with value and the older value is returned inside (index, Some(_)).

If no equivalent key existed in the map: the new key-value pair is inserted, last in order, and (index, None) is returned.

See also entry if you want to insert or modify or if you need to get the index of the corresponding key-value pair.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, i32)>::new();
assert_eq!(map.insert_full(("a", 1)), (0, None));
assert_eq!(map.insert_full(("b", 2)), (1, None));
assert_eq!(map.insert_full(("b", 3)), (1, Some(("b", 2))));
assert_eq!(map["b"].1, 3);
Source

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

Get the given key’s corresponding entry in the map for insertion and/or in-place manipulation.

§Examples
use vecset::KeyedVecSet;

let mut letters = KeyedVecSet::<char, (char, usize)>::new();

for ch in "a short treatise on fungi".chars() {
    unsafe { letters.entry(ch).and_modify(|pair| pair.1 += 1) }.or_insert((ch, 1));
}

assert_eq!(letters[&'s'].1, 2);
assert_eq!(letters[&'t'].1, 3);
assert_eq!(letters[&'u'].1, 1);
assert_eq!(letters.get(&'y'), None);
Source

pub unsafe fn entry_index_unchecked( &mut self, index: Result<usize, usize>, key: K, ) -> Entry<'_, K, V>

Get the entry in the map for insertion and/or in-place manipulation by given index and key. The index must be a valid result of Self::binary_search.

§Safety

The caller must ensure that the provided index is a valid result of binary_search for the given key. An incorrect index may lead to undefined behavior.

§Examples
use vecset::KeyedVecSet;

let mut letters = KeyedVecSet::<char, (char, usize)>::new();

for ch in "a short treatise on fungi".chars() {
    unsafe { letters.entry(ch).and_modify(|pair| pair.1 += 1) }.or_insert((ch, 1));
}

assert_eq!(letters[&'s'].1, 2);
assert_eq!(letters[&'t'].1, 3);
assert_eq!(letters[&'u'].1, 1);
assert_eq!(letters.get(&'y'), None);
Source§

impl<K, V: Keyed<K>> KeyedVecSet<K, V>

Source

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

An iterator visiting all key-value pairs in insertion order. The iterator element type is (&'a K, &'a V).

§Examples
use vecset::KeyedVecSet;

let map = KeyedVecSet::<&str, (&str, i32)>::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

for (key, val) in map.iter() {
    println!("key: {key} val: {val}");
}
Source

pub unsafe fn iter_mut(&mut self) -> impl Iterator<Item = &mut V>

An iterator visiting all key-value pairs in insertion order, with mutable references to the values. The iterator element type is (&'a K, &'a mut V).

§Safety

Changing key may cause the map to be unsorted or have duplicate keys. Those states will make KeyedVecSet working unspecified way.

§Examples
use vecset::KeyedVecSet;

let mut map = KeyedVecSet::<&str, (&str, i32)>::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

// Update all values
unsafe {
    for (_, val) in map.iter_mut() {
        *val *= 2;
    }
}

for (key, val) in &map {
    println!("key: {key} val: {val}");
}
Source

pub fn keys(&self) -> impl Iterator<Item = &K> + '_

An iterator visiting all keys in insertion order. The iterator element type is &'a K.

§Examples
use vecset::KeyedVecSet;

let map = KeyedVecSet::<&str, (&str, i32)>::from([
    ("a", 1),
    ("b", 2),
    ("c", 3),
]);

for key in map.keys() {
    println!("{key}");
}

Trait Implementations§

Source§

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

Source§

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

Returns a duplicate 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: Debug, V: Debug> Debug for KeyedVecSet<K, V>

Source§

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

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

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

Source§

fn default() -> Self

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

impl<'de, K, V> Deserialize<'de> for KeyedVecSet<K, V>
where K: Deserialize<'de> + Ord, V: Deserialize<'de> + Keyed<K>,

Available on crate feature serde only.
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<'a, K, V> Extend<&'a V> for KeyedVecSet<K, V>
where K: Ord, V: Clone + Keyed<K> + 'a,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = &'a V>,

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, V> Extend<V> for KeyedVecSet<K, V>
where K: Ord, V: Keyed<K>,

Source§

fn extend<I>(&mut self, iterable: I)
where I: IntoIterator<Item = V>,

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, V> From<&[V]> for KeyedVecSet<K, V>
where K: Ord, V: Clone + Keyed<K>,

Source§

fn from(slice: &[V]) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<&mut [V]> for KeyedVecSet<K, V>
where K: Ord, V: Clone + Keyed<K>,

Source§

fn from(slice: &mut [V]) -> Self

Converts to this type from the input type.
Source§

impl<K, V, const N: usize> From<[V; N]> for KeyedVecSet<K, V>
where K: Ord, V: Keyed<K>,

Source§

fn from(arr: [V; N]) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<Vec<V>> for KeyedVecSet<K, V>
where K: Ord + PartialEq, V: Keyed<K>,

Source§

fn from(vec: Vec<V>) -> Self

Constructs map from a vector of values.

Values with duplicated keys will be deduplicated.

Source§

impl<Item, K, V> FromIterator<Item> for KeyedVecSet<K, V>
where Self: Extend<Item>,

Source§

fn from_iter<I: IntoIterator<Item = Item>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<K, V, Q> Index<&Q> for KeyedVecSet<K, V>
where K: Borrow<Q> + Ord, Q: Ord + ?Sized, V: Keyed<K>,

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, key: &Q) -> &V

Performs the indexing (container[index]) operation. Read more
Source§

impl<K, V> Index<usize> for KeyedVecSet<K, V>

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &V

Performs the indexing (container[index]) operation. Read more
Source§

impl<'de, K, V, E> IntoDeserializer<'de, E> for KeyedVecSet<K, V>
where K: IntoDeserializer<'de, E> + Ord, V: IntoDeserializer<'de, E>, E: Error,

Available on crate feature serde only.
Source§

type Deserializer = SeqDeserializer<<KeyedVecSet<K, V> as IntoIterator>::IntoIter, E>

The type of the deserializer being converted into.
Source§

fn into_deserializer(self) -> Self::Deserializer

Convert this value into a deserializer.
Source§

impl<'a, K, V> IntoIterator for &'a KeyedVecSet<K, V>

Source§

type Item = &'a V

The type of the elements being iterated over.
Source§

type IntoIter = <&'a Vec<V> as IntoIterator>::IntoIter

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<'a, K, V> IntoIterator for &'a mut KeyedVecSet<K, V>

Source§

type Item = &'a mut V

The type of the elements being iterated over.
Source§

type IntoIter = <&'a mut Vec<V> as IntoIterator>::IntoIter

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> IntoIterator for KeyedVecSet<K, V>

Source§

type Item = V

The type of the elements being iterated over.
Source§

type IntoIter = <Vec<V> as IntoIterator>::IntoIter

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 KeyedVecSet<K, V>
where V: PartialEq,

Source§

fn eq(&self, other: &Self) -> 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 KeyedVecSet<K, V>
where K: Serialize + Ord, V: Serialize,

Available on crate feature serde only.
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, V> Eq for KeyedVecSet<K, V>
where K: Eq, V: Eq,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<K, V> UnwindSafe for KeyedVecSet<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<K> Keyed<K> for K

Source§

fn key(&self) -> &K

key accessor for the element.
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>,