// Base Enqueue() is protected to allow specialized implementations to // hide the concept of priority (e.g. PunisherQueue). // protected void Enqueue(T item, TPriority priority) { if (item == null) { throw new InvalidOperationException("Item cannot be null."); } lock (CriticalSection) { if (ItemsByPriority.TryGetValue(priority, out T oldItem) && !item.Equals(oldItem)) { throw new InvalidOperationException("An item with the same priority exists."); } if (ItemPriority.TryGetValue(GetItemKey(item), out TPriority oldPriority)) { ItemsByPriority.Remove(oldPriority); --Count; } ItemPriority[GetItemKey(item)] = priority; ItemsByPriority[priority] = item; Head = ItemsByPriority.First().Value; ++Count; } }
public void Remove(T item) { lock (CriticalSection) { object key = GetItemKey(item); if (key == null) { return; } ItemsByPriority.Remove(ItemPriority[key]); ItemPriority.Remove(key); --Count; if (key == GetItemKey(Head)) { if (IsEmpty) { Head = default(T); } else { Head = ItemsByPriority.First().Value; } } } }