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