예제 #1
0
 internal Enumerator(LinkedQueue <T> q)
 {
     this._q              = q;
     this._version        = q._version;
     this._status         = 0;
     this._currentElement = null;
 }
예제 #2
0
        /// <summary>Determines whether an element is in the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />.</summary>
        /// <returns>true if <paramref name="item" /> is found in the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />; otherwise, false.</returns>
        /// <param name="item">The object to locate in the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />. The value can be null for reference types.</param>
        public bool Contains(T item)
        {
            bool find = false;
            LinkedQueueNode <T> temp = _head;

            while (temp != null && !find)
            {
                find = temp.Value.Equals(item);
                temp = temp._next;
            }

            return(find);
        }
예제 #3
0
        /// <summary>Copies the <see cref="T:Common.Collections.Generic.LinkedQueue`1" /> elements to a new array.</summary>
        /// <returns>A new array containing elements copied from the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />.</returns>
        public T[] ToArray()
        {
            T[] array = new T[_size];
            LinkedQueueNode <T> temp = _head;
            int i = 0;

            while (temp != null)
            {
                array[i++] = temp.Value;
                temp       = temp._next;
            }

            return(array);
        }
예제 #4
0
            /// <summary>Sets the enumerator to its initial position, which is before the first element in the collection.</summary>
            /// <exception cref="T:System.InvalidOperationException">The collection was modified or disposed after the enumerator was created. </exception>
            void IEnumerator.Reset()
            {
                if (this._status == 3)
                {
                    throw new InvalidOperationException("It has been disposed");
                }

                if (this._version != this._q._version)
                {
                    throw new InvalidOperationException("The queue has been changed");
                }

                this._status         = 0;
                this._currentElement = null;
            }
예제 #5
0
        /// <summary>
        /// Removes all objects from the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />.
        /// </summary>
        public void Clear()
        {
            if (_head == null)
            {
                return;
            }

            LinkedQueueNode <T> temp = _head;

            do
            {
                temp.Invalid();
                temp = temp._next;
            }while (temp != null);

            _size = 0;
            _version++;
        }
예제 #6
0
        /// <summary>Adds an object to the end of the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />.</summary>
        /// <param name="item">The object to add to the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />. The value can be null for reference types.</param>
        public void Enqueue(T item)
        {
            LinkedQueueNode <T> newNode = new LinkedQueueNode <T>(item);

            if (_tail == null)
            {
                _head = newNode;
                _tail = newNode;
            }
            else
            {
                _tail._next = newNode;
                _tail       = newNode;
            }

            _size++;
            _version++;
        }
예제 #7
0
            /// <summary>Advances the enumerator to the next element of the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />.</summary>
            /// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
            /// <exception cref="T:System.InvalidOperationException">The collection was modified or disposed after the enumerator was created. </exception>
            public bool MoveNext()
            {
                if (this._status == 3)
                {
                    throw new InvalidOperationException("It has been disposed");
                }

                if (this._version != this._q._version)
                {
                    throw new InvalidOperationException("The queue has been changed");
                }

                if (this._status == 2)
                {
                    return(false);
                }

                if (this._q.Count == 0)
                {
                    this._status = 2;
                    return(false);
                }

                if (this._status == 0)
                {
                    this._currentElement = _q._head;
                    this._status         = 1;
                    return(true);
                }

                if (this._currentElement._next == null)
                {
                    this._status         = 2;
                    this._currentElement = null;
                    return(false);
                }

                this._currentElement = _currentElement._next;

                return(true);
            }
예제 #8
0
        /// <summary>Removes and returns the object at the beginning of the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />.</summary>
        /// <returns>The object that is removed from the beginning of the <see cref="T:Common.Collections.Generic.LinkedQueue`1" />.</returns>
        /// <exception cref="T:System.InvalidOperationException">The <see cref="T:Common.Collections.Generic.LinkedQueue`1" /> is empty.</exception>
        public LinkedQueueNode <T> Dequeue()
        {
            if (_head == null)
            {
                throw new InvalidOperationException("The queue is empty.");
            }

            LinkedQueueNode <T> node = _head;

            _head = _head._next;

            if (_head == null)
            {
                _tail = null;
            }

            _size--;
            _version++;

            return(node);
        }
예제 #9
0
 internal void Invalid()
 {
     _item = default(T);
     _next = null;
 }
예제 #10
0
 /// <summary>Releases all resources used by the <see cref="T:Common.Collections.Generic.LinkedQueue`1.Enumerator" />.</summary>
 public void Dispose()
 {
     this._status         = 3;
     this._q              = null;
     this._currentElement = null;
 }
예제 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Common.Collections.Generic.LinkedQueue`1" /> class that is empty.
 /// </summary>
 public LinkedQueue()
 {
     _head = null;
     _tail = null;
 }