//! An order-preserving immutable set constructed at compile time. usecrate::{ordered_map, OrderedMap, PhfHash}; use core::fmt; use core::iter::FusedIterator; use core::iter::IntoIterator; use phf_shared::PhfBorrow;
/// An order-preserving immutable set constructed at compile time. /// /// Unlike a `Set`, iteration order is guaranteed to match the definition /// order. /// /// ## Note /// /// The fields of this struct are public so that they may be initialized by the /// `phf_ordered_set!` macro and code generation. They are subject to change at /// any time and should never be accessed directly. pubstruct OrderedSet<T: 'static> { #[doc(hidden)] pub map: OrderedMap<T, ()>,
}
impl<T> fmt::Debug for OrderedSet<T> where
T: fmt::Debug,
{ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_set().entries(self).finish()
}
}
impl<T> OrderedSet<T> { /// Returns the number of elements in the `OrderedSet`. #[inline] pubconstfn len(&self) -> usize { self.map.len()
}
/// Returns true if the `OrderedSet` contains no elements. #[inline] pubconstfn is_empty(&self) -> bool { self.len() == 0
}
/// Returns a reference to the set's internal static instance of the given /// key. /// /// This can be useful for interning schemes. pubfn get_key<U: ?Sized>(&self, key: &U) -> Option<&T> where
U: Eq + PhfHash,
T: PhfBorrow<U>,
{ self.map.get_key(key)
}
/// Returns the index of the key within the list used to initialize /// the ordered set. pubfn get_index<U: ?Sized>(&self, key: &U) -> Option<usize> where
U: Eq + PhfHash,
T: PhfBorrow<U>,
{ self.map.get_index(key)
}
/// Returns a reference to the key at an index /// within the list used to initialize the ordered set. See `.get_index(key)`. pubfn index(&self, index: usize) -> Option<&T> { self.map.index(index).map(|(k, &())| k)
}
/// Returns true if `value` is in the `OrderedSet`. pubfn contains<U: ?Sized>(&self, value: &U) -> bool where
U: Eq + PhfHash,
T: PhfBorrow<U>,
{ self.map.contains_key(value)
}
/// Returns an iterator over the values in the set. /// /// Values are returned in the same order in which they were defined. pubfn iter(&self) -> Iter<'_, T> {
Iter {
iter: self.map.keys(),
}
}
}
impl<T> OrderedSet<T> where
T: Eq + PhfHash + PhfBorrow<T>,
{ /// Returns true if `other` shares no elements with `self`. #[inline] pubfn is_disjoint(&self, other: &OrderedSet<T>) -> bool {
!self.iter().any(|value| other.contains(value))
}
/// Returns true if `other` contains all values in `self`. #[inline] pubfn is_subset(&self, other: &OrderedSet<T>) -> bool { self.iter().all(|value| other.contains(value))
}
/// Returns true if `self` contains all values in `other`. #[inline] pubfn is_superset(&self, other: &OrderedSet<T>) -> bool {
other.is_subset(self)
}
}
impl<'a, T> IntoIterator for &'a OrderedSet<T> { type Item = &'a T; type IntoIter = Iter<'a, 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.