Skip to main content

LinkedHashSet

Struct LinkedHashSet 

Source
pub struct LinkedHashSet<T, S = RandomState> { /* private fields */ }
Expand description

A hash set that preserves insertion order and exposes a VecDeque-like API with insert_back, insert_front, [pop_front], and [pop_back].

Implemented as a thin wrapper around LinkedHashMap<T, ()>.

Elements require only Hash + Eq. They do not need to be Clone or Copy — including heap-owning types such as String, Vec<T>, or Box<T>.

§Ordering contract

insert_back and insert_front on an element that already exists are a no-op - the element keeps its current position. Use move_to_back / move_to_front to explicitly reorder an element.

§Example

use linked_hash_table::LinkedHashSet;

let mut s = LinkedHashSet::new();
s.insert_back("a");
s.insert_back("b");
s.insert_back("c");
assert_eq!(s.pop_front(), Some("a"));
assert_eq!(s.pop_back(),  Some("c"));

Implementations§

Source§

impl<T> LinkedHashSet<T>

Source

pub fn new() -> Self

Creates an empty LinkedHashSet.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty LinkedHashSet with the specified initial capacity.

Source§

impl<T, S> LinkedHashSet<T, S>

Source

pub fn with_hasher(hash_builder: S) -> Self

Creates an empty LinkedHashSet using the supplied hasher builder.

Source

pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self

Creates an empty LinkedHashSet with the given capacity and hasher.

Source

pub fn len(&self) -> usize

Returns the number of elements in the set.

Source

pub fn is_empty(&self) -> bool

Returns true if the set contains no elements.

Source

pub fn capacity(&self) -> usize

Returns the number of elements the set can hold without reallocating.

Source

pub fn hasher(&self) -> &S

Returns a reference to the set’s BuildHasher.

Source§

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

Source

pub fn insert_back(&mut self, value: T) -> bool

Inserts value at the back (most-recently-inserted end).

Returns true if the value was newly inserted. If the value already exists its position is preserved (no-op) and false is returned.

Source

pub fn insert_front(&mut self, value: T) -> bool

Inserts value at the front (least-recently-inserted end).

Returns true if the value was newly inserted. If the value already exists its position is preserved (no-op) and false is returned.

Source

pub fn insert(&mut self, value: T) -> bool

Alias for insert_back - matches the HashSet::insert signature.

Source

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

Returns true if the set contains value.

Source

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

Returns a reference to the element in the set that equals value, or None if it is not present.

Source

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

Returns a reference to the front (oldest) element, or None if the set is empty.

Source

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

Returns a reference to the back (most recently inserted) element, or None if the set is empty.

Source

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

Removes and returns the front (oldest) element, or None.

Source

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

Removes and returns the back (newest) element, or None.

Source

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

Removes value from the set. Returns true if the element was present.

Source

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

Removes the element equal to value and returns it, if present.

Source

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

Retains only the elements for which f returns true. Elements are visited in insertion order (front → back).

Source

pub fn clear(&mut self)

Removes all elements from the set.

Source

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

Moves value to the back of the ordering. Returns true if the element was found.

Source

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

Moves value to the front of the ordering. Returns true if the element was found.

Source

pub fn drain(&mut self) -> SetDrain<'_, T>

Removes all elements in insertion order, returning them via an iterator. The set is empty after this call (even if the iterator is dropped before it is fully consumed).

Source

pub fn is_subset<S2: BuildHasher>(&self, other: &LinkedHashSet<T, S2>) -> bool

Returns true if every element of self is also contained in other.

Source

pub fn is_superset<S2: BuildHasher>(&self, other: &LinkedHashSet<T, S2>) -> bool

Returns true if every element of other is also contained in self.

Source

pub fn is_disjoint<S2: BuildHasher>(&self, other: &LinkedHashSet<T, S2>) -> bool

Returns true if self and other share no elements.

Source§

impl<T, S> LinkedHashSet<T, S>

Source

pub fn iter<'a>(&'a self) -> SetIter<'a, T>

Returns an iterator over elements in insertion order.

Trait Implementations§

Source§

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

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

Source§

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

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

impl<T> Default for LinkedHashSet<T>

Source§

fn default() -> Self

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

impl<T: Hash + Eq, S: BuildHasher> Extend<T> for LinkedHashSet<T, S>

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<T: Hash + Eq> FromIterator<T> for LinkedHashSet<T>

Source§

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

Creates a value from an iterator. Read more
Source§

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

Shared-reference iterator: yields &T in insertion order.

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = SetIter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> SetIter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<T, S> IntoIterator for LinkedHashSet<T, S>

Consuming iterator: yields all elements in insertion order.

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = SetIntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> SetIntoIter<T>

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq, S1, S2> PartialEq<LinkedHashSet<T, S2>> for LinkedHashSet<T, S1>

Source§

fn eq(&self, other: &LinkedHashSet<T, S2>) -> bool

Two sets are equal only when they contain the same elements in the same order.

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: PartialEq + Eq, S> Eq for LinkedHashSet<T, S>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, S> UnsafeUnpin for LinkedHashSet<T, S>
where S: UnsafeUnpin,

§

impl<T, S> UnwindSafe for LinkedHashSet<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.