uqa_core/memory/map/owned/
set.rs1use std::borrow::Borrow;
10
11use super::{BudgetedMapIter, OwnedMap, OwnedMapIntoIter};
12
13#[derive(Debug, PartialEq, Eq)]
15pub struct OwnedSet<K>(OwnedMap<K, ()>);
16
17impl<K: Ord + Clone> Clone for OwnedSet<K> {
18 fn clone(&self) -> Self {
19 Self(self.0.clone())
20 }
21}
22
23impl<K> Default for OwnedSet<K> {
24 fn default() -> Self {
25 Self(OwnedMap::new())
26 }
27}
28
29impl<K> OwnedSet<K> {
30 pub fn new() -> Self {
31 Self::default()
32 }
33
34 pub const fn entry_bytes() -> usize {
35 OwnedMap::<K, ()>::entry_bytes()
36 }
37
38 pub fn allocated_bytes(&self) -> usize {
39 self.0.allocated_bytes()
40 }
41
42 pub fn len(&self) -> usize {
43 self.0.len()
44 }
45
46 pub fn is_empty(&self) -> bool {
47 self.0.is_empty()
48 }
49
50 pub fn iter(&self) -> OwnedSetIter<'_, K> {
51 OwnedSetIter(self.0.iter())
52 }
53}
54
55impl<K: Ord> OwnedSet<K> {
56 pub fn insert(&mut self, key: K) -> bool {
57 self.0.insert(key, ()).is_none()
58 }
59
60 pub fn contains<Q: Ord + ?Sized>(&self, key: &Q) -> bool
61 where
62 K: Borrow<Q>,
63 {
64 self.0.contains_key(key)
65 }
66}
67
68impl<K: Ord> FromIterator<K> for OwnedSet<K> {
69 fn from_iter<T: IntoIterator<Item = K>>(iter: T) -> Self {
70 Self(iter.into_iter().map(|key| (key, ())).collect())
71 }
72}
73
74pub struct OwnedSetIter<'a, K>(BudgetedMapIter<'a, K, ()>);
75
76impl<'a, K> Iterator for OwnedSetIter<'a, K> {
77 type Item = &'a K;
78
79 fn next(&mut self) -> Option<Self::Item> {
80 self.0.next().map(|(key, ())| key)
81 }
82
83 fn size_hint(&self) -> (usize, Option<usize>) {
84 self.0.size_hint()
85 }
86}
87
88impl<K> ExactSizeIterator for OwnedSetIter<'_, K> {}
89impl<K> std::iter::FusedIterator for OwnedSetIter<'_, K> {}
90
91impl<'a, K> IntoIterator for &'a OwnedSet<K> {
92 type Item = &'a K;
93 type IntoIter = OwnedSetIter<'a, K>;
94
95 fn into_iter(self) -> Self::IntoIter {
96 self.iter()
97 }
98}
99
100pub struct OwnedSetIntoIter<K>(OwnedMapIntoIter<K, ()>);
101
102impl<K> Iterator for OwnedSetIntoIter<K> {
103 type Item = K;
104
105 fn next(&mut self) -> Option<Self::Item> {
106 self.0.next().map(|(key, ())| key)
107 }
108
109 fn size_hint(&self) -> (usize, Option<usize>) {
110 self.0.size_hint()
111 }
112}
113
114impl<K> ExactSizeIterator for OwnedSetIntoIter<K> {}
115impl<K> std::iter::FusedIterator for OwnedSetIntoIter<K> {}
116
117impl<K> IntoIterator for OwnedSet<K> {
118 type Item = K;
119 type IntoIter = OwnedSetIntoIter<K>;
120
121 fn into_iter(self) -> Self::IntoIter {
122 OwnedSetIntoIter(self.0.into_iter())
123 }
124}