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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#[cfg(feature="quickcheck")]
extern crate quickcheck;
pub mod cow;
pub mod count;
pub mod iter;
pub mod test;
pub mod unbox;
use std::mem;
use std::ops::DerefMut;
pub trait BinaryTree {
type Node: Node;
fn root(&self) -> Option<&Self::Node>;
}
unsafe fn borrow<'a, T, U>(raw: *const T, _: &'a U) -> &'a T {
&*raw
}
unsafe fn borrow_mut<'a, T, U>(raw: *mut T, _: &'a U) -> &'a mut T {
&mut *raw
}
pub trait Node {
type Value;
fn left(&self) -> Option<&Self>;
fn right(&self) -> Option<&Self>;
fn value(&self) -> &Self::Value;
fn walk<'a, F>(&'a self, mut step_in: F)
where F: FnMut(&'a Self) -> WalkAction
{
use WalkAction::*;
let mut subtree = Some(self);
while let Some(mut st) = subtree {
let action = step_in(&mut st);
subtree = match action {
Left => st.left(),
Right => st.right(),
Stop => break,
};
}
}
}
pub trait NodeMut: Node + Sized {
type NodePtr: Sized + DerefMut<Target = Self>;
fn detach_left(&mut self) -> Option<Self::NodePtr>;
fn detach_right(&mut self) -> Option<Self::NodePtr>;
fn insert_left(&mut self, tree: Option<Self::NodePtr>) -> Option<Self::NodePtr>;
fn insert_right(&mut self, tree: Option<Self::NodePtr>) -> Option<Self::NodePtr>;
fn value_mut(&mut self) -> &mut Self::Value;
fn into_parts(self) -> (Self::Value, Option<Self::NodePtr>, Option<Self::NodePtr>);
fn left_mut(&mut self) -> Option<&mut Self>;
fn right_mut(&mut self) -> Option<&mut Self>;
fn rotate_left(&mut self) -> Result<(), ()> {
if let Some(mut self2) = self.detach_right() {
let mid = self2.detach_left();
self.insert_right(mid);
mem::swap(self, &mut self2);
self.insert_left(Some(self2));
Ok(())
} else {
Err(())
}
}
fn rotate_right(&mut self) -> Result<(), ()> {
if let Some(mut self2) = self.detach_left() {
let mid = self2.detach_right();
self.insert_left(mid);
mem::swap(self, &mut self2);
self.insert_right(Some(self2));
Ok(())
} else {
Err(())
}
}
fn walk_mut<'a, FI, FS>(&'a mut self, mut step_in: FI, stop: FS)
where FI: FnMut(&Self) -> WalkAction,
FS: FnOnce(&'a mut Self)
{
use WalkAction::*;
let mut node: *mut _ = self;
loop {
let action = {
let pin = ();
step_in(unsafe { borrow(node, &pin) })
};
let next = match action {
Left => unsafe { borrow_mut(node, self) }.left_mut(),
Right => unsafe { borrow_mut(node, self) }.right_mut(),
Stop => break,
};
if let Some(st) = next {
node = st;
} else {
break;
}
}
stop(unsafe { borrow_mut(node, self) });
}
fn walk_reshape<FI, FS, FO>(&mut self, mut step_in: FI, stop: FS, mut step_out: FO)
where FI: FnMut(&mut Self) -> WalkAction,
FS: FnOnce(&mut Self),
FO: FnMut(&mut Self, WalkAction)
{
use WalkAction::*;
let mut stack = Vec::with_capacity(8);
let root_action = step_in(self);
let mut subtree = match root_action {
Left => self.detach_left(),
Right => self.detach_right(),
Stop => None,
};
let mut action = root_action;
while action != Stop {
if let Some(mut st) = subtree {
action = step_in(&mut st);
subtree = match action {
Left => st.detach_left(),
Right => st.detach_right(),
Stop => None,
};
stack.push((st, action));
} else {
break;
}
}
if let Some((mut sst, _)) = stack.pop() {
stop(&mut sst);
while let Some((mut st, action)) = stack.pop() {
match action {
Left => st.insert_left(Some(sst)),
Right => st.insert_right(Some(sst)),
Stop => unreachable!(),
};
step_out(&mut st, action);
sst = st;
}
match root_action {
Left => self.insert_left(Some(sst)),
Right => self.insert_right(Some(sst)),
Stop => unreachable!(),
};
step_out(self, root_action);
} else {
stop(self)
}
}
fn insert_before<F>(&mut self, new_node: Self::NodePtr, mut step_out: F)
where F: FnMut(&mut Self, WalkAction)
{
use WalkAction::*;
if let Some(mut left) = self.detach_left() {
left.walk_reshape(|_| Right,
move |node| {
node.insert_right(Some(new_node));
},
&mut step_out);
self.insert_left(Some(left));
step_out(self, Left);
} else {
self.insert_left(Some(new_node));
}
}
fn walk_extract<FI, FE, FO>(&mut self, step_in: FI, extract: FE, mut step_out: FO) -> Option<Self::NodePtr>
where FI: FnMut(&mut Self) -> WalkAction,
FE: FnOnce(&mut Self, &mut Option<Self::NodePtr>),
FO: FnMut(&mut Self, WalkAction)
{
use WalkAction::*;
let ret = std::cell::RefCell::new(None);
self.walk_reshape(step_in,
|node| extract(node, &mut *ret.borrow_mut()),
|node, action| {
if ret.borrow().is_none() {
*ret.borrow_mut() = match action {
Left => node.detach_left(),
Right => node.detach_right(),
Stop => unreachable!(),
};
}
step_out(node, action);
});
ret.into_inner()
}
fn try_remove<F>(&mut self, mut step_out: F) -> Option<Self::NodePtr>
where F: FnMut(&mut Self, WalkAction)
{
use WalkAction::*;
if let Some(mut left) = self.detach_left() {
if self.right().is_none() {
mem::swap(&mut *left, self);
Some(left)
} else {
let mut pio = left.walk_extract(|_| Right,
|node, ret| {
if let Some(mut left) = node.detach_left() {
mem::swap(&mut *left, node);
*ret = Some(left);
}
},
&mut step_out);
if let Some(ref mut pio) = pio {
pio.insert_left(Some(left));
} else {
pio = Some(left);
}
let mut pio = pio.unwrap();
pio.insert_right(self.detach_right());
step_out(&mut pio, Left);
mem::swap(&mut *pio, self);
Some(pio)
}
} else if let Some(mut right) = self.detach_right() {
mem::swap(&mut *right, self);
Some(right)
} else {
None
}
}
}
#[derive(Clone, Copy, PartialEq)]
pub enum WalkAction {
Left,
Right,
Stop,
}