예제 #1
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);
        }
        /// <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;
        }