Example #1
0
        /// <summary>
        /// Inserts an object at the end of the <see cref="EndlessQueue{T}"/> and
        /// returns whether the collection was full, in which case the first item will be deleted allowing an
        /// empty space for the new item.
        /// </summary>
        /// <param name="item">
        /// The object to add to the <see cref="EndlessQueue{T}"/>.
        /// The value can be null for reference types.
        /// </param>
        /// <param name="deletedItem">
        /// When this method returns, contains the deleted value at the beginning of the collection
        /// if the collection was full; otherwise contains the type default value.
        /// </param>
        /// <returns>
        /// true if the number of items in the collection reached the maximum capacity before
        /// the operation performed, otherwise returns false.
        /// </returns>
        public bool Enqueue(T item, out T deletedItem)
        {
            bool flag = false;

            deletedItem = default(T);

            //Responding to replacING event.
            bool isReplacing = Count == Capacity;

            if ((isReplacing) && (ItemReplacing != null))
            {
                var args = new ItemReplacingEventArgs <T>(item, items[tail]);
                ItemReplacing(this, args);
                if (args.Handled)
                {
                    return(false);
                }
            }
            //
            if (tail == head && count > 0)
            {
                deletedItem = items[head];
                head        = NextIndex(head);
                flag        = true;
            }
            this.items[tail] = item;
            //Responding to replacED event.
            if (flag && ItemReplaced != null)
            {
                var oldItem = deletedItem;
                var args2   = new ItemReplacedEventArgs <T>(item, oldItem);
                ItemReplaced(this, args2);
            }
            //
            tail = NextIndex(tail);
            if (items.Length >= (count + 1))
            {
                count++;
            }
            version++;
            return(flag);
        }
Example #2
0
        /// <summary>
        /// Adds an object to the end of the
        /// <see cref="T:Academy.Collections.Generic.EndlessQueue`1"/>.
        /// </summary>
        /// <param name="item">
        /// The object to add to the
        /// <see cref="T:Academy.Collections.Generic.EndlessQueue`1"/>.
        /// The value can be null for reference types.
        /// </param>
        public void Enqueue(T item)
        {
            //Responding to replacING events.
            bool isReplacing = Count == Capacity;

            if ((isReplacing) && (ItemReplacing != null))
            {
                var args = new ItemReplacingEventArgs <T>(item, items[tail]);
                ItemReplacing(this, args);
                if (args.Handled)
                {
                    return;
                }
            }
            //
            if (tail == head && count > 0)
            {
                head = NextIndex(head);
            }
            var oldItem = items[tail];

            items[tail] = item;
            tail        = NextIndex(tail);
            //Responding to replacED event.
            if (isReplacing && ItemReplaced != null)
            {
                var args2 = new ItemReplacedEventArgs <T>(item, oldItem);
                ItemReplaced(this, args2);
            }
            //
            if (items.Length >= (count + 1))
            {
                count++;
            }
            version++;
        }