Example #1
0
        /// <summary>
        /// Checks if an item is in the queue.
        /// </summary>
        /// <param name="item">The object to check for in the queue.</param>
        /// <returns>True if the item is found, false otherwise.</returns>
        public bool Contains(T item)
        {
            // Use the default equality comparer for the type T.
            EqualityComparer <T> comparer = EqualityComparer <T> .Default;
            QueueNode <T>        node     = _first;

            // If there are items in the queue.
            if (node != null)
            {
                if (item != null)
                {
                    do
                    {
                        // Use the equality comparer if the item isn't null.
                        if (comparer.Equals(node.Value, item))
                        {
                            return(true);
                        }
                        node = node.Next;
                    } while (node != null);
                }
                else
                {
                    do
                    {
                        if (node.Value == null)
                        {
                            return(true);
                        }
                        node = node.Next;
                    } while (node != null);
                }
            }

            return(false);
        }
Example #2
0
 internal QueueNode(T value, QueueNode <T> next, QueueNode <T> previous) : this(value)
 {
     Next     = next;
     Previous = previous;
 }
Example #3
0
 public void Reset()
 {
     Current = default(T);
     _node   = _queue._first;
     _index  = 0;
 }
Example #4
0
        /// <summary>
        /// Copies the elements of the <see cref="LinkedListQueue{T}"/> to an <see cref="Array"/>,
        /// starting at a particular <see cref="Array"/> index.
        /// </summary>
        /// <param name="array">
        /// The one-dimensional <see cref="Array"/> that is the destination of the elements copied
        /// from <see cref="LinkedListQueue{T}"/>. The <see cref="Array"/> must have zero-based indexing.
        /// </param>
        /// <param name="index">The zero-based index in array at which copying begins.</param>
        void ICollection.CopyTo(Array array, int index)
        {
            if (_count == 0)
            {
                return;
            }

            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }

            if ((uint)index >= array.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index), _StartIndexZeroGreaterMessage);
            }

            if (array.Rank != 1)
            {
                throw new ArgumentException(_OnlySingleDimArraysMessage, nameof(array));
            }

            if (array.GetLowerBound(0) != 0)
            {
                throw new ArgumentException(_ArrayNonZeroLBMessage, nameof(array));
            }

            if (array.Length - index < _count)
            {
                throw new ArgumentOutOfRangeException(nameof(array), _DestArraySizeTooSmallMessage);
            }

            if (array is T[] testArray)
            {
                CopyTo(testArray, index);
            }
            else
            {
                Type targetType = array.GetType().GetElementType();
                Type sourceType = typeof(T);
                if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))
                {
                    throw new ArgumentException(_InvalidArrayTypeMessage, nameof(array));
                }

                if (!(array is object[] objects))
                {
                    throw new ArgumentException(_InvalidArrayTypeMessage, nameof(array));
                }

                QueueNode <T> node = _first;
                try
                {
                    if (node != null)
                    {
                        do
                        {
                            objects[index++] = node.Value;
                            node             = node.Next;
                        } while (node != null);
                    }
                }
                catch (ArrayTypeMismatchException)
                {
                    throw new ArgumentException(_InvalidArrayTypeMessage, nameof(array));
                }
            }
        }