| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftSortedList.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using Chronicler; |
| | | 9 | | using MemoryPack; |
| | | 10 | | using SwiftCollections.Diagnostics; |
| | | 11 | | using SwiftCollections.Utility; |
| | | 12 | | using System; |
| | | 13 | | using System.Collections; |
| | | 14 | | using System.Collections.Generic; |
| | | 15 | | using System.Linq; |
| | | 16 | | using System.Runtime.CompilerServices; |
| | | 17 | | using System.Text.Json.Serialization; |
| | | 18 | | |
| | | 19 | | namespace SwiftCollections; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Represents a dynamically sorted collection of elements. |
| | | 23 | | /// Provides efficient O(log n) operations for adding, removing, and checking for the presence of elements. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <remarks> |
| | | 26 | | /// The comparer is not serialized. After deserialization the list uses |
| | | 27 | | /// <see cref="Comparer{T}.Default"/>. |
| | | 28 | | /// |
| | | 29 | | /// If a custom comparer is required it can be reapplied using |
| | | 30 | | /// <see cref="SetComparer(IComparer{T})"/>. |
| | | 31 | | /// </remarks> |
| | | 32 | | /// <typeparam name="T">The type of elements in the collection.</typeparam> |
| | | 33 | | [Serializable] |
| | | 34 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 35 | | [MemoryPackable] |
| | | 36 | | public partial class SwiftSortedList<T> : IStateBacked<SwiftArrayState<T>>, ISwiftCloneable<T>, IEnumerable<T>, IEnumera |
| | | 37 | | { |
| | | 38 | | #region Constants |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The default initial capacity of the <see cref="SwiftSortedList{T}"/> if none is specified. |
| | | 42 | | /// Used to allocate a reasonable starting size to minimize resizing operations. |
| | | 43 | | /// </summary> |
| | | 44 | | public const int DefaultCapacity = 8; |
| | | 45 | | |
| | 2 | 46 | | private static readonly T[] _emptyArray = Array.Empty<T>(); |
| | | 47 | | |
| | | 48 | | #endregion |
| | | 49 | | |
| | | 50 | | #region Fields |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Represents the internal array that stores the sorted elements. |
| | | 54 | | /// </summary> |
| | | 55 | | private T[] _innerArray; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// The number of elements contained in the <see cref="SwiftSortedList{T}"/>. |
| | | 59 | | /// </summary> |
| | | 60 | | private int _count; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// The offset within the internal array where the logical start of the list begins. |
| | | 64 | | /// Used to efficiently manage insertions and deletions at both ends without excessive shifting. |
| | | 65 | | /// </summary> |
| | | 66 | | private int _offset; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// A version counter used to track modifications to the sorted list. |
| | | 70 | | /// Incremented on mutations to detect changes during enumeration and ensure enumerator validity. |
| | | 71 | | /// </summary> |
| | | 72 | | [NonSerialized] |
| | | 73 | | private uint _version; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// The comparer used to sort and compare elements in the collection. |
| | | 77 | | /// </summary> |
| | | 78 | | [NonSerialized] |
| | | 79 | | private IComparer<T> _comparer; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// An object that can be used to synchronize access to the SwiftList. |
| | | 83 | | /// </summary> |
| | | 84 | | [NonSerialized] |
| | | 85 | | private object? _syncRoot; |
| | | 86 | | |
| | | 87 | | #endregion |
| | | 88 | | |
| | | 89 | | #region Constructors |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Initializes a new instance of the SwiftSortedList class with the default capacity. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <remarks> |
| | | 95 | | /// This constructor creates an empty sorted list. |
| | | 96 | | /// Items added to the list will be automatically ordered according to the default comparer for the item type. |
| | | 97 | | /// </remarks> |
| | 116 | 98 | | public SwiftSortedList() : this(0) { } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Initializes a new, empty instance of <see cref="SwiftSortedList{T}"/> uisng the specified <see cref="IComparer{T |
| | | 102 | | /// </summary> |
| | 6 | 103 | | public SwiftSortedList(IComparer<T> comparer) : this(0, comparer) { } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Initializes a new, empty instance of <see cref="SwiftSortedList{T}"/> with the specified initial capacity and <s |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="capacity">The starting initial capacity.</param> |
| | | 109 | | /// <param name="comparer">The comparer to use. If null, the default comparer is used.</param> |
| | 71 | 110 | | public SwiftSortedList(int capacity, IComparer<T>? comparer = null) |
| | | 111 | | { |
| | 71 | 112 | | _comparer = comparer ?? Comparer<T>.Default; |
| | | 113 | | |
| | 71 | 114 | | if (capacity == 0) |
| | | 115 | | { |
| | 61 | 116 | | _innerArray = _emptyArray; |
| | 61 | 117 | | _offset = 0; |
| | | 118 | | } |
| | | 119 | | else |
| | | 120 | | { |
| | 10 | 121 | | capacity = capacity <= DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(capacity); |
| | 10 | 122 | | _innerArray = new T[capacity]; |
| | 10 | 123 | | _offset = capacity >> 1; // initial offset half of capacity |
| | | 124 | | } |
| | 10 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Initializes a new instance of the <see cref="SwiftList{T}"/> class with elements from the specified collection. |
| | | 129 | | /// The collection must have a known count for optimized memory allocation. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <exception cref="ArgumentException">Thrown if the input collection does not have a known count.</exception> |
| | 4 | 132 | | public SwiftSortedList(IEnumerable<T> items, IComparer<T>? comparer = null) |
| | | 133 | | { |
| | 4 | 134 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | | 135 | | |
| | 4 | 136 | | _comparer = comparer ?? Comparer<T>.Default; |
| | | 137 | | |
| | 4 | 138 | | if (items is ICollection<T> collection) |
| | | 139 | | { |
| | 3 | 140 | | int count = collection.Count; |
| | 3 | 141 | | if (count == 0) |
| | | 142 | | { |
| | 1 | 143 | | _innerArray = _emptyArray; |
| | 1 | 144 | | _offset = 0; |
| | | 145 | | } |
| | | 146 | | else |
| | | 147 | | { |
| | 2 | 148 | | int capacity = SwiftHashTools.NextPowerOfTwo(count <= DefaultCapacity ? DefaultCapacity : count); |
| | 2 | 149 | | _innerArray = new T[capacity]; |
| | 2 | 150 | | _offset = (capacity - count) >> 1; |
| | 2 | 151 | | collection.CopyTo(_innerArray, _offset); |
| | 2 | 152 | | SwiftArraySortHelper.Sort(_innerArray, _offset, count, _comparer); |
| | 2 | 153 | | _count = count; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | else |
| | | 157 | | { |
| | 1 | 158 | | _innerArray = new T[DefaultCapacity]; |
| | 1 | 159 | | _offset = DefaultCapacity >> 1; |
| | 1 | 160 | | AddRange(items); // Will handle capacity increases as needed |
| | | 161 | | } |
| | 1 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Initializes a new instance of the <see cref="SwiftSortedList{T}"/> class with the specified <see cref="SwiftArr |
| | | 166 | | /// </summary> |
| | | 167 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 168 | | [MemoryPackConstructor] |
| | 6 | 169 | | public SwiftSortedList(SwiftArrayState<T> state) |
| | | 170 | | { |
| | 6 | 171 | | State = state; |
| | | 172 | | |
| | | 173 | | // Validate that the internal array and comparer are not null after deserialization |
| | 6 | 174 | | SwiftThrowHelper.ThrowIfNull(_innerArray, nameof(_innerArray)); |
| | 6 | 175 | | SwiftThrowHelper.ThrowIfNull(_comparer, nameof(_comparer)); |
| | 6 | 176 | | } |
| | | 177 | | |
| | | 178 | | #endregion |
| | | 179 | | |
| | | 180 | | #region Properties |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Gets the underlying array that stores the elements of the collection. |
| | | 184 | | /// </summary> |
| | | 185 | | /// <remarks> |
| | | 186 | | /// The returned array may contain unused elements beyond the logical contents of the collection. |
| | | 187 | | ///</remarks> |
| | | 188 | | [JsonIgnore] |
| | | 189 | | [MemoryPackIgnore] |
| | 9 | 190 | | public T[] InnerArray => _innerArray; |
| | | 191 | | |
| | | 192 | | /// <inheritdoc cref="_count"/> |
| | | 193 | | [JsonIgnore] |
| | | 194 | | [MemoryPackIgnore] |
| | 29 | 195 | | public int Count => _count; |
| | | 196 | | |
| | | 197 | | /// <inheritdoc cref="_offset"/> |
| | | 198 | | [JsonIgnore] |
| | | 199 | | [MemoryPackIgnore] |
| | 22 | 200 | | public int Offset => _offset; |
| | | 201 | | |
| | | 202 | | /// <inheritdoc cref="_version"/> |
| | | 203 | | [JsonIgnore] |
| | | 204 | | [MemoryPackIgnore] |
| | 5 | 205 | | public uint Version => _version; |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Gets the current capacity of the internal array. |
| | | 209 | | /// </summary> |
| | | 210 | | [JsonIgnore] |
| | | 211 | | [MemoryPackIgnore] |
| | 20 | 212 | | public int Capacity => _innerArray.Length; |
| | | 213 | | |
| | | 214 | | /// <inheritdoc cref="_comparer"/> |
| | | 215 | | [JsonIgnore] |
| | | 216 | | [MemoryPackIgnore] |
| | 1 | 217 | | public IComparer<T> Comparer => _comparer; |
| | | 218 | | |
| | | 219 | | /// <inheritdoc/> |
| | | 220 | | [JsonIgnore] |
| | | 221 | | [MemoryPackIgnore] |
| | 1 | 222 | | public bool IsReadOnly => false; |
| | | 223 | | |
| | | 224 | | /// <inheritdoc/> |
| | | 225 | | [JsonIgnore] |
| | | 226 | | [MemoryPackIgnore] |
| | 1 | 227 | | public bool IsSynchronized => false; |
| | | 228 | | |
| | | 229 | | /// <inheritdoc/> |
| | | 230 | | [JsonIgnore] |
| | | 231 | | [MemoryPackIgnore] |
| | 1 | 232 | | public object SyncRoot => _syncRoot ??= new object(); |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Gets the element at the specified arrayIndex. |
| | | 236 | | /// </summary> |
| | | 237 | | [JsonIgnore] |
| | | 238 | | [MemoryPackIgnore] |
| | | 239 | | public T this[int index] |
| | | 240 | | { |
| | | 241 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 242 | | get |
| | | 243 | | { |
| | 2 | 244 | | SwiftThrowHelper.ThrowIfListIndexInvalid(index, _count); |
| | 1 | 245 | | return _innerArray[GetPhysicalIndex(index)]; |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// Gets or sets the current state of the array, including its items and structure. |
| | | 251 | | /// </summary> |
| | | 252 | | /// <remarks> |
| | | 253 | | /// Setting this property replaces the entire contents of the array with the specified state. |
| | | 254 | | /// The setter is intended for internal use and may reset internal metadata such as version and comparer. |
| | | 255 | | /// This property is intended for serialization and deserialization scenarios. |
| | | 256 | | /// </remarks> |
| | | 257 | | [JsonInclude] |
| | | 258 | | [MemoryPackInclude] |
| | | 259 | | public SwiftArrayState<T> State |
| | | 260 | | { |
| | | 261 | | get |
| | | 262 | | { |
| | 3 | 263 | | var items = new T[_count]; |
| | 3 | 264 | | Array.Copy(_innerArray, _offset, items, 0, _count); |
| | 3 | 265 | | return new SwiftArrayState<T>(items); |
| | | 266 | | } |
| | | 267 | | internal set |
| | | 268 | | { |
| | 6 | 269 | | SwiftThrowHelper.ThrowIfNull(value.Items, nameof(value.Items)); |
| | | 270 | | |
| | 6 | 271 | | int count = value.Items.Length; |
| | | 272 | | |
| | 6 | 273 | | if (count == 0) |
| | | 274 | | { |
| | 1 | 275 | | _innerArray = _emptyArray; |
| | 1 | 276 | | _offset = 0; |
| | 1 | 277 | | _count = 0; |
| | | 278 | | } |
| | | 279 | | else |
| | | 280 | | { |
| | 5 | 281 | | int capacity = SwiftHashTools.NextPowerOfTwo(count <= DefaultCapacity ? DefaultCapacity : count); |
| | 5 | 282 | | _innerArray = new T[capacity]; |
| | | 283 | | |
| | 5 | 284 | | int newOffset = (capacity - count) >> 1; |
| | | 285 | | |
| | 5 | 286 | | Array.Copy(value.Items, 0, _innerArray, newOffset, count); |
| | | 287 | | |
| | 5 | 288 | | _offset = newOffset; |
| | 5 | 289 | | _count = count; |
| | | 290 | | } |
| | | 291 | | |
| | 6 | 292 | | _version = 0; |
| | 6 | 293 | | _comparer = Comparer<T>.Default; |
| | 6 | 294 | | } |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | #endregion |
| | | 298 | | |
| | | 299 | | #region Collection Manipulation |
| | | 300 | | |
| | | 301 | | /// <inheritdoc/> |
| | | 302 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 303 | | public void Add(T item) |
| | | 304 | | { |
| | 101 | 305 | | int index = Search(item); |
| | 201 | 306 | | if (index < 0) index = ~index; |
| | 101 | 307 | | Insert(item, index); |
| | 101 | 308 | | } |
| | | 309 | | |
| | | 310 | | /// <summary> |
| | | 311 | | /// Inserts an item into the internal array at the specified arrayIndex, shifting elements as needed. |
| | | 312 | | /// </summary> |
| | | 313 | | private void Insert(T item, int index) |
| | | 314 | | { |
| | 101 | 315 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(index, _count, nameof(index)); |
| | 101 | 316 | | if (_offset + _count + 1 > _innerArray.Length) |
| | 33 | 317 | | Resize(_innerArray.Length * 2); |
| | | 318 | | |
| | 101 | 319 | | int physicalIndex = GetPhysicalIndex(index); |
| | | 320 | | |
| | 101 | 321 | | if (index < (uint)_count) |
| | | 322 | | { |
| | 38 | 323 | | int distanceToHead = physicalIndex - _offset; |
| | 38 | 324 | | int distanceToTail = (_offset + _count - 1) - physicalIndex; |
| | | 325 | | |
| | 38 | 326 | | if (distanceToHead >= (uint)distanceToTail) |
| | | 327 | | { |
| | 34 | 328 | | if ((uint)_offset == 0) |
| | | 329 | | { |
| | | 330 | | // Ensure capacity for recentering |
| | 1 | 331 | | Resize(_innerArray.Length * 2); |
| | 1 | 332 | | RecenterArray(); |
| | 1 | 333 | | physicalIndex = GetPhysicalIndex(index); |
| | | 334 | | } |
| | | 335 | | |
| | 34 | 336 | | _offset--; |
| | 34 | 337 | | physicalIndex--; |
| | | 338 | | // Shift elements towards the head (left) |
| | 34 | 339 | | Array.Copy(_innerArray, _offset + 1, _innerArray, _offset, distanceToHead + 1); |
| | | 340 | | } |
| | | 341 | | else // Shift elements towards the tail (right) to make space |
| | 4 | 342 | | Array.Copy(_innerArray, physicalIndex, _innerArray, physicalIndex + 1, _count - index); |
| | | 343 | | } |
| | | 344 | | |
| | 101 | 345 | | _innerArray[physicalIndex] = item; |
| | 101 | 346 | | _count++; |
| | | 347 | | |
| | 101 | 348 | | _version++; |
| | 101 | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// Adds a range of elements to the collection, ensuring they are sorted and merged efficiently. |
| | | 353 | | /// </summary> |
| | | 354 | | /// <remarks> |
| | | 355 | | /// This compacts the active item range for efficiency. Known-count sources reuse existing capacity when possible. |
| | | 356 | | /// </remarks> |
| | | 357 | | public void AddRange(IEnumerable<T> items) |
| | | 358 | | { |
| | 40 | 359 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | | 360 | | |
| | 40 | 361 | | if (items is ICollection<T> collection) |
| | | 362 | | { |
| | 36 | 363 | | AddRange(collection); |
| | 36 | 364 | | return; |
| | | 365 | | } |
| | | 366 | | |
| | 4 | 367 | | if (items is IReadOnlyCollection<T> readOnlyCollection) |
| | | 368 | | { |
| | 2 | 369 | | AddRange(readOnlyCollection); |
| | 2 | 370 | | return; |
| | | 371 | | } |
| | | 372 | | |
| | 2 | 373 | | T[] sortedItems = CopyToSortedArray(items); |
| | 2 | 374 | | if (sortedItems.Length == 0) |
| | 1 | 375 | | return; |
| | | 376 | | |
| | 1 | 377 | | if ((uint)_count == 0) |
| | | 378 | | { |
| | 1 | 379 | | InitializeFromSortedItems(sortedItems); |
| | 1 | 380 | | return; |
| | | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | MergeSortedItems(sortedItems); |
| | 0 | 384 | | } |
| | | 385 | | |
| | | 386 | | private void AddRange(ICollection<T> collection) |
| | | 387 | | { |
| | 36 | 388 | | int count = collection.Count; |
| | 36 | 389 | | if (count == 0) |
| | 1 | 390 | | return; |
| | | 391 | | |
| | 35 | 392 | | if (ShouldUseTemporaryCopy(collection)) |
| | | 393 | | { |
| | 0 | 394 | | T[] sortedItems = CopyCollectionToArray(collection); |
| | 0 | 395 | | SwiftArraySortHelper.Sort(sortedItems, 0, sortedItems.Length, _comparer); |
| | 0 | 396 | | MergeSortedItems(sortedItems); |
| | 0 | 397 | | return; |
| | | 398 | | } |
| | | 399 | | |
| | 35 | 400 | | if ((uint)_count == 0) |
| | | 401 | | { |
| | 28 | 402 | | InitializeFromCollection(collection); |
| | 28 | 403 | | return; |
| | | 404 | | } |
| | | 405 | | |
| | 7 | 406 | | MergeCollectionItems(collection, count); |
| | 7 | 407 | | } |
| | | 408 | | |
| | | 409 | | private void AddRange(IReadOnlyCollection<T> collection) |
| | | 410 | | { |
| | 2 | 411 | | int count = collection.Count; |
| | 2 | 412 | | if (count == 0) |
| | 0 | 413 | | return; |
| | | 414 | | |
| | 2 | 415 | | if ((uint)_count == 0) |
| | | 416 | | { |
| | 1 | 417 | | InitializeFromEnumerable(collection, count); |
| | 1 | 418 | | return; |
| | | 419 | | } |
| | | 420 | | |
| | 1 | 421 | | MergeEnumerableItems(collection, count); |
| | 1 | 422 | | } |
| | | 423 | | |
| | | 424 | | private bool ShouldUseTemporaryCopy(ICollection<T> collection) => |
| | 35 | 425 | | ReferenceEquals(collection, this) || |
| | 35 | 426 | | collection is T[] array && ReferenceEquals(array, _innerArray); |
| | | 427 | | |
| | | 428 | | private void InitializeFromCollection(ICollection<T> collection) |
| | | 429 | | { |
| | 28 | 430 | | int count = collection.Count; |
| | 28 | 431 | | if (count == 0) |
| | 0 | 432 | | return; |
| | | 433 | | |
| | 28 | 434 | | EnsureCapacity(count); |
| | | 435 | | |
| | 28 | 436 | | _offset = (_innerArray.Length - count) >> 1; |
| | 28 | 437 | | collection.CopyTo(_innerArray, _offset); |
| | 28 | 438 | | SwiftArraySortHelper.Sort(_innerArray, _offset, count, _comparer); |
| | | 439 | | |
| | 28 | 440 | | _count = count; |
| | 28 | 441 | | _version++; |
| | 28 | 442 | | } |
| | | 443 | | |
| | | 444 | | private void InitializeFromEnumerable(IEnumerable<T> items, int count) |
| | | 445 | | { |
| | 1 | 446 | | if (count == 0) |
| | 0 | 447 | | return; |
| | | 448 | | |
| | 1 | 449 | | EnsureCapacity(count); |
| | | 450 | | |
| | 1 | 451 | | _offset = (_innerArray.Length - count) >> 1; |
| | 1 | 452 | | int index = _offset; |
| | 66 | 453 | | foreach (T item in items) |
| | 32 | 454 | | _innerArray[index++] = item; |
| | | 455 | | |
| | 1 | 456 | | SwiftArraySortHelper.Sort(_innerArray, _offset, count, _comparer); |
| | | 457 | | |
| | 1 | 458 | | _count = count; |
| | 1 | 459 | | _version++; |
| | 1 | 460 | | } |
| | | 461 | | |
| | | 462 | | private T[] CopyToSortedArray(IEnumerable<T> items) |
| | | 463 | | { |
| | 2 | 464 | | T[] sortedItems = items.ToArray(); |
| | | 465 | | |
| | 2 | 466 | | if (sortedItems.Length > 0) |
| | 1 | 467 | | SwiftArraySortHelper.Sort(sortedItems, 0, sortedItems.Length, _comparer); |
| | | 468 | | |
| | 2 | 469 | | return sortedItems; |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | private static T[] CopyCollectionToArray(ICollection<T> collection) |
| | | 473 | | { |
| | 0 | 474 | | if (collection.Count == 0) |
| | 0 | 475 | | return _emptyArray; |
| | | 476 | | |
| | 0 | 477 | | T[] items = new T[collection.Count]; |
| | 0 | 478 | | collection.CopyTo(items, 0); |
| | 0 | 479 | | return items; |
| | | 480 | | } |
| | | 481 | | |
| | | 482 | | private void InitializeFromSortedItems(T[] sortedItems) |
| | | 483 | | { |
| | 1 | 484 | | int initialCapacity = SwiftHashTools.NextPowerOfTwo(sortedItems.Length < DefaultCapacity ? DefaultCapacity : sor |
| | 1 | 485 | | int newOffset = (initialCapacity - sortedItems.Length) >> 1; |
| | 1 | 486 | | if ((uint)_innerArray.Length < (uint)initialCapacity) |
| | 0 | 487 | | _innerArray = new T[initialCapacity]; |
| | | 488 | | |
| | 1 | 489 | | Array.Copy(sortedItems, 0, _innerArray, newOffset, sortedItems.Length); |
| | 1 | 490 | | _offset = newOffset; |
| | 1 | 491 | | _count = sortedItems.Length; |
| | 1 | 492 | | _version++; |
| | 1 | 493 | | } |
| | | 494 | | |
| | | 495 | | private void MergeSortedItems(T[] sortedItems) |
| | | 496 | | { |
| | 0 | 497 | | int newCount = _count + sortedItems.Length; |
| | 0 | 498 | | int totalRequiredCapacity = newCount + _offset; |
| | 0 | 499 | | int newCapacity = SwiftHashTools.NextPowerOfTwo(totalRequiredCapacity); |
| | 0 | 500 | | int mergedOffset = (newCapacity - newCount) >> 1; |
| | 0 | 501 | | T[] newArray = new T[newCapacity]; |
| | | 502 | | |
| | 0 | 503 | | int existingIndex = 0; |
| | 0 | 504 | | int newItemsIndex = 0; |
| | 0 | 505 | | int mergedIndex = mergedOffset; |
| | | 506 | | |
| | 0 | 507 | | while (existingIndex < _count && newItemsIndex < sortedItems.Length) |
| | | 508 | | { |
| | 0 | 509 | | T existingItem = _innerArray[_offset + existingIndex]; |
| | 0 | 510 | | T newItem = sortedItems[newItemsIndex]; |
| | | 511 | | |
| | 0 | 512 | | if (_comparer.Compare(existingItem, newItem) <= 0) |
| | | 513 | | { |
| | 0 | 514 | | newArray[mergedIndex++] = existingItem; |
| | 0 | 515 | | existingIndex++; |
| | | 516 | | } |
| | | 517 | | else |
| | | 518 | | { |
| | 0 | 519 | | newArray[mergedIndex++] = newItem; |
| | 0 | 520 | | newItemsIndex++; |
| | | 521 | | } |
| | | 522 | | } |
| | | 523 | | |
| | | 524 | | // Copy any remaining existing items |
| | 0 | 525 | | while (existingIndex < _count) |
| | 0 | 526 | | newArray[mergedIndex++] = _innerArray[_offset + existingIndex++]; |
| | | 527 | | |
| | | 528 | | // Copy any remaining new items |
| | 0 | 529 | | while (newItemsIndex < sortedItems.Length) |
| | 0 | 530 | | newArray[mergedIndex++] = sortedItems[newItemsIndex++]; |
| | | 531 | | |
| | 0 | 532 | | _innerArray = newArray; |
| | 0 | 533 | | _offset = mergedOffset; |
| | 0 | 534 | | _count = newCount; |
| | | 535 | | |
| | 0 | 536 | | _version++; |
| | 0 | 537 | | } |
| | | 538 | | |
| | | 539 | | private void MergeCollectionItems(ICollection<T> collection, int collectionCount) |
| | | 540 | | { |
| | 7 | 541 | | int existingCount = _count; |
| | 7 | 542 | | int newCount = existingCount + collectionCount; |
| | | 543 | | T[] target; |
| | | 544 | | int mergedOffset; |
| | | 545 | | |
| | 7 | 546 | | if (_innerArray.Length >= newCount) |
| | | 547 | | { |
| | 7 | 548 | | target = _innerArray; |
| | 7 | 549 | | mergedOffset = (_innerArray.Length - newCount) >> 1; |
| | 7 | 550 | | if (mergedOffset != _offset) |
| | 7 | 551 | | Array.Copy(_innerArray, _offset, target, mergedOffset, existingCount); |
| | | 552 | | } |
| | | 553 | | else |
| | | 554 | | { |
| | 0 | 555 | | int newCapacity = SwiftHashTools.NextPowerOfTwo(newCount <= DefaultCapacity ? DefaultCapacity : newCount); |
| | 0 | 556 | | target = new T[newCapacity]; |
| | 0 | 557 | | mergedOffset = (newCapacity - newCount) >> 1; |
| | 0 | 558 | | Array.Copy(_innerArray, _offset, target, mergedOffset, existingCount); |
| | | 559 | | } |
| | | 560 | | |
| | 7 | 561 | | int collectionOffset = mergedOffset + existingCount; |
| | 7 | 562 | | collection.CopyTo(target, collectionOffset); |
| | 7 | 563 | | SwiftArraySortHelper.Sort(target, mergedOffset, newCount, _comparer); |
| | | 564 | | |
| | 7 | 565 | | _innerArray = target; |
| | 7 | 566 | | _offset = mergedOffset; |
| | 7 | 567 | | _count = newCount; |
| | | 568 | | |
| | 7 | 569 | | _version++; |
| | 7 | 570 | | } |
| | | 571 | | |
| | | 572 | | private void MergeEnumerableItems(IEnumerable<T> items, int itemCount) |
| | | 573 | | { |
| | 1 | 574 | | int existingCount = _count; |
| | 1 | 575 | | int newCount = existingCount + itemCount; |
| | | 576 | | T[] target; |
| | | 577 | | int mergedOffset; |
| | | 578 | | |
| | 1 | 579 | | if (_innerArray.Length >= newCount) |
| | | 580 | | { |
| | 0 | 581 | | target = _innerArray; |
| | 0 | 582 | | mergedOffset = (_innerArray.Length - newCount) >> 1; |
| | 0 | 583 | | if (mergedOffset != _offset) |
| | 0 | 584 | | Array.Copy(_innerArray, _offset, target, mergedOffset, existingCount); |
| | | 585 | | } |
| | | 586 | | else |
| | | 587 | | { |
| | 1 | 588 | | int newCapacity = SwiftHashTools.NextPowerOfTwo(newCount <= DefaultCapacity ? DefaultCapacity : newCount); |
| | 1 | 589 | | target = new T[newCapacity]; |
| | 1 | 590 | | mergedOffset = (newCapacity - newCount) >> 1; |
| | 1 | 591 | | Array.Copy(_innerArray, _offset, target, mergedOffset, existingCount); |
| | | 592 | | } |
| | | 593 | | |
| | 1 | 594 | | int index = mergedOffset + existingCount; |
| | 66 | 595 | | foreach (T item in items) |
| | 32 | 596 | | target[index++] = item; |
| | | 597 | | |
| | 1 | 598 | | SwiftArraySortHelper.Sort(target, mergedOffset, newCount, _comparer); |
| | | 599 | | |
| | 1 | 600 | | _innerArray = target; |
| | 1 | 601 | | _offset = mergedOffset; |
| | 1 | 602 | | _count = newCount; |
| | | 603 | | |
| | 1 | 604 | | _version++; |
| | 1 | 605 | | } |
| | | 606 | | |
| | | 607 | | /// <summary> |
| | | 608 | | /// Removes and returns the minimum element in the sorter. |
| | | 609 | | /// </summary> |
| | | 610 | | /// <returns>The minimum element.</returns> |
| | | 611 | | public T PopMin() |
| | | 612 | | { |
| | 3 | 613 | | SwiftThrowHelper.ThrowIfTrue((uint)_count == 0, message: "Cannot pop from an empty list."); |
| | | 614 | | |
| | 2 | 615 | | int index = GetPhysicalIndex(0); |
| | 2 | 616 | | T ret = _innerArray[index]; |
| | 2 | 617 | | _innerArray[index] = default!; |
| | 2 | 618 | | _count--; |
| | | 619 | | // Increment _offset as we remove from the front |
| | 2 | 620 | | _offset = _count == 0 ? _innerArray.Length >> 1 : _offset + 1; |
| | | 621 | | |
| | 2 | 622 | | _version++; |
| | | 623 | | |
| | 2 | 624 | | return ret; |
| | | 625 | | } |
| | | 626 | | |
| | | 627 | | /// <summary> |
| | | 628 | | /// Removes and returns the maximum element in the sorter. |
| | | 629 | | /// </summary> |
| | | 630 | | /// <returns>The maximum element.</returns> |
| | | 631 | | public T PopMax() |
| | | 632 | | { |
| | 3 | 633 | | SwiftThrowHelper.ThrowIfTrue((uint)_count == 0, message: "Cannot pop from an empty list."); |
| | | 634 | | |
| | 2 | 635 | | int index = GetPhysicalIndex(--_count); |
| | 2 | 636 | | T ret = _innerArray[index]; |
| | 2 | 637 | | _innerArray[index] = default!; |
| | 3 | 638 | | if ((uint)_count == 0) _offset = _innerArray.Length >> 1; |
| | | 639 | | |
| | 2 | 640 | | _version++; |
| | | 641 | | |
| | 2 | 642 | | return ret; |
| | | 643 | | } |
| | | 644 | | |
| | | 645 | | /// <inheritdoc/> |
| | | 646 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 647 | | public bool Remove(T item) |
| | | 648 | | { |
| | 11 | 649 | | if ((uint)_count == 0) return false; |
| | | 650 | | |
| | 9 | 651 | | int index = Search(item); |
| | 11 | 652 | | if (index < 0) return false; |
| | | 653 | | |
| | 7 | 654 | | RemoveAt(index); |
| | 7 | 655 | | return true; |
| | | 656 | | } |
| | | 657 | | |
| | | 658 | | /// <summary> |
| | | 659 | | /// Removes the element at the specified arrayIndex from the sorted list. |
| | | 660 | | /// Shifts elements as needed to maintain the sorted order and efficient space utilization. |
| | | 661 | | /// </summary> |
| | | 662 | | /// <param name="index">The zero-based arrayIndex of the element to remove.</param> |
| | | 663 | | public void RemoveAt(int index) |
| | | 664 | | { |
| | 17 | 665 | | SwiftThrowHelper.ThrowIfListIndexInvalid(index, _count); |
| | | 666 | | |
| | 17 | 667 | | int physicalIndex = GetPhysicalIndex(index); |
| | | 668 | | |
| | 17 | 669 | | _count--; |
| | 17 | 670 | | if ((uint)index < (uint)_count) |
| | | 671 | | { |
| | 8 | 672 | | int distanceToHead = physicalIndex - _offset; |
| | 8 | 673 | | int distanceToTail = (_offset + _count) - physicalIndex; |
| | | 674 | | |
| | 8 | 675 | | if (distanceToHead < distanceToTail) |
| | | 676 | | { |
| | | 677 | | // Shift elements towards the tail (right) to fill the gap |
| | 7 | 678 | | Array.Copy(_innerArray, _offset, _innerArray, _offset + 1, distanceToHead); |
| | 7 | 679 | | _offset++; // Adjust offset since we've moved elements towards the tail |
| | | 680 | | } |
| | | 681 | | else |
| | | 682 | | { |
| | | 683 | | // Shift elements towards the head (left) to fill the gap |
| | 1 | 684 | | Array.Copy(_innerArray, physicalIndex + 1, _innerArray, physicalIndex, distanceToTail); |
| | 1 | 685 | | _innerArray[GetPhysicalIndex(_count)] = default!; // Clear the last element |
| | | 686 | | } |
| | | 687 | | } |
| | | 688 | | else // Removing the last element; simply clear it |
| | 9 | 689 | | _innerArray[GetPhysicalIndex(_count)] = default!; |
| | | 690 | | |
| | | 691 | | // Only recenter if offset is non-zero and count is less than 25% of capacity |
| | 17 | 692 | | if (_offset != 0 && (uint)_count < _innerArray.Length * 0.25) |
| | | 693 | | { |
| | 2 | 694 | | if ((uint)_count == 0) |
| | 1 | 695 | | _offset = _innerArray.Length >> 1; // Reset offset to the middle when list is empty |
| | | 696 | | else |
| | 1 | 697 | | RecenterArray(); |
| | | 698 | | } |
| | | 699 | | |
| | 17 | 700 | | _version++; |
| | 17 | 701 | | } |
| | | 702 | | |
| | | 703 | | /// <inheritdoc/> |
| | | 704 | | public void Clear() |
| | | 705 | | { |
| | 3 | 706 | | if ((uint)_count == 0) return; |
| | 1 | 707 | | Array.Clear(_innerArray, _offset, _count); |
| | 1 | 708 | | _count = 0; |
| | 1 | 709 | | _offset = _innerArray.Length >> 1; // Reset _offset to the middle of the array |
| | | 710 | | |
| | 1 | 711 | | _version++; |
| | 1 | 712 | | } |
| | | 713 | | |
| | | 714 | | /// <summary> |
| | | 715 | | /// Quickly clears the list by resetting the count and offset without modifying the internal array. |
| | | 716 | | /// Note: This leaves references in the internal array, which may prevent garbage collection of reference types. |
| | | 717 | | /// Use when performance is critical and you are certain that residual references are acceptable. |
| | | 718 | | /// </summary> |
| | | 719 | | public void FastClear() |
| | | 720 | | { |
| | 5 | 721 | | if ((uint)_count == 0) return; |
| | 3 | 722 | | _count = 0; |
| | 3 | 723 | | _offset = _innerArray.Length >> 1; // Reset offset to middle |
| | | 724 | | |
| | 3 | 725 | | _version++; |
| | 3 | 726 | | } |
| | | 727 | | |
| | | 728 | | #endregion |
| | | 729 | | |
| | | 730 | | #region Capacity Management |
| | | 731 | | |
| | | 732 | | /// <summary> |
| | | 733 | | /// Ensures that the capacity of <see cref="SwiftSortedList{T}"/> is sufficient to accommodate the specified number |
| | | 734 | | /// The capacity can increase by double to balance memory allocation efficiency and space. |
| | | 735 | | /// </summary> |
| | | 736 | | public void EnsureCapacity(int capacity) |
| | | 737 | | { |
| | 30 | 738 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 30 | 739 | | if (capacity > _innerArray.Length) |
| | 19 | 740 | | Resize(capacity); |
| | 30 | 741 | | } |
| | | 742 | | |
| | | 743 | | /// <summary> |
| | | 744 | | /// Ensures that the capacity of <see cref="SwiftSortedList{T}"/> is sufficient to accommodate the specified number |
| | | 745 | | /// The capacity can increase by double to balance memory allocation efficiency and space. |
| | | 746 | | /// </summary> |
| | | 747 | | private void Resize(int newSize) |
| | | 748 | | { |
| | 53 | 749 | | int newCapacity = newSize <= DefaultCapacity ? DefaultCapacity : newSize; |
| | | 750 | | |
| | 53 | 751 | | T[] newArray = new T[newCapacity]; |
| | 53 | 752 | | int newOffset = (newArray.Length - _count) >> 1; // Center the elements in the new array |
| | 53 | 753 | | if ((uint)_count > 0) |
| | 3 | 754 | | Array.Copy(_innerArray, _offset, newArray, newOffset, _count); |
| | | 755 | | |
| | 53 | 756 | | _innerArray = newArray; |
| | 53 | 757 | | _offset = newOffset; |
| | | 758 | | |
| | 53 | 759 | | _version++; |
| | 53 | 760 | | } |
| | | 761 | | |
| | | 762 | | /// <summary> |
| | | 763 | | /// Recenters the elements within the internal array to balance available space on both ends. |
| | | 764 | | /// This minimizes the need for shifting elements during future insertions and deletions. |
| | | 765 | | /// </summary> |
| | | 766 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 767 | | private void RecenterArray() |
| | | 768 | | { |
| | 2 | 769 | | int newOffset = (_innerArray.Length - _count) >> 1; |
| | 2 | 770 | | Array.Copy(_innerArray, _offset, _innerArray, newOffset, _count); |
| | 2 | 771 | | _offset = newOffset; |
| | | 772 | | |
| | 2 | 773 | | _version++; |
| | 2 | 774 | | } |
| | | 775 | | |
| | | 776 | | #endregion |
| | | 777 | | |
| | | 778 | | #region Utility Methods |
| | | 779 | | |
| | | 780 | | /// <summary> |
| | | 781 | | /// Sets a new comparer for the sorted list and re-sorts the elements. |
| | | 782 | | /// </summary> |
| | | 783 | | /// <param name="comparer">The new comparer to use.</param> |
| | | 784 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 785 | | public void SetComparer(IComparer<T> comparer) |
| | | 786 | | { |
| | 6 | 787 | | SwiftThrowHelper.ThrowIfNull(comparer, nameof(comparer)); |
| | 6 | 788 | | if (ReferenceEquals(comparer, _comparer)) |
| | 1 | 789 | | return; |
| | | 790 | | |
| | 5 | 791 | | _comparer = comparer; |
| | 5 | 792 | | SwiftArraySortHelper.Sort(_innerArray, _offset, _count, _comparer); |
| | 5 | 793 | | _version++; |
| | 5 | 794 | | } |
| | | 795 | | |
| | | 796 | | /// <summary> |
| | | 797 | | /// Converts a logical arrayIndex within the list to the corresponding physical arrayIndex in the internal array. |
| | | 798 | | /// </summary> |
| | | 799 | | /// <param name="logicalIndex">The logical arrayIndex within the list.</param> |
| | | 800 | | /// <returns>The physical arrayIndex within the internal array.</returns> |
| | | 801 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 379 | 802 | | private int GetPhysicalIndex(int logicalIndex) => _offset + logicalIndex; |
| | | 803 | | |
| | | 804 | | /// <summary> |
| | | 805 | | /// Returns a read-only span over the populated sorted portion of the list. |
| | | 806 | | /// </summary> |
| | | 807 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 13 | 808 | | public ReadOnlySpan<T> AsReadOnlySpan() => _innerArray.AsSpan(_offset, _count); |
| | | 809 | | |
| | | 810 | | /// <summary> |
| | | 811 | | /// Returns the minimum element in the sorter without removing it. |
| | | 812 | | /// </summary> |
| | | 813 | | /// <returns>The minimum element.</returns> |
| | | 814 | | public T PeekMin() |
| | | 815 | | { |
| | 20 | 816 | | SwiftThrowHelper.ThrowIfTrue((uint)_count == 0, message: "Cannot peek from an empty list."); |
| | 19 | 817 | | return _innerArray[GetPhysicalIndex(0)]; |
| | | 818 | | } |
| | | 819 | | |
| | | 820 | | /// <summary> |
| | | 821 | | /// Returns the maximum element in the sorter without removing it. |
| | | 822 | | /// </summary> |
| | | 823 | | /// <returns>The maximum element.</returns> |
| | | 824 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 825 | | public T PeekMax() |
| | | 826 | | { |
| | 18 | 827 | | SwiftThrowHelper.ThrowIfTrue((uint)_count == 0, message: "Cannot peek from an empty list."); |
| | 17 | 828 | | return _innerArray[GetPhysicalIndex(_count - 1)]; |
| | | 829 | | } |
| | | 830 | | |
| | | 831 | | /// <inheritdoc/> |
| | | 832 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 833 | | public bool Contains(T item) => Search(item) >= 0; |
| | | 834 | | |
| | | 835 | | /// <summary> |
| | | 836 | | /// Determines whether the <see cref="SwiftSortedList{T}"/> contains an element that matches the conditions defined |
| | | 837 | | /// </summary> |
| | | 838 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 839 | | /// <returns><c>true</c> if the <see cref="SwiftSortedList{T}"/> contains one or more elements that match the specif |
| | | 840 | | public bool Exists(Predicate<T> match) |
| | | 841 | | { |
| | 3 | 842 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 843 | | |
| | 14 | 844 | | for (int i = 0; i < _count; i++) |
| | | 845 | | { |
| | 6 | 846 | | if (match(_innerArray[GetPhysicalIndex(i)])) |
| | 1 | 847 | | return true; |
| | | 848 | | } |
| | | 849 | | |
| | 1 | 850 | | return false; |
| | | 851 | | } |
| | | 852 | | |
| | | 853 | | /// <summary> |
| | | 854 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first ma |
| | | 855 | | /// </summary> |
| | | 856 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 857 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 858 | | public T Find(Predicate<T> match) |
| | | 859 | | { |
| | 2 | 860 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 861 | | |
| | 16 | 862 | | for (int i = 0; i < _count; i++) |
| | | 863 | | { |
| | 7 | 864 | | T item = _innerArray[GetPhysicalIndex(i)]; |
| | 7 | 865 | | if (match(item)) |
| | 1 | 866 | | return item; |
| | | 867 | | } |
| | | 868 | | |
| | 1 | 869 | | return default!; |
| | | 870 | | } |
| | | 871 | | |
| | | 872 | | /// <summary> |
| | | 873 | | /// Searches for the specified item in the sorted collection and returns the arrayIndex of the first occurrence. |
| | | 874 | | /// </summary> |
| | | 875 | | /// <param name="item">The item to search for.</param> |
| | | 876 | | /// <returns>The zero-based arrayIndex of the item if found; otherwise, -1.</returns> |
| | | 877 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 878 | | public int IndexOf(T item) |
| | | 879 | | { |
| | 2 | 880 | | int index = Search(item); |
| | 2 | 881 | | return index >= 0 ? index : -1; |
| | | 882 | | } |
| | | 883 | | |
| | | 884 | | /// <summary> |
| | | 885 | | /// Determines the insertion point for a specified item in the collection. |
| | | 886 | | /// The insertion point is the arrayIndex where the item would be inserted if it were not already present. |
| | | 887 | | /// </summary> |
| | | 888 | | /// <param name="item">The item for which to find the insertion point.</param> |
| | | 889 | | /// <returns>The insertion point as a zero-based arrayIndex.</returns> |
| | | 890 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 891 | | public int InsertionPoint(T item) |
| | | 892 | | { |
| | 2 | 893 | | int index = Search(item); |
| | 2 | 894 | | return index >= 0 ? index : ~index; |
| | | 895 | | } |
| | | 896 | | |
| | | 897 | | /// <summary> |
| | | 898 | | /// Searches for the specified item in the sorted collection. |
| | | 899 | | /// </summary> |
| | | 900 | | /// <param name="item">The item to search for.</param> |
| | | 901 | | /// <returns> |
| | | 902 | | /// The arrayIndex of the item if found or where the item should be inserted if not found. |
| | | 903 | | /// </returns> |
| | | 904 | | public int Search(T item) |
| | | 905 | | { |
| | 117 | 906 | | int low = 0; |
| | 117 | 907 | | int high = _count - 1; |
| | 273 | 908 | | while ((uint)low <= high) |
| | | 909 | | { |
| | 168 | 910 | | int mid = low + ((high - low) >> 1); |
| | 168 | 911 | | T midItem = _innerArray[GetPhysicalIndex(mid)]; |
| | 168 | 912 | | int cmp = _comparer.Compare(midItem, item); |
| | 168 | 913 | | if ((uint)cmp == 0) |
| | 12 | 914 | | return mid; // Exact match found |
| | | 915 | | |
| | 156 | 916 | | if (cmp < 0) |
| | 106 | 917 | | low = mid + 1; // Search in the right half |
| | | 918 | | else |
| | 50 | 919 | | high = mid - 1; // Search in the left half |
| | | 920 | | } |
| | 105 | 921 | | return ~low; // Item not found, should be inserted at arrayIndex low |
| | | 922 | | } |
| | | 923 | | |
| | | 924 | | /// <inheritdoc/> |
| | | 925 | | public void CopyTo(T[] array, int arrayIndex) |
| | | 926 | | { |
| | 2 | 927 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 2 | 928 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(arrayIndex, array.Length, nameof(arrayIndex)); |
| | 2 | 929 | | SwiftThrowHelper.ThrowIfArgument(array.Length - arrayIndex < _count, nameof(array), "The target array is too sma |
| | | 930 | | |
| | 1 | 931 | | Array.Copy(_innerArray, _offset, array, arrayIndex, _count); |
| | 1 | 932 | | } |
| | | 933 | | |
| | | 934 | | /// <inheritdoc/> |
| | | 935 | | public void CopyTo(Array array, int arrayIndex) |
| | | 936 | | { |
| | 2 | 937 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 2 | 938 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(arrayIndex, array.Length, nameof(arrayIndex)); |
| | 2 | 939 | | SwiftThrowHelper.ThrowIfArgument(array.Length - arrayIndex < _count, nameof(array), "The target array is too sma |
| | | 940 | | |
| | 1 | 941 | | Array.Copy(_innerArray, _offset, array, arrayIndex, _count); |
| | 1 | 942 | | } |
| | | 943 | | |
| | | 944 | | /// <inheritdoc/> |
| | | 945 | | public void CloneTo(ICollection<T> output) |
| | | 946 | | { |
| | 2 | 947 | | SwiftThrowHelper.ThrowIfNull(output, nameof(output)); |
| | | 948 | | |
| | 1 | 949 | | output.Clear(); |
| | | 950 | | |
| | 8 | 951 | | foreach (var item in this) |
| | 3 | 952 | | output.Add(item); |
| | 1 | 953 | | } |
| | | 954 | | |
| | | 955 | | #endregion |
| | | 956 | | |
| | | 957 | | #region Enumerators |
| | | 958 | | |
| | | 959 | | /// <summary> |
| | | 960 | | /// Returns an enumerator that iterates through <see cref="SwiftSortedList{T}"/>. |
| | | 961 | | /// </summary> |
| | 21 | 962 | | public SwiftSorterEnumerator GetEnumerator() => new(this); |
| | 14 | 963 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 2 | 964 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 965 | | |
| | | 966 | | /// <summary> |
| | | 967 | | /// Supports simple iteration over the elements of a <see cref="SwiftSortedList{T}"/> in sorted order. |
| | | 968 | | /// </summary> |
| | | 969 | | /// <remarks> |
| | | 970 | | /// The enumerator is invalidated if the underlying collection is modified after the enumerator created. |
| | | 971 | | /// In this case, calling MoveNext or Reset will throw an InvalidOperationException. |
| | | 972 | | /// The enumerator does not support writing to the collection. |
| | | 973 | | /// </remarks> |
| | | 974 | | public struct SwiftSorterEnumerator : IEnumerator<T>, IEnumerator, IDisposable |
| | | 975 | | { |
| | | 976 | | private readonly SwiftSortedList<T> _list; |
| | | 977 | | private readonly uint _count; |
| | | 978 | | private readonly uint _version; |
| | | 979 | | private int _index; |
| | | 980 | | |
| | | 981 | | private T _current; |
| | | 982 | | |
| | | 983 | | internal SwiftSorterEnumerator(SwiftSortedList<T> sortedList) |
| | | 984 | | { |
| | 21 | 985 | | _list = sortedList; |
| | 21 | 986 | | _count = (uint)sortedList._count; |
| | 21 | 987 | | _version = sortedList._version; |
| | 21 | 988 | | _index = 0; |
| | 21 | 989 | | _current = default!; |
| | 21 | 990 | | } |
| | | 991 | | |
| | | 992 | | /// <inheritdoc/> |
| | 36 | 993 | | public T Current => _current; |
| | | 994 | | |
| | | 995 | | object IEnumerator.Current |
| | | 996 | | { |
| | | 997 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 998 | | get |
| | | 999 | | { |
| | 2 | 1000 | | SwiftThrowHelper.ThrowIfTrue((uint)_index > _count, message: "Enumerator is past the end of the collecti |
| | 1 | 1001 | | return _current!; |
| | | 1002 | | } |
| | | 1003 | | |
| | | 1004 | | } |
| | | 1005 | | |
| | | 1006 | | /// <inheritdoc/> |
| | | 1007 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1008 | | public bool MoveNext() |
| | | 1009 | | { |
| | 46 | 1010 | | SwiftThrowHelper.ThrowIfTrue(_version != _list._version, message: "Enumerator modified outside of enumeratio |
| | | 1011 | | |
| | 46 | 1012 | | if (_index < _count) |
| | | 1013 | | { |
| | 28 | 1014 | | _current = _list._innerArray[_list.GetPhysicalIndex(_index)]; |
| | 28 | 1015 | | _index++; |
| | 28 | 1016 | | return true; |
| | | 1017 | | } |
| | | 1018 | | |
| | 18 | 1019 | | _index = _list._count + 1; |
| | 18 | 1020 | | _current = default!; |
| | 18 | 1021 | | return false; |
| | | 1022 | | } |
| | | 1023 | | |
| | | 1024 | | /// <inheritdoc/> |
| | | 1025 | | public void Reset() |
| | | 1026 | | { |
| | 2 | 1027 | | SwiftThrowHelper.ThrowIfTrue(_version != _list._version, message: "Enumerator modified outside of enumeratio |
| | | 1028 | | |
| | 1 | 1029 | | _index = 0; |
| | 1 | 1030 | | _current = default!; |
| | 1 | 1031 | | } |
| | | 1032 | | |
| | | 1033 | | /// <inheritdoc/> |
| | 17 | 1034 | | public void Dispose() => _index = 0; |
| | | 1035 | | } |
| | | 1036 | | |
| | | 1037 | | #endregion |
| | | 1038 | | } |