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