phf/
set.rs

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