Home
Paste
Show Key
Listed
Keep Editing
using System.Collections; using System.Collections.Generic; namespace PgRr.System.Collections.Generic { public class UniqueQueue<T> : IEnumerable<T>, IEnumerable { private HashSet<T> hashSet = new HashSet<T>(); private Queue<T> queue = new Queue<T>(); public int Count => hashSet.Count; public void Clear() { hashSet.Clear(); queue.Clear(); } public bool Contains(T item) => hashSet.Contains(item); public void Enqueue(T item) { if (hashSet.Add(item)) { queue.Enqueue(item); } else { throw new InvalidOperationException("The queue already contains this item"); } } public T Dequeue() { T item = queue.Dequeue(); hashSet.Remove(item); return item; } public T Peek() => queue.Peek(); public IEnumerator<T> GetEnumerator() => queue.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => queue.GetEnumerator(); } }
using System.Collections; using System.Collections.Generic; namespace PgRr.System.Collections.Generic { public class UniqueQueue<T> : IEnumerable<T>, IEnumerable { private HashSet<T> hashSet = new HashSet<T>(); private Queue<T> queue = new Queue<T>(); public int Count => hashSet.Count; public void Clear() { hashSet.Clear(); queue.Clear(); } public bool Contains(T item) => hashSet.Contains(item); public void Enqueue(T item) { if (hashSet.Add(item)) { queue.Enqueue(item); } else { throw new InvalidOperationException("The queue already contains this item"); } } public T Dequeue() { T item = queue.Dequeue(); hashSet.Remove(item); return item; } public T Peek() => queue.Peek(); public IEnumerator<T> GetEnumerator() => queue.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => queue.GetEnumerator(); } }