1use core::borrow::Borrow;
3use core::iter::IntoIterator;
4use core::fmt;
5
6use PhfHash;
7use map;
8use Map;
9
10pub 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 pub fn len(&self) -> usize {
31 self.map.len()
32 }
33
34 pub fn is_empty(&self) -> bool {
36 self.len() == 0
37 }
38
39 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 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 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 pub fn is_disjoint(&self, other: &Set<T>) -> bool {
69 !self.iter().any(|value| other.contains(value))
70 }
71
72 pub fn is_subset(&self, other: &Set<T>) -> bool {
74 self.iter().all(|value| other.contains(value))
75 }
76
77 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
92pub 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> {}