Exemplo n.º 1
0
        void ICollection.CopyTo(Array array, int index)
        {
            if (array == null)
            {
                Thrower.ThrowArgumentNullException(ExceptionArgument.array);
            }

            int length = array.Length;

            if ((index < 0) || (index > length))
            {
                Thrower.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }

            int num2 = (length - index);

            if (num2 < _size)
            {
                Thrower.ThrowArgumentException(ExceptionResource.Argument_InvalidOfLen);
            }

            // If the head is above tha tail we need to do two copies. The first copy will get
            // the elements between the head and the end of the queue and the second copy will get
            // the elements between the top of the queue and the tail.
            if (_tail < _head)
            {
                int num3 = _array.Length - _head;
                Array.Copy(_array, _head, array, index, num3);
                Array.Copy(_array, 0, array, index + num3, _tail);
            }
            else
            {
                Array.Copy(_array, _head, array, index, _head - _tail);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance_ onf the CacheEntry class by using the specified item key and value and
        /// expiration police.
        /// </summary>
        /// <param name="key">The cache key used to reference the item.</param>
        /// <param name="value">The item to be added to the cache.</param>
        /// <param name="utcAbsoluteExpiration">The time at which the added object expires and is removed
        /// from the cache. If the sliding expiration will be used, the <paramref name="utcAbsoluteExpiration"/>
        /// parameter must be <see cref="CacheEntry.NoAbsoluteExpiration"/>
        /// </param>
        /// <param name="slidingExpiration">The interval between the time the added object was last accessed
        /// and the time at which that object expires. If the <paramref name="utcAbsoluteExpiration"/> is defined
        /// the <paramref name="slidingExpiration"/> parametr must be <see cref="CacheEntry.NoSlidingExpiration"/></param>
        internal CacheEntry(string key, object value, DateTime utcAbsoluteExpiration, TimeSpan slidingExpiration)
        {
            if (slidingExpiration < TimeSpan.Zero || OneYear < slidingExpiration)
            {
                throw new ArgumentNullException("value");
            }

            if (utcAbsoluteExpiration != NoAbsoluteExpiration && slidingExpiration != NoSlidingExpiration)
            {
                Thrower.ThrowArgumentException(ExceptionResource.Caching_Invalid_expiration_combination);
            }

            _value             = value;
            _utcCreated        = DateTime.UtcNow;
            _slidingExpiration = slidingExpiration;
            _utcExpires        = (_slidingExpiration > TimeSpan.Zero) ? _utcCreated + _slidingExpiration : utcAbsoluteExpiration;
            _key = key;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Copies the elements of the <see cref="AndersonTree{TKey,TValue}"/> to
        /// an <see cref="Array"/>, starting at a particular index.
        /// </summary>
        /// <param name="array">
        /// The one-dimensional <see cref="Array"/> that is the destination of the
        /// elements copied from the <see cref="AndersonTree{TKey,TValue}"/>.
        /// The array must have zero-based indexing.
        /// </param>
        /// <param name="index">
        /// The zero-based index in array at which copying begins.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="index"/> is less than 0.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="array"/>is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="array"/> is multidimensional.-or-
        /// <paramref name="index"/> is equal to or greater than the length of the
        /// array.-or-The number of elements in the source
        /// <see cref="AndersonTree{TKey,TValue}"/>is greater than the
        /// available space from <paramref name="index"/> to the end of the
        /// destination array.-or- <see cref=" KeyValuePair{TKey,TValue}"/> cannot
        /// be cast automatically to the type of the destination array.
        /// </exception>
        public void CopyTo(KeyValuePair <TKey, TValue>[] array, int index)
        {
            if ((index < 0) || (index > array.Length))
            {
                Thrower.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
            }

            if ((array.Length - index) < count_)
            {
                Thrower.ThrowArgumentException(
                    ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }

            if (count_ == 0)
            {
                return;
            }

            InOrderTreeWalk(delegate(AndersonTreeNode <TKey, TValue> node)
            {
                array[index++] = node.ToKeyValuePair();
                return(true);
            });
        }
Exemplo n.º 4
0
 public void ThrowArgumentException()
 {
     Thrower.ThrowArgumentException(ExceptionResource.Argument_Empty);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Inserts an item to the AndersonTree
        /// </summary>
        /// <param name="key">
        /// The key of the value to insert into the tree.
        /// </param>
        /// <param name="value">
        /// The value to insert in the tree.
        /// </param>
        /// <param name="add">
        /// A value indicating when the item will be added or modified.
        /// </param>
        void Insert(TKey key, TValue value, bool add)
        {
            if (!key_is_value_type_ && key == null)
            {
                Thrower.ThrowArgumentNullException(ExceptionArgument.key);
            }

            // empty tree
            if (root_.Level == 0)
            {
                root_ = new AndersonTreeNode <TKey, TValue>(key, value, sentinel_);
                count_++;
                return;
            }

            int cmp, dir;
            List <AndersonTreeNode <TKey, TValue> > path =
                new List <AndersonTreeNode <TKey, TValue> >(count_);

            AndersonTreeNode <TKey, TValue> node = root_;

            // Find the place to insert the item
            //  - If the item is found and we trying to add a new one,
            //    throw an exception - no duplicate items allowed.
            //  - If a leaf is reached, insert the item in the correct place.
            //  - Else, traverse the tree further.
            while (true)
            {
                path.Add(node);

                cmp = comparer_.Compare(key, node.Key);
                if (cmp == 0)
                {
                    if (add)
                    {
                        Thrower.ThrowArgumentException(
                            ExceptionResource.Argument_AddingDuplicate);
                    }

                    if (node.Level != 0)
                    {
                        node.Value = value;
                    }

                    return;
                }

                dir = (cmp < 0) ? 0 : 1;

                if (node.childs[dir].Level == 0)
                {
                    break;
                }

                node = node.childs[dir];
            }

            // create the new node
            node.childs[dir] =
                new AndersonTreeNode <TKey, TValue>(key, value, sentinel_);

            // Walk back and rebalance
            int top = path.Count;

            while (--top >= 0)
            {
                AndersonTreeNode <TKey, TValue> n = path[top];

                // which child ?
                if (top != 0)
                {
                    dir = (path[top - 1].Right == n) ? 1 : 0;
                }

                Skew(ref n);
                Split(ref n);

                // Fix the parent
                if (top != 0)
                {
                    path[top - 1].childs[dir] = n;
                }
                else
                {
                    root_ = n;
                }
            }
            count_++;
        }