aboutsummaryrefslogtreecommitdiffstats
path: root/utils/Collections/ConcurrentPriorityList.cs
blob: 8afcf62a48679c8305978be802c262dd8fba3422 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.Collections;
using System.Collections.Generic;

namespace Qt.DotNet.Utils.Collections.Concurrent
{
    public interface IPrioritizable<TPrio>
        where TPrio : IComparable<TPrio>, IEquatable<TPrio>
    {
        TPrio Priority { get; }
    }

    public class ConcurrentPriorityList<T, TPrio> : IEnumerable<T>
        where T : IPrioritizable<TPrio>
        where TPrio : IComparable<TPrio>, IEquatable<TPrio>
    {
        private readonly object criticalSection = new();
        private ulong timestamp = 0;

        private class PriorityComparer : IComparer<(TPrio Prio, ulong Timestamp)>
        {
            public int Compare((TPrio Prio, ulong Timestamp) x, (TPrio Prio, ulong Timestamp) y)
            {
                if (!x.Prio.Equals(y.Prio))
                    return x.Prio.CompareTo(y.Prio);
                return x.Timestamp.CompareTo(y.Timestamp);
            }
        }

        private SortedList<(TPrio Prio, ulong Timestamp), T> items = new(new PriorityComparer());
        private IEnumerable<T> Items
        {
            get
            {
                lock (criticalSection)
                    return items.Values.ToList();
            }
        }
        public IEnumerator<T> GetEnumerator() => Items.GetEnumerator();
        IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)Items).GetEnumerator();

        public void Add(T item)
        {
            if (item == null)
                throw new ArgumentNullException(nameof(item));
            lock (criticalSection)
                items.Add((item.Priority, ++timestamp), item);
        }
    }
}