/// <summary> /// Removes an item from the queue. The item does not need to be the head of the queue. /// If the item is not in the queue, an exception is thrown. If unsure, check Contains() first. /// If multiple copies of the item are enqueued, only the first one is removed. /// O(log n) /// </summary> public void Remove(TItem item) { lock (_queue) { 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 { if (!_itemToNodesCache.TryGetValue(item, out nodes)) { throw new InvalidOperationException("Cannot call Remove() on a node which is not enqueued: " + item); } removeMe = nodes[0]; if (nodes.Count == 1) { _itemToNodesCache.Remove(item); } } _queue.Remove(removeMe); nodes.Remove(removeMe); } }
/// <summary> /// Removes an item from the queue. The item does not need to be the head of the queue. /// If the item is not in the queue, an exception is thrown. If unsure, check Contains() first. /// If multiple copies of the item are enqueued, only the first one is removed. /// O(log n) /// </summary> public void Remove(TItem item) { lock (_queue) { SimpleNode removeMe = GetExistingNode(item); if (removeMe == null) { throw new InvalidOperationException("Cannot call Remove() on a node which is not enqueued: " + item); } _queue.Remove(removeMe); RemoveFromNodeCache(removeMe); } }
/// <summary> /// Removes an item from the queue. The item does not need to be the head of the queue. /// If the item is not in the queue, an exception is thrown. If unsure, check Contains() first. /// If multiple copies of the item are enqueued, only the first one is removed. /// O(n) /// </summary> public void Remove(TItem item) { lock (_queue) { try { _queue.Remove(GetExistingNode(item)); } catch (InvalidOperationException ex) { throw new InvalidOperationException("Cannot call Remove() on a node which is not enqueued: " + item, ex); } } }