]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/drop/dropck_legal_cycles.rs
2c88cfd5c8f93d37613115decc3ed2fcfe244476
[rust.git] / src / test / run-pass / drop / dropck_legal_cycles.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // run-pass
12 // This test exercises cases where cyclic structure is legal,
13 // including when the cycles go through data-structures such
14 // as `Vec` or `TypedArena`.
15 //
16 // The intent is to cover as many such cases as possible, ensuring
17 // that if the compiler did not complain circa Rust 1.x (1.2 as of
18 // this writing), then it will continue to not complain in the future.
19 //
20 // Note that while some of the tests are only exercising using the
21 // given collection as a "backing store" for a set of nodes that hold
22 // the actual cycle (and thus the cycle does not go through the
23 // collection itself in such cases), in general we *do* want to make
24 // sure to have at least one example exercising a cycle that goes
25 // through the collection, for every collection type that supports
26 // this.
27
28 // HIGH LEVEL DESCRIPTION OF THE TEST ARCHITECTURE
29 // -----------------------------------------------
30 //
31 // We pick a data structure and want to make a cyclic construction
32 // from it. Each test of interest is labelled starting with "Cycle N:
33 // { ... }" where N is the test number and the "..."`is filled in with
34 // a graphviz-style description of the graph structure that the
35 // author believes is being made. So "{ a -> b, b -> (c,d), (c,d) -> e }"
36 // describes a line connected to a diamond:
37 //
38 //                           c
39 //                          / \
40 //                     a - b   e
41 //                          \ /
42 //                           d
43 //
44 // (Note that the above directed graph is actually acyclic.)
45 //
46 // The different graph structures are often composed of different data
47 // types. Some may be built atop `Vec`, others atop `HashMap`, etc.
48 //
49 // For each graph structure, we actually *confirm* that a cycle exists
50 // (as a safe-guard against a test author accidentally leaving it out)
51 // by traversing each graph and "proving" that a cycle exists within it.
52 //
53 // To do this, while trying to keep the code uniform (despite working
54 // with different underlying collection and smart-pointer types), we
55 // have a standard traversal API:
56 //
57 // 1. every node in the graph carries a `mark` (a u32, init'ed to 0).
58 //
59 // 2. every node provides a method to visit its children
60 //
61 // 3. a traversal attmepts to visit the nodes of the graph and prove that
62 //    it sees the same node twice. It does this by setting the mark of each
63 //    node to a fresh non-zero value, and if it sees the current mark, it
64 //    "knows" that it must have found a cycle, and stops attempting further
65 //    traversal.
66 //
67 // 4. each traversal is controlled by a bit-string that tells it which child
68 //    it visit when it can take different paths. As a simple example,
69 //    in a binary tree, 0 could mean "left" (and 1, "right"), so that
70 //    "00010" means "left, left, left, right, left". (In general it will
71 //    read as many bits as it needs to choose one child.)
72 //
73 //    The graphs in this test are all meant to be very small, and thus
74 //    short bitstrings of less than 64 bits should always suffice.
75 //
76 //    (An earlier version of this test infrastructure simply had any
77 //    given traversal visit all children it encountered, in a
78 //    depth-first manner; one problem with this approach is that an
79 //    acyclic graph can still have sharing, which would then be treated
80 //    as a repeat mark and reported as a detected cycle.)
81 //
82 // The travseral code is a little more complicated because it has been
83 // programmed in a somewhat defensive manner. For example it also has
84 // a max threshold for the number of nodes it will visit, to guard
85 // against scenarios where the nodes are not correctly setting their
86 // mark when asked. There are various other methods not discussed here
87 // that are for aiding debugging the test when it runs, such as the
88 // `name` method that all nodes provide.
89 //
90 // So each test:
91 //
92 // 1. allocates the nodes in the graph,
93 //
94 // 2. sets up the links in the graph,
95 //
96 // 3. clones the "ContextData"
97 //
98 // 4. chooses a new current mark value for this test
99 //
100 // 5. initiates a traversal, potentially from multiple starting points
101 //    (aka "roots"), with a given control-string (potentially a
102 //    different string for each root). if it does start from a
103 //    distinct root, then such a test should also increment the
104 //    current mark value, so that this traversal is considered
105 //    distinct from the prior one on this graph structure.
106 //
107 //    Note that most of the tests work with the default control string
108 //    of all-zeroes.
109 //
110 // 6. assert that the context confirms that it actually saw a cycle (since a traversal
111 //    might have terminated, e.g. on a tree structure that contained no cycles).
112
113 use std::cell::{Cell, RefCell};
114 use std::cmp::Ordering;
115 use std::collections::BinaryHeap;
116 use std::collections::HashMap;
117 use std::collections::LinkedList;
118 use std::collections::VecDeque;
119 use std::collections::btree_map::BTreeMap;
120 use std::collections::btree_set::BTreeSet;
121 use std::hash::{Hash, Hasher};
122 use std::rc::Rc;
123 use std::sync::{Arc, RwLock, Mutex};
124
125 const PRINT: bool = false;
126
127 pub fn main() {
128     let c_orig = ContextData {
129         curr_depth: 0,
130         max_depth: 3,
131         visited: 0,
132         max_visits: 1000,
133         skipped: 0,
134         curr_mark: 0,
135         saw_prev_marked: false,
136         control_bits: 0,
137     };
138
139     // SANITY CHECK FOR TEST SUITE (thus unnumbered)
140     // Not a cycle: { v[0] -> (v[1], v[2]), v[1] -> v[3], v[2] -> v[3] };
141     let v: Vec<S2> = vec![Named::new("s0"),
142                           Named::new("s1"),
143                           Named::new("s2"),
144                           Named::new("s3")];
145     v[0].next.set((Some(&v[1]), Some(&v[2])));
146     v[1].next.set((Some(&v[3]), None));
147     v[2].next.set((Some(&v[3]), None));
148     v[3].next.set((None, None));
149
150     let mut c = c_orig.clone();
151     c.curr_mark = 10;
152     assert!(!c.saw_prev_marked);
153     v[0].descend_into_self(&mut c);
154     assert!(!c.saw_prev_marked); // <-- different from below, b/c acyclic above
155
156     if PRINT { println!(""); }
157
158     // Cycle 1: { v[0] -> v[1], v[1] -> v[0] };
159     // does not exercise `v` itself
160     let v: Vec<S> = vec![Named::new("s0"),
161                          Named::new("s1")];
162     v[0].next.set(Some(&v[1]));
163     v[1].next.set(Some(&v[0]));
164
165     let mut c = c_orig.clone();
166     c.curr_mark = 10;
167     assert!(!c.saw_prev_marked);
168     v[0].descend_into_self(&mut c);
169     assert!(c.saw_prev_marked);
170
171     if PRINT { println!(""); }
172
173     // Cycle 2: { v[0] -> v, v[1] -> v }
174     let v: V = Named::new("v");
175     v.contents[0].set(Some(&v));
176     v.contents[1].set(Some(&v));
177
178     let mut c = c_orig.clone();
179     c.curr_mark = 20;
180     assert!(!c.saw_prev_marked);
181     v.descend_into_self(&mut c);
182     assert!(c.saw_prev_marked);
183
184     if PRINT { println!(""); }
185
186     // Cycle 3: { hk0 -> hv0, hv0 -> hk0, hk1 -> hv1, hv1 -> hk1 };
187     // does not exercise `h` itself
188
189     let mut h: HashMap<H,H> = HashMap::new();
190     h.insert(Named::new("hk0"), Named::new("hv0"));
191     h.insert(Named::new("hk1"), Named::new("hv1"));
192     for (key, val) in h.iter() {
193         val.next.set(Some(key));
194         key.next.set(Some(val));
195     }
196
197     let mut c = c_orig.clone();
198     c.curr_mark = 30;
199     for (key, _) in h.iter() {
200         c.curr_mark += 1;
201         c.saw_prev_marked = false;
202         key.descend_into_self(&mut c);
203         assert!(c.saw_prev_marked);
204     }
205
206     if PRINT { println!(""); }
207
208     // Cycle 4: { h -> (hmk0,hmv0,hmk1,hmv1), {hmk0,hmv0,hmk1,hmv1} -> h }
209
210     let mut h: HashMap<HM,HM> = HashMap::new();
211     h.insert(Named::new("hmk0"), Named::new("hmv0"));
212     h.insert(Named::new("hmk0"), Named::new("hmv0"));
213     for (key, val) in h.iter() {
214         val.contents.set(Some(&h));
215         key.contents.set(Some(&h));
216     }
217
218     let mut c = c_orig.clone();
219     c.max_depth = 2;
220     c.curr_mark = 40;
221     for (key, _) in h.iter() {
222         c.curr_mark += 1;
223         c.saw_prev_marked = false;
224         key.descend_into_self(&mut c);
225         assert!(c.saw_prev_marked);
226         // break;
227     }
228
229     if PRINT { println!(""); }
230
231     // Cycle 5: { vd[0] -> vd[1], vd[1] -> vd[0] };
232     // does not exercise vd itself
233     let mut vd: VecDeque<S> = VecDeque::new();
234     vd.push_back(Named::new("d0"));
235     vd.push_back(Named::new("d1"));
236     vd[0].next.set(Some(&vd[1]));
237     vd[1].next.set(Some(&vd[0]));
238
239     let mut c = c_orig.clone();
240     c.curr_mark = 50;
241     assert!(!c.saw_prev_marked);
242     vd[0].descend_into_self(&mut c);
243     assert!(c.saw_prev_marked);
244
245     if PRINT { println!(""); }
246
247     // Cycle 6: { vd -> (vd0, vd1), {vd0, vd1} -> vd }
248     let mut vd: VecDeque<VD> = VecDeque::new();
249     vd.push_back(Named::new("vd0"));
250     vd.push_back(Named::new("vd1"));
251     vd[0].contents.set(Some(&vd));
252     vd[1].contents.set(Some(&vd));
253
254     let mut c = c_orig.clone();
255     c.curr_mark = 60;
256     assert!(!c.saw_prev_marked);
257     vd[0].descend_into_self(&mut c);
258     assert!(c.saw_prev_marked);
259
260     if PRINT { println!(""); }
261
262     // Cycle 7: { vm -> (vm0, vm1), {vm0, vm1} -> vm }
263     let mut vm: HashMap<usize, VM> = HashMap::new();
264     vm.insert(0, Named::new("vm0"));
265     vm.insert(1, Named::new("vm1"));
266     vm[&0].contents.set(Some(&vm));
267     vm[&1].contents.set(Some(&vm));
268
269     let mut c = c_orig.clone();
270     c.curr_mark = 70;
271     assert!(!c.saw_prev_marked);
272     vm[&0].descend_into_self(&mut c);
273     assert!(c.saw_prev_marked);
274
275     if PRINT { println!(""); }
276
277     // Cycle 8: { ll -> (ll0, ll1), {ll0, ll1} -> ll }
278     let mut ll: LinkedList<LL> = LinkedList::new();
279     ll.push_back(Named::new("ll0"));
280     ll.push_back(Named::new("ll1"));
281     for e in &ll {
282         e.contents.set(Some(&ll));
283     }
284
285     let mut c = c_orig.clone();
286     c.curr_mark = 80;
287     for e in &ll {
288         c.curr_mark += 1;
289         c.saw_prev_marked = false;
290         e.descend_into_self(&mut c);
291         assert!(c.saw_prev_marked);
292         // break;
293     }
294
295     if PRINT { println!(""); }
296
297     // Cycle 9: { bh -> (bh0, bh1), {bh0, bh1} -> bh }
298     let mut bh: BinaryHeap<BH> = BinaryHeap::new();
299     bh.push(Named::new("bh0"));
300     bh.push(Named::new("bh1"));
301     for b in bh.iter() {
302         b.contents.set(Some(&bh));
303     }
304
305     let mut c = c_orig.clone();
306     c.curr_mark = 90;
307     for b in &bh {
308         c.curr_mark += 1;
309         c.saw_prev_marked = false;
310         b.descend_into_self(&mut c);
311         assert!(c.saw_prev_marked);
312         // break;
313     }
314
315     if PRINT { println!(""); }
316
317     // Cycle 10: { btm -> (btk0, btv1), {bt0, bt1} -> btm }
318     let mut btm: BTreeMap<BTM, BTM> = BTreeMap::new();
319     btm.insert(Named::new("btk0"), Named::new("btv0"));
320     btm.insert(Named::new("btk1"), Named::new("btv1"));
321     for (k, v) in btm.iter() {
322         k.contents.set(Some(&btm));
323         v.contents.set(Some(&btm));
324     }
325
326     let mut c = c_orig.clone();
327     c.curr_mark = 100;
328     for (k, _) in &btm {
329         c.curr_mark += 1;
330         c.saw_prev_marked = false;
331         k.descend_into_self(&mut c);
332         assert!(c.saw_prev_marked);
333         // break;
334     }
335
336     if PRINT { println!(""); }
337
338     // Cycle 10: { bts -> (bts0, bts1), {bts0, bts1} -> btm }
339     let mut bts: BTreeSet<BTS> = BTreeSet::new();
340     bts.insert(Named::new("bts0"));
341     bts.insert(Named::new("bts1"));
342     for v in bts.iter() {
343         v.contents.set(Some(&bts));
344     }
345
346     let mut c = c_orig.clone();
347     c.curr_mark = 100;
348     for b in &bts {
349         c.curr_mark += 1;
350         c.saw_prev_marked = false;
351         b.descend_into_self(&mut c);
352         assert!(c.saw_prev_marked);
353         // break;
354     }
355
356     if PRINT { println!(""); }
357
358     // Cycle 11: { rc0 -> (rc1, rc2), rc1 -> (), rc2 -> rc0 }
359     let (rc0, rc1, rc2): (RCRC, RCRC, RCRC);
360     rc0 = RCRC::new("rcrc0");
361     rc1 = RCRC::new("rcrc1");
362     rc2 = RCRC::new("rcrc2");
363     rc0.0.borrow_mut().children.0 = Some(&rc1);
364     rc0.0.borrow_mut().children.1 = Some(&rc2);
365     rc2.0.borrow_mut().children.0 = Some(&rc0);
366
367     let mut c = c_orig.clone();
368     c.control_bits = 0b1;
369     c.curr_mark = 110;
370     assert!(!c.saw_prev_marked);
371     rc0.descend_into_self(&mut c);
372     assert!(c.saw_prev_marked);
373
374     if PRINT { println!(""); }
375
376     // We want to take the previous Rc case and generalize it to Arc.
377     //
378     // We can use refcells if we're single-threaded (as this test is).
379     // If one were to generalize these constructions to a
380     // multi-threaded context, then it might seem like we could choose
381     // between either a RwLock or a Mutex to hold the owned arcs on
382     // each node.
383     //
384     // Part of the point of this test is to actually confirm that the
385     // cycle exists by traversing it. We can do that just fine with an
386     // RwLock (since we can grab the child pointers in read-only
387     // mode), but we cannot lock a std::sync::Mutex to guard reading
388     // from each node via the same pattern, since once you hit the
389     // cycle, you'll be trying to acquiring the same lock twice.
390     // (We deal with this by exiting the traversal early if try_lock fails.)
391
392     // Cycle 12: { arc0 -> (arc1, arc2), arc1 -> (), arc2 -> arc0 }, refcells
393     let (arc0, arc1, arc2): (ARCRC, ARCRC, ARCRC);
394     arc0 = ARCRC::new("arcrc0");
395     arc1 = ARCRC::new("arcrc1");
396     arc2 = ARCRC::new("arcrc2");
397     arc0.0.borrow_mut().children.0 = Some(&arc1);
398     arc0.0.borrow_mut().children.1 = Some(&arc2);
399     arc2.0.borrow_mut().children.0 = Some(&arc0);
400
401     let mut c = c_orig.clone();
402     c.control_bits = 0b1;
403     c.curr_mark = 110;
404     assert!(!c.saw_prev_marked);
405     arc0.descend_into_self(&mut c);
406     assert!(c.saw_prev_marked);
407
408     if PRINT { println!(""); }
409
410     // Cycle 13: { arc0 -> (arc1, arc2), arc1 -> (), arc2 -> arc0 }, rwlocks
411     let (arc0, arc1, arc2): (ARCRW, ARCRW, ARCRW);
412     arc0 = ARCRW::new("arcrw0");
413     arc1 = ARCRW::new("arcrw1");
414     arc2 = ARCRW::new("arcrw2");
415     arc0.0.write().unwrap().children.0 = Some(&arc1);
416     arc0.0.write().unwrap().children.1 = Some(&arc2);
417     arc2.0.write().unwrap().children.0 = Some(&arc0);
418
419     let mut c = c_orig.clone();
420     c.control_bits = 0b1;
421     c.curr_mark = 110;
422     assert!(!c.saw_prev_marked);
423     arc0.descend_into_self(&mut c);
424     assert!(c.saw_prev_marked);
425
426     if PRINT { println!(""); }
427
428     // Cycle 14: { arc0 -> (arc1, arc2), arc1 -> (), arc2 -> arc0 }, mutexs
429     let (arc0, arc1, arc2): (ARCM, ARCM, ARCM);
430     arc0 = ARCM::new("arcm0");
431     arc1 = ARCM::new("arcm1");
432     arc2 = ARCM::new("arcm2");
433     arc0.1.lock().unwrap().children.0 = Some(&arc1);
434     arc0.1.lock().unwrap().children.1 = Some(&arc2);
435     arc2.1.lock().unwrap().children.0 = Some(&arc0);
436
437     let mut c = c_orig.clone();
438     c.control_bits = 0b1;
439     c.curr_mark = 110;
440     assert!(!c.saw_prev_marked);
441     arc0.descend_into_self(&mut c);
442     assert!(c.saw_prev_marked);
443 }
444
445 trait Named {
446     fn new(_: &'static str) -> Self;
447     fn name(&self) -> &str;
448 }
449
450 trait Marked<M> {
451     fn mark(&self) -> M;
452     fn set_mark(&self, mark: M);
453 }
454
455 struct S<'a> {
456     name: &'static str,
457     mark: Cell<u32>,
458     next: Cell<Option<&'a S<'a>>>,
459 }
460
461 impl<'a> Named for S<'a> {
462     fn new(name: &'static str) -> S<'a> {
463         S { name: name, mark: Cell::new(0), next: Cell::new(None) }
464     }
465     fn name(&self) -> &str { self.name }
466 }
467
468 impl<'a> Marked<u32> for S<'a> {
469     fn mark(&self) -> u32 { self.mark.get() }
470     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
471 }
472
473 struct S2<'a> {
474     name: &'static str,
475     mark: Cell<u32>,
476     next: Cell<(Option<&'a S2<'a>>, Option<&'a S2<'a>>)>,
477 }
478
479 impl<'a> Named for S2<'a> {
480     fn new(name: &'static str) -> S2<'a> {
481         S2 { name: name, mark: Cell::new(0), next: Cell::new((None, None)) }
482     }
483     fn name(&self) -> &str { self.name }
484 }
485
486 impl<'a> Marked<u32> for S2<'a> {
487     fn mark(&self) -> u32 { self.mark.get() }
488     fn set_mark(&self, mark: u32) {
489         self.mark.set(mark);
490     }
491 }
492
493 struct V<'a> {
494     name: &'static str,
495     mark: Cell<u32>,
496     contents: Vec<Cell<Option<&'a V<'a>>>>,
497 }
498
499 impl<'a> Named for V<'a> {
500     fn new(name: &'static str) -> V<'a> {
501         V { name: name,
502             mark: Cell::new(0),
503             contents: vec![Cell::new(None), Cell::new(None)]
504         }
505     }
506     fn name(&self) -> &str { self.name }
507 }
508
509 impl<'a> Marked<u32> for V<'a> {
510     fn mark(&self) -> u32 { self.mark.get() }
511     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
512 }
513
514 #[derive(Eq)]
515 struct H<'a> {
516     name: &'static str,
517     mark: Cell<u32>,
518     next: Cell<Option<&'a H<'a>>>,
519 }
520
521 impl<'a> Named for H<'a> {
522     fn new(name: &'static str) -> H<'a> {
523         H { name: name, mark: Cell::new(0), next: Cell::new(None) }
524     }
525     fn name(&self) -> &str { self.name }
526 }
527
528 impl<'a> Marked<u32> for H<'a> {
529     fn mark(&self) -> u32 { self.mark.get() }
530     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
531 }
532
533 impl<'a> PartialEq for H<'a> {
534     fn eq(&self, rhs: &H<'a>) -> bool {
535         self.name == rhs.name
536     }
537 }
538
539 impl<'a> Hash for H<'a> {
540     fn hash<H: Hasher>(&self, state: &mut H) {
541         self.name.hash(state)
542     }
543 }
544
545 #[derive(Eq)]
546 struct HM<'a> {
547     name: &'static str,
548     mark: Cell<u32>,
549     contents: Cell<Option<&'a HashMap<HM<'a>, HM<'a>>>>,
550 }
551
552 impl<'a> Named for HM<'a> {
553     fn new(name: &'static str) -> HM<'a> {
554         HM { name: name,
555              mark: Cell::new(0),
556              contents: Cell::new(None)
557         }
558     }
559     fn name(&self) -> &str { self.name }
560 }
561
562 impl<'a> Marked<u32> for HM<'a> {
563     fn mark(&self) -> u32 { self.mark.get() }
564     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
565 }
566
567 impl<'a> PartialEq for HM<'a> {
568     fn eq(&self, rhs: &HM<'a>) -> bool {
569         self.name == rhs.name
570     }
571 }
572
573 impl<'a> Hash for HM<'a> {
574     fn hash<H: Hasher>(&self, state: &mut H) {
575         self.name.hash(state)
576     }
577 }
578
579
580 struct VD<'a> {
581     name: &'static str,
582     mark: Cell<u32>,
583     contents: Cell<Option<&'a VecDeque<VD<'a>>>>,
584 }
585
586 impl<'a> Named for VD<'a> {
587     fn new(name: &'static str) -> VD<'a> {
588         VD { name: name,
589              mark: Cell::new(0),
590              contents: Cell::new(None)
591         }
592     }
593     fn name(&self) -> &str { self.name }
594 }
595
596 impl<'a> Marked<u32> for VD<'a> {
597     fn mark(&self) -> u32 { self.mark.get() }
598     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
599 }
600
601 struct VM<'a> {
602     name: &'static str,
603     mark: Cell<u32>,
604     contents: Cell<Option<&'a HashMap<usize, VM<'a>>>>,
605 }
606
607 impl<'a> Named for VM<'a> {
608     fn new(name: &'static str) -> VM<'a> {
609         VM { name: name,
610              mark: Cell::new(0),
611              contents: Cell::new(None)
612         }
613     }
614     fn name(&self) -> &str { self.name }
615 }
616
617 impl<'a> Marked<u32> for VM<'a> {
618     fn mark(&self) -> u32 { self.mark.get() }
619     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
620 }
621
622 struct LL<'a> {
623     name: &'static str,
624     mark: Cell<u32>,
625     contents: Cell<Option<&'a LinkedList<LL<'a>>>>,
626 }
627
628 impl<'a> Named for LL<'a> {
629     fn new(name: &'static str) -> LL<'a> {
630         LL { name: name,
631              mark: Cell::new(0),
632              contents: Cell::new(None)
633         }
634     }
635     fn name(&self) -> &str { self.name }
636 }
637
638 impl<'a> Marked<u32> for LL<'a> {
639     fn mark(&self) -> u32 { self.mark.get() }
640     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
641 }
642
643 struct BH<'a> {
644     name: &'static str,
645     mark: Cell<u32>,
646     contents: Cell<Option<&'a BinaryHeap<BH<'a>>>>,
647 }
648
649 impl<'a> Named for BH<'a> {
650     fn new(name: &'static str) -> BH<'a> {
651         BH { name: name,
652              mark: Cell::new(0),
653              contents: Cell::new(None)
654         }
655     }
656     fn name(&self) -> &str { self.name }
657 }
658
659 impl<'a> Marked<u32> for BH<'a> {
660     fn mark(&self) -> u32 { self.mark.get() }
661     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
662 }
663
664 impl<'a> Eq for BH<'a> { }
665
666 impl<'a> PartialEq for BH<'a> {
667     fn eq(&self, rhs: &BH<'a>) -> bool {
668         self.name == rhs.name
669     }
670 }
671
672 impl<'a> PartialOrd for BH<'a> {
673     fn partial_cmp(&self, rhs: &BH<'a>) -> Option<Ordering> {
674         Some(self.cmp(rhs))
675     }
676 }
677
678 impl<'a> Ord for BH<'a> {
679     fn cmp(&self, rhs: &BH<'a>) -> Ordering {
680         self.name.cmp(rhs.name)
681     }
682 }
683
684 struct BTM<'a> {
685     name: &'static str,
686     mark: Cell<u32>,
687     contents: Cell<Option<&'a BTreeMap<BTM<'a>, BTM<'a>>>>,
688 }
689
690 impl<'a> Named for BTM<'a> {
691     fn new(name: &'static str) -> BTM<'a> {
692         BTM { name: name,
693              mark: Cell::new(0),
694              contents: Cell::new(None)
695         }
696     }
697     fn name(&self) -> &str { self.name }
698 }
699
700 impl<'a> Marked<u32> for BTM<'a> {
701     fn mark(&self) -> u32 { self.mark.get() }
702     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
703 }
704
705 impl<'a> Eq for BTM<'a> { }
706
707 impl<'a> PartialEq for BTM<'a> {
708     fn eq(&self, rhs: &BTM<'a>) -> bool {
709         self.name == rhs.name
710     }
711 }
712
713 impl<'a> PartialOrd for BTM<'a> {
714     fn partial_cmp(&self, rhs: &BTM<'a>) -> Option<Ordering> {
715         Some(self.cmp(rhs))
716     }
717 }
718
719 impl<'a> Ord for BTM<'a> {
720     fn cmp(&self, rhs: &BTM<'a>) -> Ordering {
721         self.name.cmp(rhs.name)
722     }
723 }
724
725 struct BTS<'a> {
726     name: &'static str,
727     mark: Cell<u32>,
728     contents: Cell<Option<&'a BTreeSet<BTS<'a>>>>,
729 }
730
731 impl<'a> Named for BTS<'a> {
732     fn new(name: &'static str) -> BTS<'a> {
733         BTS { name: name,
734              mark: Cell::new(0),
735              contents: Cell::new(None)
736         }
737     }
738     fn name(&self) -> &str { self.name }
739 }
740
741 impl<'a> Marked<u32> for BTS<'a> {
742     fn mark(&self) -> u32 { self.mark.get() }
743     fn set_mark(&self, mark: u32) { self.mark.set(mark); }
744 }
745
746 impl<'a> Eq for BTS<'a> { }
747
748 impl<'a> PartialEq for BTS<'a> {
749     fn eq(&self, rhs: &BTS<'a>) -> bool {
750         self.name == rhs.name
751     }
752 }
753
754 impl<'a> PartialOrd for BTS<'a> {
755     fn partial_cmp(&self, rhs: &BTS<'a>) -> Option<Ordering> {
756         Some(self.cmp(rhs))
757     }
758 }
759
760 impl<'a> Ord for BTS<'a> {
761     fn cmp(&self, rhs: &BTS<'a>) -> Ordering {
762         self.name.cmp(rhs.name)
763     }
764 }
765
766 #[derive(Clone)]
767 struct RCRCData<'a> {
768     name: &'static str,
769     mark: Cell<u32>,
770     children: (Option<&'a RCRC<'a>>, Option<&'a RCRC<'a>>),
771 }
772 #[derive(Clone)]
773 struct RCRC<'a>(Rc<RefCell<RCRCData<'a>>>);
774
775 impl<'a> Named for RCRC<'a> {
776     fn new(name: &'static str) -> Self {
777         RCRC(Rc::new(RefCell::new(RCRCData {
778             name: name, mark: Cell::new(0), children: (None, None), })))
779     }
780     fn name(&self) -> &str { self.0.borrow().name }
781 }
782
783 impl<'a> Marked<u32> for RCRC<'a> {
784     fn mark(&self) -> u32 { self.0.borrow().mark.get() }
785     fn set_mark(&self, mark: u32) { self.0.borrow().mark.set(mark); }
786 }
787
788 impl<'a> Children<'a> for RCRC<'a> {
789     fn count_children(&self) -> usize { 2 }
790     fn descend_one_child<C>(&self, context: &mut C, index: usize)
791         where C: Context + PrePost<Self>, Self: Sized
792     {
793         let children = &self.0.borrow().children;
794         let child = match index {
795             0 => if let Some(child) = children.0 { child } else { return; },
796             1 => if let Some(child) = children.1 { child } else { return; },
797             _ => panic!("bad children"),
798         };
799         // println!("S2 {} descending into child {} at index {}", self.name, child.name, index);
800         child.descend_into_self(context);
801     }
802 }
803 #[derive(Clone)]
804 struct ARCRCData<'a> {
805     name: &'static str,
806     mark: Cell<u32>,
807     children: (Option<&'a ARCRC<'a>>, Option<&'a ARCRC<'a>>),
808 }
809 #[derive(Clone)]
810 struct ARCRC<'a>(Arc<RefCell<ARCRCData<'a>>>);
811
812 impl<'a> Named for ARCRC<'a> {
813     fn new(name: &'static str) -> Self {
814         ARCRC(Arc::new(RefCell::new(ARCRCData {
815             name: name, mark: Cell::new(0), children: (None, None), })))
816     }
817     fn name(&self) -> &str { self.0.borrow().name }
818 }
819
820 impl<'a> Marked<u32> for ARCRC<'a> {
821     fn mark(&self) -> u32 { self.0.borrow().mark.get() }
822     fn set_mark(&self, mark: u32) { self.0.borrow().mark.set(mark); }
823 }
824
825 impl<'a> Children<'a> for ARCRC<'a> {
826     fn count_children(&self) -> usize { 2 }
827     fn descend_one_child<C>(&self, context: &mut C, index: usize)
828         where C: Context + PrePost<Self>, Self: Sized
829     {
830         let children = &self.0.borrow().children;
831         match index {
832             0 => if let Some(ref child) = children.0 {
833                 child.descend_into_self(context);
834             },
835             1 => if let Some(ref child) = children.1 {
836                 child.descend_into_self(context);
837             },
838             _ => panic!("bad children!"),
839         }
840     }
841 }
842
843 #[derive(Clone)]
844 struct ARCMData<'a> {
845     mark: Cell<u32>,
846     children: (Option<&'a ARCM<'a>>, Option<&'a ARCM<'a>>),
847 }
848
849 #[derive(Clone)]
850 struct ARCM<'a>(&'static str, Arc<Mutex<ARCMData<'a>>>);
851
852 impl<'a> Named for ARCM<'a> {
853     fn new(name: &'static str) -> Self {
854         ARCM(name, Arc::new(Mutex::new(ARCMData {
855             mark: Cell::new(0), children: (None, None), })))
856     }
857     fn name(&self) -> &str { self.0 }
858 }
859
860 impl<'a> Marked<u32> for ARCM<'a> {
861     fn mark(&self) -> u32 { self.1.lock().unwrap().mark.get() }
862     fn set_mark(&self, mark: u32) { self.1.lock().unwrap().mark.set(mark); }
863 }
864
865 impl<'a> Children<'a> for ARCM<'a> {
866     fn count_children(&self) -> usize { 2 }
867     fn descend_one_child<C>(&self, context: &mut C, index: usize)
868         where C: Context + PrePost<Self>, Self: Sized
869     {
870         let ref children = if let Ok(data) = self.1.try_lock() {
871             data.children
872         } else { return; };
873         match index {
874             0 => if let Some(ref child) = children.0 {
875                 child.descend_into_self(context);
876             },
877             1 => if let Some(ref child) = children.1 {
878                 child.descend_into_self(context);
879             },
880             _ => panic!("bad children!"),
881         }
882     }
883 }
884
885 #[derive(Clone)]
886 struct ARCRWData<'a> {
887     name: &'static str,
888     mark: Cell<u32>,
889     children: (Option<&'a ARCRW<'a>>, Option<&'a ARCRW<'a>>),
890 }
891
892 #[derive(Clone)]
893 struct ARCRW<'a>(Arc<RwLock<ARCRWData<'a>>>);
894
895 impl<'a> Named for ARCRW<'a> {
896     fn new(name: &'static str) -> Self {
897         ARCRW(Arc::new(RwLock::new(ARCRWData {
898             name: name, mark: Cell::new(0), children: (None, None), })))
899     }
900     fn name(&self) -> &str { self.0.read().unwrap().name }
901 }
902
903 impl<'a> Marked<u32> for ARCRW<'a> {
904     fn mark(&self) -> u32 { self.0.read().unwrap().mark.get() }
905     fn set_mark(&self, mark: u32) { self.0.read().unwrap().mark.set(mark); }
906 }
907
908 impl<'a> Children<'a> for ARCRW<'a> {
909     fn count_children(&self) -> usize { 2 }
910     fn descend_one_child<C>(&self, context: &mut C, index: usize)
911         where C: Context + PrePost<Self>, Self: Sized
912     {
913         let children = &self.0.read().unwrap().children;
914         match index {
915             0 => if let Some(ref child) = children.0 {
916                 child.descend_into_self(context);
917             },
918             1 => if let Some(ref child) = children.1 {
919                 child.descend_into_self(context);
920             },
921             _ => panic!("bad children!"),
922         }
923     }
924 }
925
926 trait Context {
927     fn next_index(&mut self, len: usize) -> usize;
928     fn should_act(&self) -> bool;
929     fn increase_visited(&mut self);
930     fn increase_skipped(&mut self);
931     fn increase_depth(&mut self);
932     fn decrease_depth(&mut self);
933 }
934
935 trait PrePost<T> {
936     fn pre(&mut self, _: &T);
937     fn post(&mut self, _: &T);
938     fn hit_limit(&mut self, _: &T);
939 }
940
941 trait Children<'a> {
942     fn count_children(&self) -> usize;
943     fn descend_one_child<C>(&self, context: &mut C, index: usize)
944         where C: Context + PrePost<Self>, Self: Sized;
945
946     fn next_child<C>(&self, context: &mut C)
947         where C: Context + PrePost<Self>, Self: Sized
948     {
949         let index = context.next_index(self.count_children());
950         self.descend_one_child(context, index);
951     }
952
953     fn descend_into_self<C>(&self, context: &mut C)
954         where C: Context + PrePost<Self>, Self: Sized
955     {
956         context.pre(self);
957         if context.should_act() {
958             context.increase_visited();
959             context.increase_depth();
960             self.next_child(context);
961             context.decrease_depth();
962         } else {
963             context.hit_limit(self);
964             context.increase_skipped();
965         }
966         context.post(self);
967     }
968
969     fn descend<'b, C>(&self, c: &Cell<Option<&'b Self>>, context: &mut C)
970         where C: Context + PrePost<Self>, Self: Sized
971     {
972         if let Some(r) = c.get() {
973             r.descend_into_self(context);
974         }
975     }
976 }
977
978 impl<'a> Children<'a> for S<'a> {
979     fn count_children(&self) -> usize { 1 }
980     fn descend_one_child<C>(&self, context: &mut C, _: usize)
981         where C: Context + PrePost<Self>, Self: Sized {
982             self.descend(&self.next, context);
983         }
984 }
985
986 impl<'a> Children<'a> for S2<'a> {
987     fn count_children(&self) -> usize { 2 }
988     fn descend_one_child<C>(&self, context: &mut C, index: usize)
989         where C: Context + PrePost<Self>, Self: Sized
990     {
991         let children = self.next.get();
992         let child = match index {
993             0 => if let Some(child) = children.0 { child } else { return; },
994             1 => if let Some(child) = children.1 { child } else { return; },
995             _ => panic!("bad children"),
996         };
997         // println!("S2 {} descending into child {} at index {}", self.name, child.name, index);
998         child.descend_into_self(context);
999     }
1000 }
1001
1002 impl<'a> Children<'a> for V<'a> {
1003     fn count_children(&self) -> usize { self.contents.len() }
1004     fn descend_one_child<C>(&self, context: &mut C, index: usize)
1005         where C: Context + PrePost<Self>, Self: Sized
1006     {
1007         if let Some(child) = self.contents[index].get() {
1008             child.descend_into_self(context);
1009         }
1010     }
1011 }
1012
1013 impl<'a> Children<'a> for H<'a> {
1014     fn count_children(&self) -> usize { 1 }
1015     fn descend_one_child<C>(&self, context: &mut C, _: usize)
1016         where C: Context + PrePost<Self>, Self: Sized
1017     {
1018         self.descend(&self.next, context);
1019     }
1020 }
1021
1022 impl<'a> Children<'a> for HM<'a> {
1023     fn count_children(&self) -> usize {
1024         if let Some(m) = self.contents.get() { 2 * m.iter().count() } else { 0 }
1025     }
1026     fn descend_one_child<C>(&self, context: &mut C, index: usize)
1027         where C: Context + PrePost<Self>, Self: Sized
1028     {
1029         if let Some(ref hm) = self.contents.get() {
1030             for (k, v) in hm.iter().nth(index / 2) {
1031                 [k, v][index % 2].descend_into_self(context);
1032             }
1033         }
1034     }
1035 }
1036
1037 impl<'a> Children<'a> for VD<'a> {
1038     fn count_children(&self) -> usize {
1039         if let Some(d) = self.contents.get() { d.iter().count() } else { 0 }
1040     }
1041     fn descend_one_child<C>(&self, context: &mut C, index: usize)
1042         where C: Context + PrePost<Self>, Self: Sized
1043     {
1044         if let Some(ref vd) = self.contents.get() {
1045             for r in vd.iter().nth(index) {
1046                 r.descend_into_self(context);
1047             }
1048         }
1049     }
1050 }
1051
1052 impl<'a> Children<'a> for VM<'a> {
1053     fn count_children(&self) -> usize {
1054         if let Some(m) = self.contents.get() { m.iter().count() } else { 0 }
1055     }
1056     fn descend_one_child<C>(&self, context: &mut C, index: usize)
1057         where C: Context + PrePost<VM<'a>>
1058     {
1059         if let Some(ref vd) = self.contents.get() {
1060             for (_idx, r) in vd.iter().nth(index) {
1061                 r.descend_into_self(context);
1062             }
1063         }
1064     }
1065 }
1066
1067 impl<'a> Children<'a> for LL<'a> {
1068     fn count_children(&self) -> usize {
1069         if let Some(l) = self.contents.get() { l.iter().count() } else { 0 }
1070     }
1071     fn descend_one_child<C>(&self, context: &mut C, index: usize)
1072         where C: Context + PrePost<LL<'a>>
1073     {
1074         if let Some(ref ll) = self.contents.get() {
1075             for r in ll.iter().nth(index) {
1076                 r.descend_into_self(context);
1077             }
1078         }
1079     }
1080 }
1081
1082 impl<'a> Children<'a> for BH<'a> {
1083     fn count_children(&self) -> usize {
1084         if let Some(h) = self.contents.get() { h.iter().count() } else { 0 }
1085     }
1086     fn descend_one_child<C>(&self, context: &mut C, index: usize)
1087         where C: Context + PrePost<BH<'a>>
1088     {
1089         if let Some(ref bh) = self.contents.get() {
1090             for r in bh.iter().nth(index) {
1091                 r.descend_into_self(context);
1092             }
1093         }
1094     }
1095 }
1096
1097 impl<'a> Children<'a> for BTM<'a> {
1098     fn count_children(&self) -> usize {
1099         if let Some(m) = self.contents.get() { 2 * m.iter().count() } else { 0 }
1100     }
1101     fn descend_one_child<C>(&self, context: &mut C, index: usize)
1102         where C: Context + PrePost<BTM<'a>>
1103     {
1104         if let Some(ref bh) = self.contents.get() {
1105             for (k, v) in bh.iter().nth(index / 2) {
1106                 [k, v][index % 2].descend_into_self(context);
1107             }
1108         }
1109     }
1110 }
1111
1112 impl<'a> Children<'a> for BTS<'a> {
1113     fn count_children(&self) -> usize {
1114         if let Some(s) = self.contents.get() { s.iter().count() } else { 0 }
1115     }
1116     fn descend_one_child<C>(&self, context: &mut C, index: usize)
1117         where C: Context + PrePost<BTS<'a>>
1118     {
1119         if let Some(ref bh) = self.contents.get() {
1120             for r in bh.iter().nth(index) {
1121                 r.descend_into_self(context);
1122             }
1123         }
1124     }
1125 }
1126
1127 #[derive(Copy, Clone)]
1128 struct ContextData {
1129     curr_depth: usize,
1130     max_depth: usize,
1131     visited: usize,
1132     max_visits: usize,
1133     skipped: usize,
1134     curr_mark: u32,
1135     saw_prev_marked: bool,
1136     control_bits: u64,
1137 }
1138
1139 impl Context for ContextData {
1140     fn next_index(&mut self, len: usize) -> usize {
1141         if len < 2 { return 0; }
1142         let mut pow2 = len.next_power_of_two();
1143         let _pow2_orig = pow2;
1144         let mut idx = 0;
1145         let mut bits = self.control_bits;
1146         while pow2 > 1 {
1147             idx = (idx << 1) | (bits & 1) as usize;
1148             bits = bits >> 1;
1149             pow2 = pow2 >> 1;
1150         }
1151         idx = idx % len;
1152         // println!("next_index({} [{:b}]) says {}, pre(bits): {:b} post(bits): {:b}",
1153         //          len, _pow2_orig, idx, self.control_bits, bits);
1154         self.control_bits = bits;
1155         return idx;
1156     }
1157     fn should_act(&self) -> bool {
1158         self.curr_depth < self.max_depth && self.visited < self.max_visits
1159     }
1160     fn increase_visited(&mut self) { self.visited += 1; }
1161     fn increase_skipped(&mut self) { self.skipped += 1; }
1162     fn increase_depth(&mut self) {  self.curr_depth += 1; }
1163     fn decrease_depth(&mut self) {  self.curr_depth -= 1; }
1164 }
1165
1166 impl<T:Named+Marked<u32>> PrePost<T> for ContextData {
1167     fn pre(&mut self, t: &T) {
1168         for _ in 0..self.curr_depth {
1169             if PRINT { print!(" "); }
1170         }
1171         if PRINT { println!("prev {}", t.name()); }
1172         if t.mark() == self.curr_mark {
1173             for _ in 0..self.curr_depth {
1174                 if PRINT { print!(" "); }
1175             }
1176             if PRINT { println!("(probably previously marked)"); }
1177             self.saw_prev_marked = true;
1178         }
1179         t.set_mark(self.curr_mark);
1180     }
1181     fn post(&mut self, t: &T) {
1182         for _ in 0..self.curr_depth {
1183             if PRINT { print!(" "); }
1184         }
1185         if PRINT { println!("post {}", t.name()); }
1186     }
1187     fn hit_limit(&mut self, t: &T) {
1188         for _ in 0..self.curr_depth {
1189             if PRINT { print!(" "); }
1190         }
1191         if PRINT { println!("LIMIT {}", t.name()); }
1192     }
1193 }