hicc_std/
std_set.rs

1use hicc::{AbiType, ClassMutPtr};
2use std::iter::Iterator;
3
4hicc::cpp! {
5    #include <set>
6}
7
8hicc::import_class! {
9    #[cpp(class = "template<class T, class Compare, class Allocator> std::set<T, Compare, Allocator>")]
10    pub class set<T> {
11        hicc::cpp! {
12            typedef typename Self::const_iterator const_iterator;
13            typedef typename Self::const_reverse_iterator const_reverse_iterator;
14        }
15        /// ```
16        /// use hicc_std::SetInt;
17        /// let set = SetInt::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::SetInt;
25        /// let set = SetInt::new();
26        /// assert_eq!(set.size(), 0_usize);
27        /// ```
28        #[cpp(method = "size_t size() const")]
29        pub fn size(&self) -> usize;
30
31        /// ```
32        /// use hicc_std::SetInt;
33        /// let set = SetInt::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::SetInt;
41        /// let mut set = SetInt::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::SetInt;
51        /// let mut set = SetInt::new();
52        /// set.insert(&1);
53        /// set.swap(&mut SetInt::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::SetInt;
61        /// let mut set = SetInt::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::SetInt;
75        /// let mut set = SetInt::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::SetInt;
84        /// let mut set = SetInt::new();
85        /// set.insert(&1);
86        /// set.assign(&SetInt::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::SetInt;
99        /// let mut set = SetInt::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::SetInt;
108        /// let mut set = SetInt::new();
109        /// assert!(set.insert(&1));
110        /// set.erase(&1);
111        /// assert!(set.insert(&1));
112        /// ```
113        #[cpp(method = "size_t erase(const T&)")]
114        pub fn erase(&mut self, val: &T) -> usize;
115
116        #[cpp(method = "const_iterator find(const T&) const")]
117        unsafe fn find(&self, val: &T) -> *mut CppSetIter<T>;
118        #[cpp(method = "const_iterator lower_bound(const T&) const")]
119        unsafe fn lower_bound(&self, val: &T) -> *mut CppSetIter<T>;
120        #[cpp(method = "const_iterator upper_bound(const T&) const")]
121        unsafe fn upper_bound(&self, val: &T) -> *mut CppSetIter<T>;
122        #[cpp(method = "const_iterator begin() const")]
123        unsafe fn begin(&self) -> *mut CppSetIter<T>;
124        #[cpp(method = "const_iterator end() const")]
125        unsafe fn end(&self) -> *mut CppSetIter<T>;
126        #[cpp(method = "const_reverse_iterator rbegin() const")]
127        unsafe fn rbegin(&self) -> *mut CppSetRevIter<T>;
128        #[cpp(method = "const_reverse_iterator rend() const")]
129        unsafe fn rend(&self) -> *mut CppSetRevIter<T>;
130    }
131
132    unsafe impl<T: AbiType + Sync> Send for set<T> {}
133    unsafe impl<T: AbiType + Sync> Sync for set<T> {}
134
135    #[cpp(class = "template<class T, class Compare, class Allocator> std::set<T, Compare, Allocator>::const_iterator")]
136    class CppSetIter<T> {
137        hicc::cpp! {
138            typedef typename SelfContainer::const_reverse_iterator const_reverse_iterator;
139            static const T& next(Self& self) {
140                return *self++;
141            }
142        }
143        #[cpp(func = "const T& SelfMethods::next(Self&)")]
144        unsafe fn next(&mut self) -> &T;
145        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
146        fn equal(&self, other: &Self) -> bool;
147        #[cpp(func = "const_reverse_iterator hicc::make_constructor<const_reverse_iterator, Self>(Self&&)")]
148        fn into_reverse(self) -> *mut CppSetRevIter<T>;
149    }
150
151    #[cpp(class = "template<class T, class Compare, class Allocator> std::set<T, Compare, Allocator>::const_reverse_iterator")]
152    class CppSetRevIter<T> {
153        hicc::cpp! {
154            static const T& next(Self& self) {
155                return *self++;
156            }
157        }
158        #[cpp(func = "const T& SelfMethods::next(Self&)")]
159        unsafe fn next(&mut self) -> &T;
160        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
161        fn equal(&self, other: &Self) -> bool;
162    }
163}
164
165impl<T: AbiType> set<T> {
166    /// ```
167    /// use hicc_std::SetInt;
168    /// let mut set = SetInt::new();
169    /// set.insert(&1);
170    /// set.insert(&2);
171    /// set.iter().for_each(|v| println!("value = {v}"));
172    /// ```
173    pub fn iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
174        SetIter {
175            beg: unsafe { self.begin() },
176            end: unsafe { self.end() },
177        }
178    }
179
180    /// ```
181    /// use hicc_std::SetInt;
182    /// let mut set = SetInt::new();
183    /// set.insert(&1);
184    /// set.insert(&2);
185    /// set.rev_iter().for_each(|v| println!("value = {v}"));
186    /// ```
187    pub fn rev_iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
188        SetRevIter {
189            beg: unsafe { self.rbegin() },
190            end: unsafe { self.rend() },
191        }
192    }
193
194    /// ```
195    /// use hicc_std::SetInt;
196    /// let mut set = SetInt::new();
197    /// set.insert(&1);
198    /// set.insert(&2);
199    /// set.insert(&3);
200    /// let mut it = set.iter_lower_upper_bound(Some(&1), Some(&2));
201    /// assert_eq!(it.next(), Some(&1));
202    /// assert_eq!(it.next(), Some(&2));
203    /// assert_eq!(it.next(), None);
204    /// ```
205    pub fn iter_lower_upper_bound(
206        &self,
207        lower_key: Option<&T::InputType>,
208        upper_key: Option<&T::InputType>,
209    ) -> impl Iterator<Item = T::OutputRef<'_>> {
210        let beg = if let Some(key) = lower_key {
211            unsafe { self.lower_bound(key) }
212        } else {
213            unsafe { self.begin() }
214        };
215        let end = if let Some(key) = upper_key {
216            unsafe { self.upper_bound(key) }
217        } else {
218            unsafe { self.end() }
219        };
220        SetIter { beg, end }
221    }
222
223    /// ```
224    /// use hicc_std::SetInt;
225    /// let mut set = SetInt::new();
226    /// set.insert(&1);
227    /// set.insert(&2);
228    /// set.insert(&3);
229    /// let mut it = set.rev_iter_lower_upper_bound(Some(&1), Some(&2));
230    /// assert_eq!(it.next(), Some(&2));
231    /// assert_eq!(it.next(), Some(&1));
232    /// assert_eq!(it.next(), None);
233    /// ```
234    pub fn rev_iter_lower_upper_bound(
235        &self,
236        lower_key: Option<&T::InputType>,
237        upper_key: Option<&T::InputType>,
238    ) -> impl Iterator<Item = T::OutputRef<'_>> {
239        let beg = if let Some(key) = upper_key {
240            unsafe { self.upper_bound(key).into_value().into_reverse() }
241        } else {
242            unsafe { self.rbegin() }
243        };
244        let end = if let Some(key) = lower_key {
245            unsafe { self.lower_bound(key).into_value().into_reverse() }
246        } else {
247            unsafe { self.rend() }
248        };
249        SetRevIter { beg, end }
250    }
251}
252
253/// 对应`std::set<T>::const_iterator`
254struct SetIter<'a, T: AbiType + 'static> {
255    beg: ClassMutPtr<'a, CppSetIter<T>>,
256    end: ClassMutPtr<'a, CppSetIter<T>>,
257}
258
259impl<'a, T: AbiType + 'static> Iterator for SetIter<'a, T> {
260    type Item = T::OutputRef<'a>;
261    fn next(&mut self) -> Option<Self::Item> {
262        if !self.beg.equal(&self.end) {
263            return unsafe { Some(self.beg.as_deref_mut().next()) };
264        }
265        None
266    }
267}
268
269/// 对应`std::set<T>::const_reverse_iterator`
270struct SetRevIter<'a, T: AbiType + 'static> {
271    beg: ClassMutPtr<'a, CppSetRevIter<T>>,
272    end: ClassMutPtr<'a, CppSetRevIter<T>>,
273}
274
275impl<'a, T: AbiType + 'static> Iterator for SetRevIter<'a, T> {
276    type Item = T::OutputRef<'a>;
277    fn next(&mut self) -> Option<Self::Item> {
278        if !self.beg.equal(&self.end) {
279            return unsafe { Some(self.beg.as_deref_mut().next()) };
280        }
281        None
282    }
283}
284
285hicc::import_class! {
286    #[cpp(class = "template<class T, class Compare, class Allocator> std::multiset<T, Compare, Allocator>")]
287    pub class multiset<T> {
288        hicc::cpp! {
289            typedef typename Self::iterator iterator;
290            typedef typename Self::const_iterator const_iterator;
291            typedef typename Self::const_reverse_iterator const_reverse_iterator;
292        }
293        /// ```
294        /// use hicc_std::MultiSetInt;
295        /// let set = MultiSetInt::new();
296        /// assert!(set.is_empty());
297        /// ```
298        #[cpp(method = "bool empty() const")]
299        pub fn is_empty(&self) -> bool;
300
301        /// ```
302        /// use hicc_std::MultiSetInt;
303        /// let set = MultiSetInt::new();
304        /// assert_eq!(set.size(), 0);
305        /// ```
306        #[cpp(method = "size_t size() const")]
307        pub fn size(&self) -> usize;
308
309        /// ```
310        /// use hicc_std::MultiSetInt;
311        /// let set = MultiSetInt::new();
312        /// println!("set.max_size = {}", set.max_size());
313        /// ```
314        #[cpp(method = "size_t max_size() const")]
315        pub fn max_size(&self) -> usize;
316
317        /// ```
318        /// use hicc_std::MultiSetInt;
319        /// let mut set = MultiSetInt::new();
320        /// set.insert(&1);
321        /// set.clear();
322        /// assert_eq!(set.size(), 0);
323        /// ```
324        #[cpp(method = "void clear()")]
325        pub fn clear(&mut self);
326
327        /// ```
328        /// use hicc_std::MultiSetInt;
329        /// let mut set = MultiSetInt::new();
330        /// set.insert(&1);
331        /// set.swap(&mut MultiSetInt::new());
332        /// assert_eq!(set.size(), 0);
333        /// ```
334        #[cpp(method = "void swap(Self&)")]
335        pub fn swap(&mut self, other: &mut Self);
336
337        /// ```
338        /// use hicc_std::MultiSetInt;
339        /// let mut set = MultiSetInt::new();
340        /// set.insert(&1);
341        /// assert_eq!(set.count(&1), 1);
342        /// ```
343        #[cpp(method = "size_t count(const T&) const")]
344        pub fn count(&self, val: &T) -> usize;
345
346        hicc::cpp! {
347            static bool contains(const Self& self, const T& val) {
348                return self.find(val) != self.end();
349            }
350        }
351        /// ```
352        /// use hicc_std::MultiSetInt;
353        /// let mut set = MultiSetInt::new();
354        /// set.insert(&1);
355        /// assert!(set.contains(&1));
356        /// ```
357        #[cpp(func = "bool SelfMethods::contains(const Self&, const T&)")]
358        pub fn contains(&self, val: &T) -> bool;
359
360        /// ```
361        /// use hicc_std::MultiSetInt;
362        /// let mut set = MultiSetInt::new();
363        /// set.insert(&1);
364        /// set.assign(&MultiSetInt::new());
365        /// assert_eq!(set.size(), 0);
366        /// ```
367        #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
368        pub fn assign(&mut self, other: &Self);
369
370        /// ```
371        /// use hicc_std::MultiSetInt;
372        /// let mut set = MultiSetInt::new();
373        /// set.insert(&1);
374        /// set.insert(&1);
375        /// assert_eq!(set.count(&1), 2);
376        /// ```
377        #[cpp(method = "iterator insert(const T&)")]
378        pub fn insert(&mut self, val: &T);
379
380        /// ```
381        /// use hicc_std::MultiSetInt;
382        /// let mut set = MultiSetInt::new();
383        /// set.insert(&1);
384        /// set.insert(&1);
385        /// assert_eq!(set.erase(&1), 2);
386        /// ```
387        #[cpp(method = "size_t erase(const T&)")]
388        pub fn erase(&mut self, val: &T) -> usize;
389
390        #[cpp(method = "const_iterator find(const T&) const")]
391        unsafe fn find(&self, val: &T) -> *mut CppMultiSetIter<T>;
392        #[cpp(method = "const_iterator lower_bound(const T&) const")]
393        unsafe fn lower_bound(&self, val: &T) -> *mut CppMultiSetIter<T>;
394        #[cpp(method = "const_iterator upper_bound(const T&) const")]
395        unsafe fn upper_bound(&self, val: &T) -> *mut CppMultiSetIter<T>;
396        #[cpp(method = "const_iterator begin() const")]
397        unsafe fn begin(&self) -> *mut CppMultiSetIter<T>;
398        #[cpp(method = "const_iterator end() const")]
399        unsafe fn end(&self) -> *mut CppMultiSetIter<T>;
400        #[cpp(method = "const_reverse_iterator crbegin() const")]
401        unsafe fn rbegin(&self) -> *mut CppMultiSetRevIter<T>;
402        #[cpp(method = "const_reverse_iterator crend() const")]
403        unsafe fn rend(&self) -> *mut CppMultiSetRevIter<T>;
404
405    }
406
407    unsafe impl<T: AbiType + Sync> Send for multiset<T> {}
408    unsafe impl<T: AbiType + Sync> Sync for multiset<T> {}
409
410    #[cpp(class = "template<class T, class Compare, class Allocator> std::multiset<T, Compare, Allocator>::const_iterator")]
411    class CppMultiSetIter<T> {
412        hicc::cpp! {
413            typedef typename SelfContainer::const_reverse_iterator const_reverse_iterator;
414            static const T& next(Self& self) {
415                return *self++;
416            }
417        }
418        #[cpp(func = "const T& SelfMethods::next(Self&)")]
419        unsafe fn next(&mut self) -> &T;
420        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
421        fn equal(&self, other: &Self) -> bool;
422        #[cpp(func = "const_reverse_iterator hicc::make_constructor<const_reverse_iterator, Self>(Self&&)")]
423        fn into_reverse(self) -> *mut CppMultiSetRevIter<T>;
424    }
425
426    #[cpp(class = "template<class T, class Compare, class Allocator> std::multiset<T, Compare, Allocator>::const_reverse_iterator")]
427    class CppMultiSetRevIter<T> {
428        hicc::cpp! {
429            static const T& next(Self& self) {
430                return *self++;
431            }
432        }
433        #[cpp(func = "const T& SelfMethods::next(Self&)")]
434        unsafe fn next(&mut self) -> &T;
435        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
436        fn equal(&self, other: &Self) -> bool;
437    }
438}
439
440impl<T: AbiType> multiset<T> {
441    /// ```
442    /// use hicc_std::MultiSetInt;
443    /// let mut set = MultiSetInt::new();
444    /// set.insert(&1);
445    /// set.insert(&2);
446    /// set.iter().for_each(|v| println!("value = {v}"));
447    /// ```
448    pub fn iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
449        MultiSetIter {
450            beg: unsafe { self.begin() },
451            end: unsafe { self.end() },
452        }
453    }
454
455    /// ```
456    /// use hicc_std::MultiSetInt;
457    /// let mut set = MultiSetInt::new();
458    /// set.insert(&1);
459    /// set.insert(&2);
460    /// set.rev_iter().for_each(|v| println!("value = {v}"));
461    /// ```
462    pub fn rev_iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
463        MultiSetRevIter {
464            beg: unsafe { self.rbegin() },
465            end: unsafe { self.rend() },
466        }
467    }
468
469    /// ```
470    /// use hicc_std::MultiSetInt;
471    /// let mut set = MultiSetInt::new();
472    /// set.insert(&1);
473    /// set.insert(&2);
474    /// set.insert(&2);
475    /// set.insert(&3);
476    /// let mut it = set.iter_lower_upper_bound(Some(&1), Some(&2));
477    /// assert_eq!(it.next(), Some(&1));
478    /// assert_eq!(it.next(), Some(&2));
479    /// assert_eq!(it.next(), Some(&2));
480    /// assert_eq!(it.next(), None);
481    /// ```
482    pub fn iter_lower_upper_bound(
483        &self,
484        lower_key: Option<&T::InputType>,
485        upper_key: Option<&T::InputType>,
486    ) -> impl Iterator<Item = T::OutputRef<'_>> {
487        let beg = if let Some(key) = lower_key {
488            unsafe { self.lower_bound(key) }
489        } else {
490            unsafe { self.begin() }
491        };
492        let end = if let Some(key) = upper_key {
493            unsafe { self.upper_bound(key) }
494        } else {
495            unsafe { self.end() }
496        };
497        MultiSetIter { beg, end }
498    }
499
500    /// ```
501    /// use hicc_std::MultiSetInt;
502    /// let mut set = MultiSetInt::new();
503    /// set.insert(&1);
504    /// set.insert(&1);
505    /// set.insert(&2);
506    /// set.insert(&3);
507    /// let mut it = set.rev_iter_lower_upper_bound(Some(&1), Some(&2));
508    /// assert_eq!(it.next(), Some(&2));
509    /// assert_eq!(it.next(), Some(&1));
510    /// assert_eq!(it.next(), Some(&1));
511    /// assert_eq!(it.next(), None);
512    /// ```
513    pub fn rev_iter_lower_upper_bound(
514        &self,
515        lower_key: Option<&T::InputType>,
516        upper_key: Option<&T::InputType>,
517    ) -> impl Iterator<Item = T::OutputRef<'_>> {
518        let beg = if let Some(key) = upper_key {
519            unsafe { self.upper_bound(key).into_value().into_reverse() }
520        } else {
521            unsafe { self.rbegin() }
522        };
523        let end = if let Some(key) = lower_key {
524            unsafe { self.lower_bound(key).into_value().into_reverse() }
525        } else {
526            unsafe { self.rend() }
527        };
528        MultiSetRevIter { beg, end }
529    }
530}
531
532/// 对应`std::multiset<T>::const_iterator`
533struct MultiSetIter<'a, T: AbiType + 'static> {
534    beg: ClassMutPtr<'a, CppMultiSetIter<T>>,
535    end: ClassMutPtr<'a, CppMultiSetIter<T>>,
536}
537
538impl<'a, T: AbiType + 'static> Iterator for MultiSetIter<'a, T> {
539    type Item = T::OutputRef<'a>;
540    fn next(&mut self) -> Option<Self::Item> {
541        if !self.beg.equal(&self.end) {
542            return Some(unsafe { self.beg.as_deref_mut().next() });
543        }
544        None
545    }
546}
547
548/// 对应`std::multiset<T>::const_reverse_iterator`
549struct MultiSetRevIter<'a, T: AbiType + 'static> {
550    beg: ClassMutPtr<'a, CppMultiSetRevIter<T>>,
551    end: ClassMutPtr<'a, CppMultiSetRevIter<T>>,
552}
553
554impl<'a, T: AbiType + 'static> Iterator for MultiSetRevIter<'a, T> {
555    type Item = T::OutputRef<'a>;
556    fn next(&mut self) -> Option<Self::Item> {
557        if !self.beg.equal(&self.end) {
558            return Some(unsafe { self.beg.as_deref_mut().next() });
559        }
560        None
561    }
562}