Struct indexmap::set::IndexSet

source ·
pub struct IndexSet<T, S = RandomState> { /* private fields */ }
Expand description

A hash set where the iteration order of the values is independent of their hash values.

The interface is closely compatible with the standard HashSet, but also has additional features.

Order

The values have a consistent order that is determined by the sequence of insertion and removal calls on the set. The order does not depend on the values or the hash function at all. Note that insertion order and value are not affected if a re-insertion is attempted once an element is already present.

All iterators traverse the set in order. Set operation iterators like union produce a concatenated order, as do their matching “bitwise” operators. See their documentation for specifics.

The insertion order is preserved, with notable exceptions like the .remove() or .swap_remove() methods. Methods such as .sort_by() of course result in a new order, depending on the sorting order.

Indices

The values are indexed in a compact range without holes in the range 0..self.len(). For example, the method .get_full looks up the index for a value, and the method .get_index looks up the value by index.

Examples

use indexmap::IndexSet;

// Collects which letters appear in a sentence.
let letters: IndexSet<_> = "a short treatise on fungi".chars().collect();

assert!(letters.contains(&'s'));
assert!(letters.contains(&'t'));
assert!(letters.contains(&'u'));
assert!(!letters.contains(&'y'));

Implementations§

Create a new set. (Does not allocate.)

Create a new set with capacity for n elements. (Does not allocate if n is zero.)

Computes in O(n) time.

Create a new set with capacity for n elements. (Does not allocate if n is zero.)

Computes in O(n) time.

Create a new set with hash_builder.

This function is const, so it can be called in static contexts.

Computes in O(1) time.

Return a reference to the set’s BuildHasher.

Return the number of elements in the set.

Computes in O(1) time.

Returns true if the set contains no elements.

Computes in O(1) time.

Return an iterator over the values of the set, in their order

Remove all elements in the set, while preserving its capacity.

Computes in O(n) time.

Shortens the set, keeping the first len elements and dropping the rest.

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

Clears the IndexSet in the given index range, returning those values as a drain iterator.

The range may be any type that implements RangeBounds<usize>, including all of the std::ops::Range* types, or even a tuple pair of Bound start and end values. To drain the set entirely, use RangeFull like set.drain(..).

This shifts down all entries following the drained range to fill the gap, and keeps the allocated memory for reuse.

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

Splits the collection into two at the given index.

Returns a newly allocated set containing the elements in the range [at, len). After the call, the original set will be left containing the elements [0, at) with its previous capacity unchanged.

Panics if at > len.

Reserve capacity for additional more values.

Computes in O(n) time.

Shrink the capacity of the set as much as possible.

Computes in O(n) time.

Shrink the capacity of the set with a lower limit.

Computes in O(n) time.

Insert the value into the set.

If an equivalent item already exists in the set, it returns false leaving the original value in the set and without altering its insertion order. Otherwise, it inserts the new item and returns true.

Computes in O(1) time (amortized average).

Insert the value into the set, and get its index.

If an equivalent item already exists in the set, it returns the index of the existing item and false, leaving the original value in the set and without altering its insertion order. Otherwise, it inserts the new item and returns the index of the inserted item and true.

Computes in O(1) time (amortized average).

Return an iterator over the values that are in self but not other.

Values are produced in the same order that they appear in self.

Return an iterator over the values that are in self or other, but not in both.

Values from self are produced in their original order, followed by values from other in their original order.

Return an iterator over the values that are in both self and other.

Values are produced in the same order that they appear in self.

Return an iterator over all values that are in self or other.

Values from self are produced in their original order, followed by values that are unique to other in their original order.

Return true if an equivalent to value exists in the set.

Computes in O(1) time (average).

Return a reference to the value stored in the set, if it is present, else None.

Computes in O(1) time (average).

Return item index and value

Return item index, if it exists in the set

Adds a value to the set, replacing the existing value, if any, that is equal to the given one, without altering its insertion order. Returns the replaced value.

Computes in O(1) time (average).

Adds a value to the set, replacing the existing value, if any, that is equal to the given one, without altering its insertion order. Returns the index of the item and its replaced value.

Computes in O(1) time (average).

Remove the value from the set, and return true if it was present.

NOTE: This is equivalent to .swap_remove(value), if you want to preserve the order of the values in the set, use .shift_remove(value).

Computes in O(1) time (average).

Remove the value from the set, and return true if it was present.

Like Vec::swap_remove, the value is removed by swapping it with the last element of the set and popping it off. This perturbs the position of what used to be the last element!

Return false if value was not in the set.

Computes in O(1) time (average).

Remove the value from the set, and return true if it was present.

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

Return false if value was not in the set.

Computes in O(n) time (average).

Removes and returns the value in the set, if any, that is equal to the given one.

NOTE: This is equivalent to .swap_take(value), if you need to preserve the order of the values in the set, use .shift_take(value) instead.

Computes in O(1) time (average).

Removes and returns the value in the set, if any, that is equal to the given one.

Like Vec::swap_remove, the value is removed by swapping it with the last element of the set and popping it off. This perturbs the position of what used to be the last element!

Return None if value was not in the set.

Computes in O(1) time (average).

Removes and returns the value in the set, if any, that is equal to the given one.

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

Return None if value was not in the set.

Computes in O(n) time (average).

Remove the value from the set return it and the index it had.

Like Vec::swap_remove, the value is removed by swapping it with the last element of the set and popping it off. This perturbs the position of what used to be the last element!

Return None if value was not in the set.

Remove the value from the set return it and the index it had.

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

Return None if value was not in the set.

Remove the last value

This preserves the order of the remaining elements.

Computes in O(1) time (average).

Scan through each value in the set and keep those where the closure keep returns true.

The elements are visited in order, and remaining elements keep their order.

Computes in O(n) time (average).

Sort the set’s values by their default ordering.

See sort_by for details.

Sort the set’s values in place using the comparison function cmp.

Computes in O(n log n) time and O(n) space. The sort is stable.

Sort the values of the set and return a by-value iterator of the values with the result.

The sort is stable.

Sort the set’s values by their default ordering.

See sort_unstable_by for details.

Sort the set’s values in place using the comparison funtion cmp.

Computes in O(n log n) time. The sort is unstable.

Sort the values of the set and return a by-value iterator of the values with the result.

Reverses the order of the set’s values in place.

Computes in O(n) time and O(1) space.

Get a value by index

Valid indices are 0 <= index < self.len()

Computes in O(1) time.

Get the first value

Computes in O(1) time.

Get the last value

Computes in O(1) time.

Remove the value by index

Valid indices are 0 <= index < self.len()

Like Vec::swap_remove, the value is removed by swapping it with the last element of the set and popping it off. This perturbs the position of what used to be the last element!

Computes in O(1) time (average).

Remove the value by index

Valid indices are 0 <= index < self.len()

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

Computes in O(n) time (average).

Moves the position of a value from one index to another by shifting all other values in-between.

  • If from < to, the other values will shift down while the targeted value moves up.
  • If from > to, the other values will shift up while the targeted value moves down.

Panics if from or to are out of bounds.

Computes in O(n) time (average).

Swaps the position of two values in the set.

Panics if a or b are out of bounds.

Returns true if self has no elements in common with other.

Returns true if all elements of self are contained in other.

Returns true if all elements of other are contained in self.

Trait Implementations§

Returns the set intersection, cloned into a new set.

Values are collected in the same order that they appear in self.

The resulting type after applying the & operator.

Returns the set union, cloned into a new set.

Values from self are collected in their original order, followed by values that are unique to other in their original order.

The resulting type after applying the | operator.

Returns the set symmetric-difference, cloned into a new set.

Values from self are collected in their original order, followed by values from other in their original order.

The resulting type after applying the ^ operator.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Return an empty IndexSet

Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Examples
use indexmap::IndexSet;

let set1 = IndexSet::from([1, 2, 3, 4]);
let set2: IndexSet<_> = [1, 2, 3, 4].into();
assert_eq!(set1, set2);
Creates a value from an iterator. Read more

Access IndexSet values at indexed positions.

Examples

use indexmap::IndexSet;

let mut set = IndexSet::new();
for word in "Lorem ipsum dolor sit amet".split_whitespace() {
    set.insert(word.to_string());
}
assert_eq!(set[0], "Lorem");
assert_eq!(set[1], "ipsum");
set.reverse();
assert_eq!(set[0], "amet");
assert_eq!(set[1], "sit");
set.sort();
assert_eq!(set[0], "Lorem");
assert_eq!(set[1], "amet");
use indexmap::IndexSet;

let mut set = IndexSet::new();
set.insert("foo");
println!("{:?}", set[10]); // panics!

Returns a reference to the value at the supplied index.

Panics if index is out of bounds.

The returned type after indexing.
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Returns the set difference, cloned into a new set.

Values are collected in the same order that they appear in self.

The resulting type after applying the - operator.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.