1use hicc::{AbiType, ClassMutPtr};
2use std::iter::Iterator;
3
4hicc::cpp! {
5 #include <set>
6}
7
8hicc::import_class! {
9 #[cpp(class = "template<class T, class Compare, class Allocator> std::set<T, Compare, Allocator>")]
10 pub class set<T> {
11 hicc::cpp! {
12 typedef typename Self::const_iterator const_iterator;
13 typedef typename Self::const_reverse_iterator const_reverse_iterator;
14 }
15 #[cpp(method = "bool empty() const")]
21 pub fn is_empty(&self) -> bool;
22
23 #[cpp(method = "size_t size() const")]
29 pub fn size(&self) -> usize;
30
31 #[cpp(method = "size_t max_size() const")]
37 pub fn max_size(&self) -> usize;
38
39 #[cpp(method = "void clear()")]
47 pub fn clear(&mut self);
48
49 #[cpp(method = "void swap(Self&)")]
57 pub fn swap(&mut self, other: &mut Self);
58
59 #[cpp(method = "size_t count(const T&) const")]
66 pub fn count(&self, val: &T) -> usize;
67
68 hicc::cpp! {
69 static bool contains(const Self& self, const T& val) {
70 return self.find(val) != self.end();
71 }
72 }
73 #[cpp(func = "bool SelfMethods::contains(const Self&, const T&)")]
80 pub fn contains(&self, val: &T) -> bool;
81
82 #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
90 pub fn assign(&mut self, other: &Self);
91
92 hicc::cpp! {
93 static bool insert(Self& self, const T& val) {
94 return self.insert(val).second;
95 }
96 }
97 #[cpp(func = "bool SelfMethods::insert(Self&, const T&)")]
104 pub fn insert(&mut self, val: &T) -> bool;
105
106 #[cpp(method = "size_t erase(const T&)")]
114 pub fn erase(&mut self, val: &T) -> usize;
115
116 #[cpp(method = "const_iterator find(const T&) const")]
117 unsafe fn find(&self, val: &T) -> *mut CppSetIter<T>;
118 #[cpp(method = "const_iterator lower_bound(const T&) const")]
119 unsafe fn lower_bound(&self, val: &T) -> *mut CppSetIter<T>;
120 #[cpp(method = "const_iterator upper_bound(const T&) const")]
121 unsafe fn upper_bound(&self, val: &T) -> *mut CppSetIter<T>;
122 #[cpp(method = "const_iterator begin() const")]
123 unsafe fn begin(&self) -> *mut CppSetIter<T>;
124 #[cpp(method = "const_iterator end() const")]
125 unsafe fn end(&self) -> *mut CppSetIter<T>;
126 #[cpp(method = "const_reverse_iterator rbegin() const")]
127 unsafe fn rbegin(&self) -> *mut CppSetRevIter<T>;
128 #[cpp(method = "const_reverse_iterator rend() const")]
129 unsafe fn rend(&self) -> *mut CppSetRevIter<T>;
130 }
131
132 unsafe impl<T: AbiType + Sync> Send for set<T> {}
133 unsafe impl<T: AbiType + Sync> Sync for set<T> {}
134
135 #[cpp(class = "template<class T, class Compare, class Allocator> std::set<T, Compare, Allocator>::const_iterator")]
136 class CppSetIter<T> {
137 hicc::cpp! {
138 typedef typename SelfContainer::const_reverse_iterator const_reverse_iterator;
139 static const T& next(Self& self) {
140 return *self++;
141 }
142 }
143 #[cpp(func = "const T& SelfMethods::next(Self&)")]
144 unsafe fn next(&mut self) -> &T;
145 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
146 fn equal(&self, other: &Self) -> bool;
147 #[cpp(func = "const_reverse_iterator hicc::make_constructor<const_reverse_iterator, Self>(Self&&)")]
148 fn into_reverse(self) -> *mut CppSetRevIter<T>;
149 }
150
151 #[cpp(class = "template<class T, class Compare, class Allocator> std::set<T, Compare, Allocator>::const_reverse_iterator")]
152 class CppSetRevIter<T> {
153 hicc::cpp! {
154 static const T& next(Self& self) {
155 return *self++;
156 }
157 }
158 #[cpp(func = "const T& SelfMethods::next(Self&)")]
159 unsafe fn next(&mut self) -> &T;
160 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
161 fn equal(&self, other: &Self) -> bool;
162 }
163}
164
165impl<T: AbiType> set<T> {
166 pub fn iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
174 SetIter {
175 beg: unsafe { self.begin() },
176 end: unsafe { self.end() },
177 }
178 }
179
180 pub fn rev_iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
188 SetRevIter {
189 beg: unsafe { self.rbegin() },
190 end: unsafe { self.rend() },
191 }
192 }
193
194 pub fn iter_lower_upper_bound(
206 &self,
207 lower_key: Option<&T::InputType>,
208 upper_key: Option<&T::InputType>,
209 ) -> impl Iterator<Item = T::OutputRef<'_>> {
210 let beg = if let Some(key) = lower_key {
211 unsafe { self.lower_bound(key) }
212 } else {
213 unsafe { self.begin() }
214 };
215 let end = if let Some(key) = upper_key {
216 unsafe { self.upper_bound(key) }
217 } else {
218 unsafe { self.end() }
219 };
220 SetIter { beg, end }
221 }
222
223 pub fn rev_iter_lower_upper_bound(
235 &self,
236 lower_key: Option<&T::InputType>,
237 upper_key: Option<&T::InputType>,
238 ) -> impl Iterator<Item = T::OutputRef<'_>> {
239 let beg = if let Some(key) = upper_key {
240 unsafe { self.upper_bound(key).into_value().into_reverse() }
241 } else {
242 unsafe { self.rbegin() }
243 };
244 let end = if let Some(key) = lower_key {
245 unsafe { self.lower_bound(key).into_value().into_reverse() }
246 } else {
247 unsafe { self.rend() }
248 };
249 SetRevIter { beg, end }
250 }
251}
252
253struct SetIter<'a, T: AbiType + 'static> {
255 beg: ClassMutPtr<'a, CppSetIter<T>>,
256 end: ClassMutPtr<'a, CppSetIter<T>>,
257}
258
259impl<'a, T: AbiType + 'static> Iterator for SetIter<'a, T> {
260 type Item = T::OutputRef<'a>;
261 fn next(&mut self) -> Option<Self::Item> {
262 if !self.beg.equal(&self.end) {
263 return unsafe { Some(self.beg.as_deref_mut().next()) };
264 }
265 None
266 }
267}
268
269struct SetRevIter<'a, T: AbiType + 'static> {
271 beg: ClassMutPtr<'a, CppSetRevIter<T>>,
272 end: ClassMutPtr<'a, CppSetRevIter<T>>,
273}
274
275impl<'a, T: AbiType + 'static> Iterator for SetRevIter<'a, T> {
276 type Item = T::OutputRef<'a>;
277 fn next(&mut self) -> Option<Self::Item> {
278 if !self.beg.equal(&self.end) {
279 return unsafe { Some(self.beg.as_deref_mut().next()) };
280 }
281 None
282 }
283}
284
285hicc::import_class! {
286 #[cpp(class = "template<class T, class Compare, class Allocator> std::multiset<T, Compare, Allocator>")]
287 pub class multiset<T> {
288 hicc::cpp! {
289 typedef typename Self::iterator iterator;
290 typedef typename Self::const_iterator const_iterator;
291 typedef typename Self::const_reverse_iterator const_reverse_iterator;
292 }
293 #[cpp(method = "bool empty() const")]
299 pub fn is_empty(&self) -> bool;
300
301 #[cpp(method = "size_t size() const")]
307 pub fn size(&self) -> usize;
308
309 #[cpp(method = "size_t max_size() const")]
315 pub fn max_size(&self) -> usize;
316
317 #[cpp(method = "void clear()")]
325 pub fn clear(&mut self);
326
327 #[cpp(method = "void swap(Self&)")]
335 pub fn swap(&mut self, other: &mut Self);
336
337 #[cpp(method = "size_t count(const T&) const")]
344 pub fn count(&self, val: &T) -> usize;
345
346 hicc::cpp! {
347 static bool contains(const Self& self, const T& val) {
348 return self.find(val) != self.end();
349 }
350 }
351 #[cpp(func = "bool SelfMethods::contains(const Self&, const T&)")]
358 pub fn contains(&self, val: &T) -> bool;
359
360 #[cpp(func = "void hicc::make_assign<Self, Self>(Self&, const Self&)")]
368 pub fn assign(&mut self, other: &Self);
369
370 #[cpp(method = "iterator insert(const T&)")]
378 pub fn insert(&mut self, val: &T);
379
380 #[cpp(method = "size_t erase(const T&)")]
388 pub fn erase(&mut self, val: &T) -> usize;
389
390 #[cpp(method = "const_iterator find(const T&) const")]
391 unsafe fn find(&self, val: &T) -> *mut CppMultiSetIter<T>;
392 #[cpp(method = "const_iterator lower_bound(const T&) const")]
393 unsafe fn lower_bound(&self, val: &T) -> *mut CppMultiSetIter<T>;
394 #[cpp(method = "const_iterator upper_bound(const T&) const")]
395 unsafe fn upper_bound(&self, val: &T) -> *mut CppMultiSetIter<T>;
396 #[cpp(method = "const_iterator begin() const")]
397 unsafe fn begin(&self) -> *mut CppMultiSetIter<T>;
398 #[cpp(method = "const_iterator end() const")]
399 unsafe fn end(&self) -> *mut CppMultiSetIter<T>;
400 #[cpp(method = "const_reverse_iterator crbegin() const")]
401 unsafe fn rbegin(&self) -> *mut CppMultiSetRevIter<T>;
402 #[cpp(method = "const_reverse_iterator crend() const")]
403 unsafe fn rend(&self) -> *mut CppMultiSetRevIter<T>;
404
405 }
406
407 unsafe impl<T: AbiType + Sync> Send for multiset<T> {}
408 unsafe impl<T: AbiType + Sync> Sync for multiset<T> {}
409
410 #[cpp(class = "template<class T, class Compare, class Allocator> std::multiset<T, Compare, Allocator>::const_iterator")]
411 class CppMultiSetIter<T> {
412 hicc::cpp! {
413 typedef typename SelfContainer::const_reverse_iterator const_reverse_iterator;
414 static const T& next(Self& self) {
415 return *self++;
416 }
417 }
418 #[cpp(func = "const T& SelfMethods::next(Self&)")]
419 unsafe fn next(&mut self) -> &T;
420 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
421 fn equal(&self, other: &Self) -> bool;
422 #[cpp(func = "const_reverse_iterator hicc::make_constructor<const_reverse_iterator, Self>(Self&&)")]
423 fn into_reverse(self) -> *mut CppMultiSetRevIter<T>;
424 }
425
426 #[cpp(class = "template<class T, class Compare, class Allocator> std::multiset<T, Compare, Allocator>::const_reverse_iterator")]
427 class CppMultiSetRevIter<T> {
428 hicc::cpp! {
429 static const T& next(Self& self) {
430 return *self++;
431 }
432 }
433 #[cpp(func = "const T& SelfMethods::next(Self&)")]
434 unsafe fn next(&mut self) -> &T;
435 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
436 fn equal(&self, other: &Self) -> bool;
437 }
438}
439
440impl<T: AbiType> multiset<T> {
441 pub fn iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
449 MultiSetIter {
450 beg: unsafe { self.begin() },
451 end: unsafe { self.end() },
452 }
453 }
454
455 pub fn rev_iter(&self) -> impl Iterator<Item = T::OutputRef<'_>> {
463 MultiSetRevIter {
464 beg: unsafe { self.rbegin() },
465 end: unsafe { self.rend() },
466 }
467 }
468
469 pub fn iter_lower_upper_bound(
483 &self,
484 lower_key: Option<&T::InputType>,
485 upper_key: Option<&T::InputType>,
486 ) -> impl Iterator<Item = T::OutputRef<'_>> {
487 let beg = if let Some(key) = lower_key {
488 unsafe { self.lower_bound(key) }
489 } else {
490 unsafe { self.begin() }
491 };
492 let end = if let Some(key) = upper_key {
493 unsafe { self.upper_bound(key) }
494 } else {
495 unsafe { self.end() }
496 };
497 MultiSetIter { beg, end }
498 }
499
500 pub fn rev_iter_lower_upper_bound(
514 &self,
515 lower_key: Option<&T::InputType>,
516 upper_key: Option<&T::InputType>,
517 ) -> impl Iterator<Item = T::OutputRef<'_>> {
518 let beg = if let Some(key) = upper_key {
519 unsafe { self.upper_bound(key).into_value().into_reverse() }
520 } else {
521 unsafe { self.rbegin() }
522 };
523 let end = if let Some(key) = lower_key {
524 unsafe { self.lower_bound(key).into_value().into_reverse() }
525 } else {
526 unsafe { self.rend() }
527 };
528 MultiSetRevIter { beg, end }
529 }
530}
531
532struct MultiSetIter<'a, T: AbiType + 'static> {
534 beg: ClassMutPtr<'a, CppMultiSetIter<T>>,
535 end: ClassMutPtr<'a, CppMultiSetIter<T>>,
536}
537
538impl<'a, T: AbiType + 'static> Iterator for MultiSetIter<'a, T> {
539 type Item = T::OutputRef<'a>;
540 fn next(&mut self) -> Option<Self::Item> {
541 if !self.beg.equal(&self.end) {
542 return Some(unsafe { self.beg.as_deref_mut().next() });
543 }
544 None
545 }
546}
547
548struct MultiSetRevIter<'a, T: AbiType + 'static> {
550 beg: ClassMutPtr<'a, CppMultiSetRevIter<T>>,
551 end: ClassMutPtr<'a, CppMultiSetRevIter<T>>,
552}
553
554impl<'a, T: AbiType + 'static> Iterator for MultiSetRevIter<'a, T> {
555 type Item = T::OutputRef<'a>;
556 fn next(&mut self) -> Option<Self::Item> {
557 if !self.beg.equal(&self.end) {
558 return Some(unsafe { self.beg.as_deref_mut().next() });
559 }
560 None
561 }
562}