/// <inheritdoc />
        public T DequeueItemFromFront()
        {
            if (IsEmpty)
            {
                throw new InvalidOperationException("Unable to Dequeue item from front. Deque is empty");
            }

            /*
             * 1) Get the value from the current head node
             * 2) Set the head to the current head's Next node.
             * 3) Decrement the count
             * 4) Check if the queue contains items, if it does set the new head's
             * Previous to null; otherwise both tail and head are nul
             *
             * */
            T item = _head.Value;

            _head = _head.Next;

            _count--;

            if (Count != 0)
            {
                _head.Previous = null;
            }
            else
            {
                _tail = null;
            }
            _version++;

            return(item);
        }
        /// <inheritdoc />
        public T DequeueItemFromBack()
        {
            if (IsEmpty)
            {
                throw new InvalidOperationException("Unable to Dequeue item from back. Deque is empty");
            }

            /*
             * 1) Get the value from the current tail node
             * 2) Set the tail to the current tail's Previous node.
             * 3) Decrement the count
             * 4) Check if the queue contains items, if it does set the new tail's
             * Next to null; otherwise both tail and head are nul
             *
             * */
            T item = _tail.Value;

            _tail = _tail.Previous;

            _count--;

            if (Count != 0)
            {
                _tail.Next = null;
            }
            else
            {
                _head = null;
            }
            _version++;

            return(item);
        }
Beispiel #3
0
        // ******************************************************************
        public void QueueToEnd(T item)
        {
            bool lockTaken = false;

            try
            {
                _spinLock.Enter(ref lockTaken);

                if (_last != null)
                {
                    DequeNode <T> newNode = new DequeNode <T>()
                    {
                        Item = item, Previous = _last
                    };
                    _last.Next = newNode;
                    _last      = newNode;
                }
                else
                {
                    _last = new DequeNode <T>()
                    {
                        Item = item
                    };
                    _first = _last;
                }
            }
            finally
            {
                if (lockTaken)
                {
                    _spinLock.Exit();
                }
            }
        }
        /// <inheritdoc />
        public void EnqueueItemToBack(T item)
        {
            /*
             * 1) Create a node to store the item
             * 2) Set the node's Previous to point at the current tail
             * 3) If there are items in queue set the current tail's Next to the node
             * 4) Set the new node as the tail
             * 5) Finally check if there is only one item, if so head and tail are the same
             * */
            DequeNode node = new DequeNode(item);

            node.Previous = _tail;
            if (Count != 0)
            {
                _tail.Next = node;
            }
            _tail = node;

            _count++;
            if (Count == 1)
            {
                _head = _tail;
            }
            _version++;
        }
            public bool MoveNext()
            {
                if (_disposed)
                {
                    throw new ObjectDisposedException(GetType().Name);
                }
                if (_version != _parent._version)
                {
                    throw new InvalidOperationException(
                              "The Deque was modified after the enumerator was created");
                }

                if (_currentNode != null)
                {
                    _current     = _currentNode.Value;
                    _currentNode = _currentNode.Next;

                    _moveResult = true;
                }
                else
                {
                    _moveResult = false;
                }
                return(_moveResult);
            }
Beispiel #6
0
    public T PopFront()
    {
        var result = First;

        First = First.Child;
        Count--;
        return(result.Item);
    }
Beispiel #7
0
    public T PopBack()
    {
        var result = Last;

        Last = Last.Parent;
        Count--;
        return(result.Item);
    }
        /// <summary>
        /// Removes all items from the <see cref="Deque{T}"/>.
        /// </summary>
        public void Clear()
        {
            _count = 0;
            _version++;

            _head = null;
            _tail = null;
        }
 //--- Constructors ---
 internal DequeNode(int capacity, DequeNode next)
 {
     Data = new T[capacity];
     if (next != null)
     {
         this.Next = next;
         next.Prev = this;
     }
 }
        /// <summary>
        /// Create a new instance.
        /// </summary>
        /// <param name="capacity">Maximum number of items in the queue.</param>
        public WorkStealingDeque(int capacity)
        {
            _capacity = capacity;
            DequeNode nodeB = new DequeNode(_capacity, null);
            DequeNode nodeA = new DequeNode(_capacity, nodeB);

            _bottom = new BottomData(nodeA, _capacity - 1);
            _top    = new TopData(0, nodeA, _capacity - 1);
        }
Beispiel #11
0
            public void DeleteRear()
            {
                if (this.IsEmpty())
                {
                    return;
                }

                this.RearNode = this.RearNode.PrevNode;
            }
Beispiel #12
0
            public void DeleteFront()
            {
                if (this.IsEmpty())
                {
                    return;
                }

                this.FrontNode = this.FrontNode.NextNode;
            }
Beispiel #13
0
 public void PushFront(T item)
 {
     if (Count == 0)
     {
         ZeroPush(item);
     }
     else
     {
         First.Parent = new DequeNode <T>(item, null, First);;
         First        = First.Parent;
     }
     Count++;
 }
Beispiel #14
0
 public void PushBack(T item)
 {
     if (Count == 0)
     {
         ZeroPush(item);
     }
     else
     {
         Last.Child = new DequeNode <T>(item, Last, null);
         Last       = Last.Child;
     }
     Count++;
 }
Beispiel #15
0
 public void Reset()
 {
     if (_disposed)
     {
         throw new ObjectDisposedException(GetType().Name);
     }
     if (_version != _parent._version)
     {
         throw new InvalidOperationException(
                   "The Deque was modified after the enumerator was created");
     }
     _currentNode = _parent._head;
     _moveResult  = false;
 }
Beispiel #16
0
        public T PopRight()
        {
            var current = last;

            if (last.Left != null)
            {
                last.Left.Right = null;
            }
            else
            {
                first = null;
            }
            last         = last.Left;
            current.Left = null;
            N--;
            return(current.Item);
        }
Beispiel #17
0
        public T PopLeft()
        {
            var current = first;

            if (first.Right != null)
            {
                first.Right.Left = null;
            }
            else
            {
                last = null;
            }
            first         = first.Right;
            current.Right = null;
            N--;
            return(current.Item);
        }
Beispiel #18
0
        public void PushRight(T item)
        {
            DequeNode <T> dequeNode = new DequeNode <T>();

            dequeNode.Left = last;
            if (last != null)
            {
                last.Right = dequeNode;
            }
            last = dequeNode;
            if (first == null)
            {
                first = dequeNode;
            }
            dequeNode.Item = item;
            N++;
        }
        //--- Methods ---

        /// <summary>
        /// Push an item onto the tail of the queue.
        /// </summary>
        /// <remarks>
        /// NOTE: Push() and TryPop() <strong>MUST</strong> be called from the same thread.
        /// </remarks>
        /// <param name="data">Item to push onto the tail of the queue.</param>
        public void Push(T data)
        {
            // read bottom data
            BottomData curBottom = _bottom;

            // write data in current bottom cell
            curBottom.Node.Data[curBottom.Index] = data;
            BottomData newBottom;

            if (curBottom.Index != 0)
            {
                newBottom = new BottomData(curBottom.Node, curBottom.Index - 1);
            }
            else
            {
                // allocate and link a new node
                DequeNode newNode = new DequeNode(_capacity, curBottom.Node);
                newBottom = new BottomData(newNode, _capacity - 1);
            }

            // update bottom
            _bottom = newBottom;
        }
Beispiel #20
0
        // ******************************************************************
        public bool DequeueFromEnd(out T item)
        {
            bool lockTaken = false;

            try
            {
                _spinLock.Enter(ref lockTaken);

                if (_last != null)
                {
                    item = _last.Item;

                    _last = _last.Previous;
                    if (_last != null)
                    {
                        _last.Next = null;
                    }
                    else
                    {
                        _first = null;
                    }

                    return(true);
                }
            }
            finally
            {
                if (lockTaken)
                {
                    _spinLock.Exit();
                }
            }

            item = default(T);
            return(false);
        }
Beispiel #21
0
 public void InsertFront(T item)
 {
     if (this.IsEmpty())
     {
         var nextNode = new DequeNode <T>
         {
             Data     = item,
             PrevNode = null,
             NextNode = null
         };
         this.FrontNode = nextNode;
         this.RearNode  = nextNode;
     }
     else
     {
         var nextNode = new DequeNode <T>
         {
             Data     = item,
             PrevNode = null,
             NextNode = this.FrontNode
         };
         this.FrontNode = nextNode;
     }
 }
 //--- Constructors ---
 internal BottomData(DequeNode node, int index)
 {
     this.Node  = node;
     this.Index = index;
 }
 //--- Constructors ---
 internal TopData(int tag, DequeNode node, int index)
 {
     this.Tag   = tag;
     this.Node  = node;
     this.Index = index;
 }
Beispiel #24
0
 private void ZeroPush(T item)
 {
     First = new DequeNode <T>(item, null, null);
     Last  = First;
 }
Beispiel #25
0
 public Deque()
 {
     Count = 0;
     First = null;
     Last  = null;
 }
Beispiel #26
0
 public DequeNode(T i, DequeNode <T> p, DequeNode <T> c)
 {
     Item   = i;
     Parent = p;
     Child  = c;
 }
Beispiel #27
0
 public DequeEnumerator(Deque <T> parent)
 {
     _parent      = parent;
     _currentNode = _parent._head;
     _version     = _parent._version;
 }