1use core::borrow::Borrow;
3use core::iter::IntoIterator;
4use core::fmt;
5
6use crate::{map, Map, PhfHash};
7
8pub 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 pub fn len(&self) -> usize {
29 self.map.len()
30 }
31
32 pub fn is_empty(&self) -> bool {
34 self.len() == 0
35 }
36
37 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 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 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 pub fn is_disjoint(&self, other: &Set<T>) -> bool {
67 !self.iter().any(|value| other.contains(value))
68 }
69
70 pub fn is_subset(&self, other: &Set<T>) -> bool {
72 self.iter().all(|value| other.contains(value))
73 }
74
75 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
90pub 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> {}