aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Translation/PriorityQueue.cs
blob: ab593dc07fe0037800d4cbe3f45e8c7dd7cd036f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Collections.Concurrent;

namespace ARMeilleure.Translation
{
    class PriorityQueue<T>
    {
        private ConcurrentQueue<T>[] _queues;

        public PriorityQueue(int priorities)
        {
            _queues = new ConcurrentQueue<T>[priorities];

            for (int index = 0; index < priorities; index++)
            {
                _queues[index] = new ConcurrentQueue<T>();
            }
        }

        public void Enqueue(int priority, T value)
        {
            _queues[priority].Enqueue(value);
        }

        public bool TryDequeue(out T value)
        {
            for (int index = 0; index < _queues.Length; index++)
            {
                if (_queues[index].TryDequeue(out value))
                {
                    return true;
                }
            }

            value = default(T);

            return false;
        }
    }
}