phf/
ordered_set.rs

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};
7
8/// 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)]
20    pub map: OrderedMap<T, ()>,
21}
22
23impl<T> fmt::Debug for OrderedSet<T> where T: fmt::Debug {
24    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
25        fmt.debug_set().entries(self).finish()
26    }
27}
28
29impl<T> OrderedSet<T> {
30    /// Returns the number of elements in the `OrderedSet`.
31    pub fn len(&self) -> usize {
32        self.map.len()
33    }
34
35    /// Returns true if the `OrderedSet` contains no elements.
36    pub fn is_empty(&self) -> bool {
37        self.len() == 0
38    }
39
40    /// Returns a reference to the set's internal static instance of the given
41    /// key.
42    ///
43    /// This can be useful for interning schemes.
44    pub fn get_key<U: ?Sized>(&self, key: &U) -> Option<&T>
45        where U: Eq + PhfHash,
46              T: Borrow<U>
47    {
48        self.map.get_key(key)
49    }
50
51    /// Returns the index of the key within the list used to initialize
52    /// the ordered set.
53    pub fn get_index<U: ?Sized>(&self, key: &U) -> Option<usize>
54        where U: Eq + PhfHash,
55              T: Borrow<U>
56    {
57        self.map.get_index(key)
58    }
59
60    /// Returns a reference to the key at an index
61    /// within the list used to initialize the ordered set. See `.get_index(key)`.
62    pub fn index(&self, index: usize) -> Option<&T> {
63        self.map.index(index).map(|(k, &())| k)
64    }
65
66    /// Returns true if `value` is in the `Set`.
67    pub fn contains<U: ?Sized>(&self, value: &U) -> bool
68        where U: Eq + PhfHash,
69              T: Borrow<U>
70    {
71        self.map.contains_key(value)
72    }
73
74    /// Returns an iterator over the values in the set.
75    ///
76    /// Values are returned in the same order in which they were defined.
77    pub fn iter<'a>(&'a self) -> Iter<'a, T> {
78        Iter { iter: self.map.keys() }
79    }
80}
81
82impl<T> OrderedSet<T> where T: Eq + PhfHash {
83    /// Returns true if `other` shares no elements with `self`.
84    #[inline]
85    pub fn is_disjoint(&self, other: &OrderedSet<T>) -> bool {
86        !self.iter().any(|value| other.contains(value))
87    }
88
89    /// Returns true if `other` contains all values in `self`.
90    #[inline]
91    pub fn is_subset(&self, other: &OrderedSet<T>) -> bool {
92        self.iter().all(|value| other.contains(value))
93    }
94
95    /// Returns true if `self` contains all values in `other`.
96    #[inline]
97    pub fn is_superset(&self, other: &OrderedSet<T>) -> bool {
98        other.is_subset(self)
99    }
100}
101
102impl<'a, T> IntoIterator for &'a OrderedSet<T> {
103    type Item = &'a T;
104    type IntoIter = Iter<'a, T>;
105
106    fn into_iter(self) -> Iter<'a, T> {
107        self.iter()
108    }
109}
110
111/// An iterator over the values in a `OrderedSet`.
112pub struct Iter<'a, T: 'a> {
113    iter: ordered_map::Keys<'a, T, ()>,
114}
115
116impl<'a, T> Iterator for Iter<'a, T> {
117    type Item = &'a T;
118
119    #[inline]
120    fn next(&mut self) -> Option<&'a T> {
121        self.iter.next()
122    }
123
124    #[inline]
125    fn size_hint(&self) -> (usize, Option<usize>) {
126        self.iter.size_hint()
127    }
128}
129
130impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
131    #[inline]
132    fn next_back(&mut self) -> Option<&'a T> {
133        self.iter.next_back()
134    }
135}
136
137impl<'a, T> ExactSizeIterator for Iter<'a, T> {}