hicc_std/
std_unordered_set.rs

1use hicc::{AbiType, ClassMutPtr};
2use std::iter::Iterator;
3
4hicc::cpp! {
5    #include <unordered_set>
6}
7
8hicc::import_class! {
9    /// 对应`std::unordered_set`,
10    ///
11    /// 需和`include/hicc/std/unordered_set.hpp`接口定义保持一致.
12    #[cpp(class = "template <class T, class Hash, class Pred, class Allocator> std::unordered_set<T, Hash, Pred, Allocator>")]
13    pub class unordered_set<T> {
14        hicc::cpp! {
15            typedef typename Self::iterator iterator;
16            typedef typename Self::const_iterator const_iterator;
17        }
18        /// ```
19        /// use hicc_std::UnorderedSetInt;
20        /// let set = UnorderedSetInt::new();
21        /// assert!(set.is_empty());
22        /// ```
23        #[cpp(method = "bool empty() const")]
24        pub fn is_empty(&self) -> bool;
25
26        /// ```
27        /// use hicc_std::UnorderedSetInt;
28        /// let set = UnorderedSetInt::new();
29        /// assert_eq!(set.size(), 0);
30        /// ```
31        #[cpp(method = "size_t size() const")]
32        pub fn size(&self) -> usize;
33
34        /// ```
35        /// use hicc_std::UnorderedSetInt;
36        /// let set = UnorderedSetInt::new();
37        /// println!("set.max_size = {}", set.max_size());
38        /// ```
39        #[cpp(method = "size_t max_size() const")]
40        pub fn max_size(&self) -> usize;
41
42        /// ```
43        /// use hicc_std::UnorderedSetInt;
44        /// let mut set = UnorderedSetInt::new();
45        /// set.insert(&1);
46        /// set.clear();
47        /// assert!(set.is_empty());
48        /// ```
49        #[cpp(method = "void clear()")]
50        pub fn clear(&mut self);
51
52        /// ```
53        /// use hicc_std::UnorderedSetInt;
54        /// let mut set = UnorderedSetInt::new();
55        /// set.insert(&1);
56        /// set.swap(&mut UnorderedSetInt::new());
57        /// assert!(set.is_empty());
58        /// ```
59        #[cpp(method = "void swap(Self&)")]
60        pub fn swap(&mut self, other: &mut Self);
61
62        /// ```
63        /// use hicc_std::UnorderedSetInt;
64        /// let mut set = UnorderedSetInt::new();
65        /// set.insert(&1);
66        /// assert_eq!(set.count(&1), 1);
67        /// ```
68        #[cpp(method = "size_t count(const T&) const")]
69        pub fn count(&self, val: &T) -> usize;
70
71        hicc::cpp! {
72            static bool contains(const Self& self, const T& val) {
73                return self.find(val) != self.end();
74            }
75        }
76        /// ```
77        /// use hicc_std::UnorderedSetInt;
78        /// let mut set = UnorderedSetInt::new();
79        /// set.insert(&1);
80        /// assert!(set.contains(&1));
81        /// ```
82        #[cpp(func = "bool SelfMethods::contains(const Self&, const T&)")]
83        pub fn contains(&self, val: &T) -> bool;
84
85        /// ```
86        /// use hicc_std::UnorderedSetInt;
87        /// let mut set = UnorderedSetInt::new();
88        /// set.insert(&1);
89        /// set.assign(&UnorderedSetInt::new());
90        /// assert!(set.is_empty());
91        /// ```
92        #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
93        pub fn assign(&mut self, other: &Self);
94
95        hicc::cpp! {
96            static bool insert(Self& self, const T& val) {
97                return self.insert(val).second;
98            }
99        }
100        /// ```
101        /// use hicc_std::UnorderedSetInt;
102        /// let mut set = UnorderedSetInt::new();
103        /// assert!(set.insert(&1));
104        /// assert!(!set.insert(&1));
105        /// ```
106        #[cpp(func = "bool SelfMethods::insert(Self&, const T&)")]
107        pub fn insert(&mut self, val: &T) -> bool;
108
109        /// ```
110        /// use hicc_std::UnorderedSetInt;
111        /// let mut set = UnorderedSetInt::new();
112        /// set.insert(&1);
113        /// assert_eq!(set.erase(&1), 1);
114        /// ```
115        #[cpp(method = "size_t erase(const T&)")]
116        pub fn erase(&mut self, val: &T) -> usize;
117
118        #[cpp(method = "const_iterator begin() const")]
119        unsafe fn begin(&self) -> *mut CppUnorderedSetIter<T>;
120        #[cpp(method = "const_iterator end() const")]
121        unsafe fn end(&self) -> *mut CppUnorderedSetIter<T>;
122
123    }
124
125    unsafe impl<T: AbiType + Sync> Send for unordered_set<T> {}
126    unsafe impl<T: AbiType + Sync> Sync for unordered_set<T> {}
127
128    #[cpp(class = "template <class T, class Hash, class Pred, class Allocator> std::unordered_set<T, Hash, Pred, Allocator>::const_iterator")]
129    class CppUnorderedSetIter<T> {
130        hicc::cpp! {
131            static const T& next(Self& self) {
132                return *self++;
133            }
134        }
135        #[cpp(func = "const T& next(Self&)")]
136        unsafe fn next(&mut self) -> &T;
137        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
138        fn equal(&self, other: &Self) -> bool;
139    }
140}
141
142impl<T: AbiType> unordered_set<T> {
143    /// ```
144    /// use hicc_std::UnorderedSetInt;
145    /// let mut set = UnorderedSetInt::new();
146    /// set.insert(&1);
147    /// set.insert(&2);
148    /// set.iter().for_each(|v| {println!("{v}");});
149    /// ```
150    pub fn iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
151        UnorderedSetIter {
152            beg: unsafe { self.begin() },
153            end: unsafe { self.end() },
154        }
155    }
156}
157
158/// 对应`std::unordered_set<T>::const_iterator`
159struct UnorderedSetIter<'a, T: AbiType + 'static> {
160    beg: ClassMutPtr<'a, CppUnorderedSetIter<T>>,
161    end: ClassMutPtr<'a, CppUnorderedSetIter<T>>,
162}
163
164impl<'a, T: AbiType + 'static> Iterator for UnorderedSetIter<'a, T> {
165    type Item = T::OutputRef<'a>;
166    fn next(&mut self) -> Option<Self::Item> {
167        if !self.beg.equal(&self.end) {
168            return unsafe { Some(self.beg.as_deref_mut().next()) };
169        }
170        None
171    }
172}
173
174hicc::import_class! {
175    /// 对应`std::unordered_multiset`,
176    ///
177    /// 需和`include/hicc/std/unordered_set.hpp`接口定义保持一致.
178    #[cpp(class = "template <class T, class Hash, class Pred, class Allocator> std::unordered_multiset<T, Hash, Pred, Allocator>")]
179    pub class unordered_multiset<T> {
180        hicc::cpp! {
181            typedef typename Self::iterator iterator;
182            typedef typename Self::const_iterator const_iterator;
183        }
184        /// ```
185        /// use hicc_std::UnorderedMultiSetInt;
186        /// let set = UnorderedMultiSetInt::new();
187        /// assert!(set.is_empty());
188        /// ```
189        #[cpp(method = "bool empty() const")]
190        pub fn is_empty(&self) -> bool;
191
192        /// ```
193        /// use hicc_std::UnorderedMultiSetInt;
194        /// let set = UnorderedMultiSetInt::new();
195        /// assert_eq!(set.size(), 0);
196        /// ```
197        #[cpp(method = "size_t size() const")]
198        pub fn size(&self) -> usize;
199
200        /// ```
201        /// use hicc_std::UnorderedMultiSetInt;
202        /// let set = UnorderedMultiSetInt::new();
203        /// println!("set.max_size = {}", set.max_size());
204        /// ```
205        #[cpp(method = "size_t max_size() const")]
206        pub fn max_size(&self) -> usize;
207
208        /// ```
209        /// use hicc_std::UnorderedMultiSetInt;
210        /// let mut set = UnorderedMultiSetInt::new();
211        /// set.insert(&1);
212        /// set.clear();
213        /// assert!(set.is_empty());
214        /// ```
215        #[cpp(method = "void clear()")]
216        pub fn clear(&mut self);
217
218        /// ```
219        /// use hicc_std::UnorderedMultiSetInt;
220        /// let mut set = UnorderedMultiSetInt::new();
221        /// set.insert(&1);
222        /// set.swap(&mut UnorderedMultiSetInt::new());
223        /// assert!(set.is_empty());
224        /// ```
225        #[cpp(method = "void swap(Self&)")]
226        pub fn swap(&mut self, other: &mut Self);
227
228        /// ```
229        /// use hicc_std::UnorderedMultiSetInt;
230        /// let mut set = UnorderedMultiSetInt::new();
231        /// set.insert(&1);
232        /// assert_eq!(set.count(&1), 1);
233        /// ```
234        #[cpp(method = "size_t count(const T&) const")]
235        pub fn count(&self, val: &T) -> usize;
236
237        hicc::cpp! {
238            static bool contains(const Self& self, const T& val) {
239                return self.find(val) != self.end();
240            }
241        }
242        /// ```
243        /// use hicc_std::UnorderedMultiSetInt;
244        /// let mut set = UnorderedMultiSetInt::new();
245        /// set.contains(&1);
246        /// assert!(set.is_empty());
247        /// ```
248        #[cpp(func = "bool SelfMethods::contains(const Self&, const T&)")]
249        pub fn contains(&self, val: &T) -> bool;
250
251        /// ```
252        /// use hicc_std::UnorderedMultiSetInt;
253        /// let mut set = UnorderedMultiSetInt::new();
254        /// set.insert(&1);
255        /// set.assign(&UnorderedMultiSetInt::new());
256        /// assert!(set.is_empty());
257        /// ```
258        #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
259        pub fn assign(&mut self, other: &Self);
260
261        /// ```
262        /// use hicc_std::UnorderedMultiSetInt;
263        /// let mut set = UnorderedMultiSetInt::new();
264        /// set.insert(&1);
265        /// set.insert(&1);
266        /// assert_eq!(set.size(), 2);
267        /// ```
268        #[cpp(method = "iterator insert(const T&)")]
269        pub fn insert(&mut self, val: &T);
270
271        /// ```
272        /// use hicc_std::UnorderedMultiSetInt;
273        /// let mut set = UnorderedMultiSetInt::new();
274        /// set.insert(&1);
275        /// set.insert(&1);
276        /// assert_eq!(set.erase(&1), 2);
277        /// ```
278        #[cpp(method = "size_t erase(const T&)")]
279        pub fn erase(&mut self, val: &T) -> usize;
280
281        #[cpp(method = "const_iterator begin() const")]
282        unsafe fn begin(&self) -> *mut CppUnorderedMultiSetIter<T>;
283        #[cpp(method = "const_iterator end() const")]
284        unsafe fn end(&self) -> *mut CppUnorderedMultiSetIter<T>;
285
286    }
287
288    unsafe impl<T: AbiType + Sync> Send for unordered_multiset<T> {}
289    unsafe impl<T: AbiType + Sync> Sync for unordered_multiset<T> {}
290
291    #[cpp(class = "template <class T, class Hash, class Pred, class Allocator> std::unordered_multiset<T, Hash, Pred, Allocator>::const_iterator")]
292    class CppUnorderedMultiSetIter<T> {
293        hicc::cpp! {
294            static const T& next(Self& self) {
295                return *self++;
296            }
297        }
298        #[cpp(func = "const T& SelfMethods::next(Self&)")]
299        fn next(&mut self) -> &T;
300        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
301        fn equal(&self, other: &Self) -> bool;
302    }
303}
304
305impl<T: AbiType> unordered_multiset<T> {
306    /// ```
307    /// use hicc_std::UnorderedMultiSetInt;
308    /// let mut set = UnorderedMultiSetInt::new();
309    /// set.insert(&1);
310    /// set.insert(&2);
311    /// set.insert(&1);
312    /// set.iter().for_each(|v| { println!("{v}"); });
313    /// ```
314    pub fn iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
315        UnorderedMultiSetIter {
316            beg: unsafe { self.begin() },
317            end: unsafe { self.end() },
318        }
319    }
320}
321
322/// 对应`std::unordered_multiset<T>::const_iterator`
323struct UnorderedMultiSetIter<'a, T: AbiType + 'static> {
324    beg: ClassMutPtr<'a, CppUnorderedMultiSetIter<T>>,
325    end: ClassMutPtr<'a, CppUnorderedMultiSetIter<T>>,
326}
327
328impl<'a, T: AbiType + 'static> Iterator for UnorderedMultiSetIter<'a, T> {
329    type Item = T::OutputRef<'a>;
330    fn next(&mut self) -> Option<Self::Item> {
331        if self.beg.equal(&self.end) {
332            return unsafe { Some(self.beg.as_deref_mut().next()) };
333        }
334        None
335    }
336}