| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Runtime.CompilerServices; |
| | 7 | |
|
| | 8 | | namespace GDX.Collections.Generic |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// A concurrent sized buffer which loops back over itself as elements are used. |
| | 12 | | /// </summary> |
| | 13 | | /// <typeparam name="T">The type of <see cref="object" />s contained within.</typeparam> |
| | 14 | | [VisualScriptingCompatible(1)] |
| | 15 | | public struct ConcurrentCircularBuffer<T> |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Lock pattern to ensure we aren't stepping on our toes across threads. |
| | 19 | | /// </summary> |
| | 20 | | readonly object m_Lock; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Internal array of backed data for the <see cref="ConcurrentCircularBuffer{T}" />. |
| | 24 | | /// </summary> |
| | 25 | | public readonly T[] Array; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// The cached array length for <see cref="Array" />. |
| | 29 | | /// </summary> |
| | 30 | | public readonly int Capacity; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// The current size of occupied elements in the <see cref="ConcurrentCircularBuffer{T}" />. |
| | 34 | | /// </summary> |
| | 35 | | /// <remarks>CAUTION! Changing this will alter the understanding of the data.</remarks> |
| | 36 | | public int Count; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The index of the last item in <see cref="Array" />. |
| | 40 | | /// </summary> |
| | 41 | | /// <remarks>CAUTION! Changing this will alter the understanding of the data.</remarks> |
| | 42 | | public int EndIndex; |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// The index of the first item in <see cref="Array" />. |
| | 46 | | /// </summary> |
| | 47 | | /// <remarks>CAUTION! Changing this will alter the understanding of the data.</remarks> |
| | 48 | | public int StartIndex; |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Create a <see cref="ConcurrentCircularBuffer{T}" /> with a <paramref name="capacity" />. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="capacity">The maximum number of items allowed in the <see cref="ConcurrentCircularBuffer{T}" /> |
| | 54 | | public ConcurrentCircularBuffer(int capacity) |
| 14 | 55 | | { |
| 14 | 56 | | if (capacity < 1) |
| 0 | 57 | | { |
| 0 | 58 | | throw new ArgumentException("Circular buffer cannot have negative or zero capacity.", nameof(capacity)); |
| | 59 | | } |
| | 60 | |
|
| 14 | 61 | | Array = new T[capacity]; |
| 14 | 62 | | Capacity = capacity; |
| | 63 | |
|
| 14 | 64 | | Count = 0; |
| | 65 | |
|
| 14 | 66 | | StartIndex = 0; |
| 14 | 67 | | EndIndex = Count == capacity ? 0 : Count; |
| 14 | 68 | | m_Lock = new object(); |
| 14 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Create a <see cref="ConcurrentCircularBuffer{T}" /> with a <paramref name="capacity" />, filling with |
| | 73 | | /// <paramref name="targetItems" />. |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="capacity">The maximum number of items allowed in the <see cref="ConcurrentCircularBuffer{T}" /> |
| | 76 | | /// <param name="targetItems">An array of values to fill the <see cref="ConcurrentCircularBuffer{T}" /> with.</p |
| | 77 | | /// <exception cref="ArgumentException"> |
| | 78 | | /// Invalid number of entries provided to the <see cref="ConcurrentCircularBuffer{T}" /> |
| | 79 | | /// constructor. |
| | 80 | | /// </exception> |
| | 81 | | /// <exception cref="ArgumentNullException"> |
| | 82 | | /// No items were provided to the <see cref="ConcurrentCircularBuffer{T}" /> |
| | 83 | | /// constructor. |
| | 84 | | /// </exception> |
| | 85 | | public ConcurrentCircularBuffer(int capacity, T[] targetItems) |
| 0 | 86 | | { |
| 0 | 87 | | if (capacity < 1) |
| 0 | 88 | | { |
| 0 | 89 | | throw new ArgumentException("Circular buffer cannot have negative or zero capacity.", nameof(capacity)); |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | if (targetItems == null) |
| 0 | 93 | | { |
| 0 | 94 | | throw new ArgumentNullException(nameof(targetItems)); |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | if (targetItems.Length > capacity) |
| 0 | 98 | | { |
| 0 | 99 | | throw new ArgumentException("Too many items to fit circular buffer", nameof(targetItems)); |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | Array = new T[capacity]; |
| 0 | 103 | | Capacity = capacity; |
| | 104 | |
|
| 0 | 105 | | System.Array.Copy(targetItems, Array, targetItems.Length); |
| 0 | 106 | | Count = targetItems.Length; |
| | 107 | |
|
| 0 | 108 | | StartIndex = 0; |
| 0 | 109 | | EndIndex = Count == capacity ? 0 : Count; |
| 0 | 110 | | m_Lock = new object(); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Access item at <paramref name="pseudoIndex" />. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="pseudoIndex"></param> |
| | 117 | | /// <exception cref="IndexOutOfRangeException">Provided index is out of buffers range.</exception> |
| | 118 | | public T this[int pseudoIndex] |
| | 119 | | { |
| | 120 | | get => |
| 2 | 121 | | Array[ |
| | 122 | | StartIndex + |
| | 123 | | (pseudoIndex < Capacity - StartIndex ? pseudoIndex : pseudoIndex - Capacity)]; |
| | 124 | | set |
| 0 | 125 | | { |
| 0 | 126 | | lock (m_Lock) |
| 0 | 127 | | { |
| 0 | 128 | | Array[ |
| | 129 | | StartIndex + |
| | 130 | | (pseudoIndex < Capacity - StartIndex ? pseudoIndex : pseudoIndex - Capacity)] = value; |
| 0 | 131 | | } |
| 0 | 132 | | } |
| | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Add an <paramref name="item" /> to the <see cref="Array" />. |
| | 137 | | /// </summary> |
| | 138 | | /// <param name="item">The typed <see cref="object" /> to add.</param> |
| | 139 | | public void Add(T item) |
| 13 | 140 | | { |
| 13 | 141 | | PushBack(item); |
| 13 | 142 | | } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// Clear all values of the <see cref="Array" />. |
| | 146 | | /// </summary> |
| | 147 | | public void Clear() |
| 0 | 148 | | { |
| 0 | 149 | | lock (m_Lock) |
| 0 | 150 | | { |
| 0 | 151 | | for (int i = 0; i < Array.Length; i++) |
| 0 | 152 | | { |
| 0 | 153 | | Array[i] = default; |
| 0 | 154 | | } |
| | 155 | |
|
| 0 | 156 | | Count = 0; |
| 0 | 157 | | StartIndex = 0; |
| 0 | 158 | | EndIndex = 0; |
| 0 | 159 | | } |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | /// <summary> |
| | 163 | | /// Get the last item in the <see cref="Array" />. |
| | 164 | | /// </summary> |
| | 165 | | /// <returns>The last typed object in <see cref="Array" />.</returns> |
| | 166 | | public T GetBack() |
| 0 | 167 | | { |
| 0 | 168 | | return Array[(EndIndex != 0 ? EndIndex : Capacity) - 1]; |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary> |
| | 172 | | /// Get the first item in the <see cref="Array" />. |
| | 173 | | /// </summary> |
| | 174 | | /// <returns>The first typed object in <see cref="Array" />.</returns> |
| | 175 | | public T GetFront() |
| 0 | 176 | | { |
| 0 | 177 | | return Array[StartIndex]; |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | |
|
| | 181 | | /// <summary> |
| | 182 | | /// Does the <see cref="Array" /> have any items in it? |
| | 183 | | /// </summary> |
| | 184 | | /// <returns>true/false</returns> |
| | 185 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 186 | | public bool IsEmpty() |
| 0 | 187 | | { |
| 0 | 188 | | return Count == 0; |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | /// <summary> |
| | 192 | | /// Is the <see cref="Array" /> at capacity? |
| | 193 | | /// </summary> |
| | 194 | | /// <returns>true/false</returns> |
| | 195 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 196 | | public bool IsFull() |
| 0 | 197 | | { |
| 0 | 198 | | return Count == Capacity; |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | /// <summary> |
| | 202 | | /// Remove an item from the end of the <see cref="Array" />. |
| | 203 | | /// </summary> |
| | 204 | | public void PopBack() |
| 0 | 205 | | { |
| 0 | 206 | | lock (m_Lock) |
| 0 | 207 | | { |
| 0 | 208 | | if (EndIndex == 0) |
| 0 | 209 | | { |
| 0 | 210 | | EndIndex = Capacity; |
| 0 | 211 | | } |
| | 212 | |
|
| 0 | 213 | | EndIndex--; |
| 0 | 214 | | Array[EndIndex] = default; |
| 0 | 215 | | --Count; |
| 0 | 216 | | } |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | /// <summary> |
| | 220 | | /// Remove an item from the start of the <see cref="Array" />. |
| | 221 | | /// </summary> |
| | 222 | | public void PopFront() |
| 0 | 223 | | { |
| 0 | 224 | | lock (m_Lock) |
| 0 | 225 | | { |
| 0 | 226 | | Array[StartIndex] = default; |
| 0 | 227 | | if (++StartIndex == Capacity) |
| 0 | 228 | | { |
| 0 | 229 | | StartIndex = 0; |
| 0 | 230 | | } |
| | 231 | |
|
| 0 | 232 | | --Count; |
| 0 | 233 | | } |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | /// <summary> |
| | 237 | | /// Add an item to the end of the <see cref="Array" />. |
| | 238 | | /// </summary> |
| | 239 | | /// <param name="targetItem">The item to add to the end of <see cref="Array" />.</param> |
| | 240 | | public void PushBack(T targetItem) |
| 13 | 241 | | { |
| 13 | 242 | | lock (m_Lock) |
| 13 | 243 | | { |
| 13 | 244 | | if (Count == Capacity) |
| 2 | 245 | | { |
| 2 | 246 | | Array[EndIndex] = targetItem; |
| 2 | 247 | | if (++EndIndex == Capacity) |
| 0 | 248 | | { |
| 0 | 249 | | EndIndex = 0; |
| 0 | 250 | | } |
| | 251 | |
|
| 2 | 252 | | StartIndex = EndIndex; |
| 2 | 253 | | } |
| | 254 | | else |
| 11 | 255 | | { |
| 11 | 256 | | Array[EndIndex] = targetItem; |
| 11 | 257 | | if (++EndIndex == Capacity) |
| 0 | 258 | | { |
| 0 | 259 | | EndIndex = 0; |
| 0 | 260 | | } |
| | 261 | |
|
| 11 | 262 | | ++Count; |
| 11 | 263 | | } |
| 13 | 264 | | } |
| 13 | 265 | | } |
| | 266 | |
|
| | 267 | | /// <summary> |
| | 268 | | /// Add an item to the start of the <see cref="Array" />. |
| | 269 | | /// </summary> |
| | 270 | | /// <param name="targetItem">The item to add to the start of <see cref="Array" />.</param> |
| | 271 | | public void PushFront(T targetItem) |
| 0 | 272 | | { |
| 0 | 273 | | lock (m_Lock) |
| 0 | 274 | | { |
| 0 | 275 | | if (Count == Capacity) |
| 0 | 276 | | { |
| 0 | 277 | | if (StartIndex == 0) |
| 0 | 278 | | { |
| 0 | 279 | | StartIndex = Capacity; |
| 0 | 280 | | } |
| | 281 | |
|
| 0 | 282 | | StartIndex--; |
| 0 | 283 | | EndIndex = StartIndex; |
| 0 | 284 | | Array[StartIndex] = targetItem; |
| 0 | 285 | | } |
| | 286 | | else |
| 0 | 287 | | { |
| 0 | 288 | | if (StartIndex == 0) |
| 0 | 289 | | { |
| 0 | 290 | | StartIndex = Capacity; |
| 0 | 291 | | } |
| | 292 | |
|
| 0 | 293 | | StartIndex--; |
| 0 | 294 | | Array[StartIndex] = targetItem; |
| 0 | 295 | | ++Count; |
| 0 | 296 | | } |
| 0 | 297 | | } |
| 0 | 298 | | } |
| | 299 | |
|
| | 300 | | /// <summary> |
| | 301 | | /// Copy <see cref="Array" /> to an array of the same type. |
| | 302 | | /// </summary> |
| | 303 | | /// <returns>A copied version of the <see cref="Array" /> as an array.</returns> |
| | 304 | | public T[] ToArray() |
| 0 | 305 | | { |
| 0 | 306 | | T[] newArray = new T[Count]; |
| | 307 | |
|
| | 308 | | // We dont need to fill anything as its empty. |
| 0 | 309 | | if (Count <= 0) |
| 0 | 310 | | { |
| 0 | 311 | | return newArray; |
| | 312 | | } |
| | 313 | |
|
| | 314 | | int length; |
| | 315 | |
|
| | 316 | | // First Part |
| 0 | 317 | | if (StartIndex < EndIndex) |
| 0 | 318 | | { |
| 0 | 319 | | length = EndIndex - StartIndex; |
| 0 | 320 | | System.Array.Copy(Array, StartIndex, newArray, 0, length); |
| 0 | 321 | | } |
| | 322 | | else |
| 0 | 323 | | { |
| 0 | 324 | | length = Array.Length - StartIndex; |
| 0 | 325 | | System.Array.Copy(Array, StartIndex, newArray, 0, length); |
| 0 | 326 | | } |
| | 327 | |
|
| | 328 | | // Second Part |
| 0 | 329 | | if (StartIndex > EndIndex) |
| 0 | 330 | | { |
| 0 | 331 | | System.Array.Copy(Array, EndIndex, newArray, length, 0); |
| 0 | 332 | | } |
| | 333 | | else |
| 0 | 334 | | { |
| 0 | 335 | | System.Array.Copy(Array, 0, newArray, length, EndIndex); |
| 0 | 336 | | } |
| | 337 | |
|
| 0 | 338 | | return newArray; |
| 0 | 339 | | } |
| | 340 | | } |
| | 341 | | } |