* Created by: Leslie Sanford
* Last modified: 02/23/2005
* Contact: firstname.lastname@example.org
/// Provides functionality for enumerating a RandomAccessList.
internal class RalEnumerator : IEnumerator
#region Enumerator Members
#region Instance Fields
// The object at the current position.
private object current = null;
// The current index position.
private int index;
// For storing and traversing the nodes in the tree.
private System.Collections.Stack treeStack = new System.Collections.Stack();
// The first top node in the list.
private RalTopNode head;
// The current top node in the list.
private RalTopNode currentTopNode;
// The number of nodes in the list.
private int count;
/// Initializes a new instance of the Enumerator with the specified
/// head of the list and the number of nodes in the list.
/// <param name="head">
/// The head of the list.
/// <param name="count">
/// The number of nodes in the list.
public RalEnumerator(RalTopNode head, int count)
this.head = head;
this.count = count;
if(count > 0)
Debug.Assert(head != null);
#region IEnumerator Members
/// Sets the enumerator to its initial position, which is before
/// the first element in the random access list.
public void Reset()
index = -1;
currentTopNode = head;
// If the list is not empty.
if(count > 0)
// Push the first node in the list onto the stack.
/// Gets the current element in the random access list.
/// <exception cref="InvalidOperationException">
/// The enumerator is positioned before the first element in the
/// random access list or after the last element.
public object Current
if(index < 0 || index >= count)
throw new InvalidOperationException(
"The enumerator is positioned before the first " +
"element of the collection or after the last element.");
/// Advances the enumerator to the next element in the random access
/// <b>true</b> if the enumerator was successfully advanced to the
/// next element; <b>false</b> if the enumerator has passed the end
/// of the collection.
public bool MoveNext()
// Move index to the next position.
// If the index has moved beyond the end of the list, return false.
if(index >= count)
// Get the node at the top of the stack.
currentNode = (RalTreeNode)treeStack.Peek();
// Get the value at the top of the stack.
current = currentNode.Value;
// If there are still left children to traverse.
if(currentNode.LeftChild != null)
// If the left child is not null, the right child should not be
// null either.
Debug.Assert(currentNode.RightChild != null);
// Push left child onto stack.
// Else the bottom of the tree has been reached.
// If the left child is null, the right child should be null,
Debug.Assert(currentNode.RightChild == null);
// Move back up in the tree to the parent node.
// Whild the stack is not empty.
while(treeStack.Count > 0)
// Get the previous node.
previousNode = (RalTreeNode)treeStack.Peek();
// If the bottom of the left tree has been reached.
if(currentNode == previousNode.LeftChild)
// Push the right child onto the stack so that the
// right tree will now be traversed.
// Else the bottom of the right tree has been reached.
// Keep track of the current node.
currentNode = previousNode;
// Pop the stack to move back up the tree.
// If the stack is empty.
if(treeStack.Count == 0)
// Move to the next tree in the list.
currentTopNode = currentTopNode.NextNode;
// If the end of the list has not yet been reached.
if(currentTopNode != null)
// Begin with the next tree.
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The MIT License