iodyn 0.2.1

An incremental collections library making use of Adapton
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Incremental Tree Cursor
//!
//! Tree Cursor for `level_tree`
//! - a cursor within an ordered, binary tree
//! - optimised for splitting and combining trees at the cursor in a cannonical way
//! - uses non-increasing levels for each subtree to maintain cannonical form
//! - in the general case the most efficent levels will be drawn from 
//!   a negative binomial distribution


use std::fmt::Debug;
use std::hash::Hash;
use std::rc::Rc;
use std::mem;
pub use level_tree::{Tree, gen_branch_level as gen_level};

use adapton::macros::*;
use adapton::engine::*;

/// tree cursor, centered on a node of the underlying persistent tree
///
/// A cursor allows exploration of the underlying tree by following links
/// to branches or up towards root. This structure is optimised 
/// for splitting and combining trees in a cannonical way based on
/// the levels of the nodes; that is, trees with the same levels will have
/// the same structure, regaurdless of order of operations.
/// 
/// Many operations allow structural mutation of the underlying tree.
#[derive(Debug,PartialEq,Eq,Hash)]
pub struct Cursor<E: TreeUpdate+Debug+Clone+Eq+Hash+'static> {
	dirty: bool,
	// dirty flag, containing tree
	l_forest: Vec<(bool,Tree<E>)>,
	tree: Option<Tree<E>>,
	r_forest: Vec<(bool,Tree<E>)>,
}
impl<E: TreeUpdate+Debug+Clone+Eq+Hash+'static> Clone for Cursor<E> {
	fn clone(&self) -> Self {
		Cursor {
			dirty: self.dirty,
			l_forest: self.l_forest.clone(),
			tree: self.tree.clone(),
			r_forest: self.r_forest.clone(),
		}
	}
}

/// Used for updating data when the tree is mutated
///
/// When the full tree is reconstructed on demand as the user
/// moves up to the root, new persistent branches must be constructed.
/// This trait allows the user to define how data is reconstructed.
pub trait TreeUpdate where Self: Sized{
	/// This method provides references to the (potentially) newly defined left and
	/// right branches of a tree node, along with the old data in that node.
	/// For example, read size from left and right to get the new size of the branch,
	/// or copy the old data without modification for the new branch.
	///
	/// Names passed into this method are USED by the tree whose data is being
	/// rebuilt and should not be used to name additional arts.
	fn rebuild(l_branch: Option<&Self>, old_data: &Self, level: u32, name: Option<Name>, r_branch: Option<&Self>) -> Self;
}
/// marker that allows a default implementation of TreeUpdate if the data is also `Clone`
///
/// this will simply clone the old data to be used in the new node
pub trait DeriveTreeUpdate{}
impl<E: DeriveTreeUpdate + Clone> TreeUpdate for E {
	#[allow(unused_variables)]
	fn rebuild(l_branch: Option<&Self>, old_data: &Self, level: u32, name: Option<Name>, r_branch: Option<&Self>) -> Self { old_data.clone() }
}

/// cursor movement qualifier
///
/// There are a few options for cursor movement to branches:
/// - Force::No, moves to the branch if it is not empty
/// - Force::Yes, forces the move to an empty branch
/// - Force::Discard, moves to full or empty branchs, discarding the
///   currently active branch. This will effectively connect the upper
///   node to the lower node, bypassing the current one 
#[derive(Clone,Copy,PartialEq,Eq)]
pub enum Force {
	No,
	Yes,
	Discard,
}

/// Result for `Cursor::up()`
///
/// This represents the direction the cursor moved
/// to reach the higher tree node, e.g., if `Left`, then
/// calling `Cursor::down_right()` will return to the
/// previous node. `Fail` means that there is not upper
/// node and the cursor didn't move. 
#[derive(Clone,Copy,PartialEq,Eq)]
pub enum UpResult {
	Fail,
	Left,
	Right,
}

fn peek_op<E: Debug+Clone+Eq+Hash+'static>(op: &Option<Tree<E>>) -> Option<E> {
	match *op {
		None => None,
		Some(ref t) => Some(t.peek())
	}
}

const DEFAULT_DEPTH: usize = 30;

impl<E: TreeUpdate+Debug+Clone+Eq+Hash+'static>
From<Tree<E>> for Cursor<E> {
	fn from(tree: Tree<E>) -> Self {
		Cursor{
			dirty: false,
			l_forest: Vec::with_capacity(DEFAULT_DEPTH),
			tree: Some(tree),
			r_forest: Vec::with_capacity(DEFAULT_DEPTH),
		}
	}
}

impl<'a,E: TreeUpdate+Debug+Clone+Eq+Hash+'static> Cursor<E> {

	/// creates a new cursor, to an empty underlying tree
	pub fn new() -> Self {
		Cursor{
			dirty: false,
			l_forest: Vec::new(),
			tree: None,
			r_forest: Vec::new(),
		}
	}
	/// creates a new cursor, with expected depth of underlying tree
	pub fn with_depth(depth: usize) -> Self {
		Cursor{
			dirty: false,
			l_forest: Vec::with_capacity(depth),
			tree: None,
			r_forest: Vec::with_capacity(depth),
		}
	}

	/// Returns the node the cursor is focused on as a tree, plus two
	/// cursors containing every node to the left and right, focused
	/// on at the two branches of the returned tree
	pub fn split(self) -> (Cursor<E>, Option<Tree<E>>, Cursor<E>) {
		let (l_tree,r_tree) = match self.tree {
			None => (None, None),
			Some(ref t) => (t.l_tree(), t.r_tree())
		};
		(
			Cursor{
				dirty: true,
				l_forest: self.l_forest,
				tree: l_tree,
				r_forest: Vec::with_capacity(DEFAULT_DEPTH),
			},
			self.tree,
			Cursor{
				dirty: true,
				l_forest: Vec::with_capacity(DEFAULT_DEPTH),
				tree: r_tree,
				r_forest: self.r_forest,
			},
		)
	}

	/// A specialized split that returns iterators
	///
	/// Converts the cursor into two iterators, over
	/// elements to the left and right of the cursor.
	/// The tree at the cursor is returned
	/// as well. The iterators include data it its branches,
	/// but not the tree's data. Data in a tree is considered to be
	/// between the data of the left and right branches.
	pub fn into_iters(self) -> (IterL<E>,Option<Tree<E>>,IterR<E>) {
		let (l_tree,r_tree) = match self.tree {
			None => (None, None),
			Some(ref t) => (t.l_tree(), t.r_tree())
		};
		let mut l_cursor = Cursor{
			dirty: true,
			l_forest: self.l_forest,
			tree: l_tree,
			r_forest: Vec::new(),
		};
		let mut r_cursor = Cursor{
			dirty: true,
			l_forest: Vec::new(),
			tree: r_tree,
			r_forest: self.r_forest,
		};
		// iterator can't start at None if there's data, so we move up
		// if there's no upper tree, then there's no data, so it's fine.
		// otherwise, we need to seek to the starting point
		if l_cursor.tree.is_none() { l_cursor.up_discard(); } else {
			while l_cursor.down_right() {}
		}
		if r_cursor.tree.is_none() { r_cursor.up_discard(); } else {
			while r_cursor.down_left() {}
		}
		(
			IterL(l_cursor),
			self.tree,
			IterR(r_cursor),
		)
	}

	/// makes a new cursor at the given data, between the trees of the other cursors
	///
	/// The `rebuild()` method of the data type will be called, with the `data`
	/// parameter passed here as the `old_data` to that method (along with joined branches).
	pub fn join(mut l_cursor: Self, level: u32, name: Option<Name>, data: E, mut r_cursor: Self) -> Self {
		// step 1: remove center forests
		while !l_cursor.r_forest.is_empty() { assert!(l_cursor.up() != UpResult::Fail); }
		while !r_cursor.l_forest.is_empty() { assert!(r_cursor.up() != UpResult::Fail); }
		// step 2: find insertion point
		while let Some(h) = l_cursor.up_left_level() {
			if h >= level { break; }
			else { assert!(l_cursor.up() != UpResult::Fail); }
		}
		while let Some(h) = l_cursor.peek_level() {
			if h < level { break; }
			else { assert!(l_cursor.down_right_force(Force::Yes)); }
		}
		while let Some(h) = r_cursor.up_right_level() {
			if h > level { break; }
			else { assert!(r_cursor.up() != UpResult::Fail); }
		}
		while let Some(h) = r_cursor.peek_level() {
			if h <= level { break; }
			else { assert!(r_cursor.down_left_force(Force::Yes)); }
		}
		// step 3: build center tree
		let tree = Tree::new(
			level, name.clone(),
			E::rebuild(peek_op(&l_cursor.tree).as_ref(), &data, level, name, peek_op(&r_cursor.tree).as_ref()),
			l_cursor.tree.clone(),
			r_cursor.tree.clone(),
		);
		assert!(tree.is_some());
		// step4: join structures
		Cursor{
			dirty: true,
			l_forest: l_cursor.l_forest,
			tree: tree,
			r_forest: r_cursor.r_forest,
		}
	}

	/// copies the focused node as a tree
	///
	/// This is a persistent tree, so copies are Rc clones
	pub fn at_tree(&self) -> Option<Tree<E>> { self.tree.clone() }

	/// copies the left branch of the focused node
	pub fn left_tree(&self) -> Option<Tree<E>> {
		match self.tree { None => None, Some(ref t) => t.l_tree() }
	}
	/// copies the right branch of the focused node
	pub fn right_tree(&self) -> Option<Tree<E>> {
		match self.tree { None => None, Some(ref t) => t.r_tree() }
	}

	/// peek at the data of the focused tree node
	pub fn peek(&self) -> Option<E> {
		peek_op(&self.tree)
	}

	/// peek at the level of the focused tree node
	pub fn peek_level(&self) -> Option<u32> {
		self.tree.as_ref().map(|t| t.level())
	}

	/// peek at the name of the focused tree node
	/// if there is a focused tree and it has a name
	pub fn peek_name(&self) -> Option<Name> {
		match self.tree {
			None => None,
			Some(ref t) => t.name()
		}
	}

	/// peek at the level of the next upper node that
	/// is to the left of this branch, even if its not
	/// directly above
	fn up_left_level(&self) -> Option<u32> {
		match self.l_forest.last() {
			None => None,
			Some(&(_,ref t)) => Some(t.level()),
		}
	}
	/// peek at the level of the next upper node that
	/// is to the right of this branch, even if its not
	/// directly above
	fn up_right_level(&self) -> Option<u32> {
		match self.r_forest.last() {
			None => None,
			Some(&(_,ref t)) => Some(t.level()),
		}
	}

	/// move the cursor into the left branch, returning true if successful
	/// use the `Force` enum to determine the type of movement
	pub fn down_left_force(&mut self, force: Force) -> bool {
		let new_tree = match self.tree {
			None => return false,
			Some(ref t) => { 
				let lt = t.l_tree();
				if lt.is_none() && force == Force::No { return false }
				lt
			}
		};
		let old_tree = mem::replace(&mut self.tree, new_tree).unwrap();
		if force != Force::Discard {
			self.r_forest.push((self.dirty, old_tree));
			self.dirty = false;
		} else { self.dirty = true; }
		true
	}
	/// move the cursor to the left branch, without entering an empty branch
	pub fn down_left(&mut self) -> bool { self.down_left_force(Force::No) }

	/// move the cursor into the right branch, returning true if successful
	/// use the `Force` enum to determine the type of movement
	pub fn down_right_force(&mut self, force: Force) -> bool {
		let new_tree = match self.tree {
			None => return false,
			Some(ref t) => { 
				let rt = t.r_tree();
				if rt.is_none() && force == Force::No { return false }
				rt
			}
		};
		let old_tree = mem::replace(&mut self.tree, new_tree).unwrap();
		if force != Force::Discard {
			self.l_forest.push((self.dirty, old_tree));
			self.dirty = false;
		} else { self.dirty = true; }
		true
	}
	/// move the cursor to the right branch, without entering an empty branch
	pub fn down_right(&mut self) -> bool { self.down_right_force(Force::No) }

	/// move the cursor up towards the root of the underlying persistent tree
	///
	/// If the tree has been changed, the `rebuild()` method of the tree's data
	/// type will be called as a new persistent node is created. The return 
	/// value represents the direction the cursor moved
	pub fn up(&mut self) -> UpResult {
		let to_left = match (self.l_forest.last(), self.r_forest.last()) {
			(None, None) => { return UpResult::Fail },
			(Some(_), None) => true,
			(Some(&(_,ref lt)), Some(&(_,ref rt))) if rt.level() > lt.level() => true,
			_ => false,
		};
		if to_left {
			if let Some((dirty, upper_tree)) = self.l_forest.pop() {
				if self.dirty == true {
					let l_branch = upper_tree.l_tree();
					self.tree = Tree::new(
						upper_tree.level(), upper_tree.name(),
						E::rebuild(peek_op(&l_branch).as_ref(), &upper_tree.peek(), upper_tree.level(), upper_tree.name(), peek_op(&self.tree).as_ref()),
						l_branch,
						self.tree.take(),
					)
				} else { self.dirty = dirty; self.tree = Some(upper_tree) }
			} else { panic!("up: empty left forest item"); }
		} else { // right side
			if let Some((dirty, upper_tree)) = self.r_forest.pop() {
				if self.dirty == true {
					let r_branch = upper_tree.r_tree();
					self.tree = Tree::new(
						upper_tree.level(), upper_tree.name(),
						E::rebuild(peek_op(&self.tree).as_ref(), &upper_tree.peek(), upper_tree.level(), upper_tree.name(), peek_op(&r_branch).as_ref()),
						self.tree.take(),
						r_branch,
					)
				} else { self.dirty = dirty; self.tree = Some(upper_tree) }
			} else { panic!("up: empty right forest item"); }
		}
		return if to_left { UpResult::Left } else { UpResult::Right };
	}
	/// move the cursor up, discarding any changes
	///
	/// The return value represents the direction the cursor moved
	pub fn up_discard(&mut self) -> UpResult {
		let to_left = match (self.l_forest.last(), self.r_forest.last()) {
			(None, None) => { return UpResult::Fail },
			(Some(_), None) => true,
			(Some(&(_,ref lt)), Some(&(_,ref rt))) if rt.level() > lt.level() => true,
			_ => false,
		};
		if to_left {
			if let Some((dirty, upper_tree)) = self.l_forest.pop() {
				self.dirty = dirty;
				self.tree = Some(upper_tree);
			} else { panic!("up: empty left forest item"); }
		} else { // right side
			if let Some((dirty, upper_tree)) = self.r_forest.pop() {
				self.dirty = dirty;
				self.tree = Some(upper_tree);
			} else { panic!("up: empty right forest item"); }
		}
		return if to_left { UpResult::Left } else { UpResult::Right };
	}

}

#[derive(Debug,Clone,PartialEq,Eq,Hash)]
pub struct IterL<T: TreeUpdate+Debug+Clone+Eq+Hash+'static>(Cursor<T>);
#[derive(Debug,Clone,Eq,PartialEq,Hash)]
pub struct IterR<T: TreeUpdate+Debug+Clone+Eq+Hash+'static>(Cursor<T>);
impl<T: TreeUpdate+Debug+Clone+Eq+Hash+'static> IterR<T> {
	// TODO: Incrementalize
	pub fn fold_out<R,B>(mut self, init: R, bin: Rc<B>) -> R where
		R:'static + Eq+Clone+Hash+Debug,
		B:'static + Fn(R,T) -> R
	{	
		let name = self.0.peek_name();
		// the `Art::force` is hidden in `<Self as Iterator>::next()`
		match self.next() {
			None => return init,
			Some(e) => {
				let a = bin(init,e);
				match name {
					None => self.fold_out(a,bin),
					Some(n) => {
						let (n,_) = name_fork(n);
						let i = self;
						memo!( n =>> Self::fold_out , i:i, a:a ;; f:bin.clone())
					}
				}
			}
		}
	}
}

impl<T: TreeUpdate+Debug+Clone+Eq+Hash+'static>
Iterator for IterR<T> {
	type Item = T;
	fn next(&mut self) -> Option<Self::Item> {
		let result = self.0.peek();
		// choose next tree node
		if self.0.down_right() {
			while self.0.down_left() {};
		} else { loop {
			match self.0.up_discard() {
				UpResult::Left => {},
				UpResult::Right => { break },
				UpResult::Fail => {
					self.0 = Cursor::new();
					break;					
				}
			}
		}}
		return result;
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	impl DeriveTreeUpdate for usize {}

  #[test]
  fn test_movement() {
		let t = 
		Tree::new(5, Some(name_of_usize(5)),1,
			Tree::new(3, Some(name_of_usize(3)),2,
				Tree::new(0,None,4,None,None),
				Tree::new(2, Some(name_of_usize(2)),5,
					Tree::new(1, Some(name_of_usize(1)),8,
						Tree::new(0,None,10,None,None),
						Tree::new(0,None,11,None,None),
					),
					Tree::new(0,None,9,None,None),
				)
			),
			Tree::new(4, Some(name_of_usize(4)),3,
				Tree::new(0,None,6,None,None),
				Tree::new(0,None,7,None,None),
			)
		).unwrap();

		let mut c: Cursor<usize> = t.into();
		assert_eq!(Some(1), c.peek());

		assert!(c.down_left());
		assert!(c.down_right());
		assert!(c.down_left());
		assert!(c.down_right());
		assert_eq!(Some(11), c.peek());

		assert!(c.up() != UpResult::Fail);
		assert!(c.up() != UpResult::Fail);
		assert!(c.up() != UpResult::Fail);
		assert_eq!(Some(2), c.peek());

		assert!(c.down_left_force(Force::Discard));
		assert_eq!(Some(4), c.peek());

		assert!(c.up() != UpResult::Fail);
		assert!(c.down_right());
		assert!(c.down_right());
		assert_eq!(Some(7), c.peek());

		assert!(!c.down_right());
		assert!(c.down_right_force(Force::Yes));
		assert_eq!(None, c.peek());
	}

	#[test]
	fn test_split_join() {
		let t = 
		Tree::new(5, Some(name_of_usize(5)),1,
			Tree::new(3, Some(name_of_usize(3)),2,
				Tree::new(0,None,4,None,None),
				Tree::new(2, Some(name_of_usize(2)),5,
					Tree::new(1, Some(name_of_usize(1)),8,
						Tree::new(0,None,10,None,None),
						Tree::new(0,None,11,None,None),
					),
					Tree::new(0,None,9,None,None),
				)
			),
			Tree::new(4, Some(name_of_usize(4)),3,
				Tree::new(0,None,6,None,None),
				Tree::new(0,None,7,None,None),
			)
		).unwrap();

		let mut c: Cursor<usize> = t.into();
		assert!(c.down_left());
		let (mut lc, t, mut rc) = c.split();
		assert_eq!(Some(4), lc.peek());
		assert_eq!(Some(5), rc.peek());

		assert!(lc.up() == UpResult::Fail);
		assert!(rc.up() != UpResult::Fail);
		assert_eq!(Some(1), rc.peek());

		let t = t.unwrap();
		let mut j = Cursor::join(rc, t.level(), t.name(), t.peek(), lc);
		assert_eq!(Some(2), j.peek());

		assert!(j.down_left());
		assert_eq!(Some(7), j.peek());

		assert!(j.up() != UpResult::Fail);
		assert!(j.up() != UpResult::Fail);
		assert_eq!(Some(3), j.peek());
	}

	#[test]
	fn test_iter_r() {
		let t = 
		Tree::new(5, Some(name_of_usize(5)),1,
			Tree::new(3, Some(name_of_usize(3)),2,
				Tree::new(0,None,4,None,None),
				Tree::new(2, Some(name_of_usize(2)),5,
					Tree::new(1, Some(name_of_usize(1)),8,
						Tree::new(0,None,10,None,None),
						Tree::new(0,None,11,None,None),
					),
					Tree::new(0,None,9,None,None),
				)
			),
			Tree::new(4, Some(name_of_usize(4)),3,
				Tree::new(0,None,6,None,None),
				Tree::new(0,None,7,None,None),
			)
		).unwrap();
		let mut c: Cursor<usize> = t.into();

		assert!(c.down_left());
		assert!(c.down_right());
		assert!(c.down_left());
		assert!(c.down_right());
		let (_,t,iter) = c.into_iters();

		assert_eq!(Some(11),t.map(|e|e.peek()));
		let right = iter.collect::<Vec<_>>();
		assert_eq!(vec![5,9,1,6,3,7], right);

	}

}