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