| | | 1 | | using Chronicler; |
| | | 2 | | using MemoryPack; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Text.Json.Serialization; |
| | | 8 | | |
| | | 9 | | namespace SwiftCollections; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// <c>SwiftList<T></c> is a high-performance, memory-efficient dynamic list designed to outperform |
| | | 13 | | /// traditional generic lists in speed-critical applications. |
| | | 14 | | /// <para> |
| | | 15 | | /// By utilizing custom growth and |
| | | 16 | | /// shrink strategies, SwiftList optimizes memory allocation and minimizes resizing overhead, |
| | | 17 | | /// all while maintaining compact storage. With aggressive inlining and optimized algorithms, |
| | | 18 | | /// SwiftList delivers faster iteration, insertion, and overall memory management compared to |
| | | 19 | | /// standard List. It is ideal for scenarios where predictable performance and minimal |
| | | 20 | | /// memory allocations are essential. |
| | | 21 | | /// </para> |
| | | 22 | | /// <para> |
| | | 23 | | /// This implementation is optimized for performance and does not perform versioning checks. |
| | | 24 | | /// Modifying the list during enumeration may result in undefined behavior. |
| | | 25 | | /// </para> |
| | | 26 | | /// </summary> |
| | | 27 | | /// <typeparam name="T">Specifies the type of elements in the list.</typeparam> |
| | | 28 | | [Serializable] |
| | | 29 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 30 | | [MemoryPackable] |
| | | 31 | | public partial class SwiftList<T> : IStateBacked<SwiftArrayState<T>>, ISwiftCloneable<T>, IEnumerable<T>, IEnumerable, I |
| | | 32 | | { |
| | | 33 | | #region Constants |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The default initial capacity of the <see cref="SwiftList{T}"/> if none is specified. |
| | | 37 | | /// Used to allocate a reasonable starting size to minimize resizing operations. |
| | | 38 | | /// </summary> |
| | | 39 | | public const int DefaultCapacity = 8; |
| | | 40 | | |
| | 2 | 41 | | private static readonly T[] _emptyArray = Array.Empty<T>(); |
| | 2 | 42 | | private static readonly bool _clearReleasedSlots = RuntimeHelpers.IsReferenceOrContainsReferences<T>(); |
| | | 43 | | |
| | | 44 | | #endregion |
| | | 45 | | |
| | | 46 | | #region Fields |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// The internal array that stores elements of the SwiftList. Resized as needed to |
| | | 50 | | /// accommodate additional elements. Not directly exposed outside the list. |
| | | 51 | | /// </summary> |
| | | 52 | | protected T[] _innerArray; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// The current number of elements in the SwiftList. Represents the total count of |
| | | 56 | | /// valid elements stored in the list, also indicating the arrayIndex of the next insertion point. |
| | | 57 | | /// </summary> |
| | | 58 | | protected int _count; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// The version of the SwiftList, used to track modifications. |
| | | 62 | | /// </summary> |
| | | 63 | | [NonSerialized] |
| | | 64 | | protected uint _version; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// An object that can be used to synchronize access to the SwiftList. |
| | | 68 | | /// </summary> |
| | | 69 | | [NonSerialized] |
| | | 70 | | private object? _syncRoot; |
| | | 71 | | |
| | | 72 | | #endregion |
| | | 73 | | |
| | | 74 | | #region Constructors |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Initializes a new instance of the SwiftList class that is empty and has the default initial capacity. |
| | | 78 | | /// </summary> |
| | 510 | 79 | | public SwiftList() : this(0) { } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Initializes a new, empty instance of <see cref="SwiftList{T}"/> with the specified initial capacity. |
| | | 83 | | /// </summary> |
| | 484 | 84 | | public SwiftList(int capacity) |
| | | 85 | | { |
| | 484 | 86 | | if (capacity == 0) |
| | 255 | 87 | | _innerArray = _emptyArray; |
| | | 88 | | else |
| | | 89 | | { |
| | 229 | 90 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity <= DefaultCapacity ? DefaultCapacity : capacity); |
| | 229 | 91 | | _innerArray = new T[capacity]; |
| | | 92 | | } |
| | 229 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Initializes a new instance of the <see cref="SwiftList{T}"/> class with elements from the specified collection. |
| | | 97 | | /// The collection must have a known count for optimized memory allocation. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <exception cref="ArgumentException">Thrown if the input collection does not have a known count.</exception> |
| | 13 | 100 | | public SwiftList(IEnumerable<T> items) |
| | | 101 | | { |
| | 13 | 102 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | | 103 | | |
| | 13 | 104 | | if (items is ICollection<T> collection) |
| | | 105 | | { |
| | 10 | 106 | | int count = collection.Count; |
| | 10 | 107 | | if (count == 0) |
| | 1 | 108 | | _innerArray = _emptyArray; |
| | | 109 | | else |
| | | 110 | | { |
| | 9 | 111 | | _innerArray = new T[count]; |
| | 9 | 112 | | collection.CopyTo(_innerArray, 0); |
| | 9 | 113 | | _count = count; |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | else |
| | | 117 | | { |
| | 3 | 118 | | _innerArray = new T[DefaultCapacity]; |
| | 3 | 119 | | AddRange(items); // Will handle capacity increases as needed |
| | | 120 | | } |
| | 3 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Initializes a new instance of the <see cref="SwiftList{T}"/> class with the specified <see cref="SwiftArrayStat |
| | | 125 | | /// </summary> |
| | | 126 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 127 | | [MemoryPackConstructor] |
| | 10 | 128 | | public SwiftList(SwiftArrayState<T> state) |
| | | 129 | | { |
| | 10 | 130 | | State = state; |
| | | 131 | | |
| | | 132 | | // Validate that the internal array is not null after deserialization |
| | 10 | 133 | | SwiftThrowHelper.ThrowIfNull(_innerArray, nameof(_innerArray)); |
| | 10 | 134 | | } |
| | | 135 | | |
| | | 136 | | #endregion |
| | | 137 | | |
| | | 138 | | #region Properties |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets the underlying array that stores the elements of the collection. |
| | | 142 | | /// </summary> |
| | | 143 | | [JsonIgnore] |
| | | 144 | | [MemoryPackIgnore] |
| | 8 | 145 | | public T[] InnerArray => _innerArray; |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Gets the total number of elements the SwiftList can hold without resizing. |
| | | 149 | | /// Reflects the current allocated size of the internal array. |
| | | 150 | | /// </summary> |
| | | 151 | | [JsonIgnore] |
| | | 152 | | [MemoryPackIgnore] |
| | 14 | 153 | | public int Capacity => _innerArray.Length; |
| | | 154 | | |
| | | 155 | | /// <inheritdoc cref="_count"/> |
| | | 156 | | [JsonIgnore] |
| | | 157 | | [MemoryPackIgnore] |
| | 837 | 158 | | public int Count => _count; |
| | | 159 | | |
| | | 160 | | /// <inheritdoc/> |
| | | 161 | | [JsonIgnore] |
| | | 162 | | [MemoryPackIgnore] |
| | 1 | 163 | | public bool IsReadOnly => false; |
| | | 164 | | |
| | | 165 | | /// <inheritdoc/> |
| | | 166 | | [JsonIgnore] |
| | | 167 | | [MemoryPackIgnore] |
| | 1 | 168 | | public bool IsSynchronized => false; |
| | | 169 | | |
| | | 170 | | /// <inheritdoc/> |
| | | 171 | | [JsonIgnore] |
| | | 172 | | [MemoryPackIgnore] |
| | 1 | 173 | | public object SyncRoot => _syncRoot ??= new object(); |
| | | 174 | | |
| | | 175 | | /// <inheritdoc/> |
| | | 176 | | [JsonIgnore] |
| | | 177 | | [MemoryPackIgnore] |
| | 1 | 178 | | public bool IsFixedSize => false; |
| | | 179 | | |
| | | 180 | | [JsonIgnore] |
| | | 181 | | [MemoryPackIgnore] |
| | | 182 | | object? IList.this[int index] |
| | | 183 | | { |
| | 1 | 184 | | get => this[index] ?? default!; |
| | | 185 | | set |
| | | 186 | | { |
| | | 187 | | try |
| | | 188 | | { |
| | 2 | 189 | | this[index] = (T)value!; |
| | 1 | 190 | | } |
| | 1 | 191 | | catch |
| | | 192 | | { |
| | 1 | 193 | | throw new NotSupportedException($"Unsupported value type for {value}"); |
| | | 194 | | } |
| | 1 | 195 | | } |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Gets the element at the specified arrayIndex. |
| | | 200 | | /// </summary> |
| | | 201 | | [JsonIgnore] |
| | | 202 | | [MemoryPackIgnore] |
| | | 203 | | public T this[int index] |
| | | 204 | | { |
| | | 205 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 206 | | get |
| | | 207 | | { |
| | 2500 | 208 | | SwiftThrowHelper.ThrowIfListIndexInvalid(index, _count, message: "Index is out of range."); |
| | 2496 | 209 | | return _innerArray[index]; |
| | | 210 | | } |
| | | 211 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 212 | | set |
| | | 213 | | { |
| | 1514 | 214 | | SwiftThrowHelper.ThrowIfListIndexInvalid(index, _count, message: "Index is out of range."); |
| | 1514 | 215 | | _innerArray[index] = value; |
| | 1514 | 216 | | } |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Gets or sets the current state of the array, including its items and count. |
| | | 221 | | /// </summary> |
| | | 222 | | /// <remarks> |
| | | 223 | | /// Setting this property replaces the entire contents of the array with the items from the specified state. |
| | | 224 | | /// The previous contents are discarded. |
| | | 225 | | /// The version is reset when the state is set. |
| | | 226 | | /// </remarks> |
| | | 227 | | [JsonInclude] |
| | | 228 | | [MemoryPackInclude] |
| | | 229 | | public SwiftArrayState<T> State |
| | | 230 | | { |
| | | 231 | | get |
| | | 232 | | { |
| | 9 | 233 | | var items = new T[_count]; |
| | 9 | 234 | | Array.Copy(_innerArray, 0, items, 0, _count); |
| | 9 | 235 | | return new SwiftArrayState<T>(items); |
| | | 236 | | } |
| | | 237 | | internal set |
| | | 238 | | { |
| | 10 | 239 | | SwiftThrowHelper.ThrowIfNull(value.Items, nameof(value.Items)); |
| | | 240 | | |
| | 10 | 241 | | int count = value.Items.Length; |
| | | 242 | | |
| | 10 | 243 | | if (count == 0) |
| | | 244 | | { |
| | 1 | 245 | | _innerArray = _emptyArray; |
| | 1 | 246 | | _count = 0; |
| | | 247 | | } |
| | | 248 | | else |
| | | 249 | | { |
| | 9 | 250 | | _innerArray = new T[count <= DefaultCapacity ? DefaultCapacity : count]; |
| | 9 | 251 | | Array.Copy(value.Items, 0, _innerArray, 0, count); |
| | 9 | 252 | | _count = count; |
| | | 253 | | } |
| | | 254 | | |
| | 10 | 255 | | _version = 0; |
| | 10 | 256 | | } |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | #endregion |
| | | 260 | | |
| | | 261 | | #region Collection Manipulation |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// Adds an object to the end of the SwiftList. |
| | | 265 | | /// </summary> |
| | | 266 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 267 | | public virtual void Add(T item) |
| | | 268 | | { |
| | 111795 | 269 | | if ((uint)_count == (uint)_innerArray.Length) |
| | 182 | 270 | | Resize(_innerArray.Length * 2); |
| | 111795 | 271 | | _innerArray[_count++] = item; |
| | 111795 | 272 | | _version++; |
| | 111795 | 273 | | } |
| | | 274 | | |
| | | 275 | | int IList.Add(object? value) |
| | | 276 | | { |
| | | 277 | | try |
| | | 278 | | { |
| | 2 | 279 | | Add((T)value!); |
| | 1 | 280 | | } |
| | 1 | 281 | | catch (InvalidCastException) |
| | | 282 | | { |
| | 1 | 283 | | throw new NotSupportedException($"Wrong value type for {value}"); |
| | | 284 | | } |
| | | 285 | | |
| | 1 | 286 | | return _count - 1; |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Adds the elements of the specified collection to the end of the SwiftList. |
| | | 291 | | /// </summary> |
| | | 292 | | public virtual void AddRange(IEnumerable<T> items) |
| | | 293 | | { |
| | 7 | 294 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | | 295 | | |
| | 7 | 296 | | if (items is ICollection<T> collection) |
| | | 297 | | { |
| | | 298 | | // Ensure capacity to fit all new items |
| | 3 | 299 | | if (_count + collection.Count > _innerArray.Length) |
| | | 300 | | { |
| | 0 | 301 | | int newCapacity = SwiftHashTools.NextPowerOfTwo(_count + collection.Count); |
| | 0 | 302 | | Resize(newCapacity); |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | // Copy new items directly into the internal array |
| | 3 | 306 | | collection.CopyTo(_innerArray, _count); |
| | 3 | 307 | | _count += collection.Count; |
| | 3 | 308 | | _version++; |
| | | 309 | | |
| | 3 | 310 | | return; |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | // Fallback for non-ICollection, adding each item individually |
| | 38 | 314 | | foreach (T item in items) |
| | 15 | 315 | | Add(item); |
| | 4 | 316 | | } |
| | | 317 | | |
| | | 318 | | /// <summary> |
| | | 319 | | /// Adds the elements of the specified array to the end of the SwiftList. |
| | | 320 | | /// </summary> |
| | | 321 | | /// <param name="items">The array whose elements should be appended.</param> |
| | | 322 | | public virtual void AddRange(T[] items) |
| | | 323 | | { |
| | 1 | 324 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | 1 | 325 | | AddRange(items.AsSpan()); |
| | 1 | 326 | | } |
| | | 327 | | |
| | | 328 | | /// <summary> |
| | | 329 | | /// Adds the elements of the specified span to the end of the SwiftList. |
| | | 330 | | /// </summary> |
| | | 331 | | /// <param name="items">The span whose elements should be appended.</param> |
| | | 332 | | public virtual void AddRange(ReadOnlySpan<T> items) |
| | | 333 | | { |
| | 3 | 334 | | if (items.Length == 0) |
| | 1 | 335 | | return; |
| | | 336 | | |
| | 2 | 337 | | if (_count + items.Length > _innerArray.Length) |
| | | 338 | | { |
| | 1 | 339 | | int newCapacity = SwiftHashTools.NextPowerOfTwo(_count + items.Length); |
| | 1 | 340 | | Resize(newCapacity); |
| | | 341 | | } |
| | | 342 | | |
| | 2 | 343 | | items.CopyTo(_innerArray.AsSpan(_count, items.Length)); |
| | 2 | 344 | | _count += items.Length; |
| | 2 | 345 | | _version++; |
| | 2 | 346 | | } |
| | | 347 | | |
| | | 348 | | /// <summary> |
| | | 349 | | /// Removes the first occurrence of a specific object from the SwiftList. |
| | | 350 | | /// </summary> |
| | | 351 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 352 | | public virtual bool Remove(T item) |
| | | 353 | | { |
| | 3 | 354 | | int index = IndexOf(item); |
| | 4 | 355 | | if (index < 0) return false; // Item not found return false; |
| | 2 | 356 | | RemoveAt(index); |
| | 2 | 357 | | return true; |
| | | 358 | | } |
| | | 359 | | |
| | | 360 | | void IList.Remove(object? value) |
| | | 361 | | { |
| | | 362 | | try |
| | | 363 | | { |
| | 2 | 364 | | Remove((T)value!); |
| | 1 | 365 | | } |
| | 1 | 366 | | catch (InvalidCastException) |
| | | 367 | | { |
| | 1 | 368 | | throw new NotSupportedException($"Wrong value type for {value}"); |
| | | 369 | | } |
| | 1 | 370 | | } |
| | | 371 | | |
| | | 372 | | /// <summary> |
| | | 373 | | /// Removes the element at the specified arrayIndex of the SwiftList. |
| | | 374 | | /// </summary> |
| | | 375 | | public virtual void RemoveAt(int index) |
| | | 376 | | { |
| | 604 | 377 | | SwiftThrowHelper.ThrowIfListIndexInvalid(index, _count, message: "Index is out of range."); |
| | 603 | 378 | | Array.Copy(_innerArray, index + 1, _innerArray, index, _count - index - 1); |
| | 603 | 379 | | _count--; |
| | 603 | 380 | | if (_clearReleasedSlots) |
| | 2 | 381 | | _innerArray[_count] = default!; |
| | 603 | 382 | | _version++; |
| | 603 | 383 | | } |
| | | 384 | | |
| | | 385 | | /// <summary> |
| | | 386 | | /// Removes all the elements that match the conditions defined by the specified predicate. |
| | | 387 | | /// </summary> |
| | | 388 | | public virtual int RemoveAll(Predicate<T> match) |
| | | 389 | | { |
| | 11 | 390 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 391 | | |
| | 10 | 392 | | int firstMatchIndex = IndexOfFirstMatch(match); |
| | 10 | 393 | | if (firstMatchIndex >= _count) |
| | 3 | 394 | | return 0; |
| | | 395 | | |
| | 7 | 396 | | int newCount = CompactUnmatchedItems(firstMatchIndex, match); |
| | 7 | 397 | | int removedCount = _count - newCount; |
| | 7 | 398 | | ClearReleasedSlots(newCount, removedCount); |
| | 7 | 399 | | _count = newCount; |
| | 7 | 400 | | _version++; |
| | | 401 | | |
| | 7 | 402 | | return removedCount; |
| | | 403 | | } |
| | | 404 | | |
| | | 405 | | private int IndexOfFirstMatch(Predicate<T> match) |
| | | 406 | | { |
| | 10 | 407 | | int index = 0; |
| | 25 | 408 | | while (index < _count && !match(_innerArray[index])) |
| | 15 | 409 | | index++; |
| | | 410 | | |
| | 10 | 411 | | return index; |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | private int CompactUnmatchedItems(int writeIndex, Predicate<T> match) |
| | | 415 | | { |
| | 200042 | 416 | | for (int readIndex = writeIndex + 1; readIndex < _count; readIndex++) |
| | | 417 | | { |
| | 100014 | 418 | | if (!match(_innerArray[readIndex])) |
| | 50006 | 419 | | _innerArray[writeIndex++] = _innerArray[readIndex]; |
| | | 420 | | } |
| | | 421 | | |
| | 7 | 422 | | return writeIndex; |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | private void ClearReleasedSlots(int startIndex, int count) |
| | | 426 | | { |
| | 7 | 427 | | if (_clearReleasedSlots && count > 0) |
| | 2 | 428 | | Array.Clear(_innerArray, startIndex, count); |
| | 7 | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// Inserts an element into the SwiftList at the specified arrayIndex. |
| | | 433 | | /// </summary> |
| | | 434 | | public virtual void Insert(int index, T item) |
| | | 435 | | { |
| | 7 | 436 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(index, _count, message: "Index is out of range."); |
| | 6 | 437 | | if ((uint)_count == (uint)_innerArray.Length) |
| | 0 | 438 | | Resize(_innerArray.Length * 2); |
| | 6 | 439 | | if ((uint)index < (uint)_count) |
| | 4 | 440 | | Array.Copy(_innerArray, index, _innerArray, index + 1, _count - index); |
| | 6 | 441 | | _innerArray[index] = item; |
| | 6 | 442 | | _count++; |
| | 6 | 443 | | _version++; |
| | 6 | 444 | | } |
| | | 445 | | |
| | | 446 | | void IList.Insert(int index, object? value) |
| | | 447 | | { |
| | | 448 | | try |
| | | 449 | | { |
| | 2 | 450 | | Insert(index, (T)value!); |
| | 1 | 451 | | } |
| | 1 | 452 | | catch (InvalidCastException) |
| | | 453 | | { |
| | 1 | 454 | | throw new NotSupportedException($"Wrong value type for {value}"); |
| | | 455 | | } |
| | 1 | 456 | | } |
| | | 457 | | |
| | | 458 | | /// <summary> |
| | | 459 | | /// Reverses the order of the elements in the entire <see cref="SwiftList{T}"/>. |
| | | 460 | | /// </summary> |
| | | 461 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 462 | | public void Reverse() |
| | | 463 | | { |
| | 1 | 464 | | Array.Reverse(_innerArray, 0, _count); |
| | 1 | 465 | | _version++; |
| | 1 | 466 | | } |
| | | 467 | | |
| | | 468 | | /// <summary> |
| | | 469 | | /// Sorts the elements in the <see cref="SwiftList{T}"/> using the <see cref="IComparable"/> interface implementatio |
| | | 470 | | /// </summary> |
| | | 471 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 472 | | public void Sort() |
| | | 473 | | { |
| | 1 | 474 | | Array.Sort(_innerArray, 0, _count, Comparer<T>.Default); |
| | 1 | 475 | | _version++; |
| | 1 | 476 | | } |
| | | 477 | | |
| | | 478 | | /// <summary> |
| | | 479 | | /// Sorts the elements in the entire <see cref="SwiftList{T}"/> using the specified comparer. |
| | | 480 | | /// </summary> |
| | | 481 | | /// <param name="comparer">The comparer to use for comparing elements.</param> |
| | | 482 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 483 | | public void Sort(IComparer<T> comparer) |
| | | 484 | | { |
| | 2 | 485 | | Array.Sort(_innerArray, 0, _count, comparer); |
| | 2 | 486 | | _version++; |
| | 2 | 487 | | } |
| | | 488 | | |
| | | 489 | | /// <summary> |
| | | 490 | | /// Removes all elements from the <see cref="SwiftList{T}"/>, resetting its count to zero. |
| | | 491 | | /// </summary> |
| | | 492 | | public virtual void Clear() |
| | | 493 | | { |
| | 9 | 494 | | if (_clearReleasedSlots) |
| | 1 | 495 | | Array.Clear(_innerArray, 0, _count); |
| | 9 | 496 | | _count = 0; |
| | 9 | 497 | | _version++; |
| | 9 | 498 | | } |
| | | 499 | | |
| | | 500 | | /// <summary> |
| | | 501 | | /// Clears the <see cref="SwiftList{T}"/> without releasing the reference to the stored elements. |
| | | 502 | | /// Use FastClear() when you want to quickly reset the list without reallocating memory. |
| | | 503 | | /// </summary> |
| | | 504 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 505 | | public void FastClear() |
| | | 506 | | { |
| | 1 | 507 | | _count = 0; |
| | 1 | 508 | | _version++; |
| | 1 | 509 | | } |
| | | 510 | | |
| | | 511 | | #endregion |
| | | 512 | | |
| | | 513 | | #region Capacity Management |
| | | 514 | | |
| | | 515 | | /// <summary> |
| | | 516 | | /// Ensures that the capacity of <see cref="SwiftList{T}"/> is sufficient to accommodate the specified number of ele |
| | | 517 | | /// The capacity can increase by double to balance memory allocation efficiency and space. |
| | | 518 | | /// </summary> |
| | | 519 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 520 | | public void EnsureCapacity(int capacity) |
| | | 521 | | { |
| | 2 | 522 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 2 | 523 | | if (capacity > _innerArray.Length) |
| | 1 | 524 | | Resize(capacity); |
| | 2 | 525 | | } |
| | | 526 | | |
| | | 527 | | /// <summary> |
| | | 528 | | /// Resizes the internal array to accommodate the specified number of elements. |
| | | 529 | | /// </summary> |
| | | 530 | | /// <remarks> |
| | | 531 | | /// If the specified size is less than or equal to the default capacity, the internal array is |
| | | 532 | | /// set to the default capacity. |
| | | 533 | | /// Existing elements are preserved up to the current count. |
| | | 534 | | /// </remarks> |
| | | 535 | | /// <param name="newSize">The desired new size of the internal array. Must be greater than or equal to zero.</param> |
| | | 536 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 537 | | protected void Resize(int newSize) |
| | | 538 | | { |
| | 185 | 539 | | int newCapacity = newSize <= DefaultCapacity ? DefaultCapacity : newSize; |
| | | 540 | | |
| | 185 | 541 | | T[] newArray = new T[newCapacity]; |
| | 185 | 542 | | if (_count > 0) |
| | 42 | 543 | | Array.Copy(_innerArray, 0, newArray, 0, _count); |
| | 185 | 544 | | _innerArray = newArray; |
| | 185 | 545 | | _version++; |
| | 185 | 546 | | } |
| | | 547 | | |
| | | 548 | | /// <summary> |
| | | 549 | | /// Reduces the capacity of the SwiftList if the element count falls below 50% of the current capacity. |
| | | 550 | | /// Ensures efficient memory usage by resizing the internal array to match the current count when necessary. |
| | | 551 | | /// </summary> |
| | | 552 | | public void TrimExcessCapacity() |
| | | 553 | | { |
| | 2 | 554 | | int newCapacity = _count < DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(_count); |
| | 2 | 555 | | Array.Resize(ref _innerArray, newCapacity); |
| | 2 | 556 | | _version++; |
| | 2 | 557 | | } |
| | | 558 | | |
| | | 559 | | #endregion |
| | | 560 | | |
| | | 561 | | #region Utility Methods |
| | | 562 | | |
| | | 563 | | /// <summary> |
| | | 564 | | /// Searches for the specified object and returns the zero-based arrayIndex of the first occurrence within the Swift |
| | | 565 | | /// </summary> |
| | | 566 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 513 | 567 | | public int IndexOf(T item) => Array.IndexOf(_innerArray, item, 0, _count); |
| | | 568 | | |
| | | 569 | | int IList.IndexOf(object? value) |
| | | 570 | | { |
| | | 571 | | int index; |
| | | 572 | | try |
| | | 573 | | { |
| | 2 | 574 | | index = Array.IndexOf(_innerArray, (T)value!, 0, _count); |
| | 1 | 575 | | } |
| | 1 | 576 | | catch |
| | | 577 | | { |
| | 1 | 578 | | throw new NotSupportedException($"Unsupported value type for {value}"); |
| | | 579 | | } |
| | 1 | 580 | | return index; |
| | | 581 | | } |
| | | 582 | | |
| | | 583 | | /// <summary> |
| | | 584 | | /// Copies the elements of the SwiftList to a new array. |
| | | 585 | | /// </summary> |
| | | 586 | | public T[] ToArray() |
| | | 587 | | { |
| | 27 | 588 | | T[] result = new T[_count]; |
| | 27 | 589 | | Array.Copy(_innerArray, result, _count); |
| | 27 | 590 | | return result; |
| | | 591 | | } |
| | | 592 | | |
| | | 593 | | /// <summary> |
| | | 594 | | /// Returns a mutable span over the populated portion of the list. |
| | | 595 | | /// </summary> |
| | | 596 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 597 | | public Span<T> AsSpan() => _innerArray.AsSpan(0, _count); |
| | | 598 | | |
| | | 599 | | /// <summary> |
| | | 600 | | /// Returns a read-only span over the populated portion of the list. |
| | | 601 | | /// </summary> |
| | | 602 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 603 | | public ReadOnlySpan<T> AsReadOnlySpan() => _innerArray.AsSpan(0, _count); |
| | | 604 | | |
| | | 605 | | /// <summary> |
| | | 606 | | /// Returns a string that represents the current collection. |
| | | 607 | | /// </summary> |
| | | 608 | | /// <remarks> |
| | | 609 | | /// This method provides a human-readable representation of the collection's contents, |
| | | 610 | | /// which can be useful for debugging or logging purposes. |
| | | 611 | | /// </remarks> |
| | | 612 | | /// <returns>A comma-separated list of the collection's elements, or the default string representation if the collec |
| | | 613 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 614 | | public override string ToString() => _count == 0 |
| | 2 | 615 | | ? base.ToString() ?? string.Empty |
| | 2 | 616 | | : string.Join(", ", this); |
| | | 617 | | |
| | | 618 | | /// <summary> |
| | | 619 | | /// Determines whether an element is in the SwiftList. |
| | | 620 | | /// </summary> |
| | | 621 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 622 | | public bool Contains(T item) => IndexOf(item) != -1; |
| | | 623 | | |
| | | 624 | | /// <summary> |
| | | 625 | | /// Determines whether the <see cref="SwiftList{T}"/> contains an element that matches the conditions defined by the |
| | | 626 | | /// </summary> |
| | | 627 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 628 | | /// <returns><c>true</c> if the <see cref="SwiftList{T}"/> contains one or more elements that match the specified pr |
| | | 629 | | public bool Exists(Predicate<T> match) |
| | | 630 | | { |
| | 3 | 631 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 632 | | |
| | 14 | 633 | | for (int i = 0; i < _count; i++) |
| | | 634 | | { |
| | 6 | 635 | | if (match(_innerArray[i])) |
| | 1 | 636 | | return true; |
| | | 637 | | } |
| | | 638 | | |
| | 1 | 639 | | return false; |
| | | 640 | | } |
| | | 641 | | |
| | | 642 | | /// <summary> |
| | | 643 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first oc |
| | | 644 | | /// </summary> |
| | | 645 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 646 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 647 | | public T Find(Predicate<T> match) |
| | | 648 | | { |
| | 4 | 649 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 650 | | |
| | 20 | 651 | | for (int i = 0; i < _count; i++) |
| | | 652 | | { |
| | 8 | 653 | | if (match(_innerArray[i])) |
| | 1 | 654 | | return _innerArray[i]; |
| | | 655 | | } |
| | | 656 | | |
| | 2 | 657 | | return default!; |
| | | 658 | | } |
| | | 659 | | |
| | | 660 | | bool IList.Contains(object? value) |
| | | 661 | | { |
| | | 662 | | int index; |
| | | 663 | | try |
| | | 664 | | { |
| | 2 | 665 | | index = IndexOf((T)value!); |
| | 1 | 666 | | } |
| | 1 | 667 | | catch |
| | | 668 | | { |
| | 1 | 669 | | throw new NotSupportedException($"Unsupported value type for {value}"); |
| | | 670 | | } |
| | 1 | 671 | | return index != -1; |
| | | 672 | | } |
| | | 673 | | |
| | | 674 | | /// <summary> |
| | | 675 | | /// Swaps the values of two elements in the SwiftList. |
| | | 676 | | /// This method exchanges the values referenced by two variables. |
| | | 677 | | /// </summary> |
| | | 678 | | /// <param name="indexA">The first element to swap.</param> |
| | | 679 | | /// <param name="indexB">The second element to swap.</param> |
| | | 680 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 681 | | public void Swap(int indexA, int indexB) => (_innerArray[indexB], _innerArray[indexA]) = (_innerArray[indexA], _inne |
| | | 682 | | |
| | | 683 | | /// <summary> |
| | | 684 | | /// Copies the elements of the SwiftList to the specified target SwiftList. |
| | | 685 | | /// The target list will resize if it lacks sufficient capacity, |
| | | 686 | | /// but retains any existing elements beyond the copied range. |
| | | 687 | | /// </summary> |
| | | 688 | | public void CopyTo(SwiftList<T> target) |
| | | 689 | | { |
| | 2 | 690 | | if (_count + 1 > target._innerArray.Length) |
| | 1 | 691 | | target.Resize(target._innerArray.Length * 2); |
| | 2 | 692 | | Array.Copy(_innerArray, 0, target._innerArray, 0, _count); |
| | 2 | 693 | | target._count = _count; |
| | 2 | 694 | | target._version++; |
| | 2 | 695 | | } |
| | | 696 | | |
| | | 697 | | /// <inheritdoc/> |
| | | 698 | | public void CopyTo(T[] array, int arrayIndex) |
| | | 699 | | { |
| | 4 | 700 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 4 | 701 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(arrayIndex, array.Length, message: "Array index is out of range."); |
| | 4 | 702 | | SwiftThrowHelper.ThrowIfArgument(array.Length - arrayIndex < _count, nameof(array), "Destination array is not lo |
| | | 703 | | |
| | 2 | 704 | | Array.Copy(_innerArray, 0, array, arrayIndex, _count); |
| | 2 | 705 | | } |
| | | 706 | | |
| | | 707 | | /// <summary> |
| | | 708 | | /// Copies the populated elements of the SwiftList into the specified destination span. |
| | | 709 | | /// </summary> |
| | | 710 | | /// <param name="destination">The destination span.</param> |
| | | 711 | | public void CopyTo(Span<T> destination) |
| | | 712 | | { |
| | 2 | 713 | | SwiftThrowHelper.ThrowIfArgument(destination.Length < _count, nameof(destination), "Destination span is not long |
| | | 714 | | |
| | 1 | 715 | | AsSpan().CopyTo(destination); |
| | 1 | 716 | | } |
| | | 717 | | |
| | | 718 | | /// <inheritdoc/> |
| | | 719 | | public void CopyTo(Array array, int arrayIndex) |
| | | 720 | | { |
| | 3 | 721 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 3 | 722 | | SwiftThrowHelper.ThrowIfArgument(array.Rank != 1, nameof(array), "Array must be single dimensional."); |
| | 2 | 723 | | SwiftThrowHelper.ThrowIfArgument(array.GetLowerBound(0) != 0, nameof(array), "Array must have zero-based indexin |
| | 1 | 724 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(arrayIndex, array.Length, message: "Array index is out of range."); |
| | 1 | 725 | | SwiftThrowHelper.ThrowIfArgument(array.Length - arrayIndex < _count, nameof(array), "Destination array is not lo |
| | | 726 | | |
| | 1 | 727 | | Array.Copy(_innerArray, 0, array, arrayIndex, _count); |
| | 1 | 728 | | } |
| | | 729 | | |
| | | 730 | | /// <inheritdoc/> |
| | | 731 | | public void CloneTo(ICollection<T> output) |
| | | 732 | | { |
| | 1 | 733 | | output.Clear(); |
| | 8 | 734 | | foreach (var item in this) |
| | 3 | 735 | | output.Add(item); |
| | 1 | 736 | | } |
| | | 737 | | |
| | | 738 | | #endregion |
| | | 739 | | |
| | | 740 | | #region Enumerators |
| | | 741 | | |
| | | 742 | | /// <summary> |
| | | 743 | | /// Returns an enumerator that iterates through the <see cref="SwiftList{T}"/>. |
| | | 744 | | /// </summary> |
| | 26 | 745 | | public SwiftListEnumerator GetEnumerator() => new(this); |
| | 21 | 746 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 2 | 747 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 748 | | |
| | | 749 | | /// <summary> |
| | | 750 | | /// Enumerates the elements of a <see cref="SwiftList{T}"/> collection. |
| | | 751 | | /// </summary> |
| | | 752 | | /// <remarks> |
| | | 753 | | /// The enumerator provides read-only, forward-only iteration over the collection. |
| | | 754 | | /// The enumerator is invalidated if the collection is modified after the enumerator is created. |
| | | 755 | | /// In such cases, calling MoveNext or Reset will throw an InvalidOperationException. |
| | | 756 | | /// </remarks> |
| | | 757 | | public struct SwiftListEnumerator : IEnumerator<T>, IEnumerator, IDisposable |
| | | 758 | | { |
| | | 759 | | private readonly SwiftList<T> _list; |
| | | 760 | | private readonly T[] _array; |
| | | 761 | | private readonly uint _count; |
| | | 762 | | private readonly uint _version; |
| | | 763 | | private uint _index; |
| | | 764 | | |
| | | 765 | | private T _current; |
| | | 766 | | |
| | | 767 | | internal SwiftListEnumerator(SwiftList<T> list) |
| | | 768 | | { |
| | 26 | 769 | | _list = list; |
| | 26 | 770 | | _array = list._innerArray; |
| | 26 | 771 | | _count = (uint)list._count; |
| | 26 | 772 | | _version = list._version; |
| | 26 | 773 | | _index = 0; |
| | 26 | 774 | | _current = default!; |
| | 26 | 775 | | } |
| | | 776 | | |
| | | 777 | | /// <inheritdoc/> |
| | 436 | 778 | | public T Current => _current; |
| | | 779 | | |
| | | 780 | | object IEnumerator.Current |
| | | 781 | | { |
| | | 782 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 783 | | get |
| | | 784 | | { |
| | 3 | 785 | | SwiftThrowHelper.ThrowIfTrue(_index >= _count, message: "Enumeration has either not started or has alrea |
| | 2 | 786 | | return _current!; |
| | | 787 | | } |
| | | 788 | | } |
| | | 789 | | |
| | | 790 | | /// <inheritdoc/> |
| | | 791 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 792 | | public bool MoveNext() |
| | | 793 | | { |
| | 249 | 794 | | SwiftThrowHelper.ThrowIfTrue(_version != _list._version, message: "Collection was modified during enumeratio |
| | | 795 | | |
| | 270 | 796 | | if (_index >= _count) return false; |
| | 226 | 797 | | _current = _array[_index++]; |
| | 226 | 798 | | return true; |
| | | 799 | | } |
| | | 800 | | |
| | | 801 | | /// <inheritdoc/> |
| | | 802 | | public void Reset() |
| | | 803 | | { |
| | 2 | 804 | | SwiftThrowHelper.ThrowIfTrue(_version != _list._version, message: "Collection was modified during enumeratio |
| | | 805 | | |
| | 1 | 806 | | _index = 0; |
| | 1 | 807 | | _current = default!; |
| | 1 | 808 | | } |
| | | 809 | | |
| | | 810 | | /// <inheritdoc/> |
| | 22 | 811 | | public void Dispose() => _index = 0; |
| | | 812 | | } |
| | | 813 | | |
| | | 814 | | #endregion |
| | | 815 | | } |