1use hicc::{AbiType, ClassMutPtr};
2use std::iter::Iterator;
3use std::marker::PhantomData;
4
5hicc::cpp! {
6 #include <unordered_map>
7}
8
9hicc::import_class! {
10 #[cpp(class = "template <class K, class V, class Hash, class Pred, class Allocator> std::unordered_map<K, V, Hash, Pred, Allocator>")]
11 pub class unordered_map<K, V> {
12 hicc::cpp! {
13 typedef typename Self::iterator iterator;
14 typedef typename Self::const_iterator const_iterator;
15 }
16 #[cpp(method = "bool empty() const")]
22 pub fn is_empty(&self) -> bool;
23
24 #[cpp(method = "size_t size() const")]
30 pub fn size(&self) -> usize;
31
32 #[cpp(method = "size_t max_size() const")]
38 pub fn max_size(&self) -> usize;
39
40 #[cpp(method = "void clear()")]
49 pub fn clear(&mut self);
50
51 #[cpp(method = "void swap(Self&)")]
61 pub fn swap(&mut self, other: &mut Self);
62
63 #[cpp(method = "size_t count(const K&) const")]
71 pub fn count(&self, key: &K) -> usize;
72
73 hicc::cpp! {
74 static bool contains(const Self& self, const K& key) {
75 return self.find(key) != self.end();
76 }
77 }
78 #[cpp(func = "bool SelfMethods::contains(const Self&, const K&)")]
86 pub fn contains(&self, key: &K) -> bool;
87
88 #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
96 pub fn assign(&mut self, other: &Self);
97
98 hicc::cpp! {
99 static bool insert(Self& self, const K& key, const V& val) {
100 return self.insert(std::make_pair(key, val)).second;
101 }
102 }
103 #[cpp(func = "bool SelfMethods::insert(Self&, const K&, const V&)")]
111 pub fn insert(&mut self, key: &K, val: &V) -> bool;
112
113 #[cpp(method = "size_t erase(const K&)")]
121 pub fn erase(&mut self, key: &K) -> usize;
122
123 #[cpp(method = "const_iterator find(const K&) const")]
124 unsafe fn find(&self, key: &K) -> *mut CppUnorderedMapIter<K, V>;
125 #[cpp(method = "iterator find(const K&)")]
126 unsafe fn find_mut(&mut self, key: &K) -> *mut CppUnorderedMapIterMut<K, V>;
127 #[cpp(method = "const_iterator begin() const")]
128 unsafe fn begin(&self) -> *mut CppUnorderedMapIter<K, V>;
129 #[cpp(method = "iterator begin()")]
130 unsafe fn begin_mut(&mut self) -> *mut CppUnorderedMapIterMut<K, V>;
131 #[cpp(method = "const_iterator end() const")]
132 unsafe fn end(&self) -> *mut CppUnorderedMapIter<K, V>;
133 #[cpp(method = "iterator end()")]
134 unsafe fn end_mut(&mut self) -> *mut CppUnorderedMapIterMut<K, V>;
135 }
136
137 unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Send for unordered_map<K, V> {}
138 unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Sync for unordered_map<K, V> {}
139
140 #[cpp(class = "template <class K, class V, class Hash, class Pred, class Allocator> std::unordered_map<K, V, Hash, Pred, Allocator>::const_iterator")]
141 class CppUnorderedMapIter<K, V> {
142 hicc::cpp! {
143 static void next(Self& self) {
144 ++self;
145 }
146 static const K& key(const Self& self) {
147 return self->first;
148 }
149 static const V& value(const Self& self) {
150 return self->second;
151 }
152 }
153 #[cpp(func = "void SelfMethods::next(Self&)")]
154 fn next(&mut self);
155 #[cpp(func = "const K& SelfMethods::key(const Self&)")]
156 fn as_key(&self) -> &K;
157 #[cpp(func = "const V& SelfMethods::value(const Self&)")]
158 fn as_value(&self) -> &V;
159 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
160 fn equal(&self, other: &Self) -> bool;
161 }
162
163 #[cpp(class = "template <class K, class V, class Hash, class Pred, class Allocator> std::unordered_map<K, V, Hash, Pred, Allocator>::iterator")]
164 class CppUnorderedMapIterMut<K, V> {
165 hicc::cpp! {
166 static void next(Self& self) {
167 ++self;
168 }
169 static const K& key(const Self& self) {
170 return self->first;
171 }
172 static V& value(Self& self) {
173 return self->second;
174 }
175 }
176 #[cpp(func = "void SelfMethods::next(Self&)")]
177 fn next(&mut self);
178 #[cpp(func = "const K& SelfMethods::key(const Self&)")]
179 fn as_key(&self) -> &K;
180 #[cpp(func = "V& SelfMethods::value(Self&)")]
181 fn as_value(&mut self) -> &mut V;
182 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
183 fn equal(&self, other: &Self) -> bool;
184 }
185}
186
187impl<K: AbiType + 'static, V: AbiType + 'static> unordered_map<K, V> {
188 pub fn get(&self, key: &K::InputType) -> Option<V::OutputRef<'_>> {
196 unsafe {
197 let it = self.find(key);
198 if !it.equal(&self.end()) {
199 return Some(it.as_deref().as_value());
200 }
201 }
202 None
203 }
204 pub fn get_mut(&mut self, key: &K::InputType) -> Option<V::OutputRefMut<'_>> {
219 unsafe {
220 let mut it = self.find_mut(key);
221 if !it.equal(&self.end_mut()) {
222 return Some(it.as_deref_mut().as_value());
223 }
224 }
225 None
226 }
227 pub fn iter(&self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
235 UnorderedMapIter {
236 beg: unsafe { self.begin() },
237 end: unsafe { self.end() },
238 mark: PhantomData,
239 }
240 }
241
242 pub fn iter_mut(&mut self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
252 let beg = unsafe { self.begin_mut() };
253 let end = unsafe { self.end_mut() };
254 UnorderedMapIterMut {
255 beg,
256 end,
257 mark: PhantomData,
258 }
259 }
260}
261
262struct UnorderedMapIter<'a, K: AbiType + 'static, V: AbiType + 'static> {
264 beg: ClassMutPtr<'static, CppUnorderedMapIter<K, V>>,
265 end: ClassMutPtr<'static, CppUnorderedMapIter<K, V>>,
266 mark: PhantomData<&'a unordered_map<K, V>>,
267}
268
269impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for UnorderedMapIter<'a, K, V> {
270 type Item = (K::OutputRef<'a>, V::OutputRef<'a>);
271 fn next(&mut self) -> Option<Self::Item> {
272 if !self.beg.equal(&self.end) {
273 unsafe {
274 let key = self.beg.as_deref().as_key();
275 let val = self.beg.as_deref().as_value();
276 self.beg.next();
277 return Some((key, val));
278 }
279 }
280 None
281 }
282}
283
284struct UnorderedMapIterMut<'a, K: AbiType + 'static, V: AbiType + 'static> {
286 beg: ClassMutPtr<'static, CppUnorderedMapIterMut<K, V>>,
287 end: ClassMutPtr<'static, CppUnorderedMapIterMut<K, V>>,
288 mark: PhantomData<&'a mut unordered_map<K, V>>,
289}
290
291impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for UnorderedMapIterMut<'a, K, V> {
292 type Item = (K::OutputRef<'a>, V::OutputRefMut<'a>);
293 fn next(&mut self) -> Option<Self::Item> {
294 if !self.beg.equal(&self.end) {
295 unsafe {
296 let key = self.beg.as_deref().as_key();
297 let val = self.beg.as_deref_mut().as_value();
298 self.beg.next();
299 return Some((key, val));
300 }
301 }
302 None
303 }
304}
305
306hicc::import_class! {
307 #[cpp(class = "template <class K, class V, class Hash, class Pred, class Allocator> std::unordered_multimap<K, V, Hash, Pred, Allocator>")]
308 pub class unordered_multimap<K, V> {
309 hicc::cpp! {
310 typedef typename Self::iterator iterator;
311 typedef typename Self::const_iterator const_iterator;
312 }
313 #[cpp(method = "bool empty() const")]
319 pub fn is_empty(&self) -> bool;
320
321 #[cpp(method = "size_t size() const")]
327 pub fn size(&self) -> usize;
328
329 #[cpp(method = "size_t max_size() const")]
335 pub fn max_size(&self) -> usize;
336
337 #[cpp(method = "void clear()")]
346 pub fn clear(&mut self);
347
348 #[cpp(method = "void swap(Self&)")]
358 pub fn swap(&mut self, other: &mut Self);
359
360 #[cpp(method = "size_t count(const K&) const")]
369 pub fn count(&self, key: &K) -> usize;
370
371 hicc::cpp! {
372 static bool contains(const Self& self, const K& key) {
373 return self.find(key) != self.end();
374 }
375 }
376 #[cpp(func = "bool SelfMethods::contains(const Self&, const K&)")]
384 pub fn contains(&self, key: &K) -> bool;
385
386
387 #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
395 pub fn assign(&mut self, other: &Self);
396
397 hicc::cpp! {
398 static void insert(Self& self, const K& key, const V& val) {
399 self.insert(std::make_pair(key, val));
400 }
401 }
402 #[cpp(func = "void SelfMethods::insert(Self&, const K&, const V&)")]
410 pub fn insert(&mut self, key: &K, val: &V);
411
412 #[cpp(method = "size_t erase(const K&)")]
420 pub fn erase(&mut self, key: &K) -> usize;
421
422 #[cpp(method = "const_iterator find(const K&) const")]
423 unsafe fn find(&self, key: &K) -> *mut CppUnorderedMultiMapIter<K, V>;
424 #[cpp(method = "iterator find(const K&)")]
425 unsafe fn find_mut(&mut self, key: &K) -> *mut CppUnorderedMultiMapIterMut<K, V>;
426 #[cpp(method = "const_iterator begin() const")]
427 unsafe fn begin(&self) -> *mut CppUnorderedMultiMapIter<K, V>;
428 #[cpp(method = "iterator begin()")]
429 unsafe fn begin_mut(&mut self) -> *mut CppUnorderedMultiMapIterMut<K, V>;
430 #[cpp(method = "const_iterator end() const")]
431 unsafe fn end(&self) -> *mut CppUnorderedMultiMapIter<K, V>;
432 #[cpp(method = "iterator end()")]
433 unsafe fn end_mut(&mut self) -> *mut CppUnorderedMultiMapIterMut<K, V>;
434 }
435
436 unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Send for unordered_multimap<K, V> {}
437 unsafe impl<K: AbiType + Sync, V: AbiType + Sync> Sync for unordered_multimap<K, V> {}
438
439 #[cpp(class = "template <class K, class V, class Hash, class Pred, class Allocator> std::unordered_multimap<K, V, Hash, Pred, Allocator>::const_iterator")]
440 class CppUnorderedMultiMapIter<K, V> {
441 hicc::cpp! {
442 static void next(Self& self) {
443 ++self;
444 }
445 static const K& key(const Self& self) {
446 return self->first;
447 }
448 static const V& value(const Self& self) {
449 return self->second;
450 }
451 }
452 #[cpp(func = "void SelfMethods::next(Self&)")]
453 fn next(&mut self);
454 #[cpp(func = "const K& SelfMethods::key(const Self&)")]
455 fn as_key(&self) -> &K;
456 #[cpp(func = "const V& SelfMethods::value(const Self&)")]
457 fn as_value(&self) -> &V;
458 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
459 fn equal(&self, other: &Self) -> bool;
460 }
461
462 #[cpp(class = "template <class K, class V, class Hash, class Pred, class Allocator> std::unordered_multimap<K, V, Hash, Pred, Allocator>::iterator")]
463 class CppUnorderedMultiMapIterMut<K, V> {
464 hicc::cpp! {
465 static void next(Self& self) {
466 ++self;
467 }
468 static const K& key(const Self& self) {
469 return self->first;
470 }
471 static V& value(Self& self) {
472 return self->second;
473 }
474 }
475 #[cpp(func = "void SelfMethods::next(Self&)")]
476 fn next(&mut self);
477 #[cpp(func = "const K& SelfMethods::key(const Self&)")]
478 fn as_key(&self) -> &K;
479 #[cpp(func = "V& SelfMethods::value(Self&)")]
480 fn as_value(&mut self) -> &mut V;
481 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
482 fn equal(&self, other: &Self) -> bool;
483 }
484}
485
486impl<K: AbiType, V: AbiType> unordered_multimap<K, V> {
487 pub fn get(&self, key: &K::InputType) -> Option<V::OutputRef<'_>> {
495 unsafe {
496 let it = self.find(key);
497 if !it.equal(&self.end()) {
498 return Some(it.as_deref().as_value());
499 }
500 }
501 None
502 }
503 pub fn get_mut(&mut self, key: &K::InputType) -> Option<V::OutputRefMut<'_>> {
518 unsafe {
519 let mut it = self.find_mut(key);
520 if !it.equal(&self.end_mut()) {
521 return Some(it.as_deref_mut().as_value());
522 }
523 }
524 None
525 }
526
527 pub fn iter(&self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRef<'_>)> {
535 UnorderedMultiMapIter {
536 beg: unsafe { self.begin() },
537 end: unsafe { self.end() },
538 mark: PhantomData,
539 }
540 }
541
542 pub fn iter_mut(&mut self) -> impl Iterator<Item = (K::OutputRef<'_>, V::OutputRefMut<'_>)> {
552 let beg = unsafe { self.begin_mut() };
553 let end = unsafe { self.end_mut() };
554 UnorderedMultiMapIterMut {
555 beg,
556 end,
557 mark: PhantomData,
558 }
559 }
560}
561
562struct UnorderedMultiMapIter<'a, K: AbiType + 'static, V: AbiType + 'static> {
564 beg: ClassMutPtr<'static, CppUnorderedMultiMapIter<K, V>>,
565 end: ClassMutPtr<'static, CppUnorderedMultiMapIter<K, V>>,
566 mark: PhantomData<&'a unordered_multimap<K, V>>,
567}
568
569impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator for UnorderedMultiMapIter<'a, K, V> {
570 type Item = (K::OutputRef<'a>, V::OutputRef<'a>);
571 fn next(&mut self) -> Option<Self::Item> {
572 if !self.beg.equal(&self.end) {
573 unsafe {
574 let key = self.beg.as_deref().as_key();
575 let val = self.beg.as_deref().as_value();
576 self.beg.next();
577 return Some((key, val));
578 }
579 }
580 None
581 }
582}
583struct UnorderedMultiMapIterMut<'a, K: AbiType + 'static, V: AbiType + 'static> {
585 beg: ClassMutPtr<'static, CppUnorderedMultiMapIterMut<K, V>>,
586 end: ClassMutPtr<'static, CppUnorderedMultiMapIterMut<K, V>>,
587 mark: PhantomData<&'a unordered_multimap<K, V>>,
588}
589
590impl<'a, K: AbiType + 'static, V: AbiType + 'static> Iterator
591 for UnorderedMultiMapIterMut<'a, K, V>
592{
593 type Item = (K::OutputRef<'a>, V::OutputRefMut<'a>);
594 fn next(&mut self) -> Option<Self::Item> {
595 if !self.beg.equal(&self.end) {
596 unsafe {
597 let key = self.beg.as_deref().as_key();
598 let val = self.beg.as_deref_mut().as_value();
599 self.beg.next();
600 return Some((key, val));
601 }
602 }
603 None
604 }
605}