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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use crate::{
    AdjContainer,
    traits::*, 
    iter::*, 
    GenericGraph,
    sw_graph::SwContainer,
    graph::NodeContainer,
    generic_graph::{Dfs, DfsWithIndex, Bfs}
};

/// unify graph ensembles in a trait
pub trait WithGraph<T, G> {
    /// * access additional information at index
    fn at(&self, index: usize) -> &T;

    /// * mutable access to additional information at index
    fn at_mut(&mut self, index: usize) -> &mut T;

    /// * returns reference to the underlying topology aka, the `GenericGraph`
    /// * use this to call functions regarding the topology
    fn graph(&self) -> &G;

    /// * sorts Adjecaency List
    fn sort_adj(&mut self);
}

///  Collection mut Graph iterators
pub trait GraphIteratorsMut<T, G, A>
where
    T: Node,
    A: AdjContainer<T>
{
    /// * iterate over mutable additional information of neighbors of vertex `index`
    /// * iterator returns `&mut T`
    /// * `sort_adj` will affect the order
    /// * **panics** if index out of bounds
    fn contained_iter_neighbors_mut(&mut self, index: usize) -> NContainedIterMut<'_, T, A, IterWrapper>;

    /// * iterate over mutable additional information of neighbors of vertex `index`
    /// * iterator returns `(index_neighbor: usize, neighbor: &mut T)`
    /// * `sort_adj` will affect the order
    /// * **panics** if index out of bounds
    fn contained_iter_neighbors_mut_with_index(&mut self, index: usize) -> INContainedIterMut<'_, T, A>;

    /// * get iterator over mutable additional information stored at each vertex in order of the indices
    /// * iterator returns a `Node` (for example `EmptyNode` or whatever you used)
    fn contained_iter_mut(&mut self) -> ContainedIterMut<'_, T, A>;
}

/// Collection of Graph iterators
pub trait GraphIterators<T, G, A>
    where
        T: Node,
        A: AdjContainer<T>
{
    /// * get iterator over additional information stored at each vertex in order of the indices
    /// * iterator returns a `Node` (for example `EmptyNode` or whatever you used)
    /// * similar to `self.container_iter().map(|container| container.contained())`
    fn contained_iter(&self) -> ContainedIter<'_, T, A>;

    /// * iterate over additional information of neighbors of vertex `index`
    /// * iterator returns `&T`
    /// * `sort_adj` will affect the order
    /// * **panics** if index out of bounds
    fn contained_iter_neighbors(&self, index: usize) -> NContainedIter<'_, T, A, IterWrapper>;

    /// * iterate over additional information of neighbors of vertex `index`
    /// * iterator returns (`index_neighbor`,`&T`)
    /// * `sort_adj` will affect the order
    /// * **panics** if index out of bounds
    fn contained_iter_neighbors_with_index(&self, index: usize) -> NIContainedIter<T, A>;

    /// * get iterator over AdjContainer in order of the indices
    /// * iterator returns `AdjContainer<Node>`, i.e., `A`
    fn container_iter(&self) -> core::slice::Iter<'_, A>;

    /// * iterate over additional information of neighbors of vertex `index`
    /// * iterator returns `&T`
    /// * `sort_adj` will affect the order
    /// * **panics** if index out of bounds
    fn container_iter_neighbors(&self, index: usize) -> NContainerIter<'_, T, A, IterWrapper>;

    /// # returns `Iterator`
    ///
    /// * the iterator will iterate over the vertices in depth first search order,
    /// beginning with vertex `index`.
    /// * iterator returns `node`
    ///
    /// Order
    ///------------------------
    /// Order is guaranteed to be in DFS order, however
    /// if this order is not unambigouse
    /// adding edges and especially removing edges will shuffle the order.
    ///
    /// Note:
    /// ----------------------
    /// Will only iterate over vertices within the connected component that contains vertex `index`
    fn dfs(&self, index: usize) -> Dfs<'_, T, A>;

    /// # returns `Iterator`
    ///
    /// * the iterator will iterate over the vertices in depth first search order,
    /// beginning with vertex `index`.
    /// * Iterator returns tuple `(index, node)`
    ///
    /// Order
    ///------------------------
    /// Order is guaranteed to be in DFS order, however
    /// if this order is not unambigouse
    /// adding edges and especially removing edges will shuffle the order.
    ///
    /// Note:
    /// ----------------------
    /// Will only iterate over vertices within the connected component that contains vertex `index`
    fn dfs_with_index(&self, index: usize) -> DfsWithIndex<'_, T, A>;

    /// # returns `Iterator`
    ///
    /// * the iterator will iterate over the vertices in breadth first search order,
    /// beginning with vertex `index`.
    /// * Iterator returns tuple `(index, node, depth)`
    ///
    /// ### depth
    /// * starts at 0 (i.e. the first element in the iterator will have `depth = 0`)
    /// * `depth` equals number of edges in the *shortest path* from the *current* vertex to the
    /// *first* vertex (i.e. to the vertex with index `index`)
    ///
    /// Order
    ///------------------------
    /// Order is guaranteed to be in BFS order, however
    /// if this order is not unambigouse
    /// adding edges and especially removing edges will shuffle the order.
    ///
    /// Note:
    /// ----------------------
    /// Will only iterate over vertices within the connected component that contains vertex `index`
    fn bfs_index_depth(&self, index: usize) -> Bfs<'_, T, A>;
}


impl<T, E> GraphIterators<T, GenericGraph<T, NodeContainer<T>>, NodeContainer<T>> for E
where
    T: Node,
    E: WithGraph<T, GenericGraph<T, NodeContainer<T>>>,
{
    fn contained_iter(&self) -> ContainedIter<'_, T, NodeContainer<T>>
    {
        self.graph().contained_iter()
    }

    fn contained_iter_neighbors(&self, index: usize) -> NContainedIter<'_, T, NodeContainer<T>, IterWrapper>
    {
        self.graph().contained_iter_neighbors(index)
    }

    fn container_iter(&self) -> core::slice::Iter<'_, NodeContainer<T>>
    {
        self.graph().container_iter()
    }

    fn container_iter_neighbors(&self, index: usize) -> NContainerIter<'_, T, NodeContainer<T>, IterWrapper>
    {
        self.graph().container_iter_neighbors(index)
    }

    fn contained_iter_neighbors_with_index(&self, index: usize) -> NIContainedIter<T, NodeContainer<T>> {
        self.graph().contained_iter_neighbors_with_index(index)
    }

    fn dfs(&self, index: usize) -> Dfs<'_, T, NodeContainer<T>>
    {
        self.graph().dfs(index)
    }

    fn dfs_with_index(&self, index: usize) -> DfsWithIndex<'_, T, NodeContainer<T>>
    {
        self.graph().dfs_with_index(index)
    }

    fn bfs_index_depth(&self, index: usize) -> Bfs<'_, T, NodeContainer<T>>
    {
        self.graph().bfs_index_depth(index)
    }
}

impl<T, E> GraphIterators<T, GenericGraph<T, SwContainer<T>>, SwContainer<T>> for E
where
    T: Node,
    E: WithGraph<T, GenericGraph<T, SwContainer<T>>>,
{
    fn contained_iter(&self) -> ContainedIter<'_, T, SwContainer<T>>
    {
        self.graph().contained_iter()
    }

    fn contained_iter_neighbors(&self, index: usize) -> NContainedIter<'_, T, SwContainer<T>, IterWrapper>
    {
        self.graph().contained_iter_neighbors(index)
    }

    fn container_iter(&self) -> core::slice::Iter<'_, SwContainer<T>>
    {
        self.graph().container_iter()
    }

    fn container_iter_neighbors(&self, index: usize) -> NContainerIter<'_, T, SwContainer<T>, IterWrapper>
    {
        self.graph().container_iter_neighbors(index)
    }

    fn contained_iter_neighbors_with_index(&self, index: usize)
        -> NIContainedIter<T, SwContainer<T>>
    {
        self.graph().contained_iter_neighbors_with_index(index)
    }

    fn dfs(&self, index: usize) -> Dfs<'_, T, SwContainer<T>>
    {
        self.graph().dfs(index)
    }

    fn dfs_with_index(&self, index: usize) -> DfsWithIndex<'_, T, SwContainer<T>>
    {
        self.graph().dfs_with_index(index)
    }

    fn bfs_index_depth(&self, index: usize) -> Bfs<'_, T, SwContainer<T>>
    {
        self.graph().bfs_index_depth(index)
    }
}