Mercury library  1.0
Translation of CFG grammars into an object model (Bachelor's thesis).
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
TreeEnumerators.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 
5 namespace Mercury.Nucleus.Trees
6 {
7 //=============================================================================
8 // DFSTreeEnumerator
9 //=============================================================================
10 
13  public class DFSTreeEnumerator<T> : IEnumerator<T>, IEnumerator
14  {
15  //--[ Private fields ]-----------------------------------------------
16 
17  private bool end; // past the end
18  private Tree<T> root; // root node
19  private Stack<Tree<T>> nodes; // stack of nodes
20 
21  //--[ Constructors ]-------------------------------------------------
22 
29  {
30  this.end = false;
31  this.root = root;
32  this.nodes = new Stack<Tree<T>>();
33  }
34 
35  //--[ Properties ]---------------------------------------------------
36 
41  public T Current { get { return nodes.Peek().Value; } }
42 
47  object IEnumerator.Current { get { return nodes.Peek().Value; } }
48 
49  //--[ Interface implementation ]-------------------------------------
50 
51  #region [ IEnumerator ]
52 
53  public void Dispose()
54  {
55  nodes.Clear();
56  return;
57  }
58 
59  public bool MoveNext()
60  {
61  if (end) throw new InvalidOperationException();
62 
63  if (nodes.Count == 0)
64  { nodes.Push(root); return true; }
65 
66  var tree = nodes.Pop();
67 
68  for (int ix = tree.Children.Count - 1; ix >= 0; --ix)
69  nodes.Push(tree[ix]);
70 
71  return !(end = (nodes.Count == 0));
72  }
73 
74  public void Reset()
75  {
76  nodes.Clear();
77  end = false;
78  }
79 
80  #endregion
81 
82  }
83 
84 //=============================================================================
85 // BFSTreeEnumerator
86 //=============================================================================
87 
90  public class BFSTreeEnumerator<T> : IEnumerator<T>, IEnumerator
91  {
92  //--[ Private fields ]-----------------------------------------------
93 
94  private bool end; // past the end
95  private Tree<T> root; // root node
96  private Queue<Tree<T>> queue; // queue of nodes to explore
97 
98  //--[ Constructors ]-------------------------------------------------
99 
106  {
107  this.end = false;
108  this.root = root;
109  this.queue = new Queue<Tree<T>>();
110  }
111 
112  //--[ Properties ]---------------------------------------------------
113 
118  public T Current { get { return queue.Peek().Value; } }
119 
124  object IEnumerator.Current { get { return queue.Peek().Value; } }
125 
126  //--[ Interface methods ]--------------------------------------------
127 
128  #region [ IEnumerator ]
129 
130  public void Dispose()
131  {
132  queue.Clear();
133  return;
134  }
135 
136  public bool MoveNext()
137  {
138  if (end) throw new InvalidOperationException();
139 
140  if (queue.Count == 0)
141  { queue.Enqueue(root); return true; }
142 
143  Tree<T> tree = queue.Dequeue();
144  for (int ix = 0; ix < tree.Children.Count; ++ix)
145  queue.Enqueue(tree[ix]);
146 
147  return !(end = (queue.Count == 0));
148  }
149 
150  public void Reset()
151  {
152  end = false;
153  queue.Clear();
154  }
155 
156  #endregion
157 
158  }
159 }
DFSTreeEnumerator(Tree< T > root)
Creates a new instance of the DFSTreeEnumerator{T} class.
BFSTreeEnumerator(Tree< T > root)
Creates a new instance of the BFSTreeEnumerator{T} class.
A non-balanced tree implementation that can be referenced from many other trees. Because of that...
Definition: Tree.cs:30