pub struct OrderedHashSet<T, S = DefaultHashBuilder> { /* private fields */ }Expand description
A hash set which preserves the order of insertion.
Backed by hashbrown and its default hashing algorithm, currently AHash. The hashing
algorithm can be changed on a per-map basis with with_hasher and with_capacity_and_hasher.
More details on hashing algorithms and why AHash was selected in the hashbrown crate.
To switch to the same hasher as HashMap use RandomState. This will switch to SipHash, a
HashDos resistant algorithm but it may be slower in some cases.
Being backed by a HashMap, this container has all the same requirements on its keys as
HashMap. Specifically, Keys must implement Eq and Hash. You can typically derive
these for types with #[derive(PartialEq, Eq, hash)]. If you implement these yourself you must
ensure the following holds:
k1 == k2 -> hash(k1) == hash(k2)You are also responsible to ensure Keys do not mutate such that their hash can change while in
the map. For more information, check HashMap.
This container has some additional memory overhead on top of its backing HashMap. OrderedHashSet
holds two pointers to maintain the linked list. Each Value inserted has the overhead of three pointers, one for the key
and two for the linked list properties.
Implementations§
Source§impl<T> OrderedHashSet<T, DefaultHashBuilder>
impl<T> OrderedHashSet<T, DefaultHashBuilder>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty set.
Capacity is 0 so the set will not allocated until inserted into.
use ordered_hash_map::OrderedHashSet;
let set = OrderedHashSet::<i32>::new();Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create an empty set with at least the specified capacity.
The map will allocate if capacity is nonzero.
use ordered_hash_map::OrderedHashSet;
let set = OrderedHashSet::<i32>::with_capacity(10);
assert!(set.capacity() >= 10)Sourcepub fn drain(&mut self) -> Drain<'_, T> ⓘ
pub fn drain(&mut self) -> Drain<'_, T> ⓘ
Returns a draining iterator over the elements in insertion order.
§Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
let mut set: OrderedHashSet<i32> = [1i32, 2, 3, 4, 5].iter().collect();
assert_eq!(set.drain().next(), Some(1));
assert!(set.is_empty());Source§impl<T, S> OrderedHashSet<T, S>
impl<T, S> OrderedHashSet<T, S>
Sourcepub const fn with_hasher(hash_builder: S) -> Self
pub const fn with_hasher(hash_builder: S) -> Self
Returns a draining iterator over the elements in insertion order.
§Performance
This iterator visits each element once instead of each bucket once. This iterator is O(len) which a usual map iterator is O(capacity) and may visit empty buckets.
use std::collections::hash_map::RandomState;
let rs = RandomState::new();
let set = OrderedHashSet::<i32, _>::with_hasher(rs);Sourcepub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
Create an empty set with the given capacity and which will use the given hasher.
The map will allocate if capacity is nonzero.
use std::collections::hash_map::RandomState;
let rs = RandomState::new();
let set = OrderedHashSet::<i32, _>::with_capacity_and_hasher(10, rs);
assert!(set.capacity() >= 10)Source§impl<T, S> OrderedHashSet<T, S>
impl<T, S> OrderedHashSet<T, S>
Sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Insert a new element into the set. Returns whether the key already existed.
let mut set = OrderedHashSet::<i32>::new();
assert!(!set.insert(4));
assert!(set.insert(4));Sourcepub fn get<Q>(&self, value: &Q) -> Option<&T>
pub fn get<Q>(&self, value: &Q) -> Option<&T>
Returns a reference to the value stored in the set.
let mut set = OrderedHashSet::<String>::new();
set.insert("A".to_string());
// Stored as String but can use &str to get
assert_eq!(set.get("A"), Some(&String::from("A")));Sourcepub fn remove<Q>(&mut self, value: &Q) -> bool
pub fn remove<Q>(&mut self, value: &Q) -> bool
Remove an element from the set, returning whether or not the element existed.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements.
§Panics
Panics if the new size would overflow a usize.
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.
§Errors
Returns an error if the new size would overflow a usize.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the map as much as possible. May leave some space in accordance with the resize policy.
Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the map down to a lower limit. May leave some space in accordance with the resize policy.
Sourcepub fn pop_back(&mut self) -> Option<T>
pub fn pop_back(&mut self) -> Option<T>
Removes and returns the most recently inserted value, if there is one.
Sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Remove and return the least recently inserted value, if there is one.
Sourcepub fn move_to_front<Q>(&mut self, key: &Q)
pub fn move_to_front<Q>(&mut self, key: &Q)
Move an element to the front of the list
Trait Implementations§
Source§impl<T, S> Clone for OrderedHashSet<T, S>
impl<T, S> Clone for OrderedHashSet<T, S>
Source§impl<T, S> Debug for OrderedHashSet<T, S>where
T: Debug,
impl<T, S> Debug for OrderedHashSet<T, S>where
T: Debug,
Source§impl<T, S> Default for OrderedHashSet<T, S>where
S: Default,
impl<T, S> Default for OrderedHashSet<T, S>where
S: Default,
Source§impl<'de, T> Deserialize<'de> for OrderedHashSet<T>
impl<'de, T> Deserialize<'de> for OrderedHashSet<T>
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<'a, T, S> Extend<&'a T> for OrderedHashSet<T, S>
impl<'a, T, S> Extend<&'a T> for OrderedHashSet<T, S>
Source§fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a 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, S> Extend<T> for OrderedHashSet<T, S>
impl<T, S> Extend<T> for OrderedHashSet<T, S>
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<'a, T, S> FromIterator<&'a T> for OrderedHashSet<T, S>
impl<'a, T, S> FromIterator<&'a T> for OrderedHashSet<T, S>
Source§impl<T, S> FromIterator<T> for OrderedHashSet<T, S>
impl<T, S> FromIterator<T> for OrderedHashSet<T, S>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<T, Q, S> Index<&Q> for OrderedHashSet<T, S>
impl<T, Q, S> Index<&Q> for OrderedHashSet<T, S>
Source§impl<'a, T, S> IntoIterator for &'a OrderedHashSet<T, S>
impl<'a, T, S> IntoIterator for &'a OrderedHashSet<T, S>
Source§impl<T, S> IntoIterator for OrderedHashSet<T, S>
impl<T, S> IntoIterator for OrderedHashSet<T, S>
Source§impl<T, S> PartialEq for OrderedHashSet<T, S>
impl<T, S> PartialEq for OrderedHashSet<T, S>
Source§impl<T, S> Serialize for OrderedHashSet<T, S>
impl<T, S> Serialize for OrderedHashSet<T, S>
impl<T, S> Eq for OrderedHashSet<T, S>
Auto Trait Implementations§
impl<T, S> Freeze for OrderedHashSet<T, S>where
S: Freeze,
impl<T, S> RefUnwindSafe for OrderedHashSet<T, S>where
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, S> Send for OrderedHashSet<T, S>
impl<T, S> Sync for OrderedHashSet<T, S>
impl<T, S> Unpin for OrderedHashSet<T, S>where
S: Unpin,
impl<T, S> UnwindSafe for OrderedHashSet<T, S>
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,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.