Ejemplo n.º 1
0
        internal void InsertItemInPriorityChain(PriorityItem <T> item,
                                                PriorityChain <T> chain, PriorityItem <T> after)
        {
            Debug.Assert(chain != null,
                         "PriorityQueue.InsertItemInPriorityChain:" +
                         " a chain must be provided.");
            Debug.Assert(item.Chain == null && item.PriorityPrev == null &&
                         item.PriorityNext == null,
                         "PriorityQueue.InsertItemInPriorityChain:" +
                         " item must not already be in a priority chain.");

            item.Chain = chain;

            if (after == null)
            {
                // Note: passing null for after means insert at the head.

                if (chain.Head != null)
                {
                    Debug.Assert(chain.Tail != null,
                                 "PriorityQueue.InsertItemInPriorityChain:" +
                                 " both the head and the tail should not be null.");

                    chain.Head.PriorityPrev = item;
                    item.PriorityNext       = chain.Head;
                    chain.Head = item;
                }
                else
                {
                    Debug.Assert(chain.Tail == null,
                                 "PriorityQueue.InsertItemInPriorityChain:" +
                                 " both the head and the tail should be null.");

                    chain.Head = chain.Tail = item;
                }
            }
            else
            {
                item.PriorityPrev = after;

                if (after.PriorityNext != null)
                {
                    item.PriorityNext = after.PriorityNext;
                    after.PriorityNext.PriorityPrev = item;
                    after.PriorityNext = item;
                }
                else
                {
                    Debug.Assert(item.Chain.Tail == after,
                                 "PriorityQueue.InsertItemInPriorityChain:" +
                                 " the chain's tail should be the item we " +
                                 "are inserting after.");
                    after.PriorityNext = item;
                    chain.Tail         = item;
                }
            }

            chain.Count++;
        }
Ejemplo n.º 2
0
        public void ChangeItemPriority(PriorityItem <T> item,
                                       WorkPriority priority) // NOTE: should be Priority
        {
            // Remove the item from its current priority and insert it into
            // the new priority chain.  Note that this does not change the
            // sequential ordering.

            // Step 1: Remove the item from the priority chain.
            RemoveItemFromPriorityChain(item);

            // Step 2: Insert the item into the new priority chain.
            // Find the existing chain for this priority, or create a new one
            // if one does not exist.
            PriorityChain <T> chain = GetChain(priority);

            InsertItemInPriorityChain(item, chain);
        }
Ejemplo n.º 3
0
        public void RemoveItem(PriorityItem <T> item)
        {
            Debug.Assert(item != null,
                         "PriorityQueue.RemoveItem: invalid item.");
            Debug.Assert(item.Chain != null,
                         "PriorityQueue.RemoveItem: a chain should exist.");

            PriorityChain <T> chain = item.Chain;

            // Step 1: Remove the item from its priority chain.
            RemoveItemFromPriorityChain(item);

            // Step 2: Remove the item from the sequential chain.
            RemoveItemFromSequentialChain(item);

            // Note: we do not clean up empty chains on purpose to reduce churn.
        }
Ejemplo n.º 4
0
        public PriorityItem <T> Enqueue(WorkPriority priority, T data)
        // NOTE: should be Priority
        {
            // Find the existing chain for this priority, or create a new one
            // if one does not exist.
            PriorityChain <T> chain = GetChain(priority);

            // Wrap the item in a PriorityItem so we can put it in our
            // linked list.
            PriorityItem <T> priorityItem = new PriorityItem <T>(data);

            // Step 1: Append this to the end of the "sequential" linked list.
            InsertItemInSequentialChain(priorityItem, _tail);

            // Step 2: Append the item into the priority chain.
            InsertItemInPriorityChain(priorityItem, chain, chain.Tail);

            return(priorityItem);
        }
Ejemplo n.º 5
0
        private PriorityChain <T> GetChain(WorkPriority priority)
        // NOTE: should be Priority
        {
            PriorityChain <T> chain = null;

            int count = _priorityChains.Count;

            if (count > 0)
            {
                if (priority == (WorkPriority)_priorityChains.Keys[0])
                {
                    chain = _priorityChains.Values[0];
                }
                else if (priority ==
                         (WorkPriority)_priorityChains.Keys[count - 1])
                {
                    chain = _priorityChains.Values[count - 1];
                }
                else if ((priority > (WorkPriority)_priorityChains.Keys[0]) &&
                         (priority < (WorkPriority)_priorityChains.Keys[count - 1]))
                {
                    _priorityChains.TryGetValue((int)priority, out chain);
                }
            }

            if (chain == null)
            {
                if (_cacheReusableChains.Count > 0)
                {
                    chain          = _cacheReusableChains.Pop();
                    chain.Priority = priority;
                }
                else
                {
                    chain = new PriorityChain <T>(priority);
                }

                _priorityChains.Add((int)priority, chain);
            }

            return(chain);
        }
Ejemplo n.º 6
0
        public T Peek()
        {
            T data = default(T);

            // Get the max-priority chain.
            int count = _priorityChains.Count;

            if (count > 0)
            {
                PriorityChain <T> chain = _priorityChains.Values[count - 1];
                Debug.Assert(chain != null,
                             "PriorityQueue.Peek: a chain should exist.");

                PriorityItem <T> item = chain.Head;
                Debug.Assert(item != null,
                             "PriorityQueue.Peek: a priority item should exist.");

                data = item.Data;
            }

            return(data);
        }
Ejemplo n.º 7
0
        private void InsertItemInPriorityChain(PriorityItem <T> item,
                                               PriorityChain <T> chain)
        {
            // Scan along the sequential chain, in the previous direction,
            // looking for an item that is already in the new chain.  We will
            // insert ourselves after the item we found.  We can short-circuit
            // this search if the new chain is empty.
            if (chain.Head == null)
            {
                Debug.Assert(chain.Tail == null,
                             "PriorityQueue.InsertItemInPriorityChain:" +
                             " both the head and the tail should be null.");
                InsertItemInPriorityChain(item, chain, null);
            }
            else
            {
                Debug.Assert(chain.Tail != null,
                             "PriorityQueue.InsertItemInPriorityChain:" +
                             " both the head and the tail should not be null.");

                PriorityItem <T> after = null;

                // Search backwards along the sequential chain looking for an
                // item already in this list.
                for (after = item.SequentialPrev; after != null;
                     after = after.SequentialPrev)
                {
                    if (after.Chain == chain)
                    {
                        break;
                    }
                }

                InsertItemInPriorityChain(item, chain, after);
            }
        }
Ejemplo n.º 8
0
        public T Dequeue()
        {
            // Get the max-priority chain.
            int count = _priorityChains.Count;

            if (count > 0)
            {
                PriorityChain <T> chain = _priorityChains.Values[count - 1];
                Debug.Assert(chain != null,
                             "PriorityQueue.Dequeue: a chain should exist.");

                PriorityItem <T> item = chain.Head;
                Debug.Assert(item != null,
                             "PriorityQueue.Dequeue: a priority item should exist.");

                RemoveItem(item);

                return(item.Data);
            }
            else
            {
                throw new InvalidOperationException();
            }
        }