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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use crate::{AdjContainer, traits::*, iter::*, GenericGraph};
use crate::generic_graph::{Dfs, DfsWithIndex, Bfs};
/// # Access internal random number generator
pub trait HasRng<Rng>
where Rng: rand::Rng
{
/// # Access RNG
/// If, for some reason, you want access to the internal random number generator: Here you go
fn rng(&mut self) -> &mut Rng;
/// # If you need to exchange the internal rng
/// * returns old rng
fn swap_rng(&mut self, rng: Rng) -> Rng;
}
/// # Create a markov chain by doing markov steps
pub trait MarkovChain<S, Res> {
/// * undo a markov step, return result-state
/// * if you want to undo more than one step
/// see [`undo_steps`](#method.undo_steps)
fn undo_step(&mut self, step: S) -> Res;
/// * undo a markov, **panic** on invalid result state
/// * for undoing multiple steps see [`undo_steps_quiet`](#method.undo_steps_quiet)
fn undo_step_quiet(&mut self, step: S) -> ();
/// # Markov step
/// * use this to perform a markov step step
/// * for doing multiple markov steps at once, use [`m_steps`](#method.m_steps)
fn m_step(&mut self) -> S;
/// # Markov steps
/// * use this to perform multiple markov steps at once
/// * result `Vec<S>` can be used to undo the steps with `self.undo_steps(result)`
fn m_steps(&mut self, count: usize) -> Vec<S> {
let mut vec = Vec::with_capacity(count);
for _ in 0..count {
vec.push(
self.m_step()
);
}
vec
}
/// # Undo markov steps
/// * Note: uses undo_step in correct order and returns result
/// ## Important:
/// * look at specific implementation of `undo_step`, every thing mentioned there applies to each step
fn undo_steps(&mut self, steps: Vec<S>) -> Vec<Res> {
steps.into_iter()
.rev()
.map(|step| self.undo_step(step))
.collect()
}
/// # Undo markov steps
/// * Note: uses `undo_step_quiet` in correct order
/// ## Important:
/// * look at specific implementation of `undo_step_quiet`, every thing mentioned there applies to each step
fn undo_steps_quiet(&mut self, steps: Vec<S>) -> () {
let iter = steps.into_iter()
.rev();
for step in iter {
self.undo_step_quiet(step);
}
}
}
/// For easy sampling of your ensemble
pub trait SimpleSample{
/// # Randomizes self according to model
/// * this is intended for creation of initial sample
/// * used in [`simple_sample`](#method.simple_sample)
/// and [`simple_sample_vec`](#method.simple_sample_vec)
fn randomize(&mut self);
/// # do the following `times` times:
/// 1) `f(self)`
/// 2) `self.randomize()`
fn simple_sample<F>(&mut self, times: usize, mut f: F)
where F: FnMut(&Self) -> ()
{
for _ in 0..times {
f(self);
self.randomize();
}
}
/// # do the following `times` times:
/// 1) `res = f(self)`
/// 2) `self.randomize()`
/// ## res is collected into Vector
fn simple_sample_vec<F, G>(&mut self, times: usize, mut f: F) -> Vec<G>
where F: FnMut(&Self) -> G
{
let mut vec = Vec::with_capacity(times);
for _ in 0..times {
vec.push(f(self));
self.randomize();
}
vec
}
}
/// 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;
}
/// Collection mut Graph iterators
pub trait GraphIteratorsMut<'a, T, G, A>
where
Self: WithGraph<T, G>,
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(&'a mut self, index: usize) -> NContainedIterMut<'a, 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(&'a mut self) -> ContainedIterMut<'a, T, A>;
}
/// Collection of Graph iterators
pub trait GraphIterators<'a, T, G, A>
where
Self: WithGraph<T, G>,
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(&'a self) -> ContainedIter<'a, T, A>
where 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(&'a self, index: usize) -> NContainedIter<'a, T, A>
where T: 'a;
/// * get iterator over AdjContainer in order of the indices
/// * iterator returns `AdjContainer<Node>`, i.e., `A`
fn container_iter(&'a self) -> core::slice::Iter<'a, A>
where 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 container_iter_neighbors(&'a self, index: usize) -> NContainerIter<'a, T, A>
where T: 'a;
/// # 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(&'a self, index: u32) -> Dfs<'a, T, A>
where 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(&'a self, index: u32) -> DfsWithIndex<'a, T, A>
where 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(&'a self, index: u32) -> Bfs<'a, T, A>
where T: 'a;
}
impl<'a, T, A, E> GraphIterators<'a, T, GenericGraph<T, A>, A> for E
where
T: Node,
A: AdjContainer<T>,
E: WithGraph<T, GenericGraph<T, A>>,
{
fn contained_iter(&'a self) -> ContainedIter<'a, T, A>
where T: 'a {
self.graph().contained_iter()
}
fn contained_iter_neighbors(&'a self, index: usize) -> NContainedIter<'a, T, A>
where T: 'a
{
self.graph().contained_iter_neighbors(index)
}
fn container_iter(&'a self) -> core::slice::Iter<'a, A>
where T: 'a {
self.graph().container_iter()
}
fn container_iter_neighbors(&'a self, index: usize) -> NContainerIter<'a, T, A>
where T: 'a {
self.graph().container_iter_neighbors(index)
}
fn dfs(&'a self, index: u32) -> Dfs<'a, T, A>
where T: 'a {
self.graph().dfs(index)
}
fn dfs_with_index(&'a self, index: u32) -> DfsWithIndex<'a, T, A>
where T: 'a {
self.graph().dfs_with_index(index)
}
fn bfs_index_depth(&'a self, index: u32) -> Bfs<'a, T, A>
where T: 'a {
self.graph().bfs_index_depth(index)
}
}