Skip to main content

OrderedArraySet

Struct OrderedArraySet 

Source
pub struct OrderedArraySet<T, const S: usize> { /* private fields */ }
Expand description

An ordered set based on an ordered array. This structures provides a subset of the methods offered by the standard library’s BTreeSet.

§Examples

use static_mapset::OrderedArraySet;

let books = OrderedArraySet::new([
    "A Dance With Dragons",
    "To Kill a Mockingbird",
    "The Odyssey",
    "The Great Gatsby"
]);

// Check for a specific one.
if !books.contains("The Winds of Winter") {
    println!("We have {} books, but The Winds of Winter ain't one.",
             books.iter().count());
}

// Iterate over everything.
for book in &books {
    println!("{book}");
}

A OrderedArraySet with a known list of items can be initialized from an array:

use static_mapset::OrderedArraySet;

let set = OrderedArraySet::from([1, 2, 3]);

Implementations§

Source§

impl<T, const S: usize> OrderedArraySet<T, S>

Source

pub fn new(items: [T; S]) -> Self
where T: Ord,

Sorts the array and creates an OrderedArraySet. Duplicate keys are removed, keeping the last occurrence.

The array is sorted. Duplicate elements are removed, keeping the last occurrence.

§Complexity

Linearithmic in self.len().

§Examples
use static_mapset::OrderedArraySet;

let set = OrderedArraySet::new([3, 1, 2, 2]);
assert_eq!(set.first(), Some(&1));
Source

pub const fn new_unchecked(ordered_items: [T; S]) -> Self

Creates an OrderedArraySet from an owned array without sorting.

The array must be sorted in ascending order with no duplicate elements.

§Complexity

Constant.

§Examples
use static_mapset::OrderedArraySet;

static MAP: OrderedArraySet<i32, 2> = OrderedArraySet::new_unchecked([1, 2]);
assert!(MAP.contains(&2));

// Incorrect key order or duplicates cause undefined behavior:
// let map = OrderedArraySet::new_unchecked([3, 2]);
// let map = OrderedArraySet::new_unchecked([2, 2]);
Source

pub fn is_valid(&self) -> bool
where T: PartialOrd,

Returns true if the map is ordered correctly.

§Complexity

Linear in self.len().

§Examples
use static_mapset::OrderedArraySet;

let map = OrderedArraySet::new_unchecked([1, 2, 3]);
assert!(map.is_valid());

let map = OrderedArraySet::new_unchecked([1, 2, 2]);
assert!(!map.is_valid());
let map = OrderedArraySet::new_unchecked([1.0, 2.0, f64::NAN]);
assert!(!map.is_valid());
Source

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

Returns true if the set contains an element equal to the value.

The value may be any borrowed form of the set’s element type, but the ordering on the borrowed form must match the ordering on the element type. If equal elements are present, this returns true if any matching element exists.

§Complexity

Logarithmic in self.len().

§Examples
use static_mapset::OrderedArraySet;

let set = OrderedArraySet::from([1, 2, 3]);
assert_eq!(set.contains(&1), true);
assert_eq!(set.contains(&4), false);

let set = OrderedArraySet::new_unchecked([1.0, 2.0, 3.0]);
// f64::NAN is incomparable, so value lookups by NaN never match.
assert_eq!(set.contains(&f64::NAN), false);
Source

pub fn difference<'a, O: Ordered<'a, T>>( &'a self, other: &'a O, ) -> Difference<'a, T, Iter<'a, T>, O::Iter>
where T: PartialOrd,

Visits the elements representing the difference, i.e., the elements that are in self but not in other, in ascending order.

§Complexity

Linear in self.len() + other.len() to iterate all elements.

§Examples
use static_mapset::OrderedArraySet;

let a = OrderedArraySet::new([1, 2]);
let b = OrderedArraySet::new([2, 3]);

let diff: Vec<_> = a.difference(&b).cloned().collect();
assert_eq!(diff, [1]);
Source

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

Returns a reference to the first element in the set, if any. This element is always the minimum of all elements in the set.

§Complexity

Constant.

§Examples

Basic usage:

use static_mapset::OrderedArraySet;

let set: OrderedArraySet<i32, 0> = OrderedArraySet::new([]);
assert_eq!(set.first(), None);

let set = OrderedArraySet::new([2, 1]);
assert_eq!(set.first(), Some(&1));
Source

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

Returns a reference to the element in the set, if any, that is equal to the value.

The value may be any borrowed form of the set’s element type, but the ordering on the borrowed form must match the ordering on the element type.

§Complexity

Logarithmic in self.len().

§Examples
use static_mapset::OrderedArraySet;

let set = OrderedArraySet::from([1, 2, 3]);
assert_eq!(set.get(&2), Some(&2));
assert_eq!(set.get(&4), None);

let set = OrderedArraySet::new_unchecked([1.0, 2.0, 3.0]);
// f64::NAN is incomparable, so value lookups by NaN never match.
assert_eq!(set.get(&f64::NAN), None);
Source

pub fn intersection<'a, O: Ordered<'a, T>>( &'a self, other: &'a O, ) -> Intersection<'a, T, Iter<'a, T>, O::Iter>
where T: PartialOrd,

Visits the elements representing the intersection, i.e., the elements that are both in self and other, in ascending order.

§Complexity

Linear in self.len() + other.len() to iterate all elements.

§Examples
use static_mapset::OrderedArraySet;

let a = OrderedArraySet::new([1, 2]);
let b = OrderedArraySet::new([2, 3]);

let intersection: Vec<_> = a.intersection(&b).cloned().collect();
assert_eq!(intersection, [2]);
Source

pub fn is_disjoint<'a, O: Ordered<'a, T>>(&'a self, other: &'a O) -> bool
where T: PartialOrd,

Returns true if self has no elements in common with other. This is equivalent to checking for an empty intersection.

§Complexity

Linear in self.len() + other.len().

§Examples
use static_mapset::OrderedArraySet;

let a = OrderedArraySet::from([1, 2, 3]);
let b = OrderedArraySet::from([4, 5]);
let c = OrderedArraySet::from([1, 5]);

assert_eq!(a.is_disjoint(&b), true);
assert_eq!(a.is_disjoint(&c), false);
Source

pub const fn is_empty(&self) -> bool

Returns true if the set contains no elements.

§Complexity

Constant.

§Examples
use static_mapset::OrderedArraySet;

let set: OrderedArraySet<i32, 0> = OrderedArraySet::new([]);
assert!(set.is_empty());
let set = OrderedArraySet::new([1]);
assert!(!set.is_empty());
Source

pub fn is_subset<'a, O: Ordered<'a, T>>(&'a self, other: &'a O) -> bool
where T: PartialOrd,

Returns true if the set is a subset of another, i.e., other contains at least all the elements in self.

§Complexity

Linear in self.len() + other.len().

§Examples
use static_mapset::OrderedArraySet;

let sup = OrderedArraySet::from([1, 2, 3]);
let set = OrderedArraySet::from([2]);
let not_subset = OrderedArraySet::from([2, 4]);

assert_eq!(set.is_subset(&sup), true);
assert_eq!(not_subset.is_subset(&sup), false);
Source

pub fn is_superset<'a, O: Ordered<'a, T>>(&'a self, other: &'a O) -> bool
where T: PartialOrd,

Returns true if the set is a superset of another, i.e., self contains at least all the elements in other.

§Complexity

Linear in self.len() + other.len().

§Examples
use static_mapset::OrderedArraySet;

let sub = OrderedArraySet::from([1, 2]);
let set = OrderedArraySet::from([0, 1, 2]);
let not_superset = OrderedArraySet::from([0, 1]);

assert_eq!(set.is_superset(&sub), true);
assert_eq!(not_superset.is_superset(&sub), false);
Source

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

Gets an iterator that visits the elements in the set in ascending order.

§Complexity

Linear in self.len() to iterate all elements.

§Examples
use static_mapset::OrderedArraySet;

let set = OrderedArraySet::new([2, 1]);

let items: Vec<_> = set.iter().cloned().collect();
assert_eq!(items, [1, 2]);
Source

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

Returns a reference to the last element in the set, if any. This element is always the maximum of all elements in the set.

§Complexity

Constant.

§Examples

Basic usage:

use static_mapset::OrderedArraySet;

let set: OrderedArraySet<i32, 0> = OrderedArraySet::new([]);
assert_eq!(set.last(), None);

let set = OrderedArraySet::new([1]);
assert_eq!(set.last(), Some(&1));

let set = OrderedArraySet::new([1, 2]);
assert_eq!(set.last(), Some(&2));
Source

pub fn lower_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
where T: Borrow<Q>, Q: PartialOrd + ?Sized,

Returns a [Cursor] pointing at the gap before the smallest element greater than the given bound.

Passing Bound::Included(x) will return a cursor pointing to the gap before the smallest element greater than or equal to x.

Passing Bound::Excluded(x) will return a cursor pointing to the gap before the smallest element greater than x.

Passing Bound::Unbounded will return a cursor pointing to the gap before the smallest element in the set. With equal elements, Included points before the first equal element and Excluded points after the last equal element.

§Complexity

Logarithmic in self.len() to find the bounds, and linear in the number of yielded elements to iterate them all.

§Examples

use static_mapset::OrderedArraySet;
use std::ops::Bound;

let set = OrderedArraySet::from([1, 2, 3, 4]);

let cursor = set.lower_bound(Bound::Included(&2));
assert_eq!(cursor.peek_prev(), Some(&1));
assert_eq!(cursor.peek_next(), Some(&2));

let cursor = set.lower_bound(Bound::Excluded(&2));
assert_eq!(cursor.peek_prev(), Some(&2));
assert_eq!(cursor.peek_next(), Some(&3));

let cursor = set.lower_bound(Bound::Unbounded);
assert_eq!(cursor.peek_prev(), None);
assert_eq!(cursor.peek_next(), Some(&1));

let set = OrderedArraySet::new_unchecked([1.0, 2.0, 3.0]);
// f64::NAN is incomparable, so the cursor points to the gap after the last key.
let cursor = set.lower_bound(Bound::Included(&f64::NAN));
assert_eq!(cursor.peek_prev(), Some(&3.0));
Source

pub fn range<Q, R>(&self, range: R) -> Iter<'_, T>
where T: Borrow<Q>, Q: PartialOrd + ?Sized, R: RangeBounds<Q>,

Constructs a double-ended iterator over a sub-range of elements in the set. The simplest way is to use the range syntax min..max, thus range(min..max) will yield elements from min (inclusive) to max (exclusive). The range may also be entered as (Bound<T>, Bound<T>), so for example range((Excluded(4), Included(10))) will yield a left-exclusive, right-inclusive range from 4 to 10.

§Panics

Panics if range start > end. Panics if range start == end and both bounds are Excluded. Panics if start (or end() is incomparable and end (or start) is bounded.

§Complexity

Logarithmic in self.len().

§Examples
use static_mapset::OrderedArraySet;
use std::ops::Bound::Included;

let set = OrderedArraySet::new([3, 5, 8]);
for &elem in set.range((Included(&4), Included(&8))) {
    println!("{elem}");
}
assert_eq!(Some(&5), set.range(4..).next());
Source

pub fn union<'a, O: Ordered<'a, T>>( &'a self, other: &'a O, ) -> Union<'a, T, Iter<'a, T>, O::Iter>
where T: PartialOrd,

Visits the elements representing the union, i.e., all the elements in self or other, without duplicates, in ascending order.

§Complexity

Linear in self.len() + other.len() to iterate all elements.

§Examples
use static_mapset::OrderedArraySet;

let a = OrderedArraySet::new([1]);
let b = OrderedArraySet::new([2]);

let union: Vec<_> = a.union(&b).cloned().collect();
assert_eq!(union, [1, 2]);
Source

pub fn upper_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
where T: Borrow<Q>, Q: PartialOrd + ?Sized,

Returns a [Cursor] pointing at the gap after the greatest element smaller than the given bound.

Passing Bound::Included(x) will return a cursor pointing to the gap after the greatest element smaller than or equal to x.

Passing Bound::Excluded(x) will return a cursor pointing to the gap after the greatest element smaller than x.

Passing Bound::Unbounded will return a cursor pointing to the gap after the greatest element in the set. With equal elements, Excluded points before the first equal element and Included points after the last equal element.

§Complexity

Logarithmic in self.len().

§Examples

use static_mapset::OrderedArraySet;
use std::ops::Bound;

let set = OrderedArraySet::from([1, 2, 3, 4]);

let cursor = set.upper_bound(Bound::Included(&3));
assert_eq!(cursor.peek_prev(), Some(&3));
assert_eq!(cursor.peek_next(), Some(&4));

let cursor = set.upper_bound(Bound::Excluded(&3));
assert_eq!(cursor.peek_prev(), Some(&2));
assert_eq!(cursor.peek_next(), Some(&3));

let cursor = set.upper_bound(Bound::Unbounded);
assert_eq!(cursor.peek_prev(), Some(&4));
assert_eq!(cursor.peek_next(), None);

let set = OrderedArraySet::new_unchecked([1.0, 2.0, 3.0]);
// f64::NAN is incomparable, so the cursor points to the gap after the last key.
let cursor = set.upper_bound(Bound::Included(&f64::NAN));
assert_eq!(cursor.peek_prev(), Some(&3.0));

Trait Implementations§

Source§

impl<T: Clone, const S: usize> Clone for OrderedArraySet<T, S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, const S: usize> Debug for OrderedArraySet<T, S>

Source§

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

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

impl<T: Eq, const S: usize> Eq for OrderedArraySet<T, S>

Source§

impl<T, const S: usize> From<[T; S]> for OrderedArraySet<T, S>
where T: Ord,

Source§

fn from(value: [T; S]) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash, const S: usize> Hash for OrderedArraySet<T, S>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T, const S: usize> IntoIterator for OrderedArraySet<T, S>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, S>

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<'a, T, const S: usize> IntoIterator for &'a OrderedArraySet<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: Ord, const S: usize> Ord for OrderedArraySet<T, S>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<'a, T: 'a + PartialOrd, const S: usize> Ordered<'a, T> for OrderedArraySet<T, S>

Source§

type Iter = Iter<'a, T>

Source§

fn ordered_iter(&'a self) -> Iter<'a, T>

Source§

impl<T: PartialEq, const S: usize> PartialEq for OrderedArraySet<T, S>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialOrd, const S: usize> PartialOrd for OrderedArraySet<T, S>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: PartialEq, const S: usize> StructuralPartialEq for OrderedArraySet<T, S>

Auto Trait Implementations§

§

impl<T, const S: usize> Forget for OrderedArraySet<T, S>

§

impl<T, const S: usize> Freeze for OrderedArraySet<T, S>
where [T; S]: Freeze,

§

impl<T, const S: usize> Move for OrderedArraySet<T, S>

§

impl<T, const S: usize> RefUnwindSafe for OrderedArraySet<T, S>

§

impl<T, const S: usize> Send for OrderedArraySet<T, S>
where [T; S]: Send,

§

impl<T, const S: usize> Sync for OrderedArraySet<T, S>
where [T; S]: Sync,

§

impl<T, const S: usize> Unpin for OrderedArraySet<T, S>
where [T; S]: Unpin,

§

impl<T, const S: usize> UnsafeUnpin for OrderedArraySet<T, S>

§

impl<T, const S: usize> UnwindSafe for OrderedArraySet<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<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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.