phf/
set.rs

1//! An immutable set constructed at compile time.
2use core::borrow::Borrow;
3use core::iter::IntoIterator;
4use core::fmt;
5
6use PhfHash;
7use map;
8use Map;
9
10/// An immutable set constructed at compile time.
11///
12/// ## Note
13///
14/// The fields of this struct are public so that they may be initialized by the
15/// `phf_set!` macro and code generation. They are subject to change at any
16/// time and should never be accessed directly.
17pub struct Set<T: 'static> {
18    #[doc(hidden)]
19    pub map: Map<T, ()>,
20}
21
22impl<T> fmt::Debug for Set<T> where T: fmt::Debug {
23    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
24        fmt.debug_set().entries(self).finish()
25    }
26}
27
28impl<T> Set<T> {
29    /// Returns the number of elements in the `Set`.
30    pub fn len(&self) -> usize {
31        self.map.len()
32    }
33
34    /// Returns true if the `Set` contains no elements.
35    pub fn is_empty(&self) -> bool {
36        self.len() == 0
37    }
38
39    /// Returns a reference to the set's internal static instance of the given
40    /// key.
41    ///
42    /// This can be useful for interning schemes.
43    pub fn get_key<U: ?Sized>(&self, key: &U) -> Option<&T>
44        where U: Eq + PhfHash,
45              T: Borrow<U>
46    {
47        self.map.get_key(key)
48    }
49
50    /// Returns true if `value` is in the `Set`.
51    pub fn contains<U: ?Sized>(&self, value: &U) -> bool
52        where U: Eq + PhfHash,
53              T: Borrow<U>
54    {
55        self.map.contains_key(value)
56    }
57
58    /// Returns an iterator over the values in the set.
59    ///
60    /// Values are returned in an arbitrary but fixed order.
61    pub fn iter<'a>(&'a self) -> Iter<'a, T> {
62        Iter { iter: self.map.keys() }
63    }
64}
65
66impl<T> Set<T> where T: Eq + PhfHash {
67    /// Returns true if `other` shares no elements with `self`.
68    pub fn is_disjoint(&self, other: &Set<T>) -> bool {
69        !self.iter().any(|value| other.contains(value))
70    }
71
72    /// Returns true if `other` contains all values in `self`.
73    pub fn is_subset(&self, other: &Set<T>) -> bool {
74        self.iter().all(|value| other.contains(value))
75    }
76
77    /// Returns true if `self` contains all values in `other`.
78    pub fn is_superset(&self, other: &Set<T>) -> bool {
79        other.is_subset(self)
80    }
81}
82
83impl<'a, T> IntoIterator for &'a Set<T> {
84    type Item = &'a T;
85    type IntoIter = Iter<'a, T>;
86
87    fn into_iter(self) -> Iter<'a, T> {
88        self.iter()
89    }
90}
91
92/// An iterator over the values in a `Set`.
93pub struct Iter<'a, T: 'static> {
94    iter: map::Keys<'a, T, ()>,
95}
96
97impl<'a, T> Iterator for Iter<'a, T> {
98    type Item = &'a T;
99
100    fn next(&mut self) -> Option<&'a T> {
101        self.iter.next()
102    }
103
104    fn size_hint(&self) -> (usize, Option<usize>) {
105        self.iter.size_hint()
106    }
107}
108
109impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
110    fn next_back(&mut self) -> Option<&'a T> {
111        self.iter.next_back()
112    }
113}
114
115impl<'a, T> ExactSizeIterator for Iter<'a, T> {}