OrderedHashSet

Struct OrderedHashSet 

Source
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>

Source

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();
Source

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)
Source

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>

Source

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);
Source

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

pub fn hasher(&self) -> &S

Returns a reference to the sets hasher.

use std::collections::hash_map::RandomState;

let rs = RandomState::new();
let set = OrderedHashSet::<i32, _>::with_hasher(rs);
let rs: &RandomState = set.hasher();
Source

pub fn capacity(&self) -> usize

The number of elements the set can hold without reallocating.

Source

pub fn len(&self) -> usize

The number of elements currently held in the set.

Source

pub fn is_empty(&self) -> bool

Returns whether the set is empty.

Source

pub fn clear(&mut self)

Removes all values from the set.

Source

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

Returns an 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.

Source§

impl<T, S> OrderedHashSet<T, S>
where T: Eq + Hash, S: BuildHasher,

Source

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));
Source

pub fn get<Q>(&self, value: &Q) -> Option<&T>
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

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")));
Source

pub fn remove<Q>(&mut self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Remove an element from the set, returning whether or not the element existed.

Source

pub fn take<Q>(&mut self, value: &Q) -> Option<T>
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Remove and return an element from the set

Source

pub fn contains<Q>(&mut self, value: &Q) -> bool
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Check if the set contains this key.

Source

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.

Source

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.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the map as much as possible. May leave some space in accordance with the resize policy.

Source

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.

Source

pub fn pop_back(&mut self) -> Option<T>

Removes and returns the most recently inserted value, if there is one.

Source

pub fn back(&self) -> Option<&T>

Returns the most recently inserted value, if there is one

Source

pub fn pop_front(&mut self) -> Option<T>

Remove and return the least recently inserted value, if there is one.

Source

pub fn front(&self) -> Option<&T>

Returns the most recently inserted value, if there is one

Source

pub fn move_to_front<Q>(&mut self, key: &Q)
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Move an element to the front of the list

Source

pub fn move_to_back<Q>(&mut self, key: &Q)
where T: Borrow<Q>, Q: Hash + Eq + ?Sized,

Move and element to the back of the list

Trait Implementations§

Source§

impl<T, S> Clone for OrderedHashSet<T, S>
where T: Hash + Eq + Clone, S: BuildHasher + Clone,

Source§

fn clone(&self) -> Self

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<T, S> Debug for OrderedHashSet<T, S>
where T: Debug,

Source§

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

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

impl<T, S> Default for OrderedHashSet<T, S>
where S: Default,

Source§

fn default() -> Self

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

impl<'de, T> Deserialize<'de> for OrderedHashSet<T>
where T: Eq + Hash + Deserialize<'de>,

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, T, S> Extend<&'a T> for OrderedHashSet<T, S>
where T: 'a + Eq + Hash + Copy, S: BuildHasher,

Source§

fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)

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<T, S> Extend<T> for OrderedHashSet<T, S>
where T: Eq + Hash, S: BuildHasher,

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

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<'a, T, S> FromIterator<&'a T> for OrderedHashSet<T, S>
where T: 'a + Eq + Hash + Copy, S: BuildHasher + Default,

Source§

fn from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T, S> FromIterator<T> for OrderedHashSet<T, S>
where T: Eq + Hash, S: BuildHasher + Default,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<T, Q, S> Index<&Q> for OrderedHashSet<T, S>
where T: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized, S: BuildHasher,

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<'a, T, S> IntoIterator for &'a OrderedHashSet<T, S>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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<T, S> IntoIterator for OrderedHashSet<T, S>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

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<T, S> PartialEq for OrderedHashSet<T, S>
where T: Eq + Hash, S: BuildHasher,

Source§

fn eq(&self, other: &OrderedHashSet<T, S>) -> 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<T, S> Serialize for OrderedHashSet<T, S>
where T: Eq + Hash + Serialize, S: BuildHasher,

Source§

fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where Ser: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T, S> Eq for OrderedHashSet<T, S>
where T: Eq + Hash, S: BuildHasher,

Auto Trait Implementations§

§

impl<T, S> Freeze for OrderedHashSet<T, S>
where S: Freeze,

§

impl<T, S> RefUnwindSafe for OrderedHashSet<T, S>

§

impl<T, S> Send for OrderedHashSet<T, S>
where T: Send, S: Send,

§

impl<T, S> Sync for OrderedHashSet<T, S>
where T: Sync, S: Sync,

§

impl<T, S> Unpin for OrderedHashSet<T, S>
where S: Unpin,

§

impl<T, S> UnwindSafe for OrderedHashSet<T, S>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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<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>,