Skip to main content

OrderedSliceSet

Struct OrderedSliceSet 

Source
pub struct OrderedSliceSet<'a, T> { /* private fields */ }
Expand description

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

§Examples

use static_mapset::OrderedSliceSet;

let data = [
    "A Dance With Dragons",
    "To Kill a Mockingbird",
    "The Odyssey",
    "The Great Gatsby"
];
let books = OrderedSliceSet::new_unchecked(&data);

// 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}");
}

Implementations§

Source§

impl<'a, T> OrderedSliceSet<'a, T>

Source

pub fn new(items: &'a mut [T]) -> Self
where T: Ord,

Sorts the slice and creates an OrderedSliceSet. Duplicate keys are removed, keeping the last occurrence.

§Complexity

Linearithmic in self.len().

§Examples
use static_mapset::OrderedSliceSet;

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

pub const fn new_unchecked(ordered_items: &'a [T]) -> Self

Creates an OrderedSliceSet from an immutable slice without sorting.

The slice must be sorted in ascending order with no duplicate keys.

§Complexity

Constant.

§Examples
use static_mapset::OrderedSliceSet;

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

// Incorrect key order or duplicates cause undefined behavior:
// let map = OrderedSliceSet::new_unchecked(&[3, 2]);
// let map = OrderedSliceSet::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::OrderedSliceSet;

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

let map = OrderedSliceSet::new_unchecked(&[1, 2, 2]);
assert!(!map.is_valid());
let map = OrderedSliceSet::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::OrderedSliceSet;

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

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

pub fn difference<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::OrderedSliceSet;

let mut a_data = [1, 2];
let a = OrderedSliceSet::new(&mut a_data);

let mut b_data = [2, 3];
let b = OrderedSliceSet::new(&mut b_data);

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::OrderedSliceSet;

let empty: &[i32] = &[];
let set = OrderedSliceSet::new_unchecked(empty);
assert_eq!(set.first(), None);

let mut data = [1, 2];
let set = OrderedSliceSet::new(&mut data);
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::OrderedSliceSet;

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

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

pub fn intersection<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::OrderedSliceSet;

let mut a_data = [1, 2];
let a = OrderedSliceSet::new(&mut a_data);

let mut b_data = [2, 3];
let b = OrderedSliceSet::new(&mut b_data);

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

pub fn is_disjoint<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::OrderedSliceSet;

let mut a_data = [1, 2, 3];
let a = OrderedSliceSet::new(&mut a_data);

let mut b_data = [4, 5];
let b = OrderedSliceSet::new(&mut b_data);

let mut c_data = [1, 5];
let c = OrderedSliceSet::new(&mut c_data);

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::OrderedSliceSet;

let data: &[i32] = &[];
let set = OrderedSliceSet::new_unchecked(data);
assert!(set.is_empty());
let data = &[1];
let set = OrderedSliceSet::new_unchecked(data);
assert!(!set.is_empty());
Source

pub fn is_subset<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::OrderedSliceSet;

let mut sup_data = [1, 2, 3];
let sup = OrderedSliceSet::new(&mut sup_data);

let mut set_data = [2];
let set = OrderedSliceSet::new(&mut set_data);

let mut not_subset_data = [2, 4];
let not_subset = OrderedSliceSet::new(&mut not_subset_data);

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

pub fn is_superset<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::OrderedSliceSet;

let mut sub_data = [1, 2];
let sub = OrderedSliceSet::new(&mut sub_data);

let mut set_data = [0, 1, 2];
let set = OrderedSliceSet::new(&mut set_data);

let mut not_superset_data = [0, 1];
let not_superset = OrderedSliceSet::new(&mut not_superset_data);

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::OrderedSliceSet;

let mut data = [2, 1];
let set = OrderedSliceSet::new(&mut data);

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::OrderedSliceSet;

let empty: &[i32] = &[];
let set = OrderedSliceSet::new_unchecked(empty);
assert_eq!(set.last(), None);

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

let mut data = [1, 2];
let set = OrderedSliceSet::new(&mut data);
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::OrderedSliceSet;
use std::ops::Bound;

let data = [1, 2, 3, 4];
let set = OrderedSliceSet::new_unchecked(&data);

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 data = [1.0, 2.0, 3.0];
let set = OrderedSliceSet::new_unchecked(&data);
// 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::OrderedSliceSet;
use std::ops::Bound::Included;

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

pub fn union<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::OrderedSliceSet;

let mut a_data = [1];
let a = OrderedSliceSet::new(&mut a_data);

let mut b_data = [2];
let b = OrderedSliceSet::new(&mut b_data);

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::OrderedSliceSet;
use std::ops::Bound;

let data = [1, 2, 3, 4];
let set = OrderedSliceSet::new_unchecked(&data);

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 data = [1.0, 2.0, 3.0];
let set = OrderedSliceSet::new_unchecked(&data);
// 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<'a, T> Clone for OrderedSliceSet<'a, T>

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<'a, T> Copy for OrderedSliceSet<'a, T>

Source§

impl<'a, T: Debug> Debug for OrderedSliceSet<'a, T>

Source§

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

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

impl<'a, T: Eq> Eq for OrderedSliceSet<'a, T>

Source§

impl<'a, T: Hash> Hash for OrderedSliceSet<'a, T>

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<'a, T> IntoIterator for &'a OrderedSliceSet<'a, T>

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<'a, T: Ord> Ord for OrderedSliceSet<'a, T>

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: PartialOrd> Ordered<'a, T> for OrderedSliceSet<'a, T>

Source§

type Iter = Iter<'a, T>

Source§

fn ordered_iter(&'a self) -> Self::Iter

Source§

impl<'a, T: PartialEq> PartialEq for OrderedSliceSet<'a, T>

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<'a, T: PartialOrd> PartialOrd for OrderedSliceSet<'a, T>

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<'a, T: PartialEq> StructuralPartialEq for OrderedSliceSet<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Forget for OrderedSliceSet<'a, T>

§

impl<'a, T> Freeze for OrderedSliceSet<'a, T>
where &'a [T]: Freeze,

§

impl<'a, T> Move for OrderedSliceSet<'a, T>

§

impl<'a, T> RefUnwindSafe for OrderedSliceSet<'a, T>
where &'a [T]: RefUnwindSafe,

§

impl<'a, T> Send for OrderedSliceSet<'a, T>
where &'a [T]: Send,

§

impl<'a, T> Sync for OrderedSliceSet<'a, T>
where &'a [T]: Sync,

§

impl<'a, T> Unpin for OrderedSliceSet<'a, T>
where &'a [T]: Unpin,

§

impl<'a, T> UnsafeUnpin for OrderedSliceSet<'a, T>
where &'a [T]: UnsafeUnpin,

§

impl<'a, T> UnwindSafe for OrderedSliceSet<'a, T>
where &'a [T]: UnwindSafe,

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.