1//! An order-preserving immutable set constructed at compile time.
2use core::borrow::Borrow;
3use core::iter::IntoIterator;
4use core::fmt;
5use ordered_map;
6use {PhfHash, OrderedMap};
78/// An order-preserving immutable set constructed at compile time.
9///
10/// Unlike a `Set`, iteration order is guaranteed to match the definition
11/// order.
12///
13/// ## Note
14///
15/// The fields of this struct are public so that they may be initialized by the
16/// `phf_ordered_set!` macro and code generation. They are subject to change at
17/// any time and should never be accessed directly.
18pub struct OrderedSet<T: 'static> {
19#[doc(hidden)]
20pub map: OrderedMap<T, ()>,
21}
2223impl<T> fmt::Debug for OrderedSet<T> where T: fmt::Debug {
24fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
25 fmt.debug_set().entries(self).finish()
26 }
27}
2829impl<T> OrderedSet<T> {
30/// Returns the number of elements in the `OrderedSet`.
31pub fn len(&self) -> usize {
32self.map.len()
33 }
3435/// Returns true if the `OrderedSet` contains no elements.
36pub fn is_empty(&self) -> bool {
37self.len() == 0
38}
3940/// Returns a reference to the set's internal static instance of the given
41 /// key.
42 ///
43 /// This can be useful for interning schemes.
44pub fn get_key<U: ?Sized>(&self, key: &U) -> Option<&T>
45where U: Eq + PhfHash,
46 T: Borrow<U>
47 {
48self.map.get_key(key)
49 }
5051/// Returns the index of the key within the list used to initialize
52 /// the ordered set.
53pub fn get_index<U: ?Sized>(&self, key: &U) -> Option<usize>
54where U: Eq + PhfHash,
55 T: Borrow<U>
56 {
57self.map.get_index(key)
58 }
5960/// Returns a reference to the key at an index
61 /// within the list used to initialize the ordered set. See `.get_index(key)`.
62pub fn index(&self, index: usize) -> Option<&T> {
63self.map.index(index).map(|(k, &())| k)
64 }
6566/// Returns true if `value` is in the `Set`.
67pub fn contains<U: ?Sized>(&self, value: &U) -> bool
68where U: Eq + PhfHash,
69 T: Borrow<U>
70 {
71self.map.contains_key(value)
72 }
7374/// Returns an iterator over the values in the set.
75 ///
76 /// Values are returned in the same order in which they were defined.
77pub fn iter<'a>(&'a self) -> Iter<'a, T> {
78 Iter { iter: self.map.keys() }
79 }
80}
8182impl<T> OrderedSet<T> where T: Eq + PhfHash {
83/// Returns true if `other` shares no elements with `self`.
84#[inline]
85pub fn is_disjoint(&self, other: &OrderedSet<T>) -> bool {
86 !self.iter().any(|value| other.contains(value))
87 }
8889/// Returns true if `other` contains all values in `self`.
90#[inline]
91pub fn is_subset(&self, other: &OrderedSet<T>) -> bool {
92self.iter().all(|value| other.contains(value))
93 }
9495/// Returns true if `self` contains all values in `other`.
96#[inline]
97pub fn is_superset(&self, other: &OrderedSet<T>) -> bool {
98 other.is_subset(self)
99 }
100}
101102impl<'a, T> IntoIterator for &'a OrderedSet<T> {
103type Item = &'a T;
104type IntoIter = Iter<'a, T>;
105106fn into_iter(self) -> Iter<'a, T> {
107self.iter()
108 }
109}
110111/// An iterator over the values in a `OrderedSet`.
112pub struct Iter<'a, T: 'a> {
113 iter: ordered_map::Keys<'a, T, ()>,
114}
115116impl<'a, T> Iterator for Iter<'a, T> {
117type Item = &'a T;
118119#[inline]
120fn next(&mut self) -> Option<&'a T> {
121self.iter.next()
122 }
123124#[inline]
125fn size_hint(&self) -> (usize, Option<usize>) {
126self.iter.size_hint()
127 }
128}
129130impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
131#[inline]
132fn next_back(&mut self) -> Option<&'a T> {
133self.iter.next_back()
134 }
135}
136137impl<'a, T> ExactSizeIterator for Iter<'a, T> {}