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
use super::Size;
use rust::*;
#[derive(Debug,PartialEq,Eq)]
pub struct Visit<T> {
pub data : T,
pub size : Size,
}
pub struct BfsTree<Iter> {
pub iter : Iter,
pub size : Size,
}
impl<Item,Iter> BfsTree<Splitted<Iter>>
where Iter: Iterator<Item=Item>
{
pub fn from<Treelike>( treelike: Treelike, size: Size ) -> Self
where Treelike: IntoIterator<Item=Item,IntoIter=Iter>
{
Self{ iter: Splitted::<Iter>::from( treelike ), size: size }
}
}
impl<Iter> BfsTree<Iter> {
pub fn wrap( self ) -> Bfs<Iter> { Bfs::Tree( self )}
}
pub struct BfsForest<Iter> {
pub iter : Iter,
pub size : Size,
}
impl<Item,Iter> BfsForest<Splitted<Iter>>
where Iter: Iterator<Item=Item>
{
pub fn from<Treelike>( treelike: Treelike, size: Size ) -> Self
where Treelike: IntoIterator<Item=Item,IntoIter=Iter>
{
Self{ iter: Splitted::<Iter>::from( treelike ), size: size }
}
}
impl<Iter> BfsForest<Iter> {
pub fn wrap( self ) -> Bfs<Iter> { Bfs::Forest( self )}
}
pub enum Bfs<Iter> {
Tree( BfsTree <Iter> ),
Forest( BfsForest<Iter> ),
}
impl<T,Iter> Bfs<Iter>
where Iter: Iterator<Item=Visit<T>>
{
pub fn iter( self ) -> Iter {
match self {
Bfs::Tree( tree ) => tree.iter,
Bfs::Forest( forest ) => forest.iter,
}
}
pub fn iter_and_size( self ) -> ( Iter, Size ) {
match self {
Bfs::Tree( tree ) => (tree.iter, tree.size),
Bfs::Forest( forest ) => (forest.iter, forest.size),
}
}
pub fn tree_iter( self ) -> Option<Iter> {
match self {
Bfs::Tree( tree ) => Some( tree.iter ),
_ => None,
}
}
pub fn forest_iter( self ) -> Option<Iter> {
match self {
Bfs::Forest( forest ) => Some( forest.iter ),
_ => None,
}
}
}
pub trait Split {
type Item;
type Iter: ExactSizeIterator;
fn split( self ) -> ( Self::Item, Self::Iter, u32 );
}
pub struct Splitted<Iter> {
pub(crate) iters : VecDeque<Iter>,
}
impl<Treelike,Item,Iter> From<Treelike> for Splitted<Iter>
where Treelike : IntoIterator<Item=Item,IntoIter=Iter>
, Iter : Iterator<Item=Item>
{
fn from( treelike: Treelike ) -> Self {
let mut iters = VecDeque::new();
iters.push_back( treelike.into_iter() );
Splitted{ iters }
}
}
impl<T,Item,Iter> Iterator for Splitted<Iter>
where Iter : ExactSizeIterator<Item=Item>
, Item : Split<Iter=Iter,Item=T>
{
type Item = Visit<T>;
#[inline] fn next( &mut self ) -> Option<Self::Item> {
loop {
let next_item =
if let Some( ref mut iter ) = self.iters.front_mut() {
iter.next()
} else {
return None;
};
if let Some( item ) = next_item {
let ( data, iter, node_cnt ) = item.split();
let degree = iter.len();
self.iters.push_back( iter );
return Some( Visit{ data, size: Size{ degree: degree as u32, node_cnt }});
} else {
self.iters.pop_front();
}
}
}
}
pub struct Moved<Iter>( pub(crate) Iter );
impl<'a,T,Iter> Iterator for Moved<Iter>
where Iter : Iterator<Item=Visit<&'a T>>
, T : 'a
{
type Item = Visit<T>;
fn next( &mut self ) -> Option<Visit<T>> {
self.0.next().map( |item| Visit {
data : unsafe{ ptr::read( item.data )},
size : item.size,
})
}
}