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    /// 对应`std::multimap<K, V>`
598    ///
599    /// 需和`include/hicc/std/map.hpp`接口定义保持一致.
600    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>")]
601    pub class multimap<K, V> {
602        hicc::cpp! {
603            typedef typename Self::iterator iterator;
604            typedef typename Self::const_iterator const_iterator;
605            typedef typename Self::reverse_iterator reverse_iterator;
606            typedef typename Self::const_reverse_iterator const_reverse_iterator;
607        }
608        /// ```
609        /// use hicc_std::MultiMapIntInt;
610        /// let map = MultiMapIntInt::new();
611        /// assert!(map.is_empty());
612        /// ```
613        #[cpp(method = "bool empty() const")]
614        pub fn is_empty(&self) -> bool;
615
616        /// ```
617        /// use hicc_std::MultiMapIntInt;
618        /// let map = MultiMapIntInt::new();
619        /// assert_eq!(map.size(), 0_usize);
620        /// ```
621        #[cpp(method = "size_t size() const")]
622        pub fn size(&self) -> usize;
623
624        /// ```
625        /// use hicc_std::MultiMapIntInt;
626        /// let map = MultiMapIntInt::new();
627        /// println!("map.max_size() = {}", map.max_size());
628        /// ```
629        #[cpp(method = "size_t max_size() const")]
630        pub fn max_size(&self) -> usize;
631
632        /// ```
633        /// use hicc_std::MultiMapIntInt;
634        /// let mut map = MultiMapIntInt::new();
635        /// map.insert(&1, &2);
636        /// assert_eq!(map.size(), 1_usize);
637        /// map.clear();
638        /// assert_eq!(map.size(), 0_usize);
639        /// ```
640        #[cpp(method = "void clear()")]
641        pub fn clear(&mut self);
642
643        /// ```
644        /// use hicc_std::MultiMapIntInt;
645        /// let mut map1 = MultiMapIntInt::new();
646        /// map1.insert(&1, &2);
647        /// let mut map2 = MultiMapIntInt::new();
648        /// map2.swap(&mut map1);
649        /// assert_eq!(map1.size(), 0_usize);
650        /// assert_eq!(map2.size(), 1_usize);
651        /// ```
652        #[cpp(method = "void swap(Self&)")]
653        pub fn swap(&mut self, other: &mut Self);
654
655        /// ```
656        /// use hicc_std::MultiMapIntInt;
657        /// let mut map = MultiMapIntInt::new();
658        /// map.insert(&1, &2);
659        /// map.insert(&1, &2);
660        /// assert_eq!(map.count(&1), 2_usize);
661        /// assert_eq!(map.count(&2), 0);
662        /// ```
663        #[cpp(method = "size_t count(const K&) const")]
664        pub fn count(&self, key: &K) -> usize;
665
666        hicc::cpp! {
667            static bool contains(const Self& self, const K& key) {
668                return self.find(key) != self.end();
669            }
670        }
671        /// ```
672        /// use hicc_std::MultiMapIntInt;
673        /// let mut map = MultiMapIntInt::new();
674        /// map.insert(&1, &2);
675        /// assert!(map.contains(&1));
676        /// assert!(!map.contains(&2));
677        /// ```
678        #[cpp(func = "bool SelfMethods::contains(const Self&, const K&)")]
679        pub fn contains(&self, key: &K) -> bool;
680
681        /// ```
682        /// use hicc_std::MultiMapIntInt;
683        /// let mut map = MultiMapIntInt::new();
684        /// map.insert(&1, &2);
685        /// map.assign(&mut MultiMapIntInt::new());
686        /// assert!(map.is_empty());
687        /// ```
688        #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
689        pub fn assign(&mut self, other: &Self);
690
691        hicc::cpp! {
692            static void insert(Self& self, const K& key, const V& val) {
693                self.insert(std::make_pair(key, val));
694            }
695        }
696        /// ```
697        /// use hicc_std::MultiMapIntInt;
698        /// let mut map = MultiMapIntInt::new();
699        /// map.insert(&1, &2);
700        /// map.insert(&1, &3);
701        /// assert_eq!(map.count(&1), 2);
702        /// ```
703        #[cpp(func = "void SelfMethods::insert(Self&, const K&, const V&)")]
704        pub fn insert(&mut self, key: &K, val: &V);
705
706        /// ```
707        /// use hicc_std::MultiMapIntInt;
708        /// let mut map = MultiMapIntInt::new();
709        /// map.insert(&1, &2);
710        /// map.insert(&1, &3);
711        /// assert_eq!(map.erase(&1), 2);
712        /// ```
713        #[cpp(method = "size_t erase(const K&)")]
714        pub fn erase(&mut self, key: &K) -> usize;
715
716        #[cpp(method = "const_iterator find(const K&) const")]
717        unsafe fn find(&self, key: &K) -> *mut CppMultiMapIter<K, V>;
718        #[cpp(method = "iterator find(const K&)")]
719        unsafe fn find_mut(&mut self, key: &K) -> *mut CppMultiMapIterMut<K, V>;
720        #[cpp(method = "const_iterator lower_bound(const K&) const")]
721        unsafe fn lower_bound(&self, key: &K) -> *mut CppMultiMapIter<K, V>;
722        #[cpp(method = "iterator lower_bound(const K&)")]
723        unsafe fn lower_bound_mut(&mut self, key: &K) -> *mut CppMultiMapIterMut<K, V>;
724        #[cpp(method = "const_iterator upper_bound(const K&) const")]
725        unsafe fn upper_bound(&self, key: &K) -> *mut CppMultiMapIter<K, V>;
726        #[cpp(method = "iterator upper_bound(const K&)")]
727        unsafe fn upper_bound_mut(&mut self, key: &K) -> *mut CppMultiMapIterMut<K, V>;
728        #[cpp(method = "const_iterator begin() const")]
729        unsafe fn begin(&self) -> *mut CppMultiMapIter<K, V>;
730        #[cpp(method = "iterator begin() ")]
731        unsafe fn begin_mut(&mut self) -> *mut CppMultiMapIterMut<K, V>;
732        #[cpp(method = "const_iterator end() const")]
733        unsafe fn end(&self) -> *mut CppMultiMapIter<K, V>;
734        #[cpp(method = "iterator end() ")]
735        unsafe fn end_mut(&mut self) -> *mut CppMultiMapIterMut<K, V>;
736        #[cpp(method = "const_reverse_iterator rbegin() const")]
737        unsafe fn rbegin(&self) -> *mut CppMultiMapRevIter<K, V>;
738        #[cpp(method = "reverse_iterator rbegin()")]
739        unsafe fn rbegin_mut(&mut self) -> *mut CppMultiMapRevIterMut<K, V>;
740        #[cpp(method = "const_reverse_iterator rend() const")]
741        unsafe fn rend(&self) -> *mut CppMultiMapRevIter<K, V>;
742        #[cpp(method = "reverse_iterator rend()")]
743        unsafe fn rend_mut(&mut self) -> *mut CppMultiMapRevIterMut<K, V>;
744    }
745
746    unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Send for multimap<K, V> {}
747    unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Sync for multimap<K, V> {}
748
749    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>::const_iterator")]
750    class CppMultiMapIter<K, V> {
751        hicc::cpp! {
752            typedef typename SelfContainer::const_reverse_iterator const_reverse_iterator;
753            static void next(Self& self) {
754                ++self;
755            }
756            static const K& key(const Self& self) {
757                return self->first;
758            }
759            static const V& value(const Self& self) {
760                return self->second;
761            }
762        }
763        #[cpp(func = "void SelfMethods::next(Self&)")]
764        fn next(&mut self);
765        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
766        fn as_key(&self) -> &K;
767        #[cpp(func = "const V& SelfMethods::value(const Self&)")]
768        fn as_value(&self) -> &V;
769        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
770        fn equal(&self, other: &Self) -> bool;
771        #[cpp(func = "const_reverse_iterator hicc::make_constructor<const_reverse_iterator, Self>(Self&&)")]
772        fn into_reverse(self) -> *mut CppMultiMapRevIter<K, V>;
773    }
774
775    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>::iterator")]
776    class CppMultiMapIterMut<K, V> {
777        hicc::cpp! {
778            typedef typename SelfContainer::reverse_iterator reverse_iterator;
779            static void next(Self& self) {
780                ++self;
781            }
782            static const K& key(const Self& self) {
783                return self->first;
784            }
785            static V& value(Self& self) {
786                return self->second;
787            }
788        }
789        #[cpp(func = "void SelfMethods::next(Self&)")]
790        fn next(&mut self);
791        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
792        fn as_key(&self) -> &K;
793        #[cpp(func = "V& SelfMethods::value(Self&)")]
794        fn as_value(&mut self) -> &mut V;
795        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
796        fn equal(&self, other: &Self) -> bool;
797        #[cpp(func = "reverse_iterator hicc::make_constructor<reverse_iterator, Self>(Self&&)")]
798        fn into_reverse(self) -> *mut CppMultiMapRevIterMut<K, V>;
799    }
800
801    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>::const_reverse_iterator")]
802    class CppMultiMapRevIter<K, V> {
803        hicc::cpp! {
804            static void next(Self& self) {
805                ++self;
806            }
807            static const K& key(const Self& self) {
808                return self->first;
809            }
810            static const V& value(const Self& self) {
811                return self->second;
812            }
813        }
814        #[cpp(func = "void SelfMethods::next(Self&)")]
815        fn next(&mut self);
816        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
817        fn as_key(&self) -> &K;
818        #[cpp(func = "const V& SelfMethods::value(const Self&)")]
819        fn as_value(&self) -> &V;
820        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
821        fn equal(&self, other: &Self) -> bool;
822    }
823
824    #[cpp(class = "template<class K, class V, class Compare, class Allocator> std::multimap<K, V, Compare, Allocator>::reverse_iterator")]
825    class CppMultiMapRevIterMut<K, V> {
826        hicc::cpp! {
827            static void next(Self& self) {
828                ++self;
829            }
830            static const K& key(const Self& self) {
831                return self->first;
832            }
833            static V& value(Self& self) {
834                return self->second;
835            }
836        }
837        #[cpp(func = "void SelfMethods::next(Self&)")]
838        fn next(&mut self);
839        #[cpp(func = "const K& SelfMethods::key(const Self&)")]
840        fn as_key(&self) -> &K;
841        #[cpp(func = "V& SelfMethods::value(Self&)")]
842        fn as_value(&mut self) -> &mut V;
843        #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
844        fn equal(&self, other: &Self) -> bool;
845    }
846}
847
848impl<K: AbiType + 'static, V: AbiType + 'static> multimap<K, V> {
849    /// ```
850    /// use hicc_std::MultiMapIntInt;
851    /// let mut map = MultiMapIntInt::new();
852    /// map.insert(&1, &2);
853    /// map.insert(&1, &1);
854    /// map.iter().for_each(|(k, v)| println!("key = {k}, value = {v}"));
855    /// ```
856    pub fn iter(&self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
857        MultiMapIter {
858            beg: unsafe { self.begin() },
859            end: unsafe { self.end() },
860            mark: PhantomData,
861        }
862    }
863
864    /// ```
865    /// use hicc_std::MultiMapIntInt;
866    /// let mut map = MultiMapIntInt::new();
867    /// map.insert(&1, &2);
868    /// map.insert(&2, &3);
869    /// map.iter_mut().for_each(|(_, v)| *v += 1);
870    /// let mut it = map.iter();
871    /// assert_eq!(it.next(), Some((&1, &3)));
872    /// assert_eq!(it.next(), Some((&2, &4)));
873    /// assert!(it.next().is_none());
874    /// ```
875    pub fn iter_mut(&mut self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
876        let beg = unsafe { self.begin_mut() };
877        let end = unsafe { self.end_mut() };
878        MultiMapIterMut {
879            beg,
880            end,
881            mark: PhantomData,
882        }
883    }
884
885    /// ```
886    /// use hicc_std::MultiMapIntInt;
887    /// let mut map = MultiMapIntInt::new();
888    /// map.insert(&1, &2);
889    /// map.insert(&1, &1);
890    /// map.rev_iter().for_each(|(k, v)| println!("key = {k}, value = {v}"));
891    /// ```
892    pub fn rev_iter(&self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
893        MultiMapRevIter {
894            beg: unsafe { self.rbegin() },
895            end: unsafe { self.rend() },
896            mark: PhantomData,
897        }
898    }
899
900    /// ```
901    /// use hicc_std::MultiMapIntInt;
902    /// let mut map = MultiMapIntInt::new();
903    /// map.insert(&1, &2);
904    /// map.insert(&2, &3);
905    /// map.rev_iter_mut().for_each(|(_, v)| *v += 1);
906    /// let mut it = map.iter();
907    /// assert_eq!(it.next(), Some((&1, &3)));
908    /// assert_eq!(it.next(), Some((&2, &4)));
909    /// assert!(it.next().is_none());
910    /// ```
911    pub fn rev_iter_mut(
912        &mut self,
913    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
914        let beg = unsafe { self.rbegin_mut() };
915        let end = unsafe { self.rend_mut() };
916        MultiMapRevIterMut {
917            beg,
918            end,
919            mark: PhantomData,
920        }
921    }
922
923    /// ```
924    /// use hicc_std::MultiMapIntInt;
925    /// let mut map = MultiMapIntInt::new();
926    /// map.insert(&1, &2);
927    /// map.insert(&2, &3);
928    /// map.insert(&3, &4);
929    /// let mut it = map.iter_lower_upper_bound(Some(&1), Some(&2));
930    /// assert_eq!(it.next(), Some((&1, &2)));
931    /// assert_eq!(it.next(), Some((&2, &3)));
932    /// assert_eq!(it.next(), None);
933    /// ```
934    pub fn iter_lower_upper_bound(
935        &self,
936        lower_key: Option<&K::InputType>,
937        upper_key: Option<&K::InputType>,
938    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
939        let beg = if let Some(key) = lower_key {
940            unsafe { self.lower_bound(key) }
941        } else {
942            unsafe { self.begin() }
943        };
944        let end = if let Some(key) = upper_key {
945            unsafe { self.upper_bound(key) }
946        } else {
947            unsafe { self.end() }
948        };
949        MultiMapIter {
950            beg,
951            end,
952            mark: PhantomData,
953        }
954    }
955
956    /// ```
957    /// use hicc_std::MultiMapIntInt;
958    /// let mut map = MultiMapIntInt::new();
959    /// map.insert(&1, &2);
960    /// map.insert(&2, &3);
961    /// map.insert(&3, &4);
962    /// map.iter_lower_upper_bound_mut(Some(&1), Some(&2)).for_each(|(_, v)| *v -= 1);
963    /// let mut it = map.iter_lower_upper_bound(Some(&1), Some(&2));
964    /// assert_eq!(it.next(), Some((&1, &1)));
965    /// assert_eq!(it.next(), Some((&2, &2)));
966    /// assert_eq!(it.next(), None);
967    /// ```
968    pub fn iter_lower_upper_bound_mut(
969        &mut self,
970        lower_key: Option<&K::InputType>,
971        upper_key: Option<&K::InputType>,
972    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
973        let beg = if let Some(key) = lower_key {
974            unsafe { self.lower_bound_mut(key) }
975        } else {
976            unsafe { self.begin_mut() }
977        };
978        let end = if let Some(key) = upper_key {
979            unsafe { self.upper_bound_mut(key) }
980        } else {
981            unsafe { self.end_mut() }
982        };
983        MultiMapIterMut {
984            beg,
985            end,
986            mark: PhantomData,
987        }
988    }
989
990    /// ```
991    /// use hicc_std::MultiMapIntInt;
992    /// let mut map = MultiMapIntInt::new();
993    /// map.insert(&1, &0);
994    /// map.insert(&2, &1);
995    /// map.insert(&3, &2);
996    /// let mut it = map.rev_iter_lower_upper_bound(Some(&1), Some(&2));
997    /// assert_eq!(it.next(), Some((&2, &1)));
998    /// assert_eq!(it.next(), Some((&1, &0)));
999    /// assert_eq!(it.next(), None);
1000    /// ```
1001    pub fn rev_iter_lower_upper_bound(
1002        &self,
1003        lower_key: Option<&K::InputType>,
1004        upper_key: Option<&K::InputType>,
1005    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
1006        let beg = if let Some(key) = upper_key {
1007            unsafe { self.upper_bound(key).into_value() }.into_reverse()
1008        } else {
1009            unsafe { self.rbegin() }
1010        };
1011        let end = if let Some(key) = lower_key {
1012            unsafe { self.lower_bound(key).into_value() }.into_reverse()
1013        } else {
1014            unsafe { self.rend() }
1015        };
1016        MultiMapRevIter {
1017            beg,
1018            end,
1019            mark: PhantomData,
1020        }
1021    }
1022
1023    /// ```
1024    /// use hicc_std::MultiMapIntInt;
1025    /// let mut map = MultiMapIntInt::new();
1026    /// map.insert(&1, &0);
1027    /// map.insert(&2, &1);
1028    /// map.insert(&3, &2);
1029    /// map.rev_iter_lower_upper_bound_mut(Some(&1), Some(&2)).for_each(|(_, v)| *v -= 1);
1030    /// let mut it = map.rev_iter_lower_upper_bound(Some(&1), Some(&2));
1031    /// assert_eq!(it.next(), Some((&2, &0)));
1032    /// assert_eq!(it.next(), Some((&1, &-1)));
1033    /// assert_eq!(it.next(), None);
1034    /// ```
1035    pub fn rev_iter_lower_upper_bound_mut(
1036        &mut self,
1037        lower_key: Option<&K::InputType>,
1038        upper_key: Option<&K::InputType>,
1039    ) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
1040        let beg = if let Some(key) = upper_key {
1041            unsafe { self.upper_bound_mut(key).into_value() }.into_reverse()
1042        } else {
1043            unsafe { self.rbegin_mut() }
1044        };
1045        let end = if let Some(key) = lower_key {
1046            unsafe { self.lower_bound_mut(key).into_value() }.into_reverse()
1047        } else {
1048            unsafe { self.rend_mut() }
1049        };
1050        MultiMapRevIterMut {
1051            beg,
1052            end,
1053            mark: PhantomData,
1054        }
1055    }
1056}
1057
1058/// 对应`std::multimap<K, V>::const_iterator`
1059struct MultiMapIter<'a, K: AbiType + 'static, V: AbiType + 'static> {
1060    beg: ClassMutPtr<'static, CppMultiMapIter<K, V>>,
1061    end: ClassMutPtr<'static, CppMultiMapIter<K, V>>,
1062    mark: PhantomData<&'a multimap<K, V>>,
1063}
1064
1065impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MultiMapIter<'a, K, V> {
1066    type Item = (K::OutputRef<'a>, V::OutputRef<'a>);
1067    fn next(&mut self) -> Option<Self::Item> {
1068        if !self.beg.equal(&self.end) {
1069            unsafe {
1070                let key = self.beg.as_deref().as_key();
1071                let val = self.beg.as_deref().as_value();
1072                self.beg.next();
1073                return Some((key, val));
1074            }
1075        }
1076        None
1077    }
1078}
1079
1080/// 对应`std::multimap<K, V>::const_reverse_iterator`
1081struct MultiMapRevIter<'a, K: AbiType + 'static, V: AbiType + 'static> {
1082    beg: ClassMutPtr<'static, CppMultiMapRevIter<K, V>>,
1083    end: ClassMutPtr<'static, CppMultiMapRevIter<K, V>>,
1084    mark: PhantomData<&'a multimap<K, V>>,
1085}
1086
1087impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MultiMapRevIter<'a, K, V> {
1088    type Item = (K::OutputRef<'a>, V::OutputRef<'a>);
1089    fn next(&mut self) -> Option<Self::Item> {
1090        if !self.beg.equal(&self.end) {
1091            unsafe {
1092                let key = self.beg.as_deref().as_key();
1093                let val = self.beg.as_deref().as_value();
1094                self.beg.next();
1095                return Some((key, val));
1096            }
1097        }
1098        None
1099    }
1100}
1101
1102/// 对应`std::multimap<K, V>::iterator`
1103struct MultiMapIterMut<'a, K: AbiType + 'static, V: AbiType + 'static> {
1104    beg: ClassMutPtr<'static, CppMultiMapIterMut<K, V>>,
1105    end: ClassMutPtr<'static, CppMultiMapIterMut<K, V>>,
1106    mark: PhantomData<&'a mut multimap<K, V>>,
1107}
1108
1109impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MultiMapIterMut<'a, K, V> {
1110    type Item = (K::OutputRef<'a>, V::OutputRefMut<'a>);
1111    fn next(&mut self) -> Option<Self::Item> {
1112        if !self.beg.equal(&self.end) {
1113            unsafe {
1114                let key = self.beg.as_deref().as_key();
1115                let val = self.beg.as_deref_mut().as_value();
1116                self.beg.next();
1117                return Some((key, val));
1118            }
1119        }
1120        None
1121    }
1122}
1123
1124/// 对应`std::multimap<K, V>::reverse_iterator`
1125struct MultiMapRevIterMut<'a, K: AbiType + 'static, V: AbiType + 'static> {
1126    beg: ClassMutPtr<'static, CppMultiMapRevIterMut<K, V>>,
1127    end: ClassMutPtr<'static, CppMultiMapRevIterMut<K, V>>,
1128    mark: PhantomData<&'a mut multimap<K, V>>,
1129}
1130
1131impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for MultiMapRevIterMut<'a, K, V> {
1132    type Item = (K::OutputRef<'a>, V::OutputRefMut<'a>);
1133    fn next(&mut self) -> Option<Self::Item> {
1134        if !self.beg.equal(&self.end) {
1135            unsafe {
1136                let key = self.beg.as_deref().as_key();
1137                let val = self.beg.as_deref_mut().as_value();
1138                self.beg.next();
1139                return Some((key, val));
1140            }
1141        }
1142        None
1143    }
1144}