hicc_std/
std_map.rs

1use hicc::{AbiType, ClassMutPtr};
2use std::iter::Iterator;
3use std::marker::PhantomData;
4
5hicc::cpp! {
6    #include <map>
7}
8
9hicc::import_class! {
10    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::map<K, V, Compare, Allocator>")]
11    pub class map<K, V> {
12        hicc::cpp! {
13            typedef typename Self::iterator iterator;
14            typedef typename Self::reverse_iterator reverse_iterator;
15            typedef typename Self::const_iterator const_iterator;
16            typedef typename Self::const_reverse_iterator const_reverse_iterator;
17        }
18        /// ```
19        /// use hicc_std::MapIntInt;
20        /// let map = MapIntInt::new();
21        /// assert!(map.is_empty());
22        /// ```
23        #[cpp(method = "bool empty() const")]
24        pub fn is_empty(&self) -> bool;
25
26        /// ```
27        /// use hicc_std::MapIntInt;
28        /// let map = MapIntInt::new();
29        /// assert_eq!(map.size(), 0_usize);
30        /// ```
31        #[cpp(method = "size_t size() const")]
32        pub fn size(&self) -> usize;
33
34        /// ```
35        /// use hicc_std::MapIntInt;
36        /// let map = MapIntInt::new();
37        /// println!("map.max_size() = {}", map.max_size());
38        /// ```
39        #[cpp(method = "size_t max_size() const")]
40        pub fn max_size(&self) -> usize;
41
42        /// ```
43        /// use hicc_std::MapIntInt;
44        /// let mut map = MapIntInt::new();
45        /// map.insert(&1, &2);
46        /// assert_eq!(map.size(), 1_usize);
47        /// map.clear();
48        /// assert_eq!(map.size(), 0_usize);
49        /// ```
50        #[cpp(method = "void clear()")]
51        pub fn clear(&mut self);
52
53        /// ```
54        /// use hicc_std::MapIntInt;
55        /// let mut map1 = MapIntInt::new();
56        /// map1.insert(&1, &2);
57        /// let mut map2 = MapIntInt::new();
58        /// map2.swap(&mut map1);
59        /// assert_eq!(map1.size(), 0_usize);
60        /// assert_eq!(map2.size(), 1_usize);
61        /// ```
62        #[cpp(method = "void swap(Self&)")]
63        pub fn swap(&mut self, other: &mut Self);
64
65        /// ```
66        /// use hicc_std::MapIntInt;
67        /// let mut map = MapIntInt::new();
68        /// map.insert(&1, &2);
69        /// assert_eq!(map.count(&1), 1);
70        /// assert_eq!(map.count(&2), 0);
71        /// ```
72        #[cpp(method = "size_t count(const K&) const")]
73        pub fn count(&self, key: &K) -> usize;
74
75        hicc::cpp! {
76            static bool contains(const Self& self, const K& key) {
77                return self.find(key) != self.end();
78            }
79        }
80        /// ```
81        /// use hicc_std::MapIntInt;
82        /// let mut map = MapIntInt::new();
83        /// map.insert(&1, &2);
84        /// assert!(map.contains(&1));
85        /// assert!(!map.contains(&2));
86        /// ```
87        #[cpp(func = "bool SelfMethods::contains(const Self&, const K&)")]
88        pub fn contains(&self, key: &K) -> bool;
89
90        /// ```
91        /// use hicc_std::MapIntInt;
92        /// let mut map = MapIntInt::new();
93        /// map.insert(&1, &2);
94        /// map.assign(&mut MapIntInt::new());
95        /// assert!(map.is_empty());
96        /// ```
97        #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
98        pub fn assign(&mut self, other: &Self);
99
100        hicc::cpp! {
101            static bool insert(Self& self, const K& key, const V& val) {
102                return self.insert(std::make_pair(key, val)).second;
103            }
104        }
105        /// ```
106        /// use hicc_std::MapIntInt;
107        /// let mut map = MapIntInt::new();
108        /// assert!(map.insert(&1, &2));
109        /// assert_eq!(map.get(&1), Some(&2));
110        /// assert!(!map.insert(&1, &2));
111        /// ```
112        #[cpp(func = "bool SelfMethods::insert(Self&, const K&, const V&)")]
113        pub fn insert(&mut self, key: &K, val: &V) -> bool;
114
115        /// ```
116        /// use hicc_std::MapIntInt;
117        /// let mut map = MapIntInt::new();
118        /// assert_eq!(map.erase(&1), 0_usize);
119        /// assert!(map.insert(&1, &2));
120        /// assert_eq!(map.erase(&1), 1_usize);
121        /// ```
122        #[cpp(method = "size_t erase(const K&)")]
123        pub fn erase(&mut self, key: &K) -> usize;
124
125        #[cpp(method = "const_iterator find(const K&) const")]
126        unsafe fn find(&self, key: &K) -> *mut CppMapIter<K, V>;
127        #[cpp(method = "iterator find(const K&)")]
128        unsafe fn find_mut(&mut self, key: &K) -> *mut CppMapIterMut<K, V>;
129        #[cpp(method = "const_iterator lower_bound(const K&) const")]
130        unsafe fn lower_bound(&self, key: &K) -> *mut CppMapIter<K, V>;
131        #[cpp(method = "iterator lower_bound(const K&)")]
132        unsafe fn lower_bound_mut(&mut self, key: &K) -> *mut CppMapIterMut<K, V>;
133        #[cpp(method = "const_iterator upper_bound(const K&) const")]
134        unsafe fn upper_bound(&self, key: &K) -> *mut CppMapIter<K, V>;
135        #[cpp(method = "iterator upper_bound(const K&)")]
136        unsafe fn upper_bound_mut(&mut self, key: &K) -> *mut CppMapIterMut<K, V>;
137
138        #[cpp(method = "const_iterator begin() const")]
139        unsafe fn begin(&self) -> *mut CppMapIter<K, V>;
140        #[cpp(method = "iterator begin()")]
141        unsafe fn begin_mut(&mut self) -> *mut CppMapIterMut<K, V>;
142        #[cpp(method = "const_iterator end() const")]
143        unsafe fn end(&self) -> *mut CppMapIter<K, V>;
144        #[cpp(method = "iterator end()")]
145        unsafe fn end_mut(&mut self) -> *mut CppMapIterMut<K, V>;
146        #[cpp(method = "const_reverse_iterator rbegin() const")]
147        unsafe fn rbegin(&self) -> *mut CppMapRevIter<K, V>;
148        #[cpp(method = "reverse_iterator rbegin()")]
149        unsafe fn rbegin_mut(&mut self) -> *mut CppMapRevIterMut<K, V>;
150        #[cpp(method = "const_reverse_iterator rend() const")]
151        unsafe fn rend(&self) -> *mut CppMapRevIter<K, V>;
152        #[cpp(method = "reverse_iterator rend()")]
153        unsafe fn rend_mut(&mut self) -> *mut CppMapRevIterMut<K, V>;
154    }
155
156    unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Send for map<K, V> {}
157    unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Sync for map<K, V> {}
158
159    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::map<K, V, Compare, Allocator>::const_iterator")]
160    class CppMapIter<K, V> {
161        hicc::cpp! {
162            typedef typename SelfContainer::const_reverse_iterator const_reverse_iterator;
163            static void next(Self& self) {
164                ++self;
165            }
166            static const K& key(const Self& self) {
167                return self->first;
168            }
169            static const V& value(const Self& self) {
170                return self->second;
171            }
172        }
173        #[cpp(func = "void SelfMethods::next(Self&)")]
174        unsafe fn next(&mut self);
175        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
176        unsafe fn as_key(&self) -> &K;
177        #[cpp(func = "const V& SelfMethods::value(const Self&)")]
178        unsafe fn as_value(&self) -> &V;
179        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
180        fn equal(&self, other: &Self) -> bool;
181        #[cpp(func = "const_reverse_iterator hicc::make_constructor<const_reverse_iterator, Self>(Self&&)")]
182        fn into_reverse(self) -> *mut CppMapRevIter<K, V>;
183    }
184
185    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::map<K, V, Compare, Allocator>::iterator")]
186    class CppMapIterMut<K, V> {
187        hicc::cpp! {
188            typedef typename SelfContainer::reverse_iterator reverse_iterator;
189            static void next(Self& self) {
190                ++self;
191            }
192            static const K& key(const Self& self) {
193                return self->first;
194            }
195            static V& value(Self& self) {
196                return self->second;
197            }
198        }
199        #[cpp(func = "void SelfMethods::next(Self&)")]
200        unsafe fn next(&mut self);
201        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
202        unsafe fn as_key(&self) -> &K;
203        #[cpp(func = "V& SelfMethods::value(Self&)")]
204        unsafe fn as_value(&mut self) -> &mut V;
205        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
206        fn equal(&self, other: &Self) -> bool;
207        #[cpp(func = "reverse_iterator hicc::make_constructor<reverse_iterator, Self>(Self&&)")]
208        fn into_reverse(self) -> *mut CppMapRevIterMut<K, V>;
209    }
210
211    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::map<K, V, Compare, Allocator>::const_reverse_iterator")]
212    class CppMapRevIter<K, V> {
213        hicc::cpp! {
214            static void next(Self& self) {
215                ++self;
216            }
217            static const K& key(const Self& self) {
218                return self->first;
219            }
220            static const V& value(const Self& self) {
221                return self->second;
222            }
223        }
224        #[cpp(func = "void SelfMethods::next(Self&)")]
225        unsafe fn next(&mut self);
226        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
227        unsafe fn as_key(&self) -> &K;
228        #[cpp(func = "const V& SelfMethods::value(const Self&)")]
229        unsafe fn as_value(&self) -> &V;
230        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
231        fn equal(&self, other: &Self) -> bool;
232    }
233
234    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::map<K, V, Compare, Allocator>::reverse_iterator")]
235    class CppMapRevIterMut<K, V> {
236        hicc::cpp! {
237            static void next(Self& self) {
238                ++self;
239            }
240            static const K& key(const Self& self) {
241                return self->first;
242            }
243            static V& value(Self& self) {
244                return self->second;
245            }
246        }
247        #[cpp(func = "void SelfMethods::next(Self&)")]
248        unsafe fn next(&mut self);
249        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
250        unsafe fn as_key(&self) -> &K;
251        #[cpp(func = "V& SelfMethods::value(Self&)")]
252        unsafe fn as_value(&mut self) -> &mut V;
253        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
254        fn equal(&self, other: &Self) -> bool;
255    }
256}
257
258impl<K: AbiType, V: AbiType> map<K, V> {
259    /// ```
260    /// use hicc_std::MapIntInt;
261    /// let mut map = MapIntInt::new();
262    /// map.insert(&1, &2);
263    /// assert!(map.get(&1).is_some());
264    /// assert_eq!(map.get(&1), Some(&2));
265    /// ```
266    pub fn get(&self, key: &K::InputType) -> Option<V::OutputRef<'_>> {
267        unsafe {
268            let it = self.find(key);
269            if !it.equal(&self.end()) {
270                return Some(it.as_deref().as_value());
271            }
272        }
273        None
274    }
275    /// ```
276    /// use hicc_std::MapIntInt;
277    /// let mut map = MapIntInt::new();
278    /// map.insert(&1, &2);
279    /// assert!(map.get(&2).is_none());
280    /// assert_eq!(map.get(&1), Some(&2));
281    ///
282    /// use hicc_std::{string, MapIntString};
283    /// use hicc::AbiClass;
284    /// let mut map = MapIntString::new();
285    /// map.insert(&1, &string::from(c"hello"));
286    /// assert!(*map.get(&1).unwrap() == string::from(c"hello"));
287    /// map.get_mut(&1).unwrap().write(string::from(c"world"));
288    /// assert!(*map.get(&1).unwrap() == string::from(c"world"));
289    /// ```
290    pub fn get_mut(&mut self, key: &K::InputType) -> Option<V::OutputRefMut<'_>> {
291        unsafe {
292            let end = self.end_mut().into_value();
293            let mut it = self.find_mut(key);
294            if !it.equal(&end) {
295                return Some(it.as_deref_mut().as_value());
296            }
297        }
298        None
299    }
300
301    /// ```
302    /// use hicc_std::MapIntInt;
303    /// let mut map = MapIntInt::new();
304    /// map.insert(&1, &2);
305    /// map.insert(&2, &3);
306    /// let mut it = map.iter();
307    /// assert_eq!(it.next(), Some((&1, &2)));
308    /// assert_eq!(it.next(), Some((&2, &3)));
309    /// assert!(it.next().is_none());
310    /// ```
311    pub fn iter(&self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
312        MapIter {
313            beg: unsafe { self.begin() },
314            end: unsafe { self.end() },
315            mark: PhantomData,
316        }
317    }
318
319    /// ```
320    /// use hicc_std::MapIntInt;
321    /// let mut map = MapIntInt::new();
322    /// map.insert(&1, &2);
323    /// map.insert(&2, &3);
324    /// map.iter_mut().for_each(|(_, v)| *v += 1);
325    /// let mut it = map.iter();
326    /// assert_eq!(it.next(), Some((&1, &3)));
327    /// assert_eq!(it.next(), Some((&2, &4)));
328    /// assert!(it.next().is_none());
329    /// ```
330    pub fn iter_mut(&mut self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
331        let beg = unsafe { self.begin_mut() };
332        let end = unsafe { self.end_mut() };
333        MapIterMut {
334            beg,
335            end,
336            mark: PhantomData,
337        }
338    }
339
340    /// ```
341    /// use hicc_std::MapIntInt;
342    /// let mut map = MapIntInt::new();
343    /// map.insert(&1, &2);
344    /// map.insert(&2, &3);
345    /// let mut it = map.rev_iter();
346    /// assert_eq!(it.next(), Some((&2, &3)));
347    /// assert_eq!(it.next(), Some((&1, &2)));
348    /// assert!(it.next().is_none());
349    /// ```
350    pub fn rev_iter(&self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
351        MapRevIter {
352            beg: unsafe { self.rbegin() },
353            end: unsafe { self.rend() },
354            mark: PhantomData,
355        }
356    }
357
358    /// ```
359    /// use hicc_std::MapIntInt;
360    /// let mut map = MapIntInt::new();
361    /// map.insert(&1, &2);
362    /// map.insert(&2, &3);
363    /// map.rev_iter_mut().for_each(|(_, v)| *v += 1);
364    /// let mut it = map.rev_iter();
365    /// assert_eq!(it.next(), Some((&2, &4)));
366    /// assert_eq!(it.next(), Some((&1, &3)));
367    /// assert!(it.next().is_none());
368    /// ```
369    pub fn rev_iter_mut(
370        &mut self,
371    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
372        let beg = unsafe { self.rbegin_mut() };
373        let end = unsafe { self.rend_mut() };
374        MapRevIterMut {
375            beg,
376            end,
377            mark: PhantomData,
378        }
379    }
380
381    /// ```
382    /// use hicc_std::MapIntInt;
383    /// let mut map = MapIntInt::new();
384    /// map.insert(&1, &2);
385    /// map.insert(&2, &3);
386    /// map.insert(&3, &4);
387    /// let mut it = map.iter_lower_upper_bound(Some(&1), Some(&2));
388    /// assert_eq!(it.next(), Some((&1, &2)));
389    /// assert_eq!(it.next(), Some((&2, &3)));
390    /// assert_eq!(it.next(), None);
391    /// ```
392    pub fn iter_lower_upper_bound(
393        &self,
394        lower_key: Option<&K::InputType>,
395        upper_key: Option<&K::InputType>,
396    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
397        let beg = if let Some(key) = lower_key {
398            unsafe { self.lower_bound(key) }
399        } else {
400            unsafe { self.begin() }
401        };
402        let end = if let Some(key) = upper_key {
403            unsafe { self.upper_bound(key) }
404        } else {
405            unsafe { self.end() }
406        };
407        MapIter {
408            beg,
409            end,
410            mark: PhantomData,
411        }
412    }
413
414    /// ```
415    /// use hicc_std::MapIntInt;
416    /// let mut map = MapIntInt::new();
417    /// map.insert(&1, &2);
418    /// map.insert(&2, &3);
419    /// map.insert(&3, &4);
420    /// map.iter_lower_upper_bound_mut(Some(&1), Some(&2)).for_each(|(_, v)| *v -= 1);
421    /// let mut it = map.iter_lower_upper_bound(Some(&1), Some(&2));
422    /// assert_eq!(it.next(), Some((&1, &1)));
423    /// assert_eq!(it.next(), Some((&2, &2)));
424    /// assert_eq!(it.next(), None);
425    /// ```
426    pub fn iter_lower_upper_bound_mut(
427        &mut self,
428        lower_key: Option<&K::InputType>,
429        upper_key: Option<&K::InputType>,
430    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
431        let beg = if let Some(key) = lower_key {
432            unsafe { self.lower_bound_mut(key) }
433        } else {
434            unsafe { self.begin_mut() }
435        };
436        let end = if let Some(key) = upper_key {
437            unsafe { self.upper_bound_mut(key) }
438        } else {
439            unsafe { self.end_mut() }
440        };
441        MapIterMut {
442            beg,
443            end,
444            mark: PhantomData,
445        }
446    }
447
448    /// ```
449    /// use hicc_std::MapIntInt;
450    /// let mut map = MapIntInt::new();
451    /// map.insert(&1, &0);
452    /// map.insert(&2, &1);
453    /// map.insert(&3, &2);
454    /// let mut it = map.rev_iter_lower_upper_bound(Some(&1), Some(&2));
455    /// assert_eq!(it.next(), Some((&2, &1)));
456    /// assert_eq!(it.next(), Some((&1, &0)));
457    /// assert_eq!(it.next(), None);
458    /// ```
459    pub fn rev_iter_lower_upper_bound(
460        &self,
461        lower_key: Option<&K::InputType>,
462        upper_key: Option<&K::InputType>,
463    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
464        let beg = if let Some(key) = upper_key {
465            unsafe { self.upper_bound(key).into_value().into_reverse() }
466        } else {
467            unsafe { self.rbegin() }
468        };
469        let end = if let Some(key) = lower_key {
470            unsafe { self.lower_bound(key).into_value().into_reverse() }
471        } else {
472            unsafe { self.rend() }
473        };
474        MapRevIter {
475            beg,
476            end,
477            mark: PhantomData,
478        }
479    }
480
481    /// ```
482    /// use hicc_std::MapIntInt;
483    /// let mut map = MapIntInt::new();
484    /// map.insert(&1, &0);
485    /// map.insert(&2, &1);
486    /// map.insert(&3, &2);
487    /// map.rev_iter_lower_upper_bound_mut(Some(&1), Some(&2)).for_each(|(_, v)| *v -= 1);
488    /// let mut it = map.rev_iter_lower_upper_bound(Some(&1), Some(&2));
489    /// assert_eq!(it.next(), Some((&2, &0)));
490    /// assert_eq!(it.next(), Some((&1, &-1)));
491    /// assert_eq!(it.next(), None);
492    /// ```
493    pub fn rev_iter_lower_upper_bound_mut(
494        &mut self,
495        lower_key: Option<&K::InputType>,
496        upper_key: Option<&K::InputType>,
497    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
498        let beg = if let Some(key) = upper_key {
499            unsafe { self.upper_bound_mut(key).into_value() }.into_reverse()
500        } else {
501            unsafe { self.rbegin_mut() }
502        };
503        let end = if let Some(key) = lower_key {
504            unsafe { self.lower_bound_mut(key).into_value() }.into_reverse()
505        } else {
506            unsafe { self.rend_mut() }
507        };
508        MapRevIterMut {
509            beg,
510            end,
511            mark: PhantomData,
512        }
513    }
514}
515
516/// 对应`std::map<K, V>::const_iterator`
517struct MapIter<'a, K: AbiType + 'static, V: AbiType + 'static> {
518    beg: ClassMutPtr<'static, CppMapIter<K, V>>,
519    end: ClassMutPtr<'static, CppMapIter<K, V>>,
520    mark: PhantomData<&'a map<K, V>>,
521}
522
523impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MapIter<'a, K, V> {
524    type Item = (K::OutputRef<'a>, V::OutputRef<'a>);
525    fn next(&mut self) -> Option<Self::Item> {
526        if self.beg.equal(&self.end) {
527            return None;
528        }
529        let key = unsafe { self.beg.as_deref().as_key() };
530        let val = unsafe { self.beg.as_deref().as_value() };
531        unsafe { self.beg.next() };
532        Some((key, val))
533    }
534}
535
536/// 对应`std::map<K, V>::iterator`
537struct MapIterMut<'a, K: AbiType + 'static, V: AbiType + 'static> {
538    beg: ClassMutPtr<'static, CppMapIterMut<K, V>>,
539    end: ClassMutPtr<'static, CppMapIterMut<K, V>>,
540    mark: PhantomData<&'a mut map<K, V>>,
541}
542
543impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MapIterMut<'a, K, V> {
544    type Item = (K::OutputRef<'a>, V::OutputRefMut<'a>);
545    fn next(&mut self) -> Option<Self::Item> {
546        if self.beg.equal(&self.end) {
547            return None;
548        }
549        let key = unsafe { self.beg.as_deref().as_key() };
550        let val = unsafe { self.beg.as_deref_mut().as_value() };
551        unsafe { self.beg.next() };
552        Some((key, val))
553    }
554}
555
556/// 对应`std::map<K, V>::const_reverse_iterator`
557struct MapRevIter<'a, K: AbiType + 'static, V: AbiType + 'static> {
558    beg: ClassMutPtr<'static, CppMapRevIter<K, V>>,
559    end: ClassMutPtr<'static, CppMapRevIter<K, V>>,
560    mark: PhantomData<&'a map<K, V>>,
561}
562
563impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MapRevIter<'a, K, V> {
564    type Item = (K::OutputRef<'a>, V::OutputRef<'a>);
565    fn next(&mut self) -> Option<Self::Item> {
566        if self.beg.equal(&self.end) {
567            return None;
568        }
569        let key = unsafe { self.beg.as_deref().as_key() };
570        let val = unsafe { self.beg.as_deref().as_value() };
571        unsafe { self.beg.next() };
572        Some((key, val))
573    }
574}
575
576/// 对应`std::map<K, V>::reverse_iterator`
577struct MapRevIterMut<'a, K: AbiType + 'static, V: AbiType + 'static> {
578    beg: ClassMutPtr<'static, CppMapRevIterMut<K, V>>,
579    end: ClassMutPtr<'static, CppMapRevIterMut<K, V>>,
580    mark: PhantomData<&'a mut map<K, V>>,
581}
582
583impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MapRevIterMut<'a, K, V> {
584    type Item = (K::OutputRef<'a>, V::OutputRefMut<'a>);
585    fn next(&mut self) -> Option<Self::Item> {
586        if self.beg.equal(&self.end) {
587            return None;
588        }
589        let key = unsafe { self.beg.as_deref().as_key() };
590        let val = unsafe { self.beg.as_deref_mut().as_value() };
591        unsafe { self.beg.next() };
592        Some((key, val))
593    }
594}
595
596hicc::import_class! {
597    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>")]
598    pub class multimap<K, V> {
599        hicc::cpp! {
600            typedef typename Self::iterator iterator;
601            typedef typename Self::const_iterator const_iterator;
602            typedef typename Self::reverse_iterator reverse_iterator;
603            typedef typename Self::const_reverse_iterator const_reverse_iterator;
604        }
605        /// ```
606        /// use hicc_std::MultiMapIntInt;
607        /// let map = MultiMapIntInt::new();
608        /// assert!(map.is_empty());
609        /// ```
610        #[cpp(method = "bool empty() const")]
611        pub fn is_empty(&self) -> bool;
612
613        /// ```
614        /// use hicc_std::MultiMapIntInt;
615        /// let map = MultiMapIntInt::new();
616        /// assert_eq!(map.size(), 0_usize);
617        /// ```
618        #[cpp(method = "size_t size() const")]
619        pub fn size(&self) -> usize;
620
621        /// ```
622        /// use hicc_std::MultiMapIntInt;
623        /// let map = MultiMapIntInt::new();
624        /// println!("map.max_size() = {}", map.max_size());
625        /// ```
626        #[cpp(method = "size_t max_size() const")]
627        pub fn max_size(&self) -> usize;
628
629        /// ```
630        /// use hicc_std::MultiMapIntInt;
631        /// let mut map = MultiMapIntInt::new();
632        /// map.insert(&1, &2);
633        /// assert_eq!(map.size(), 1_usize);
634        /// map.clear();
635        /// assert_eq!(map.size(), 0_usize);
636        /// ```
637        #[cpp(method = "void clear()")]
638        pub fn clear(&mut self);
639
640        /// ```
641        /// use hicc_std::MultiMapIntInt;
642        /// let mut map1 = MultiMapIntInt::new();
643        /// map1.insert(&1, &2);
644        /// let mut map2 = MultiMapIntInt::new();
645        /// map2.swap(&mut map1);
646        /// assert_eq!(map1.size(), 0_usize);
647        /// assert_eq!(map2.size(), 1_usize);
648        /// ```
649        #[cpp(method = "void swap(Self&)")]
650        pub fn swap(&mut self, other: &mut Self);
651
652        /// ```
653        /// use hicc_std::MultiMapIntInt;
654        /// let mut map = MultiMapIntInt::new();
655        /// map.insert(&1, &2);
656        /// map.insert(&1, &2);
657        /// assert_eq!(map.count(&1), 2_usize);
658        /// assert_eq!(map.count(&2), 0);
659        /// ```
660        #[cpp(method = "size_t count(const K&) const")]
661        pub fn count(&self, key: &K) -> usize;
662
663        hicc::cpp! {
664            static bool contains(const Self& self, const K& key) {
665                return self.find(key) != self.end();
666            }
667        }
668        /// ```
669        /// use hicc_std::MultiMapIntInt;
670        /// let mut map = MultiMapIntInt::new();
671        /// map.insert(&1, &2);
672        /// assert!(map.contains(&1));
673        /// assert!(!map.contains(&2));
674        /// ```
675        #[cpp(func = "bool SelfMethods::contains(const Self&, const K&)")]
676        pub fn contains(&self, key: &K) -> bool;
677
678        /// ```
679        /// use hicc_std::MultiMapIntInt;
680        /// let mut map = MultiMapIntInt::new();
681        /// map.insert(&1, &2);
682        /// map.assign(&mut MultiMapIntInt::new());
683        /// assert!(map.is_empty());
684        /// ```
685        #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
686        pub fn assign(&mut self, other: &Self);
687
688        hicc::cpp! {
689            static void insert(Self& self, const K& key, const V& val) {
690                self.insert(std::make_pair(key, val));
691            }
692        }
693        /// ```
694        /// use hicc_std::MultiMapIntInt;
695        /// let mut map = MultiMapIntInt::new();
696        /// map.insert(&1, &2);
697        /// map.insert(&1, &3);
698        /// assert_eq!(map.count(&1), 2);
699        /// ```
700        #[cpp(func = "void SelfMethods::insert(Self&, const K&, const V&)")]
701        pub fn insert(&mut self, key: &K, val: &V);
702
703        /// ```
704        /// use hicc_std::MultiMapIntInt;
705        /// let mut map = MultiMapIntInt::new();
706        /// map.insert(&1, &2);
707        /// map.insert(&1, &3);
708        /// assert_eq!(map.erase(&1), 2);
709        /// ```
710        #[cpp(method = "size_t erase(const K&)")]
711        pub fn erase(&mut self, key: &K) -> usize;
712
713        #[cpp(method = "const_iterator find(const K&) const")]
714        unsafe fn find(&self, key: &K) -> *mut CppMultiMapIter<K, V>;
715        #[cpp(method = "iterator find(const K&)")]
716        unsafe fn find_mut(&mut self, key: &K) -> *mut CppMultiMapIterMut<K, V>;
717        #[cpp(method = "const_iterator lower_bound(const K&) const")]
718        unsafe fn lower_bound(&self, key: &K) -> *mut CppMultiMapIter<K, V>;
719        #[cpp(method = "iterator lower_bound(const K&)")]
720        unsafe fn lower_bound_mut(&mut self, key: &K) -> *mut CppMultiMapIterMut<K, V>;
721        #[cpp(method = "const_iterator upper_bound(const K&) const")]
722        unsafe fn upper_bound(&self, key: &K) -> *mut CppMultiMapIter<K, V>;
723        #[cpp(method = "iterator upper_bound(const K&)")]
724        unsafe fn upper_bound_mut(&mut self, key: &K) -> *mut CppMultiMapIterMut<K, V>;
725        #[cpp(method = "const_iterator begin() const")]
726        unsafe fn begin(&self) -> *mut CppMultiMapIter<K, V>;
727        #[cpp(method = "iterator begin() ")]
728        unsafe fn begin_mut(&mut self) -> *mut CppMultiMapIterMut<K, V>;
729        #[cpp(method = "const_iterator end() const")]
730        unsafe fn end(&self) -> *mut CppMultiMapIter<K, V>;
731        #[cpp(method = "iterator end() ")]
732        unsafe fn end_mut(&mut self) -> *mut CppMultiMapIterMut<K, V>;
733        #[cpp(method = "const_reverse_iterator rbegin() const")]
734        unsafe fn rbegin(&self) -> *mut CppMultiMapRevIter<K, V>;
735        #[cpp(method = "reverse_iterator rbegin()")]
736        unsafe fn rbegin_mut(&mut self) -> *mut CppMultiMapRevIterMut<K, V>;
737        #[cpp(method = "const_reverse_iterator rend() const")]
738        unsafe fn rend(&self) -> *mut CppMultiMapRevIter<K, V>;
739        #[cpp(method = "reverse_iterator rend()")]
740        unsafe fn rend_mut(&mut self) -> *mut CppMultiMapRevIterMut<K, V>;
741    }
742
743    unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Send for multimap<K, V> {}
744    unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Sync for multimap<K, V> {}
745
746    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>::const_iterator")]
747    class CppMultiMapIter<K, V> {
748        hicc::cpp! {
749            typedef typename SelfContainer::const_reverse_iterator const_reverse_iterator;
750            static void next(Self& self) {
751                ++self;
752            }
753            static const K& key(const Self& self) {
754                return self->first;
755            }
756            static const V& value(const Self& self) {
757                return self->second;
758            }
759        }
760        #[cpp(func = "void SelfMethods::next(Self&)")]
761        fn next(&mut self);
762        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
763        fn as_key(&self) -> &K;
764        #[cpp(func = "const V& SelfMethods::value(const Self&)")]
765        fn as_value(&self) -> &V;
766        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
767        fn equal(&self, other: &Self) -> bool;
768        #[cpp(func = "const_reverse_iterator hicc::make_constructor<const_reverse_iterator, Self>(Self&&)")]
769        fn into_reverse(self) -> *mut CppMultiMapRevIter<K, V>;
770    }
771
772    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>::iterator")]
773    class CppMultiMapIterMut<K, V> {
774        hicc::cpp! {
775            typedef typename SelfContainer::reverse_iterator reverse_iterator;
776            static void next(Self& self) {
777                ++self;
778            }
779            static const K& key(const Self& self) {
780                return self->first;
781            }
782            static V& value(Self& self) {
783                return self->second;
784            }
785        }
786        #[cpp(func = "void SelfMethods::next(Self&)")]
787        fn next(&mut self);
788        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
789        fn as_key(&self) -> &K;
790        #[cpp(func = "V& SelfMethods::value(Self&)")]
791        fn as_value(&mut self) -> &mut V;
792        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
793        fn equal(&self, other: &Self) -> bool;
794        #[cpp(func = "reverse_iterator hicc::make_constructor<reverse_iterator, Self>(Self&&)")]
795        fn into_reverse(self) -> *mut CppMultiMapRevIterMut<K, V>;
796    }
797
798    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>::const_reverse_iterator")]
799    class CppMultiMapRevIter<K, V> {
800        hicc::cpp! {
801            static void next(Self& self) {
802                ++self;
803            }
804            static const K& key(const Self& self) {
805                return self->first;
806            }
807            static const V& value(const Self& self) {
808                return self->second;
809            }
810        }
811        #[cpp(func = "void SelfMethods::next(Self&)")]
812        fn next(&mut self);
813        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
814        fn as_key(&self) -> &K;
815        #[cpp(func = "const V& SelfMethods::value(const Self&)")]
816        fn as_value(&self) -> &V;
817        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
818        fn equal(&self, other: &Self) -> bool;
819    }
820
821    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>::reverse_iterator")]
822    class CppMultiMapRevIterMut<K, V> {
823        hicc::cpp! {
824            static void next(Self& self) {
825                ++self;
826            }
827            static const K& key(const Self& self) {
828                return self->first;
829            }
830            static V& value(Self& self) {
831                return self->second;
832            }
833        }
834        #[cpp(func = "void SelfMethods::next(Self&)")]
835        fn next(&mut self);
836        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
837        fn as_key(&self) -> &K;
838        #[cpp(func = "V& SelfMethods::value(Self&)")]
839        fn as_value(&mut self) -> &mut V;
840        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
841        fn equal(&self, other: &Self) -> bool;
842    }
843}
844
845impl<K: AbiType + 'static, V: AbiType + 'static> multimap<K, V> {
846    /// ```
847    /// use hicc_std::MultiMapIntInt;
848    /// let mut map = MultiMapIntInt::new();
849    /// map.insert(&1, &2);
850    /// map.insert(&1, &1);
851    /// map.iter().for_each(|(k, v)| println!("key = {k}, value = {v}"));
852    /// ```
853    pub fn iter(&self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
854        MultiMapIter {
855            beg: unsafe { self.begin() },
856            end: unsafe { self.end() },
857            mark: PhantomData,
858        }
859    }
860
861    /// ```
862    /// use hicc_std::MultiMapIntInt;
863    /// let mut map = MultiMapIntInt::new();
864    /// map.insert(&1, &2);
865    /// map.insert(&2, &3);
866    /// map.iter_mut().for_each(|(_, v)| *v += 1);
867    /// let mut it = map.iter();
868    /// assert_eq!(it.next(), Some((&1, &3)));
869    /// assert_eq!(it.next(), Some((&2, &4)));
870    /// assert!(it.next().is_none());
871    /// ```
872    pub fn iter_mut(&mut self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
873        let beg = unsafe { self.begin_mut() };
874        let end = unsafe { self.end_mut() };
875        MultiMapIterMut {
876            beg,
877            end,
878            mark: PhantomData,
879        }
880    }
881
882    /// ```
883    /// use hicc_std::MultiMapIntInt;
884    /// let mut map = MultiMapIntInt::new();
885    /// map.insert(&1, &2);
886    /// map.insert(&1, &1);
887    /// map.rev_iter().for_each(|(k, v)| println!("key = {k}, value = {v}"));
888    /// ```
889    pub fn rev_iter(&self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
890        MultiMapRevIter {
891            beg: unsafe { self.rbegin() },
892            end: unsafe { self.rend() },
893            mark: PhantomData,
894        }
895    }
896
897    /// ```
898    /// use hicc_std::MultiMapIntInt;
899    /// let mut map = MultiMapIntInt::new();
900    /// map.insert(&1, &2);
901    /// map.insert(&2, &3);
902    /// map.rev_iter_mut().for_each(|(_, v)| *v += 1);
903    /// let mut it = map.iter();
904    /// assert_eq!(it.next(), Some((&1, &3)));
905    /// assert_eq!(it.next(), Some((&2, &4)));
906    /// assert!(it.next().is_none());
907    /// ```
908    pub fn rev_iter_mut(
909        &mut self,
910    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
911        let beg = unsafe { self.rbegin_mut() };
912        let end = unsafe { self.rend_mut() };
913        MultiMapRevIterMut {
914            beg,
915            end,
916            mark: PhantomData,
917        }
918    }
919
920    /// ```
921    /// use hicc_std::MultiMapIntInt;
922    /// let mut map = MultiMapIntInt::new();
923    /// map.insert(&1, &2);
924    /// map.insert(&2, &3);
925    /// map.insert(&3, &4);
926    /// let mut it = map.iter_lower_upper_bound(Some(&1), Some(&2));
927    /// assert_eq!(it.next(), Some((&1, &2)));
928    /// assert_eq!(it.next(), Some((&2, &3)));
929    /// assert_eq!(it.next(), None);
930    /// ```
931    pub fn iter_lower_upper_bound(
932        &self,
933        lower_key: Option<&K::InputType>,
934        upper_key: Option<&K::InputType>,
935    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
936        let beg = if let Some(key) = lower_key {
937            unsafe { self.lower_bound(key) }
938        } else {
939            unsafe { self.begin() }
940        };
941        let end = if let Some(key) = upper_key {
942            unsafe { self.upper_bound(key) }
943        } else {
944            unsafe { self.end() }
945        };
946        MultiMapIter {
947            beg,
948            end,
949            mark: PhantomData,
950        }
951    }
952
953    /// ```
954    /// use hicc_std::MultiMapIntInt;
955    /// let mut map = MultiMapIntInt::new();
956    /// map.insert(&1, &2);
957    /// map.insert(&2, &3);
958    /// map.insert(&3, &4);
959    /// map.iter_lower_upper_bound_mut(Some(&1), Some(&2)).for_each(|(_, v)| *v -= 1);
960    /// let mut it = map.iter_lower_upper_bound(Some(&1), Some(&2));
961    /// assert_eq!(it.next(), Some((&1, &1)));
962    /// assert_eq!(it.next(), Some((&2, &2)));
963    /// assert_eq!(it.next(), None);
964    /// ```
965    pub fn iter_lower_upper_bound_mut(
966        &mut self,
967        lower_key: Option<&K::InputType>,
968        upper_key: Option<&K::InputType>,
969    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
970        let beg = if let Some(key) = lower_key {
971            unsafe { self.lower_bound_mut(key) }
972        } else {
973            unsafe { self.begin_mut() }
974        };
975        let end = if let Some(key) = upper_key {
976            unsafe { self.upper_bound_mut(key) }
977        } else {
978            unsafe { self.end_mut() }
979        };
980        MultiMapIterMut {
981            beg,
982            end,
983            mark: PhantomData,
984        }
985    }
986
987    /// ```
988    /// use hicc_std::MultiMapIntInt;
989    /// let mut map = MultiMapIntInt::new();
990    /// map.insert(&1, &0);
991    /// map.insert(&2, &1);
992    /// map.insert(&3, &2);
993    /// let mut it = map.rev_iter_lower_upper_bound(Some(&1), Some(&2));
994    /// assert_eq!(it.next(), Some((&2, &1)));
995    /// assert_eq!(it.next(), Some((&1, &0)));
996    /// assert_eq!(it.next(), None);
997    /// ```
998    pub fn rev_iter_lower_upper_bound(
999        &self,
1000        lower_key: Option<&K::InputType>,
1001        upper_key: Option<&K::InputType>,
1002    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
1003        let beg = if let Some(key) = upper_key {
1004            unsafe { self.upper_bound(key).into_value() }.into_reverse()
1005        } else {
1006            unsafe { self.rbegin() }
1007        };
1008        let end = if let Some(key) = lower_key {
1009            unsafe { self.lower_bound(key).into_value() }.into_reverse()
1010        } else {
1011            unsafe { self.rend() }
1012        };
1013        MultiMapRevIter {
1014            beg,
1015            end,
1016            mark: PhantomData,
1017        }
1018    }
1019
1020    /// ```
1021    /// use hicc_std::MultiMapIntInt;
1022    /// let mut map = MultiMapIntInt::new();
1023    /// map.insert(&1, &0);
1024    /// map.insert(&2, &1);
1025    /// map.insert(&3, &2);
1026    /// map.rev_iter_lower_upper_bound_mut(Some(&1), Some(&2)).for_each(|(_, v)| *v -= 1);
1027    /// let mut it = map.rev_iter_lower_upper_bound(Some(&1), Some(&2));
1028    /// assert_eq!(it.next(), Some((&2, &0)));
1029    /// assert_eq!(it.next(), Some((&1, &-1)));
1030    /// assert_eq!(it.next(), None);
1031    /// ```
1032    pub fn rev_iter_lower_upper_bound_mut(
1033        &mut self,
1034        lower_key: Option<&K::InputType>,
1035        upper_key: Option<&K::InputType>,
1036    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
1037        let beg = if let Some(key) = upper_key {
1038            unsafe { self.upper_bound_mut(key).into_value() }.into_reverse()
1039        } else {
1040            unsafe { self.rbegin_mut() }
1041        };
1042        let end = if let Some(key) = lower_key {
1043            unsafe { self.lower_bound_mut(key).into_value() }.into_reverse()
1044        } else {
1045            unsafe { self.rend_mut() }
1046        };
1047        MultiMapRevIterMut {
1048            beg,
1049            end,
1050            mark: PhantomData,
1051        }
1052    }
1053}
1054
1055/// 对应`std::multimap<K, V>::const_iterator`
1056struct MultiMapIter<'a, K: AbiType + 'static, V: AbiType + 'static> {
1057    beg: ClassMutPtr<'static, CppMultiMapIter<K, V>>,
1058    end: ClassMutPtr<'static, CppMultiMapIter<K, V>>,
1059    mark: PhantomData<&'a multimap<K, V>>,
1060}
1061
1062impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MultiMapIter<'a, K, V> {
1063    type Item = (K::OutputRef<'a>, V::OutputRef<'a>);
1064    fn next(&mut self) -> Option<Self::Item> {
1065        if !self.beg.equal(&self.end) {
1066            unsafe {
1067                let key = self.beg.as_deref().as_key();
1068                let val = self.beg.as_deref().as_value();
1069                self.beg.next();
1070                return Some((key, val));
1071            }
1072        }
1073        None
1074    }
1075}
1076
1077/// 对应`std::multimap<K, V>::const_reverse_iterator`
1078struct MultiMapRevIter<'a, K: AbiType + 'static, V: AbiType + 'static> {
1079    beg: ClassMutPtr<'static, CppMultiMapRevIter<K, V>>,
1080    end: ClassMutPtr<'static, CppMultiMapRevIter<K, V>>,
1081    mark: PhantomData<&'a multimap<K, V>>,
1082}
1083
1084impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MultiMapRevIter<'a, K, V> {
1085    type Item = (K::OutputRef<'a>, V::OutputRef<'a>);
1086    fn next(&mut self) -> Option<Self::Item> {
1087        if !self.beg.equal(&self.end) {
1088            unsafe {
1089                let key = self.beg.as_deref().as_key();
1090                let val = self.beg.as_deref().as_value();
1091                self.beg.next();
1092                return Some((key, val));
1093            }
1094        }
1095        None
1096    }
1097}
1098
1099/// 对应`std::multimap<K, V>::iterator`
1100struct MultiMapIterMut<'a, K: AbiType + 'static, V: AbiType + 'static> {
1101    beg: ClassMutPtr<'static, CppMultiMapIterMut<K, V>>,
1102    end: ClassMutPtr<'static, CppMultiMapIterMut<K, V>>,
1103    mark: PhantomData<&'a mut multimap<K, V>>,
1104}
1105
1106impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MultiMapIterMut<'a, K, V> {
1107    type Item = (K::OutputRef<'a>, V::OutputRefMut<'a>);
1108    fn next(&mut self) -> Option<Self::Item> {
1109        if !self.beg.equal(&self.end) {
1110            unsafe {
1111                let key = self.beg.as_deref().as_key();
1112                let val = self.beg.as_deref_mut().as_value();
1113                self.beg.next();
1114                return Some((key, val));
1115            }
1116        }
1117        None
1118    }
1119}
1120
1121/// 对应`std::multimap<K, V>::reverse_iterator`
1122struct MultiMapRevIterMut<'a, K: AbiType + 'static, V: AbiType + 'static> {
1123    beg: ClassMutPtr<'static, CppMultiMapRevIterMut<K, V>>,
1124    end: ClassMutPtr<'static, CppMultiMapRevIterMut<K, V>>,
1125    mark: PhantomData<&'a mut multimap<K, V>>,
1126}
1127
1128impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MultiMapRevIterMut<'a, K, V> {
1129    type Item = (K::OutputRef<'a>, V::OutputRefMut<'a>);
1130    fn next(&mut self) -> Option<Self::Item> {
1131        if !self.beg.equal(&self.end) {
1132            unsafe {
1133                let key = self.beg.as_deref().as_key();
1134                let val = self.beg.as_deref_mut().as_value();
1135                self.beg.next();
1136                return Some((key, val));
1137            }
1138        }
1139        None
1140    }
1141}