use alloc::boxed::Box; use alloc::vec::Vec; use core::cmp::Ordering; use core::fmt; use core::hash::{Hash, Hasher}; use core::ops::{self, Bound, Index, RangeBounds};
/// A dynamically-sized slice of values in an [`IndexSet`]. /// /// This supports indexed operations much like a `[T]` slice, /// but not any hashed operations on the values. /// /// Unlike `IndexSet`, `Slice` does consider the order for [`PartialEq`] /// and [`Eq`], and it also implements [`PartialOrd`], [`Ord`], and [`Hash`]. #[repr(transparent)] pubstruct Slice<T> { pub(crate) entries: [Bucket<T>],
}
// SAFETY: `Slice<T>` is a transparent wrapper around `[Bucket<T>]`, // and reference lifetimes are bound together in function signatures. #[allow(unsafe_code)] impl<T> Slice<T> { pub(super) constfn from_slice(entries: &[Bucket<T>]) -> &Self { unsafe { &*(entries as *const [Bucket<T>] as *constSelf) }
}
/// Return the number of elements in the set slice. pubconstfn len(&self) -> usize { self.entries.len()
}
/// Returns true if the set slice contains no elements. pubconstfn is_empty(&self) -> bool { self.entries.is_empty()
}
/// Get a value by index. /// /// Valid indices are *0 <= index < self.len()* pubfn get_index(&self, index: usize) -> Option<&T> { self.entries.get(index).map(Bucket::key_ref)
}
/// Returns a slice of values in the given range of indices. /// /// Valid indices are *0 <= index < self.len()* pubfn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<& style='color:red'>Self> { let range = try_simplify_range(range, self.entries.len())?; self.entries.get(range).map(Self::from_slice)
}
/// Get the first value. pubfn first(&self) -> Option<&T> { self.entries.first().map(Bucket::key_ref)
}
/// Get the last value. pubfn last(&self) -> Option<&T> { self.entries.last().map(Bucket::key_ref)
}
/// Divides one slice into two at an index. /// /// ***Panics*** if `index > len`. pubfn split_at(&self, index: usize) -> (&Self, &Self) { let (first, second) = self.entries.split_at(index);
(Self::from_slice(first), Self::from_slice(second))
}
/// Returns the first value and the rest of the slice, /// or `None` if it is empty. pubfn split_first(&self) -> Option<(&T, &>Self)> { iflet [first, rest @ ..] = &self.entries {
Some((&first.key, Self::from_slice(rest)))
} else {
None
}
}
/// Returns the last value and the rest of the slice, /// or `None` if it is empty. pubfn split_last(&self) -> Option<(&T, &Self)> { iflet [rest @ .., last] = &self.entries {
Some((&last.key, Self::from_slice(rest)))
} else {
None
}
}
/// Return an iterator over the values of the set slice. pubfn iter(&self) -> Iter<'_, T> {
Iter::new(&self.entries)
}
/// Search over a sorted set for a value. /// /// Returns the position where that value is present, or the position where it can be inserted /// to maintain the sort. See [`slice::binary_search`] for more details. /// /// Computes in **O(log(n))** time, which is notably less scalable than looking the value up in /// the set this is a slice from using [`IndexSet::get_index_of`], but this can also position /// missing values. pubfn binary_search(&self, x: &T) -> Result<usize, usize> where
T: Ord,
{ self.binary_search_by(|p| p.cmp(x))
}
/// Search over a sorted set with a comparator function. /// /// Returns the position where that value is present, or the position where it can be inserted /// to maintain the sort. See [`slice::binary_search_by`] for more details. /// /// Computes in **O(log(n))** time. #[inline] pubfn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize> where
F: FnMut(&'a T) -> Ordering,
{ self.entries.binary_search_by(move |a| f(&a.key))
}
/// Search over a sorted set with an extraction function. /// /// Returns the position where that value is present, or the position where it can be inserted /// to maintain the sort. See [`slice::binary_search_by_key`] for more details. /// /// Computes in **O(log(n))** time. #[inline] pubfn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize> where
F: FnMut(&'a T) -> B,
B: Ord,
{ self.binary_search_by(|k| f(k).cmp(b))
}
/// Returns the index of the partition point of a sorted set according to the given predicate /// (the index of the first element of the second partition). /// /// See [`slice::partition_point`] for more details. /// /// Computes in **O(log(n))** time. #[must_use] pubfn partition_point<P>(&self, mut pred: P) -> usize where
P: FnMut(&T) -> bool,
{ self.entries.partition_point(move |a| pred(&a.key))
}
}
impl<'a, T> IntoIterator for &'a Slice<T> { type IntoIter = Iter<'a, T>; type Item = &'a T;
// We can't have `impl<I: RangeBounds<usize>> Index<I>` because that conflicts with `Index<usize>`. // Instead, we repeat the implementations for all the core range types.
macro_rules! impl_index {
($($range:ty),*) => {$( impl<T, S> Index<$range> for IndexSet<T, S> { type Output = Slice<T>;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.