pub struct PetitSet<T, const CAP: usize> { /* private fields */ }
Expand description

A set-like data structure with a fixed maximum size

This data structure does not require the Hash or Ord traits, and instead uses linear iteration to find entries. Iteration order is guaranteed to be stable, and elements are not re-compressed upon removal.

Under the hood, this is a [PetitMap<T, (), CAP>].

Principally, this data structure should be used for relatively small sets, where iteration performance, stable-order, stack-allocation and uniqueness are more important than insertion or look-up speed. Iteration, insertion and checking whether an element is in the set are O(CAP). Indexing into a particular element is O(1), as is removing an element at a specific index.

The values are stored as Options within an array, so niche optimization can significantly reduce memory footprint.

The maximum size of this type is given by the const-generic type parameter CAP. Entries in this structure are guaranteed to be unique.

Implementations

Create a new empty PetitSet.

The capacity is given by the generic parameter CAP.

Returns the index of the next filled slot, if any

Returns None if the cursor is larger than CAP

Returns the index of the next empty slot, if any

Returns None if the cursor is larger than CAP

Return the capacity of the PetitSet

Returns the current number of elements in the PetitSet

Are there exactly 0 elements in the PetitSet?

Are there exactly CAP elements in the PetitSet?

Returns an iterator over the elements of the PetitSet

Returns a reference to the provided index of the underlying array

Returns Some(&T) if the index is in-bounds and has an element

Returns a mutable reference to the provided index of the underlying array

Returns Some(&mut T) if the index is in-bounds and has an element

Removes all elements from the set without allocation

Removes the element at the provided index

Returns true if an element was found

Panics

Panics if the provided index is larger than CAP.

Removes the element at the provided index

Returns Some(T) if an element was found at that index, or None if no element was there.

Panics

Panics if the provided index is larger than CAP.

Swaps the element in index_a with the element in index_b

Panics

Panics if either index is greater than CAP.

Inserts an element into the next empty index of the set, without checking for uniqueness

Returns Some(index) if the operation succeeded, or None if it failed.

Warning

This API is very easy to misuse and will completely break your PetitSet if you do. Avoid it unless you are guaranteed by construction that no duplicates exist.

Returns the index for the provided element, if it exists in the set

Is the provided element in the set?

Attempt to insert a new element to the set in the first available slot.

Inserts the element if able, then returns the Result of that operation. This is either a SuccesfulSetInsertion or a CapacityError.

Insert a new element to the set in the first available slot

Returns a SuccesfulSetInsertion, which encodes both the index at which the element is stored and whether the element was already present.

Panics

Panics if the set is full and the item is not a duplicate

Insert a new element to the set at the provided index

If a matching element already existed in the set, it will be moved to the supplied index. Any element that was previously there will be moved to the matching element’s original index.

Returns Some(T) of any element removed by this operation.

Panics

Panics if the provided index is larger than CAP.

Inserts multiple new elements to the set. Duplicate elements are discarded.

Returns a CapacityError if the extension cannot be completed because the set is full.

Removes the element from the set, if it exists

Returns Some(index) if the element was found, or None if no matching element is found

Removes an element from the set, if it exists, returning both the value that compared equal and the index at which it was stored.

Swaps the positions of element_a with the position of element_b

Returns true if both elements were found and succesfully swapped.

Are the two PetitSets element-for-element identical, in the same order?

Retains only the elements specified by the predicate.

In other words, remove all elements e such that f(&e) returns false. The elements are visited in order.

Constructs a new PetitSet by consuming values from an iterator.

The consumed values will be stored in order, with duplicate elements discarded.

Returns an error if the iterator produces more than CAP distinct elements. The returned error will include both the element that could not be inserted, and a PetitSet containing all elements up to that point.

Example
use petitset::CapacityError;
use petitset::PetitSet;

let elems = vec![1, 2, 1, 4, 3, 1];
let set = PetitSet::<_, 5>::try_from_iter(elems.iter().copied());
assert_eq!(set, Ok(PetitSet::from_raw_array_unchecked([Some(1), Some(2), Some(4), Some(3), None])));

let failed = PetitSet::<_, 3>::try_from_iter(elems.iter().copied());
assert_eq!(failed, Err(CapacityError((PetitSet::from_raw_array_unchecked([Some(1), Some(2), Some(4)]), 3))));

Construct a PetitSet directly from an array, without checking for duplicates.

It is a logic error if any two non-None values in the array are equal, as elements are expected to be unique. If this occurs, the PetitSet returned may behave unpredictably.

Do the sets contain any common elements?

Examples
use petitset::PetitSet;

let set_a: PetitSet<usize, 3> = PetitSet::from_iter([7, 13, 5]);
let set_b: PetitSet<usize, 5> = PetitSet::from_iter([15, 7, 3, 4, 5]);
let mut set_c: PetitSet<usize, 1> = PetitSet::default();
set_c.insert(42);

assert!(!set_a.is_disjoint(&set_b));
assert!(!set_b.is_disjoint(&set_a));

assert!(set_a.is_disjoint(&set_c));
assert!(set_c.is_disjoint(&set_a));

Are all elements in self contained in other?

Examples
use petitset::PetitSet;

let set_a: PetitSet<usize, 3> = PetitSet::from_iter([1, 2, 3]);
let set_b: PetitSet<usize, 5> = PetitSet::from_iter([2, 3]);

assert!(set_a.is_subset(&set_a));

assert!(!set_a.is_subset(&set_b));
assert!(set_b.is_subset(&set_a));

Are all elements in other contained in self?

Examples
use petitset::PetitSet;

let set_a: PetitSet<usize, 3> = PetitSet::from_iter([1, 2, 3]);
let set_b: PetitSet<usize, 5> = PetitSet::from_iter([2, 3]);

assert!(set_a.is_superset(&set_a));

assert!(set_a.is_superset(&set_b));
assert!(!set_b.is_superset(&set_a));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Inserts multiple new elements to the set. Duplicate elements are discarded.

Panics

Panics if the set would overflow due to the insertion of non-duplicate items

🔬 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

Panics if the iterator contains more than CAP distinct elements.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. 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

Tests set-equality between the two sets

This is order and cap size-independent. Use the equivalent method for elementwise-equality.

Uses an inefficient O(n^2) algorithm due to minimal trait bounds.

This method tests for !=.

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

Performs the conversion.

Performs the conversion.

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.