Skip to content

Commit

Permalink
Inline and simplify TryRemove, and delete unused method
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmill committed Jun 11, 2017
1 parent 2ef48f7 commit 064c8cc
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions Priority Queue/SimplePriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,6 @@ private SimpleNode EnqueueNoLockOrCache(TItem item, TPriority priority)
return node;
}

/// <summary>
/// Enqueue the item with the given priority, without calling lock(_queue)
/// </summary>
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);
}

/// <summary>
/// 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'.
Expand Down Expand Up @@ -423,13 +409,31 @@ public bool TryRemove(TItem item)
{
lock(_queue)
{
SimpleNode removeMe = GetExistingNode(item);
if(removeMe == null)
SimpleNode removeMe;
IList<SimpleNode> 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;
}
}
Expand Down

0 comments on commit 064c8cc

Please sign in to comment.