Exemple #1
0
        // Recursive method for getting the value at the specified position.
        private object GetValue(int index, RalTreeNode node)
        {
            object result;

            // If the position of the value to get has been reached.
            if (index == 0)
            {
                // Get the value.
                result = node.Value;
            }
            // Else the position of the value to get has not been reached.
            else
            {
                int n = node.Count / 2;

                // If the value is in the left subtree.
                if (index <= n)
                {
                    Debug.Assert(node.LeftChild != null);

                    // Descend into the left subtree.
                    result = GetValue(index - 1, node.LeftChild);
                }
                // Else the value is in the right subtree.
                else
                {
                    Debug.Assert(node.RightChild != null);

                    // Descend into the right subtree.
                    result = GetValue(index - 1 - n, node.RightChild);
                }
            }

            return(result);
        }
Exemple #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;
        }
Exemple #3
0
        // Creates subtrees within the random access list.
        private RalTreeNode CreateSubTree(int count)
        {
            RalTreeNode result = null;

            if (count > 0)
            {
                int c = count / 2;

                result = new RalTreeNode(
                    null,
                    CreateSubTree(c),
                    CreateSubTree(c));
            }

            return(result);
        }
Exemple #4
0
        // Recursive method for setting the value at the specified position.
        private RalTreeNode SetValue(object value, int index, RalTreeNode node)
        {
            RalTreeNode result;

            // If the position of the value to set has been reached.
            if (index == 0)
            {
                // Set the value.
                result = new RalTreeNode(
                    value,
                    node.LeftChild,
                    node.RightChild);
            }
            // Else if the position of the value to set has not been reached.
            else
            {
                Debug.Assert(node.LeftChild != null);

                int n = Count / 2;

                // If the value is in the left subtree.
                if (index <= n)
                {
                    // Descend into the left subtree.
                    result = new RalTreeNode(
                        node.Value,
                        node.LeftChild.SetValue(value, index - 1),
                        node.RightChild);
                }
                // Else if the value is in the right subtree.
                else
                {
                    Debug.Assert(node.RightChild != null);

                    // Descend into the right subtree.
                    result = new RalTreeNode(
                        node.Value,
                        node.LeftChild,
                        node.RightChild.SetValue(value, index - 1 - n));
                }
            }

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Initializes an instance of the RandomAccessListNode with the
        /// specified value, left child, and right child.
        /// </summary>
        /// <param name="value">
        /// The value to store in the node.
        /// </param>
        /// <param name="leftChild">
        /// The left child.
        /// </param>
        /// <param name="rightChild">
        /// The right child.
        /// </param>
        public RalTreeNode(
            object value,
            RalTreeNode leftChild,
            RalTreeNode rightChild)
        {
            this.value      = value;
            this.leftChild  = leftChild;
            this.rightChild = rightChild;

            count = 1;

            if (leftChild != null)
            {
                count += leftChild.Count * 2;

                Debug.Assert(rightChild != null);
                Debug.Assert(count == 1 + leftChild.Count + rightChild.Count);
            }
        }
Exemple #6
0
        /// <summary>
        /// Prepends a value to the random access list.
        /// </summary>
        /// <param name="value">
        /// The value to prepend to the list.
        /// </param>
        /// <returns>
        /// A new random access list with the specified value prepended to the
        /// list.
        /// </returns>
        public RandomAccessList Cons(object value)
        {
            RandomAccessList result;

            // If the list is empty, or there is only one tree in the list, or
            // the first tree is smaller than the second tree.
            if (Count == 0 ||
                first.NextNode == null ||
                first.Root.Count < first.NextNode.Root.Count)
            {
                // Create a new first node with the specified value.
                RalTreeNode newRoot = new RalTreeNode(value, null, null);

                // Create a new random access list.
                result = new RandomAccessList(
                    new RalTopNode(newRoot, first),
                    Count + 1);
            }
            // Else the first and second trees in the list are the same size.
            else
            {
                Debug.Assert(first.Root.Count == first.NextNode.Root.Count);

                // Create a new first node with the old first and second node
                // as the left and right children respectively.
                RalTreeNode newRoot = new RalTreeNode(
                    value,
                    first.Root,
                    first.NextNode.Root);

                // Create a new random access list.
                result = new RandomAccessList(
                    new RalTopNode(newRoot, first.NextNode.NextNode),
                    Count + 1);
            }

            return(result);
        }