hicc_std/std_forward_list.rs
1use hicc::{AbiType, ClassMutPtr};
2use std::iter::Iterator;
3
4hicc::cpp! {
5 #include <forward_list>
6}
7
8hicc::import_class! {
9 #[cpp(class = "template<class T, class Allocator> std::forward_list<T, Allocator>")]
10 pub class forward_list<T> {
11 hicc::cpp! {
12 typedef typename Self::iterator iterator;
13 typedef typename Self::const_iterator const_iterator;
14 }
15 /// ```
16 /// use hicc_std::ForwardListInt;
17 /// let list = ForwardListInt::new();
18 /// assert!(list.is_empty());
19 /// ```
20 #[cpp(method = "bool empty() const")]
21 pub fn is_empty(&self) -> bool;
22
23 /// ```
24 /// use hicc_std::ForwardListInt;
25 /// let mut list = ForwardListInt::new();
26 /// list.push_front(&1);
27 /// assert!(list.max_size() >= 1);
28 /// ```
29 #[cpp(method = "size_t max_size() const")]
30 pub fn max_size(&self) -> usize;
31
32 /// ```
33 /// use hicc_std::ForwardListInt;
34 /// let mut list = ForwardListInt::new();
35 /// list.push_front(&1);
36 /// list.clear();
37 /// assert!(list.is_empty());
38 /// ```
39 #[cpp(method = "void clear()")]
40 pub fn clear(&mut self);
41
42 /// ```
43 /// use hicc_std::ForwardListInt;
44 /// let mut list = ForwardListInt::new();
45 /// list.resize(10, &1);
46 /// assert_eq!(list.iter().count(), 10);
47 /// list.iter().for_each(|v| { println!("{v}"); });
48 /// ```
49 #[cpp(method = "void resize(size_t, const T&)")]
50 pub fn resize(&mut self, n: usize, val: &T);
51
52 /// 如果为空则忽略.
53 /// ```
54 /// use hicc_std::ForwardListInt;
55 /// let mut list = ForwardListInt::new();
56 /// list.push_front(&1);
57 /// list.pop_front();
58 /// assert!(list.is_empty());
59 /// ```
60 pub fn pop_front(&mut self) {
61 if !self.is_empty() {
62 unsafe { self._pop_front() };
63 }
64 }
65 #[cpp(method = "void pop_front()")]
66 unsafe fn _pop_front(&mut self);
67
68 /// ```
69 /// use hicc_std::ForwardListInt;
70 /// let mut list = ForwardListInt::new();
71 /// list.push_front(&1);
72 /// assert_eq!(list.front(), Some(&1));
73 /// ```
74 #[cpp(method = "void push_front(const T&)")]
75 pub fn push_front(&mut self, val: &T);
76
77 /// ```
78 /// use hicc_std::ForwardListInt;
79 /// let mut list = ForwardListInt::new();
80 /// list.push_front(&1);
81 /// list.swap(&mut ForwardListInt::new());
82 /// assert!(list.is_empty());
83 /// ```
84 #[cpp(method = "void swap(Self&)")]
85 pub fn swap(&mut self, other: &mut Self);
86
87 /// ```
88 /// use hicc_std::ForwardListInt;
89 /// let mut list = ForwardListInt::new();
90 /// list.assign(10, &1);
91 /// assert_eq!(list.iter().count(), 10);
92 /// assert_eq!(list.front(), Some(&1));
93 /// list.iter().for_each(|v| { assert_eq!(v, &1); });
94 /// ```
95 #[cpp(method = "void assign(size_t, const T&)")]
96 pub fn assign(&mut self, ncount: usize, val: &T);
97
98 /// ```
99 /// use hicc_std::ForwardListInt;
100 /// let mut list = ForwardListInt::new();
101 /// list.push_front(&2);
102 /// list.push_front(&1);
103 /// assert_eq!(list.front(), Some(&1));
104 /// list.reverse();
105 /// assert_eq!(list.front(), Some(&2));
106 /// list.iter().for_each(|v| { println!("{v}"); });
107 /// ```
108 #[cpp(method = "void reverse()")]
109 pub fn reverse(&mut self);
110
111 /// ```
112 /// use hicc_std::ForwardListInt;
113 /// let mut list = ForwardListInt::new();
114 /// list.push_front(&1);
115 /// assert_eq!(list.front(), Some(&1));
116 /// ```
117 pub fn front(&self) -> Option<T::OutputRef<'_>> {
118 if !self.is_empty() {
119 return Some(unsafe { self._front() });
120 }
121 None
122 }
123
124 #[cpp(method = "const T& front() const")]
125 unsafe fn _front(&self) -> &T;
126
127 /// ```
128 /// use hicc_std::ForwardListInt;
129 /// let mut list = ForwardListInt::new();
130 /// list.push_front(&1);
131 /// *list.front_mut().unwrap() = 2;
132 /// assert_eq!(list.front(), Some(&2));
133 ///
134 /// use hicc_std::{string, ForwardListString};
135 /// use hicc::AbiClass;
136 /// let mut list = ForwardListString::new();
137 /// list.push_front(&string::from(c"hello"));
138 /// list.front_mut().unwrap().write(string::from(c"world"));
139 /// assert_eq!(list.front() , Some(string::from(c"world").into_ref()));
140 /// ```
141 pub fn front_mut(&mut self) -> Option<T::OutputRefMut<'_>> {
142 if !self.is_empty() {
143 return Some(unsafe { self._front_mut() });
144 }
145 None
146 }
147 #[cpp(method = "const T& front() const")]
148 unsafe fn _front_mut(&mut self) -> &mut T;
149
150
151 /// ```
152 /// use hicc_std::ForwardListInt;
153 /// let mut list = ForwardListInt::new();
154 /// list.push_front(&1);
155 /// list.push_front(&2);
156 /// list.push_front(&1);
157 /// list.remove(&1);
158 /// assert_eq!(list.iter().count(), 1);
159 /// assert_eq!(list.front(), Some(&2));
160 /// ```
161 #[cpp(method = "void remove(const T&)")]
162 pub fn remove(&mut self, val: &T);
163
164
165 hicc::cpp! {
166 static void remove_if(Self& self, std::function<bool(const T&)> pred) {
167 self.remove_if(pred);
168 }
169 }
170 /// ```
171 /// use hicc_std::ForwardListInt;
172 /// let mut list = ForwardListInt::new();
173 /// list.push_front(&1);
174 /// list.push_front(&2);
175 /// list.push_front(&3);
176 /// list.remove_if(|v: &i32| -> bool {
177 /// (v & 1) == 1
178 /// }.into());
179 /// assert_eq!(list.iter().count(), 1);
180 /// assert_eq!(list.front(), Some(&2));
181 /// ```
182 #[cpp(func = "void SelfMethods::remove_if(Self&, std::function<bool(const T&)>)")]
183 pub fn remove_if<'a>(&'a mut self, pred: hicc::Function<fn(&'a T::InputType) -> bool>);
184
185 hicc::cpp! {
186 static void sort(Self& self, std::function<bool(const T&, const T&)> comp) {
187 self.sort(comp);
188 }
189 }
190 /// ```
191 /// use hicc_std::ForwardListInt;
192 /// let mut list = ForwardListInt::new();
193 /// list.push_front(&1);
194 /// list.push_front(&2);
195 /// list.push_front(&3);
196 /// assert_eq!(list.front(), Some(&3));
197 /// list.sort(|v1: &i32, v2: &i32| -> bool {
198 /// v1 < v2
199 /// }.into());
200 /// let mut it = list.iter();
201 /// assert_eq!(it.next(), Some(&1));
202 /// assert_eq!(it.next(), Some(&2));
203 /// assert_eq!(it.next(), Some(&3));
204 /// assert_eq!(it.next(), None);
205 /// ```
206 #[cpp(func = "void SelfMethods::sort(Self&, std::function<bool(const T&, const T&)>)")]
207 pub fn sort<'a>(&'a mut self, comp: hicc::Function<fn(&'a T::InputType, &'a T::InputType) -> bool>);
208
209 hicc::cpp! {
210 static void merge(Self& self, Self& other, std::function<bool(const T&, const T&)> comp) {
211 if (self.get_allocator() == other.get_allocator()) {
212 self.merge(other, comp);
213 }
214 }
215 }
216 /// 如果不满足如下条件则忽略.
217 /// 1. `self.get_allocator() == other.get_allocator()`.
218 /// ```
219 /// use hicc_std::ForwardListInt;
220 /// let mut list = ForwardListInt::new();
221 /// list.push_front(&1);
222 /// list.push_front(&3);
223 /// let cmp = |v1: &i32, v2: &i32| -> bool {
224 /// v1 < v2
225 /// };
226 /// list.sort(cmp.clone().into());
227 /// let mut other = ForwardListInt::new();
228 /// other.push_front(&2);
229 /// other.push_front(&4);
230 /// other.sort(cmp.clone().into());
231 /// list.merge(&mut other, cmp.into());
232 /// let mut it = list.iter();
233 /// assert!(other.is_empty());
234 /// assert_eq!(it.next(), Some(&1));
235 /// assert_eq!(it.next(), Some(&2));
236 /// assert_eq!(it.next(), Some(&3));
237 /// assert_eq!(it.next(), Some(&4));
238 /// assert_eq!(it.next(), None);
239 /// ```
240 #[cpp(func = "void SelfMethods::merge(Self&, Self&, std::function<bool(const T&, const T&)>)")]
241 pub fn merge<'a>(&'a mut self, other: &mut Self, comp: hicc::Function<fn(&'a T::InputType, &'a T::InputType) -> bool>);
242
243
244 hicc::cpp! {
245 static void unique(Self& self, std::function<bool(const T&, const T&)> comp) {
246 self.unique(comp);
247 }
248 }
249 /// ```
250 /// use hicc_std::ForwardListInt;
251 /// let mut list = ForwardListInt::new();
252 /// list.push_front(&3);
253 /// list.push_front(&3);
254 /// list.push_front(&1);
255 /// list.push_front(&1);
256 /// let mut n = 0;
257 /// list.unique_with(|v1: &i32, v2: &i32| -> bool {
258 /// v1 == v2
259 /// }.into());
260 /// let mut it = list.iter();
261 /// assert_eq!(it.next(), Some(&1));
262 /// assert_eq!(it.next(), Some(&3));
263 /// assert_eq!(it.next(), None);
264 /// ```
265 #[cpp(func = "void SelfMethods::unique(Self&, std::function<bool(const T&, const T&)>)")]
266 pub fn unique_with<'a>(&'a mut self, comp: hicc::Function<fn(&'a T::InputType, &'a T::InputType) -> bool>);
267
268 /// ```
269 /// use hicc_std::ForwardListInt;
270 /// let mut list = ForwardListInt::new();
271 /// list.push_front(&3);
272 /// list.push_front(&3);
273 /// list.push_front(&1);
274 /// list.push_front(&1);
275 /// list.unique();
276 /// let mut it = list.iter();
277 /// assert_eq!(it.next(), Some(&1));
278 /// assert_eq!(it.next(), Some(&3));
279 /// assert_eq!(it.next(), None);
280 /// ```
281 #[cpp(method = "void unique()")]
282 pub fn unique(&mut self);
283
284 #[cpp(method = "const_iterator begin() const")]
285 unsafe fn begin(&self) -> *mut CppForwardListIter<T>;
286 #[cpp(method = "const_iterator end() const")]
287 unsafe fn end(&self) -> *mut CppForwardListIter<T>;
288 #[cpp(method = "iterator begin()")]
289 unsafe fn begin_mut(&mut self) -> *mut CppForwardListIterMut<T>;
290 #[cpp(method = "iterator end()")]
291 unsafe fn end_mut(&mut self) -> *mut CppForwardListIterMut<T>;
292
293 hicc::cpp! {
294 static void insert_after(Self& self, iterator& pos, size_t count, const T& val) {
295 if (pos != self.end()) {
296 self.insert_after(pos, count, val);
297 }
298 }
299 static void insert_after(Self& self, size_t count, const T& val) {
300 self.insert_after(self.before_begin(), count, val);
301 }
302 }
303 #[cpp(func = "void insert_after(Self&, iterator&, size_t, const T&)")]
304 unsafe fn insert_after(&mut self, pos: &mut CppForwardListIterMut<T>, ncount: usize, val: &T);
305 /// ```
306 /// use hicc_std::ForwardListInt;
307 /// let mut list = ForwardListInt::new();
308 /// list.insert_front(2, &1);
309 /// let mut it = list.iter();
310 /// assert_eq!(it.next(), Some(&1));
311 /// assert_eq!(it.next(), Some(&1));
312 /// assert_eq!(it.next(), None);
313 /// ```
314 #[cpp(func = "void insert_after(Self&, size_t, const T&)")]
315 pub fn insert_front(&mut self, ncount: usize, val: &T);
316
317
318 hicc::cpp! {
319 static void splice_after(Self& self, iterator& pos, Self& other) {
320 if (pos != self.end() && self.get_allocator() == other.get_allocator() && &self != &other) {
321 self.splice_after(pos, other);
322 }
323 }
324 static void splice_after(Self& self, Self& other) {
325 if (self.get_allocator() == other.get_allocator() && &self != &other) {
326 self.splice_after(self.before_begin(), other);
327 }
328 }
329 }
330 #[cpp(func = "void splice_after(Self&, iterator&, Self&)")]
331 unsafe fn splice_after(&mut self, pos: &mut CppForwardListIterMut<T>, other: &mut Self);
332 /// 不满足下面条件则不做任何修改:
333 /// 1. `self.get_allocator() == other.get_allocator()`
334 /// 2. `&self != &other`
335 /// ```
336 /// use hicc_std::ForwardListInt;
337 /// let mut src = ForwardListInt::new();
338 /// src.push_front(&1);
339 /// src.push_front(&2);
340 /// let mut dst = ForwardListInt::new();
341 /// dst.splice_front(&mut src);
342 /// assert!(src.is_empty());
343 /// let mut it = dst.iter();
344 /// assert_eq!(it.next(), Some(&2));
345 /// assert_eq!(it.next(), Some(&1));
346 /// assert_eq!(it.next(), None);
347 /// ```
348 #[cpp(func = "void splice_after(Self&, Self&)")]
349 pub fn splice_front(&mut self, other: &mut Self);
350
351 hicc::cpp! {
352 static void erase_after(Self& self, iterator& pos) {
353 const_iterator it = pos;
354 if (it != self.end() && ++it != self.end()) {
355 self.erase_after(pos);
356 }
357 }
358 }
359 #[cpp(func = "void erase_after(Self&, iterator&)")]
360 unsafe fn erase_after(&mut self, pos: &mut CppForwardListIterMut<T>);
361 }
362
363 unsafe impl<T: AbiType + Sync> Send for forward_list<T> {}
364 unsafe impl<T: AbiType + Sync> Sync for forward_list<T> {}
365
366 #[cpp(class = "template<class T, class Allocator> std::forward_list<T, Allocator>::const_iterator")]
367 class CppForwardListIter<T> {
368 hicc::cpp! {
369 static const T& next(Self& self) {
370 return *self++;
371 }
372 static const T& get(const Self& self) {
373 return *self;
374 }
375 }
376 #[cpp(func = "const T& SelfMethods::next(Self&)")]
377 unsafe fn next(&mut self) -> &T;
378 #[cpp(func = "const T& SelfMethods::get(const Self&)")]
379 unsafe fn get(&self) -> &T;
380 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
381 fn equal(&self, other: &Self) -> bool;
382 }
383
384 #[cpp(class = "template<class T, class Allocator> std::forward_list<T, Allocator>::iterator")]
385 class CppForwardListIterMut<T> {
386 hicc::cpp! {
387 static T& next(Self& self) {
388 return *self++;
389 }
390 static const T& get(const Self& self) {
391 return *self;
392 }
393 static bool has_next(const Self& self, const Self& end) {
394 return self != end && ++Self(self) != end;
395 }
396 }
397 #[cpp(func = "T& SelfMethods::next(Self&)")]
398 unsafe fn next(&mut self) -> &mut T;
399 #[cpp(func = "const T& SelfMethods::get(const Self&)")]
400 unsafe fn get(&self) -> &T;
401 #[cpp(func = "const T& SelfMethods::get(const Self&)")]
402 unsafe fn get_mut(&mut self) -> &mut T;
403 #[cpp(func = "bool hicc::make_eq<Self, Self>(const Self&, const Self&)")]
404 fn equal(&self, other: &Self) -> bool;
405 #[cpp(func = "bool SelfMethods::has_next(const Self&, const Self&)")]
406 fn has_next(&self, other: &Self) -> bool;
407 }
408}
409
410impl<T: AbiType> forward_list<T> {
411 /// ```
412 /// use hicc_std::ForwardListInt;
413 /// let mut list = ForwardListInt::new();
414 /// list.push_front(&1);
415 /// list.push_front(&2);
416 /// let mut it = list.iter();
417 /// assert_eq!(it.next(), Some(&2));
418 /// assert_eq!(it.next(), Some(&1));
419 /// assert_eq!(it.next(), None);
420 /// ```
421 pub fn iter(&self) -> ForwardListIter<'_, T> {
422 ForwardListIter {
423 beg: unsafe { self.begin() },
424 end: unsafe { self.end() },
425 }
426 }
427 /// ```
428 /// use hicc_std::ForwardListInt;
429 /// let mut list = ForwardListInt::new();
430 /// list.push_front(&1);
431 /// list.iter_mut().for_each(|v| *v -= 1);
432 /// assert_eq!(list.front(), Some(&0));
433 /// ```
434 pub fn iter_mut(&mut self) -> ForwardListIterMut<'_, T> {
435 let beg = unsafe { self.begin_mut() };
436 let end = unsafe { self.end_mut() };
437 ForwardListIterMut {
438 list: self,
439 beg,
440 end,
441 }
442 }
443}
444
445/// 对应`std::forward_list<T>::const_iterator`
446pub struct ForwardListIter<'a, T: AbiType + 'static> {
447 beg: ClassMutPtr<'a, CppForwardListIter<T>>,
448 end: ClassMutPtr<'a, CppForwardListIter<T>>,
449}
450
451impl<'a, T: AbiType + 'static> Iterator for ForwardListIter<'a, T> {
452 type Item = T::OutputRef<'a>;
453 fn next(&mut self) -> Option<Self::Item> {
454 if !self.beg.equal(&self.end) {
455 return Some(unsafe { self.beg.as_deref_mut().next() });
456 }
457 None
458 }
459}
460
461impl<T: AbiType + 'static> ForwardListIter<'_, T> {
462 /// ```
463 /// use hicc_std::ForwardListInt;
464 /// let mut list = ForwardListInt::new();
465 /// let it = list.iter();
466 /// assert_eq!(it.get(), None);
467 /// list.push_front(&1);
468 /// let mut it = list.iter();
469 /// assert_eq!(it.get(), Some(&1));
470 /// assert_eq!(it.next(), Some(&1));
471 /// assert_eq!(it.get(), None);
472 /// assert_eq!(it.next(), None);
473 /// ```
474 pub fn get(&self) -> Option<T::OutputRef<'_>> {
475 if !self.beg.equal(&self.end) {
476 return Some(unsafe { self.beg.get() });
477 }
478 None
479 }
480}
481
482/// 对应`std::forward_list<T>::iterator`
483pub struct ForwardListIterMut<'a, T: AbiType + 'static> {
484 list: &'a mut forward_list<T>,
485 beg: ClassMutPtr<'a, CppForwardListIterMut<T>>,
486 end: ClassMutPtr<'a, CppForwardListIterMut<T>>,
487}
488
489impl<'a, T: AbiType + 'static> Iterator for ForwardListIterMut<'a, T> {
490 type Item = T::OutputRefMut<'a>;
491 fn next(&mut self) -> Option<Self::Item> {
492 if !self.beg.equal(&self.end) {
493 return Some(unsafe { self.beg.as_deref_mut().next() });
494 }
495 None
496 }
497}
498
499impl<T: AbiType + 'static> ForwardListIterMut<'_, T> {
500 /// ```
501 /// use hicc_std::ForwardListInt;
502 /// let mut list = ForwardListInt::new();
503 /// let it = list.iter_mut();
504 /// assert_eq!(it.get(), None);
505 /// list.push_front(&1);
506 /// let mut it = list.iter_mut();
507 /// assert_eq!(it.get(), Some(&1));
508 /// assert_eq!(it.next(), Some(&mut 1));
509 /// assert_eq!(it.get(), None);
510 /// assert_eq!(it.next(), None);
511 /// ```
512 pub fn get(&self) -> Option<T::OutputRef<'_>> {
513 if !self.beg.equal(&self.end) {
514 return Some(unsafe { self.beg.get() });
515 }
516 None
517 }
518
519 /// ```
520 /// use hicc_std::ForwardListInt;
521 /// let mut list = ForwardListInt::new();
522 /// let mut it = list.iter_mut();
523 /// assert_eq!(it.get_mut(), None);
524 /// list.push_front(&1);
525 /// let mut it = list.iter_mut();
526 /// assert_eq!(it.get_mut(), Some(&mut 1));
527 /// assert_eq!(it.next(), Some(&mut 1));
528 /// assert_eq!(it.get(), None);
529 /// assert_eq!(it.next(), None);
530 /// ```
531 pub fn get_mut(&mut self) -> Option<T::OutputRefMut<'_>> {
532 if !self.beg.equal(&self.end) {
533 return Some(unsafe { self.beg.get_mut() });
534 }
535 None
536 }
537
538 /// 当前是否是最后一个节点.只有`has_next`返回`true`, `erase_after`操作才会成功.
539 /// ```
540 /// use hicc_std::ForwardListInt;
541 /// let mut list = ForwardListInt::new();
542 /// let it = list.iter_mut();
543 /// assert_eq!(it.has_next(), false);
544 /// list.insert_front(2, &1);
545 /// let mut it = list.iter_mut();
546 /// assert_eq!(it.has_next(), true);
547 /// assert_eq!(it.next(), Some(&mut 1));
548 /// assert_eq!(it.has_next(), false);
549 /// assert_eq!(it.next(), Some(&mut 1));
550 /// assert_eq!(it.next(), None);
551 /// ```
552 pub fn has_next(&self) -> bool {
553 self.beg.has_next(&self.end)
554 }
555 /// 如果当前节点已经是`std::forwarod_list::end()`则无效果. 可调用`insert_front`.
556 /// ```
557 /// use hicc_std::ForwardListInt;
558 /// let mut list = ForwardListInt::new();
559 /// list.push_front(&1);
560 /// let mut it = list.iter_mut();
561 /// it.insert_after(1, &2);
562 /// assert_eq!(it.next(), Some(&mut 1));
563 /// assert_eq!(it.next(), Some(&mut 2));
564 /// assert_eq!(it.next(), None);
565 /// assert_eq!(list.front(), Some(&1));
566 /// ```
567 pub fn insert_after(&mut self, ncount: usize, val: &T::InputType) {
568 unsafe { self.list.insert_after(&mut self.beg, ncount, val) };
569 }
570
571 /// 不满足下面条件则不做任何修改:
572 /// 1. `self.get_allocator() == other.get_allocator()`
573 /// 1. `self.has_next() == true`
574 /// ```
575 /// use hicc_std::ForwardListInt;
576 /// let mut list = ForwardListInt::new();
577 /// let mut other = ForwardListInt::new();
578 /// other.push_front(&2);
579 /// list.push_front(&1);
580 /// let mut it = list.iter_mut();
581 /// it.splice_after(&mut other);
582 /// assert!(other.is_empty());
583 /// assert_eq!(it.next(), Some(&mut 1));
584 /// assert_eq!(it.next(), Some(&mut 2));
585 /// assert_eq!(it.next(), None);
586 /// assert_eq!(list.front(), Some(&1));
587 /// ```
588 pub fn splice_after(&mut self, other: &mut forward_list<T>) {
589 unsafe { self.list.splice_after(&mut self.beg, other) };
590 }
591
592 /// 无后续节点也可以安全调用.
593 /// ```
594 /// use hicc_std::ForwardListInt;
595 /// let mut list = ForwardListInt::new();
596 /// let mut it = list.iter_mut();
597 /// it.erase_after();
598 /// list.push_front(&1);
599 /// list.push_front(&2);
600 /// list.push_front(&3);
601 /// let mut it = list.iter_mut();
602 /// it.erase_after();
603 /// it.erase_after();
604 /// it.erase_after();
605 /// assert_eq!(it.next(), Some(&mut 3));
606 /// assert_eq!(it.next(), None);
607 /// ```
608 pub fn erase_after(&mut self) {
609 unsafe { self.list.erase_after(&mut self.beg) };
610 }
611}