示例#1
0
            internal override bool BreadthFirstTreeWalk(TreeWalkPredicate <T> action)
            {
                VersionCheck();

                if (root == null)
                {
                    return(true);
                }

                Queue <Node> processQueue = new Queue <Node>();

                processQueue.Enqueue(root);
                Node current;

                while (processQueue.Count != 0)
                {
                    current = processQueue.Dequeue();
                    if (IsWithinRange(current.Item) && !action(current))
                    {
                        return(false);
                    }
                    if (current.Left != null && (!_lBoundActive || Comparer.Compare(_min, current.Item) < 0))
                    {
                        processQueue.Enqueue(current.Left);
                    }
                    if (current.Right != null && (!_uBoundActive || Comparer.Compare(_max, current.Item) > 0))
                    {
                        processQueue.Enqueue(current.Right);
                    }
                }
                return(true);
            }
示例#2
0
        public bool ContainsValue(TValue value)
        {
            TreeWalkPredicate <KeyValuePair <TKey, TValue> > action = null;
            bool found = false;

            if (value == null)
            {
                if (action == null)
                {
                    action = delegate(SortedSet <KeyValuePair <TKey, TValue> > .Node node) {
                        if (node.Item.Value == null)
                        {
                            found = true;
                            return(false);
                        }
                        return(true);
                    };
                }
                this._set.InOrderTreeWalk(action);
            }
            else
            {
                EqualityComparer <TValue> valueComparer = EqualityComparer <TValue> .Default;
                this._set.InOrderTreeWalk(delegate(SortedSet <KeyValuePair <TKey, TValue> > .Node node) {
                    if (valueComparer.Equals(node.Item.Value, value))
                    {
                        found = true;
                        return(false);
                    }
                    return(true);
                });
            }
            return(found);
        }
示例#3
0
            internal override bool InOrderTreeWalk(TreeWalkPredicate <T> action)
            {
                VersionCheck();

                if (root == null)
                {
                    return(true);
                }

                // The maximum height of a red-black tree is 2*lg(n+1).
                // See page 264 of "Introduction to algorithms" by Thomas H. Cormen
                Stack <Node> stack   = new Stack <Node>(2 * (int)SortedSet <T> .Log2(count + 1)); // this is not exactly right if count is out of date, but the stack can grow
                Node         current = root;

                while (current != null)
                {
                    if (IsWithinRange(current.Item))
                    {
                        stack.Push(current);
                        current = current.Left;
                    }
                    else if (_lBoundActive && Comparer.Compare(_min, current.Item) > 0)
                    {
                        current = current.Right;
                    }
                    else
                    {
                        current = current.Left;
                    }
                }

                while (stack.Count != 0)
                {
                    current = stack.Pop();
                    if (!action(current))
                    {
                        return(false);
                    }

                    Node node = current.Right;
                    while (node != null)
                    {
                        if (IsWithinRange(node.Item))
                        {
                            stack.Push(node);
                            node = node.Left;
                        }
                        else if (_lBoundActive && Comparer.Compare(_min, node.Item) > 0)
                        {
                            node = node.Right;
                        }
                        else
                        {
                            node = node.Left;
                        }
                    }
                }
                return(true);
            }
示例#4
0
 void ICollection.CopyTo(Array array, int index)
 {
     if (array == null)
     {
         System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.array);
     }
     if (array.Rank != 1)
     {
         System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Arg_RankMultiDimNotSupported);
     }
     if (array.GetLowerBound(0) != 0)
     {
         System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Arg_NonZeroLowerBound);
     }
     if (index < 0)
     {
         System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.arrayIndex, System.ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
     }
     if ((array.Length - index) < this.dictionary.Count)
     {
         System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Arg_ArrayPlusOffTooSmall);
     }
     TValue[] localArray = array as TValue[];
     if (localArray != null)
     {
         this.CopyTo(localArray, index);
     }
     else
     {
         TreeWalkPredicate <KeyValuePair <TKey, TValue> > action = null;
         object[] objects = (object[])array;
         if (objects == null)
         {
             System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Argument_InvalidArrayType);
         }
         try
         {
             if (action == null)
             {
                 action = delegate(SortedSet <KeyValuePair <TKey, TValue> > .Node node) {
                     objects[index++] = node.Item.Value;
                     return(true);
                 };
             }
             this.dictionary._set.InOrderTreeWalk(action);
         }
         catch (ArrayTypeMismatchException)
         {
             System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Argument_InvalidArrayType);
         }
     }
 }