1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#![allow(missing_debug_implementations)]
use super::node::{AANode, Node};
use alloc::vec::Vec;
use core::{iter::FusedIterator, marker::PhantomData};
pub trait IterContent<T> {
fn content(self) -> T;
}
impl<T> IterContent<T> for T {
fn content(self) -> Self {
self
}
}
pub struct AAIter<'a, C, T> {
stack: Vec<(bool, &'a AANode<C>)>,
len: usize,
_ty: PhantomData<T>
}
impl<'a, C, T> AAIter<'a, C, T> {
pub(super) fn new(root: &'a AANode<C>, len: usize) -> Self {
let mut stack = Vec::with_capacity(root.level() as usize * 2 + 1);
stack.push((false, root));
Self {
stack,
len,
_ty: PhantomData::default()
}
}
}
impl<'a, C, T> Iterator for AAIter<'a, C, T>
where
&'a C: IterContent<T>
{
type Item = T;
fn next(&mut self) -> Option<T> {
loop {
let (visited_left, last) = self.stack.pop()?;
if let Some(Node { left_child, .. }) = last.as_ref() {
self.stack.push((true, last));
if !visited_left && !left_child.is_nil() {
self.stack.push((false, left_child));
} else {
break;
}
}
}
let (_, last) = self.stack.pop()?;
match last.as_ref() {
None => unreachable!(),
Some(Node {
content,
right_child,
..
}) => {
if !right_child.is_nil() {
self.stack.push((false, right_child));
}
self.len -= 1;
Some(content.content())
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
}
impl<'a, C, T> ExactSizeIterator for AAIter<'a, C, T> where &'a C: IterContent<T> {}
impl<'a, C, T> FusedIterator for AAIter<'a, C, T> where &'a C: IterContent<T> {}
pub struct AAIntoIter<C, T> {
stack: Vec<AANode<C>>,
len: usize,
_ty: PhantomData<T>
}
impl<C, T> AAIntoIter<C, T> {
pub(super) fn new(root: AANode<C>, len: usize) -> Self {
let mut stack = Vec::with_capacity(root.level() as usize * 2 + 1);
stack.push(root);
Self {
stack,
len,
_ty: PhantomData::default()
}
}
}
impl<C, T> Iterator for AAIntoIter<C, T>
where
C: IterContent<T>
{
type Item = T;
fn next(&mut self) -> Option<T> {
loop {
let last = self.stack.pop()?;
if let Some(Node {
level,
content,
left_child,
right_child
}) = last.unbox()
{
self.stack.push(
Node {
level,
content,
left_child: AANode::new(),
right_child
}
.into()
);
if !left_child.is_nil() {
self.stack.push(left_child);
} else {
break;
}
}
}
let last = self.stack.pop()?;
match last.unbox() {
None => unreachable!(),
Some(Node {
content,
right_child,
..
}) => {
if !right_child.is_nil() {
self.stack.push(right_child);
}
self.len -= 1;
Some(content.content())
}
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
}
impl<C, T> ExactSizeIterator for AAIntoIter<C, T> where C: IterContent<T> {}
impl<C, T> FusedIterator for AAIntoIter<C, T> where C: IterContent<T> {}