From 064c8ccfbd8daccc2cab1cbf6bf6e040608b9a87 Mon Sep 17 00:00:00 2001 From: Douglas Miller Date: Sun, 4 Jun 2017 21:50:22 -0700 Subject: [PATCH] Inline and simplify TryRemove, and delete unused method --- Priority Queue/SimplePriorityQueue.cs | 40 +++++++++++++++------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/Priority Queue/SimplePriorityQueue.cs b/Priority Queue/SimplePriorityQueue.cs index d1bed70..7d21867 100644 --- a/Priority Queue/SimplePriorityQueue.cs +++ b/Priority Queue/SimplePriorityQueue.cs @@ -216,20 +216,6 @@ private SimpleNode EnqueueNoLockOrCache(TItem item, TPriority priority) return node; } - /// - /// Enqueue the item with the given priority, without calling lock(_queue) - /// - private void EnqueueNoLock(TItem item, TPriority priority) - { - SimpleNode node = new SimpleNode(item); - if(_queue.Count == _queue.MaxSize) - { - _queue.Resize(_queue.MaxSize * 2 + 1); - } - _queue.Enqueue(node, priority); - AddToNodeCache(node); - } - /// /// Enqueue a node to the priority queue. Lower values are placed in front. Ties are broken by first-in-first-out. /// This queue automatically resizes itself, so there's no concern of the queue becoming 'full'. @@ -423,13 +409,31 @@ public bool TryRemove(TItem item) { lock(_queue) { - SimpleNode removeMe = GetExistingNode(item); - if(removeMe == null) + SimpleNode removeMe; + IList nodes; + if (item == null) { - return false; + if (_nullNodesCache.Count == 0) + { + return false; + } + removeMe = _nullNodesCache[0]; + nodes = _nullNodesCache; + } + else + { + if (!_itemToNodesCache.TryGetValue(item, out nodes)) + { + return false; + } + removeMe = nodes[0]; + if (nodes.Count == 1) + { + _itemToNodesCache.Remove(item); + } } _queue.Remove(removeMe); - RemoveFromNodeCache(removeMe); + nodes.Remove(removeMe); return true; } }