PriorityQueue.cs 896 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Collections.Concurrent;
  2. namespace ARMeilleure.Translation
  3. {
  4. class PriorityQueue<T>
  5. {
  6. private ConcurrentQueue<T>[] _queues;
  7. public PriorityQueue(int priorities)
  8. {
  9. _queues = new ConcurrentQueue<T>[priorities];
  10. for (int index = 0; index < priorities; index++)
  11. {
  12. _queues[index] = new ConcurrentQueue<T>();
  13. }
  14. }
  15. public void Enqueue(int priority, T value)
  16. {
  17. _queues[priority].Enqueue(value);
  18. }
  19. public bool TryDequeue(out T value)
  20. {
  21. for (int index = 0; index < _queues.Length; index++)
  22. {
  23. if (_queues[index].TryDequeue(out value))
  24. {
  25. return true;
  26. }
  27. }
  28. value = default(T);
  29. return false;
  30. }
  31. }
  32. }