blob: 9a38b4e2adf6cde2d5e305eb1fb81f046400c54f [file] [log] [blame]
Joel Galenson2370d122020-10-12 16:02:26 -07001//! A hash set where the elements are held by weak pointers and compared by pointer.
2
3use std::collections::hash_map::RandomState;
4use std::fmt::{self, Debug};
5use std::hash::BuildHasher;
6use std::iter::FromIterator;
7use std::ops::Deref;
8
9use super::traits::*;
10use super::ptr_weak_key_hash_map as base;
11use super::by_ptr::ByPtr;
12
13pub use super::PtrWeakHashSet;
14
15impl <T: WeakElement> PtrWeakHashSet<T, RandomState>
16 where T::Strong: Deref
17{
18 /// Creates an empty `PtrWeakHashSet`.
19 pub fn new() -> Self {
20 PtrWeakHashSet(base::PtrWeakKeyHashMap::new())
21 }
22
23 /// Creates an empty `PtrWeakHashSet` with the given capacity.
24 pub fn with_capacity(capacity: usize) -> Self {
25 PtrWeakHashSet(base::PtrWeakKeyHashMap::with_capacity(capacity))
26 }
27}
28
29impl <T: WeakElement, S: BuildHasher> PtrWeakHashSet<T, S>
30 where T::Strong: Deref
31{
32 /// Creates an empty `PtrWeakHashSet` with the given capacity and hasher.
33 pub fn with_hasher(hash_builder: S) -> Self {
34 PtrWeakHashSet(base::PtrWeakKeyHashMap::with_hasher(hash_builder))
35 }
36
37 /// Creates an empty `PtrWeakHashSet` with the given capacity and hasher.
38 pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
39 PtrWeakHashSet(base::PtrWeakKeyHashMap::with_capacity_and_hasher(capacity, hash_builder))
40 }
41
42 /// Returns a reference to the map's `BuildHasher`.
43 pub fn hasher(&self) -> &S {
44 self.0.hasher()
45 }
46
47 /// Returns the number of elements the map can hold without reallocating.
48 pub fn capacity(&self) -> usize {
49 self.0.capacity()
50 }
51
52 /// Removes all mappings whose keys have expired.
53 pub fn remove_expired(&mut self) {
54 self.0.remove_expired()
55 }
56
57 /// Reserves room for additional elements.
58 pub fn reserve(&mut self, additional_capacity: usize) {
59 self.0.reserve(additional_capacity)
60 }
61
62 /// Shrinks the capacity to the minimum allowed to hold the current number of elements.
63 pub fn shrink_to_fit(&mut self) {
64 self.0.shrink_to_fit()
65 }
66
67 /// Returns an over-approximation of the number of elements.
68 pub fn len(&self) -> usize {
69 self.0.len()
70 }
71
72 /// Is the set known to be empty?
73 ///
74 /// This could answer `false` for an empty set whose elements have
75 /// expired but have yet to be collected.
76 pub fn is_empty(&self) -> bool {
77 self.len() == 0
78 }
79
80
81 /// The proportion of buckets that are used.
82 ///
83 /// This is an over-approximation because of expired elements.
84 pub fn load_factor(&self) -> f32 {
85 self.0.load_factor()
86 }
87
88 /// Removes all associations from the map.
89 pub fn clear(&mut self) {
90 self.0.clear()
91 }
92
93 /// Returns true if the map contains the specified key.
94 pub fn contains(&self, key: &T::Strong) -> bool {
95 self.0.contains_key(key)
96 }
97
98 /// Unconditionally inserts the value, returning the old value if already present. Does not
99 /// replace the key.
100 pub fn insert(&mut self, key: T::Strong) -> bool {
101 self.0.insert(key, ()).is_some()
102 }
103
104 /// Removes the entry with the given key, if it exists, and returns the value.
105 pub fn remove(&mut self, key: &T::Strong) -> bool {
106 self.0.remove(key).is_some()
107 }
108
109 /// Removes all mappings not satisfying the given predicate.
110 ///
111 /// Also removes any expired mappings.
112 pub fn retain<F>(&mut self, mut f: F)
113 where F: FnMut(T::Strong) -> bool
114 {
115 self.0.retain(|k, _| f(k))
116 }
117
118 /// Is self a subset of other?
119 pub fn is_subset<S1>(&self, other: &PtrWeakHashSet<T, S1>) -> bool
120 where S1: BuildHasher
121 {
122 self.0.domain_is_subset(&other.0)
123 }
124}
125
126/// An iterator over the elements of a set.
127pub struct Iter<'a, T: 'a>(base::Keys<'a, ByPtr<T>, ()>);
128
129impl<'a, T: WeakElement> Iterator for Iter<'a, T> {
130 type Item = T::Strong;
131
132 fn next(&mut self) -> Option<Self::Item> {
133 self.0.next()
134 }
135
136 fn size_hint(&self) -> (usize, Option<usize>) {
137 self.0.size_hint()
138 }
139}
140
141/// An iterator over the elements of a set.
142pub struct IntoIter<T>(base::IntoIter<ByPtr<T>, ()>);
143
144impl<T: WeakElement> Iterator for IntoIter<T> {
145 type Item = T::Strong;
146
147 fn next(&mut self) -> Option<Self::Item> {
148 self.0.next().map(|pair| pair.0)
149 }
150
151 fn size_hint(&self) -> (usize, Option<usize>) {
152 self.0.size_hint()
153 }
154}
155
156/// A draining iterator over the elements of a set.
157pub struct Drain<'a, T: 'a>(base::Drain<'a, ByPtr<T>, ()>);
158
159impl<'a, T: WeakElement> Iterator for Drain<'a, T> {
160 type Item = T::Strong;
161
162 fn next(&mut self) -> Option<Self::Item> {
163 self.0.next().map(|pair| pair.0)
164 }
165
166 fn size_hint(&self) -> (usize, Option<usize>) {
167 self.0.size_hint()
168 }
169}
170
171impl<T: WeakElement, S> PtrWeakHashSet<T, S>
172 where T::Strong: Deref
173{
174 /// Gets an iterator over the keys and values.
175 pub fn iter(&self) -> Iter<T> {
176 Iter(self.0.keys())
177 }
178
179 /// Gets a draining iterator, which removes all the values but retains the storage.
180 pub fn drain(&mut self) -> Drain<T> {
181 Drain(self.0.drain())
182 }
183}
184
185impl<T, S, S1> PartialEq<PtrWeakHashSet<T, S1>> for PtrWeakHashSet<T, S>
186 where T: WeakElement,
187 T::Strong: Deref,
188 S: BuildHasher,
189 S1: BuildHasher
190{
191 fn eq(&self, other: &PtrWeakHashSet<T, S1>) -> bool {
192 self.0 == other.0
193 }
194}
195
196impl<T: WeakElement, S: BuildHasher> Eq for PtrWeakHashSet<T, S>
197 where T::Strong: Deref
198{ }
199
200impl<T: WeakElement, S: BuildHasher + Default> Default for PtrWeakHashSet<T, S>
201 where T::Strong: Deref
202{
203 fn default() -> Self {
204 PtrWeakHashSet(base::PtrWeakKeyHashMap::<T, (), S>::default())
205 }
206}
207
208impl<T, S> FromIterator<T::Strong> for PtrWeakHashSet<T, S>
209 where T: WeakElement,
210 T::Strong: Deref,
211 S: BuildHasher + Default
212{
213 fn from_iter<I: IntoIterator<Item=T::Strong>>(iter: I) -> Self {
214 PtrWeakHashSet(base::PtrWeakKeyHashMap::<T, (), S>::from_iter(
215 iter.into_iter().map(|k| (k, ()))))
216 }
217}
218
219impl<T, S> Extend<T::Strong> for PtrWeakHashSet<T, S>
220 where T: WeakElement,
221 T::Strong: Deref,
222 S: BuildHasher
223{
224 fn extend<I: IntoIterator<Item=T::Strong>>(&mut self, iter: I) {
225 self.0.extend(iter.into_iter().map(|k| (k, ())))
226 }
227}
228
229impl<T, S> Debug for PtrWeakHashSet<T, S>
230 where T: WeakElement,
231 T::Strong: Debug
232{
233 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234 self.0.fmt(f)
235 }
236}
237
238impl<T: WeakElement, S> IntoIterator for PtrWeakHashSet<T, S> {
239 type Item = T::Strong;
240 type IntoIter = IntoIter<T>;
241
242 fn into_iter(self) -> Self::IntoIter {
243 IntoIter(self.0.into_iter())
244 }
245}
246
247impl<'a, T: WeakElement, S> IntoIterator for &'a PtrWeakHashSet<T, S>
248 where T::Strong: Deref
249{
250 type Item = T::Strong;
251 type IntoIter = Iter<'a, T>;
252
253 fn into_iter(self) -> Self::IntoIter {
254 Iter(self.0.keys())
255 }
256}
257