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>
impl<'a, T> OrderedSliceSet<'a, T>
Sourcepub const fn new_unchecked(ordered_items: &'a [T]) -> Self
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]);Sourcepub fn is_valid(&self) -> boolwhere
T: PartialOrd,
pub fn is_valid(&self) -> boolwhere
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());Sourcepub fn contains<Q>(&self, value: &Q) -> bool
pub fn contains<Q>(&self, value: &Q) -> bool
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);Sourcepub fn difference<O: Ordered<'a, T>>(
&'a self,
other: &'a O,
) -> Difference<'a, T, Iter<'a, T>, O::Iter>where
T: PartialOrd,
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]);Sourcepub fn first(&self) -> Option<&T>
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));Sourcepub fn get<Q>(&self, value: &Q) -> Option<&T>
pub fn get<Q>(&self, value: &Q) -> Option<&T>
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);Sourcepub fn intersection<O: Ordered<'a, T>>(
&'a self,
other: &'a O,
) -> Intersection<'a, T, Iter<'a, T>, O::Iter>where
T: PartialOrd,
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]);Sourcepub fn is_disjoint<O: Ordered<'a, T>>(&'a self, other: &'a O) -> boolwhere
T: PartialOrd,
pub fn is_disjoint<O: Ordered<'a, T>>(&'a self, other: &'a O) -> boolwhere
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);Sourcepub const fn is_empty(&self) -> bool
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());Sourcepub fn is_subset<O: Ordered<'a, T>>(&'a self, other: &'a O) -> boolwhere
T: PartialOrd,
pub fn is_subset<O: Ordered<'a, T>>(&'a self, other: &'a O) -> boolwhere
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);Sourcepub fn is_superset<O: Ordered<'a, T>>(&'a self, other: &'a O) -> boolwhere
T: PartialOrd,
pub fn is_superset<O: Ordered<'a, T>>(&'a self, other: &'a O) -> boolwhere
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);Sourcepub fn iter(&self) -> Iter<'_, T>
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]);Sourcepub fn last(&self) -> Option<&T>
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));Sourcepub fn lower_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
pub fn lower_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
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));Sourcepub fn range<Q, R>(&self, range: R) -> Iter<'_, T>
pub fn range<Q, R>(&self, range: R) -> Iter<'_, T>
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());Sourcepub fn union<O: Ordered<'a, T>>(
&'a self,
other: &'a O,
) -> Union<'a, T, Iter<'a, T>, O::Iter>where
T: PartialOrd,
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]);Sourcepub fn upper_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
pub fn upper_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, T>
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));