Skip to content

Commit

Permalink
Inline and simplify Remove
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmill committed Jun 11, 2017
1 parent 570439c commit b8fc5ee
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions Priority Queue/SimplePriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,31 @@ public void Remove(TItem item)
{
lock(_queue)
{
SimpleNode removeMe = GetExistingNode(item);
if (removeMe == null)
SimpleNode removeMe;
IList<SimpleNode> nodes;
if (item == null)
{
if (_nullNodesCache.Count == 0)
{
throw new InvalidOperationException("Cannot call Remove() on a node which is not enqueued: " + item);
}
removeMe = _nullNodesCache[0];
nodes = _nullNodesCache;
}
else
{
throw new InvalidOperationException("Cannot call Remove() on a node which is not enqueued: " + item);
if (!_itemToNodesCache.TryGetValue(item, out nodes) || nodes.Count == 0)
{
throw new InvalidOperationException("Cannot call Remove() on a node which is not enqueued: " + item);
}
removeMe = nodes[0];
if (nodes.Count == 0)
{
_itemToNodesCache.Remove(item);
}
}
_queue.Remove(removeMe);
RemoveFromNodeCache(removeMe);
nodes.Remove(removeMe);
}
}

Expand Down

0 comments on commit b8fc5ee

Please sign in to comment.