| | | 1 | | namespace CounterpointCollective.Collections |
| | | 2 | | { |
| | | 3 | | |
| | | 4 | | public class StructQueue<T> where T : struct |
| | | 5 | | { |
| | 15 | 6 | | private int nextSegmentSize = 8; |
| | 15 | 7 | | private readonly Queue<Segment> _segments = []; |
| | | 8 | | private Segment lastSegment; |
| | | 9 | | |
| | 10299172 | 10 | | public int Count { get; private set; } |
| | | 11 | | |
| | 15 | 12 | | public StructQueue() |
| | | 13 | | { |
| | 15 | 14 | | lastSegment = new(nextSegmentSize); |
| | 15 | 15 | | nextSegmentSize *= 2; |
| | 15 | 16 | | _segments.Enqueue(lastSegment); |
| | 15 | 17 | | } |
| | | 18 | | |
| | | 19 | | public void Enqueue(out Handle handle) |
| | | 20 | | { |
| | 2249890 | 21 | | if (!lastSegment.TryEnqueue(out handle)) |
| | | 22 | | { |
| | 23 | 23 | | var newSegment = new Segment(nextSegmentSize); |
| | 23 | 24 | | nextSegmentSize *= 2; |
| | 23 | 25 | | _segments.Enqueue(newSegment); |
| | 23 | 26 | | lastSegment = newSegment; |
| | 23 | 27 | | if (!lastSegment.TryEnqueue(out handle)) |
| | | 28 | | { |
| | 0 | 29 | | throw new InvalidOperationException("Failed to enqueue item. This cannot happen."); |
| | | 30 | | } |
| | | 31 | | } |
| | 2249890 | 32 | | Count++; |
| | 2249890 | 33 | | } |
| | | 34 | | |
| | | 35 | | public bool TryDequeue(out Handle handle) |
| | | 36 | | { |
| | 2249779 | 37 | | var s = _segments.Peek(); |
| | | 38 | | |
| | 2249779 | 39 | | if (s.TryDequeue(out handle)) |
| | | 40 | | { |
| | 2249779 | 41 | | if (s.IsEmpty && _segments.Count > 1) |
| | | 42 | | { |
| | | 43 | | // remove empty segment, we have a next one. |
| | 23 | 44 | | _segments.Dequeue(); |
| | | 45 | | } |
| | 2249779 | 46 | | Count--; |
| | 2249779 | 47 | | return true; |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | handle = default; |
| | 0 | 51 | | return false; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public void Dequeue(out Handle handle) |
| | | 55 | | { |
| | 2249778 | 56 | | if (!TryDequeue(out handle)) |
| | | 57 | | { |
| | 0 | 58 | | throw new InvalidOperationException("Queue is empty."); |
| | | 59 | | } |
| | 2249778 | 60 | | } |
| | | 61 | | |
| | | 62 | | public bool TryPeek(out Handle handle) |
| | | 63 | | { |
| | 1299033 | 64 | | var s = _segments.Peek(); |
| | | 65 | | |
| | 1299033 | 66 | | if (s.TryPeek(out handle)) |
| | | 67 | | { |
| | 1299032 | 68 | | return true; |
| | | 69 | | } |
| | | 70 | | |
| | 1 | 71 | | handle = default; |
| | 1 | 72 | | return false; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | internal struct Slot |
| | | 76 | | { |
| | | 77 | | public const byte State_Free = 0; |
| | | 78 | | public const byte State_Enqueued = 1; |
| | | 79 | | public const byte State_Dequeued = 2; |
| | | 80 | | public T Item; |
| | 11197470 | 81 | | public int Next { get; set; } |
| | 8999229 | 82 | | public byte State { get; set; } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | public readonly struct Handle : IDisposable |
| | | 86 | | { |
| | | 87 | | private readonly Segment _segment; |
| | | 88 | | private readonly int _index; |
| | | 89 | | |
| | | 90 | | internal Handle(Segment segment, int index) |
| | | 91 | | { |
| | 5798701 | 92 | | _segment = segment; |
| | 5798701 | 93 | | _index = index; |
| | 5798701 | 94 | | } |
| | | 95 | | |
| | 8998551 | 96 | | public ref T GetRef() => ref _segment.GetRef(_index).Item; |
| | | 97 | | |
| | | 98 | | public void Dispose() |
| | 2249781 | 99 | | => _segment.Free(_index); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | internal class Segment |
| | | 103 | | { |
| | | 104 | | private readonly Slot[] Slots; |
| | | 105 | | |
| | | 106 | | private int freeTop; |
| | | 107 | | |
| | | 108 | | private int queueStart; |
| | | 109 | | private int queueEnd; |
| | | 110 | | |
| | 2249779 | 111 | | public bool IsEmpty => queueStart == -1; |
| | | 112 | | |
| | | 113 | | |
| | 38 | 114 | | public Segment(int size) |
| | | 115 | | { |
| | 38 | 116 | | Slots = new Slot[size]; |
| | | 117 | | |
| | 38 | 118 | | queueStart = -1; |
| | 38 | 119 | | queueEnd = -1; |
| | | 120 | | |
| | | 121 | | // init free slots links |
| | 2098448 | 122 | | for (int i = 0; i < size - 1; i++) |
| | 1049186 | 123 | | Slots[i].Next = i + 1; |
| | 38 | 124 | | Slots[size - 1].Next = -1; |
| | | 125 | | |
| | 38 | 126 | | freeTop = 0; |
| | 38 | 127 | | } |
| | | 128 | | |
| | | 129 | | public bool TryEnqueue(out Handle handle) |
| | | 130 | | { |
| | | 131 | | int idx; |
| | | 132 | | do |
| | | 133 | | { |
| | 2249913 | 134 | | idx = freeTop; |
| | 2249959 | 135 | | if (idx < 0) { handle = default; return false; } |
| | 2249890 | 136 | | } while (Interlocked.CompareExchange(ref freeTop, Slots[idx].Next, idx) != idx); |
| | | 137 | | |
| | | 138 | | |
| | 2249890 | 139 | | ref var slot = ref Slots[idx]; |
| | 2249890 | 140 | | slot.State = Slot.State_Enqueued; |
| | 2249890 | 141 | | slot.Next = -1; |
| | | 142 | | |
| | 2249890 | 143 | | if (queueStart == -1) |
| | | 144 | | { |
| | 1100982 | 145 | | queueStart = idx; |
| | | 146 | | } else |
| | | 147 | | { |
| | 1148908 | 148 | | Slots[queueEnd].Next = idx; |
| | | 149 | | } |
| | 2249890 | 150 | | queueEnd = idx; |
| | | 151 | | |
| | 2249890 | 152 | | handle = new Handle(this, idx); |
| | 2249890 | 153 | | return true; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | public bool TryDequeue(out Handle handle) |
| | | 157 | | { |
| | 2249779 | 158 | | if (queueStart == -1) |
| | | 159 | | { |
| | 0 | 160 | | handle = default; |
| | 0 | 161 | | return false; |
| | | 162 | | } |
| | | 163 | | |
| | 2249779 | 164 | | Slots[queueStart].State = Slot.State_Dequeued; |
| | 2249779 | 165 | | handle = new Handle(this, queueStart); |
| | 2249779 | 166 | | queueStart = Slots[queueStart].Next; |
| | 2249779 | 167 | | if (queueStart == -1) |
| | | 168 | | { |
| | 1100978 | 169 | | queueEnd = -1; |
| | | 170 | | } |
| | | 171 | | |
| | 2249779 | 172 | | return true; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | public bool TryPeek(out Handle handle) |
| | | 176 | | { |
| | 1299033 | 177 | | if (queueStart == -1) |
| | | 178 | | { |
| | 1 | 179 | | handle = default; |
| | 1 | 180 | | return false; |
| | | 181 | | } |
| | 1299032 | 182 | | handle = new Handle(this, queueStart); |
| | 1299032 | 183 | | return true; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | public void Free(int index) |
| | | 187 | | { |
| | 2249781 | 188 | | ref var slot = ref Slots[index]; |
| | 2249781 | 189 | | switch(slot.State) |
| | | 190 | | { |
| | | 191 | | case Slot.State_Enqueued: |
| | 1 | 192 | | throw new InvalidOperationException("Cannot dispose an enqueued item."); |
| | | 193 | | case Slot.State_Dequeued: |
| | 2249779 | 194 | | slot.State = Slot.State_Free; |
| | 2249779 | 195 | | slot.Item = default; |
| | | 196 | | |
| | | 197 | | int oldTop; |
| | | 198 | | do |
| | | 199 | | { |
| | 2249779 | 200 | | oldTop = freeTop; |
| | 2249779 | 201 | | slot.Next = oldTop; |
| | 2249779 | 202 | | } while (Interlocked.CompareExchange(ref freeTop, index, oldTop) != oldTop); |
| | | 203 | | break; |
| | | 204 | | default: |
| | | 205 | | break; |
| | | 206 | | } |
| | 2249779 | 207 | | } |
| | | 208 | | |
| | 8998551 | 209 | | public ref Slot GetRef(int index) => ref Slots[index]; |
| | | 210 | | } |
| | | 211 | | } |
| | | 212 | | } |