pub struct PetitMap<K, V, const CAP: usize> { /* private fields */ }
Expand description

A map-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.

Only CAP entries may be stored at once.

Principally, this data structure should be used for relatively small maps, 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 are in the map are O(CAP). Retrieving a specific element is 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. Keys are guaranteed to be unique.

Implementations

Create a new empty PetitMap.

The capacity is given by the generic parameter CAP.

Returns a reference to the value at the provided index.

Returns Some((K, V)) if the index is in-bounds and has an element.

Panics

Panics if the provided index is larger than CAP.

Returns a mutable reference to the value at the provided index.

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

Panics

Panics if the provided index is larger than CAP.

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 key-value pair at the provided index

Returns Some((K, V)) if the index was full.

Panics

Panics if the provided index is larger than CAP.

Returns an iterator over the key value pairs

An iterator visiting all keys in in a first-in, first-out order

An iterator visiting all values in in a first-in, first-out order

The item type is a &'a V

An iterator visiting all values in in a first-in, first-out order

The item type is a &'a mut V

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

Returns the current number of key-value pairs in the PetitMap

Returns the maximum number of elements that can be stored in the PetitMap

Are there exactly 0 elements in the PetitMap?

Are there exactly CAP elements in the PetitMap?

Swaps the element in index_a with the element in index_b

Panics

Panics if either index is greater than CAP.

Removes all elements from the map without de-allocation

Inserts a key-value pair into the next empty index of the map, 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 PetitMap if you do. Avoid it unless you are guaranteed by construction that no duplicates exist.

Attempts to store the value into the map, which can be looked up by the key

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

Stores the value in the map, which can be looked up by the key

Returns a SuccesfulMapInsertion, which encodes both the index at which the element is stored and whether the key was already present. If a key was already present, the previous value is also returned.

Panics

Panics if the map was full and the key was a non-duplicate.

Insert a new key-value pair at the provided index

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

Returns Some((K, V)) of any element removed by this operation.

Panics

Panics if the provided index is larger than CAP.

Returns the index for the provided key, if it exists in the map

Does the map contain the provided key?

Returns a reference to the value corresponding to the key.

Returns Some(&V) if the key is found

Returns the key-value pair corresponding to the supplied key.

Returns Some(&K, &V) if the key is found

Returns a mutable reference to the value corresponding to the key.

Returns Some(&mut V) if the key is found

Removes the key-value pair from the map if the key is found

Returns Some((index)) if it was found

Removes and returns the key-value pair from the map if the key is found

Returns Some((index, (K,V))) if it was found

Swaps the positions of element_a with the position of element_b

Returns true if both keys were found and succesfully swapped.

Retains only the elements specified by the predicate.

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

Constructs a new PetitMap 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 PetitMap containing all elements up to that point.

Example
use petitset::CapacityError;
use petitset::PetitMap;

let elems = vec![(1, 11), (2, 21), (1, 12), (4, 41), (3, 31), (1, 13)];
let set = PetitMap::<_,_, 5>::try_from_iter(elems.iter().copied());
assert_eq!(set, Ok(PetitMap::from_raw_array_unchecked([Some((1,13)), Some((2, 21)), Some((4, 41)), Some((3, 31)), None])));

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

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

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

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

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 key-value pairs to the map.

Duplicate keys will overwrite existing values.

Panics

Panics if the map would overflow due to the insertion of non-duplicate keys

🔬 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 maps

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.