Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the RalTopNode with the specified 
        /// root of the tree this node represents and the next top node in the
        /// list.
        /// </summary>
        /// <param name="root">
        /// The root node of the tree this top node represents.
        /// </param>
        /// <param name="nextNode">
        /// The next top node in the list.
        /// </param>
        public RalTopNode(RalTreeNode root, RalTopNode nextNode)
        {
            Debug.Assert(root != null);

            this.root = root;
            this.nextNode = nextNode;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the RalTopNode with the specified
        /// root of the tree this node represents and the next top node in the
        /// list.
        /// </summary>
        /// <param name="root">
        /// The root node of the tree this top node represents.
        /// </param>
        /// <param name="nextNode">
        /// The next top node in the list.
        /// </param>
        public RalTopNode(RalTreeNode root, RalTopNode nextNode)
        {
            Debug.Assert(root != null);

            this.root     = root;
            this.nextNode = nextNode;
        }
        /// <summary>
        /// Initializes a new instance of the Enumerator with the specified
        /// head of the list and the number of nodes in the list.
        /// </summary>
        /// <param name="head">
        /// The head of the list.
        /// </param>
        /// <param name="count">
        /// The number of nodes in the list.
        /// </param>
        public RalEnumerator(RalTopNode head, int count)
        {
            this.head  = head;
            this.count = count;

            if (count > 0)
            {
                Debug.Assert(head != null);
            }

            Reset();
        }
        /// <summary>
        /// Initializes a new instance of the Enumerator with the specified 
        /// head of the list and the number of nodes in the list.
        /// </summary>
        /// <param name="head">
        /// The head of the list.
        /// </param>
        /// <param name="count">
        /// The number of nodes in the list.
        /// </param>
        public RalEnumerator(RalTopNode head, int count)
        {
            this.head = head;
            this.count = count;

            if(count > 0)
            {
                Debug.Assert(head != null);
            }
           
            Reset();
        }
        /// <summary>
        /// Sets the enumerator to its initial position, which is before
        /// the first element in the random access list.
        /// </summary>
        public void Reset()
        {
            index          = -1;
            currentTopNode = head;
            treeStack.Clear();

            //  If the list is not empty.
            if (count > 0)
            {
                // Push the first node in the list onto the stack.
                treeStack.Push(head.Root);
            }
        }
        /// <summary>
        /// Sets the enumerator to its initial position, which is before 
        /// the first element in the random access list.
        /// </summary>
        public void Reset()
        {
            index = -1;
            currentTopNode = head;
            treeStack.Clear();

            //  If the list is not empty.
            if(count > 0)
            {
                // Push the first node in the list onto the stack.
                treeStack.Push(head.Root);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Gets the value at the specified element in the random access list.
        /// </summary>
        /// <param name="index">
        /// An integer that represents the position of the random access list
        /// element to get.
        /// </param>
        /// <returns>
        /// The value at the specified position in the random access list.
        /// </returns>
        public object GetValue(int index)
        {
            int        i           = index;
            RalTopNode currentNode = this;

            // Find the top node containing the specified element.
            while (i >= currentNode.Root.Count)
            {
                i          -= currentNode.Root.Count;
                currentNode = currentNode.NextNode;

                Debug.Assert(currentNode != null);
            }

            return(currentNode.Root.GetValue(i));
        }
        /// <summary>
        /// Initialize an instance of the Array class with the specified array 
        /// length.
        /// </summary>
        /// <param name="length">
        /// The length of the array.
        /// </param>
        public Array(int length)
        {
            // Precondition.
            if(length < 0)
            {
                throw new ArgumentOutOfRangeException("length", length,
                    "Array length out of range.");
            }

            this.length = length;

            int n = length;
            int exponent;
            int count;

            head = null;

            /*
             * The following algorithm creates the trees for the array. The
             * trees have the form of a random access list.
             */

            // While there are still nodes to create.
            while(n > 0)
            {
                // Get the log based 2 of the number of nodes.
                exponent = (int)Math.Log(n, 2);

                // Get the number of nodes for each subtree.
                count = ((int)Math.Pow(2, exponent) - 1) / 2;

                // Create the top node representing the subtree.
                head = new RalTopNode(
                    new RalTreeNode(
                        null, 
                        CreateSubTree(count), 
                        CreateSubTree(count)),
                    head);

                // Get the remaining number of nodes to create.
                n -= head.Root.Count;
            }            
        }
Beispiel #9
0
        /// <summary>
        /// Initialize an instance of the Array class with the specified array
        /// length.
        /// </summary>
        /// <param name="length">
        /// The length of the array.
        /// </param>
        public Array(int length)
        {
            // Precondition.
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length", length,
                                                      "Array length out of range.");
            }

            this.length = length;

            int n = length;
            int exponent;
            int count;

            head = null;

            /*
             * The following algorithm creates the trees for the array. The
             * trees have the form of a random access list.
             */

            // While there are still nodes to create.
            while (n > 0)
            {
                // Get the log based 2 of the number of nodes.
                exponent = (int)Math.Log(n, 2);

                // Get the number of nodes for each subtree.
                count = ((int)Math.Pow(2, exponent) - 1) / 2;

                // Create the top node representing the subtree.
                head = new RalTopNode(
                    new RalTreeNode(
                        null,
                        CreateSubTree(count),
                        CreateSubTree(count)),
                    head);

                // Get the remaining number of nodes to create.
                n -= head.Root.Count;
            }
        }
Beispiel #10
0
        /// <summary>
        /// Sets the specified element in the current random access list to the
        /// specified value.
        /// </summary>
        /// <param name="value">
        /// The new value for the specified element.
        /// </param>
        /// <param name="index">
        /// An integer that represents the position of the random access list
        /// element to set.
        /// </param>
        /// <returns>
        /// A new random access list top node with the element at the specified
        /// position set to the specified value.
        /// </returns>
        public RalTopNode SetValue(object value, int index)
        {
            RalTopNode result;

            // If the element is in the tree represented by the current top
            // node.
            if (index < Root.Count)
            {
                // Descend into the tree.
                result = new RalTopNode(
                    root.SetValue(value, index),
                    NextNode);
            }
            // Else the element is further along in the list.
            else
            {
                // Move to the next top node.
                result = new RalTopNode(
                    root,
                    NextNode.SetValue(value, index - Root.Count));
            }

            return(result);
        }
Beispiel #11
0
        /// <summary>
        /// Sets the specified element in the current random access list to the 
        /// specified value.
        /// </summary>
        /// <param name="value">
        /// The new value for the specified element. 
        /// </param>
        /// <param name="index">
        /// An integer that represents the position of the random access list  
        /// element to set. 
        /// </param>
        /// <returns>
        /// A new random access list top node with the element at the specified 
        /// position set to the specified value.
        /// </returns>
        public RalTopNode SetValue(object value, int index)
        {
            RalTopNode result;

            // If the element is in the tree represented by the current top
            // node.
            if(index < Root.Count)
            {
                // Descend into the tree.
                result = new RalTopNode(
                    root.SetValue(value, index),
                    NextNode);
            }
            // Else the element is further along in the list.
            else
            {
                // Move to the next top node.
                result = new RalTopNode(
                    root,
                    NextNode.SetValue(value, index - Root.Count));
            }

            return result;
        }
        /// <summary>
        /// Initializes a new instance of the RandomAccessList class.
        /// </summary>
		public RandomAccessList()
		{
            count = 0;
            first = null;
		}
        /// <summary>
        /// Advances the enumerator to the next element in the random access 
        /// list.
        /// </summary>
        /// <returns>
        /// <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.
        /// </returns>
        public bool MoveNext()
        {
            // Move index to the next position.
            index++;

            // If the index has moved beyond the end of the list, return false.
            if(index >= count)
                return false;

            RalTreeNode currentNode; 

            // 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.
                treeStack.Push(currentNode.LeftChild); 
            }
            // Else the bottom of the tree has been reached.
            else
            {
                // If the left child is null, the right child should be null, 
                // too.
                Debug.Assert(currentNode.RightChild == null);

                // Move back up in the tree to the parent node.
                treeStack.Pop();
                    
                RalTreeNode previousNode;

                // 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.
                        treeStack.Push(previousNode.RightChild);

                        // Finished.
                        break;
                    }
                    // Else the bottom of the right tree has been reached.
                    else
                    {
                        // Keep track of the current node.
                        currentNode = previousNode;

                        // Pop the stack to move back up the tree.
                        treeStack.Pop();
                    }
                }

                // 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.
                        treeStack.Push(currentTopNode.Root);
                    }
                }                    
            }

            return true;
        }
 /// <summary>
 /// Initializes a new instance of the RandomAccessList class with the
 /// specified first top node and the number of elements in the list.
 /// </summary>
 /// <param name="first">
 /// The first top node in the list.
 /// </param>
 /// <param name="count">
 /// The number of nodes in the list.
 /// </param>
 private RandomAccessList(RalTopNode first, int count)
 {
     this.first = first;
     this.count = count;
 }
 /// <summary>
 /// Initializes a new instance of the RandomAccessList class.
 /// </summary>
 public RandomAccessList()
 {
     count = 0;
     first = null;
 }
        /// <summary>
        /// Advances the enumerator to the next element in the random access
        /// list.
        /// </summary>
        /// <returns>
        /// <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.
        /// </returns>
        public bool MoveNext()
        {
            // Move index to the next position.
            index++;

            // If the index has moved beyond the end of the list, return false.
            if (index >= count)
            {
                return(false);
            }

            RalTreeNode currentNode;

            // 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.
                treeStack.Push(currentNode.LeftChild);
            }
            // Else the bottom of the tree has been reached.
            else
            {
                // If the left child is null, the right child should be null,
                // too.
                Debug.Assert(currentNode.RightChild == null);

                // Move back up in the tree to the parent node.
                treeStack.Pop();

                RalTreeNode previousNode;

                // 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.
                        treeStack.Push(previousNode.RightChild);

                        // Finished.
                        break;
                    }
                    // Else the bottom of the right tree has been reached.
                    else
                    {
                        // Keep track of the current node.
                        currentNode = previousNode;

                        // Pop the stack to move back up the tree.
                        treeStack.Pop();
                    }
                }

                // 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.
                        treeStack.Push(currentTopNode.Root);
                    }
                }
            }

            return(true);
        }
 /// <summary>
 /// Initializes a new instance of the Array class with the specified 
 /// head of the random access list and the length of the array.
 /// </summary>
 /// <param name="head">
 /// The head of the random access list.
 /// </param>
 /// <param name="length">
 /// The length of the array.
 /// </param>
 private Array(RalTopNode head, int length)
 {
     this.head = head;
     this.length = length;            
 }
 /// <summary>
 /// Initializes a new instance of the RandomAccessList class with the
 /// specified first top node and the number of elements in the list.
 /// </summary>
 /// <param name="first">
 /// The first top node in the list.
 /// </param>
 /// <param name="count">
 /// The number of nodes in the list.
 /// </param>
 private RandomAccessList(RalTopNode first, int count)
 {
     this.first = first;
     this.count = count;
 }
Beispiel #19
0
 /// <summary>
 /// Initializes a new instance of the Array class with the specified
 /// head of the random access list and the length of the array.
 /// </summary>
 /// <param name="head">
 /// The head of the random access list.
 /// </param>
 /// <param name="length">
 /// The length of the array.
 /// </param>
 private Array(RalTopNode head, int length)
 {
     this.head   = head;
     this.length = length;
 }