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
mod gen;
pub use self::gen::*;
use rand::Rng;
use std::fmt::{self, Debug};
use std::ops::{Deref, DerefMut};
use std::collections::VecDeque;
pub trait Tree
where Self: Sized + Debug + Clone
{
type Environment;
type Action;
fn tree<R: Rng>(tg: &mut TreeGen<R>) -> BoxTree<Self> {
Self::child(tg, 0)
}
fn child<R: Rng>(tg: &mut TreeGen<R>, current_depth: usize) -> BoxTree<Self> {
if tg.have_reached_a_leaf(current_depth) {
Self::leaf(tg, current_depth)
} else {
Self::branch(tg, current_depth)
}
}
fn branch<R: Rng>(tg: &mut TreeGen<R>, current_depth: usize) -> BoxTree<Self>;
fn leaf<R: Rng>(tg: &mut TreeGen<R>, current_depth: usize) -> BoxTree<Self>;
fn count_children(&mut self) -> usize;
fn children(&self) -> Vec<&BoxTree<Self>>;
fn children_mut(&mut self) -> Vec<&mut BoxTree<Self>>;
fn evaluate(&self, env: &Self::Environment) -> Self::Action;
}
#[derive(Clone, PartialEq, Eq)]
pub struct BoxTree<T>(Box<T>) where T: Tree;
impl<T> BoxTree<T>
where T: Tree
{
pub fn inner(self) -> T {
*self.0
}
pub fn count_nodes(&mut self) -> usize {
self.fold(0, |count, _, _, _| count + 1)
}
pub fn get(&mut self, target_index: usize) -> Option<T> {
let mut node = None;
self.map_while(|current, index, _| if index == target_index {
node = Some(current.clone());
false
} else {
true
});
node
}
pub fn map<F>(&mut self, mut f: F)
where F: FnMut(&mut T, usize, usize)
{
self.map_while(|p, i, d| {
f(p, i, d);
true
})
}
pub fn map_while<F>(&mut self, mut f: F)
where F: FnMut(&mut T, usize, usize) -> bool
{
let mut stack: VecDeque<(&mut Self, usize)> = VecDeque::new();
stack.push_back((self, 0));
let mut i = 0;
while let Some((node, depth)) = stack.pop_back() {
if !f(node, i, depth) {
break;
}
let mut children = node.children_mut();
children.reverse();
for child in children {
stack.push_back((child, depth + 1));
}
i += 1;
}
}
pub fn fold<F, V>(&mut self, value: V, mut f: F) -> V
where F: FnMut(V, &mut T, usize, usize) -> V
{
self.fold_while(value, |v, p, i, d| (true, f(v, p, i, d)))
}
pub fn fold_while<F, V>(&mut self, mut value: V, mut f: F) -> V
where F: FnMut(V, &mut T, usize, usize) -> (bool, V)
{
let mut stack: VecDeque<(&mut Self, usize)> = VecDeque::new();
stack.push_back((self, 0));
let mut i = 0;
while let Some((node, depth)) = stack.pop_back() {
value = match f(value, node, i, depth) {
(true, value) => value,
(false, value) => return value,
};
let mut children = node.children_mut();
children.reverse();
for child in children {
stack.push_back((child, depth + 1));
}
i += 1;
}
value
}
}
impl<T> Debug for BoxTree<T>
where T: Tree
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl<T> fmt::Display for BoxTree<T>
where T: Tree + fmt::Display
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T> Deref for BoxTree<T>
where T: Tree
{
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
impl<T> DerefMut for BoxTree<T>
where T: Tree
{
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> From<T> for BoxTree<T>
where T: Tree
{
fn from(tree: T) -> BoxTree<T> {
BoxTree(Box::new(tree))
}
}