| | | 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 | | /// Represents a high-performance set of unique values with efficient operations for addition, removal, and lookup |
| | | 12 | | /// </summary> |
| | | 13 | | /// <typeparam name="T">The type of elements in the set.</typeparam> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// The comparer is not serialized. After deserialization the set reverts |
| | | 16 | | /// to the same default comparer selection used by a new instance. String values |
| | | 17 | | /// use SwiftCollections' deterministic default comparer. Object values use a |
| | | 18 | | /// SwiftCollections comparer that hashes strings deterministically, while other |
| | | 19 | | /// object-value determinism still depends on the underlying value type's |
| | | 20 | | /// <see cref="object.GetHashCode()"/> implementation. Other types use |
| | | 21 | | /// <see cref="EqualityComparer{T}.Default"/>. |
| | | 22 | | /// |
| | | 23 | | /// If a custom comparer is required it can be reapplied using |
| | | 24 | | /// <see cref="SetComparer(IEqualityComparer{T})"/>. |
| | | 25 | | /// </remarks> |
| | | 26 | | [Serializable] |
| | | 27 | | [JsonConverter(typeof(SwiftStateJsonConverterFactory))] |
| | | 28 | | [MemoryPackable] |
| | | 29 | | public sealed partial class SwiftHashSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection< |
| | | 30 | | { |
| | | 31 | | #region Constants |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The default initial capacity of the set. |
| | | 35 | | /// </summary> |
| | | 36 | | public const int DefaultCapacity = 8; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Determines the maximum allowable load factor before resizing the hash set to maintain performance. |
| | | 40 | | /// </summary> |
| | | 41 | | private const float _LoadFactorThreshold = 0.85f; |
| | | 42 | | |
| | | 43 | | #endregion |
| | | 44 | | |
| | | 45 | | #region Fields |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// The array containing the entries of the SwiftHashSet. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <remarks> |
| | | 51 | | /// Capacity will always be a power of two for efficient pooling cache. |
| | | 52 | | /// </remarks> |
| | | 53 | | private Entry[] _entries; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// The total number of entries in the hash set |
| | | 57 | | /// </summary> |
| | | 58 | | private int _count; |
| | | 59 | | |
| | | 60 | | private int _lastIndex; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// A mask used for efficiently computing the entry index from a hash code. |
| | | 64 | | /// This is typically the size of the entry array minus one, assuming the size is a power of two. |
| | | 65 | | /// </summary> |
| | | 66 | | private int _entryMask; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// The comparer used to determine equality of keys and to generate hash codes. |
| | | 70 | | /// </summary> |
| | | 71 | | private IEqualityComparer<T> _comparer; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Specifies the dynamic growth factor for resizing, adjusted based on recent usage patterns. |
| | | 75 | | /// </summary> |
| | | 76 | | private int _adaptiveResizeFactor; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Tracks the count threshold at which the hash set should resize based on the load factor. |
| | | 80 | | /// </summary> |
| | | 81 | | private uint _nextResizeCount; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Represents the moving average of the fill rate, used to dynamically adjust resizing behavior. |
| | | 85 | | /// </summary> |
| | | 86 | | private double _movingFillRate; |
| | | 87 | | |
| | | 88 | | private int _maxStepCount; |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// A version counter used to track modifications to the set. |
| | | 92 | | /// Incremented on mutations to detect changes during enumeration and ensure enumerator validity. |
| | | 93 | | /// </summary> |
| | | 94 | | private uint _version; |
| | | 95 | | |
| | | 96 | | #endregion |
| | | 97 | | |
| | | 98 | | #region Nested Types |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Represents a single value in the set, including its hash code for quick access. |
| | | 102 | | /// </summary> |
| | | 103 | | private struct Entry |
| | | 104 | | { |
| | | 105 | | public T Value; |
| | | 106 | | public int HashCode; // Lower 31 bits of hash code, -1 if unused |
| | | 107 | | public bool IsUsed; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | #endregion |
| | | 111 | | |
| | | 112 | | #region Constructors |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Initialize a new instance of <see cref="SwiftHashSet{T}"/> with customizable capacity and comparer for optimal p |
| | | 116 | | /// </summary> |
| | 183 | 117 | | public SwiftHashSet() : this(DefaultCapacity, null) { } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc cref="SwiftHashSet()"/> |
| | 18 | 120 | | public SwiftHashSet(IEqualityComparer<T> comparer) : this(DefaultCapacity, comparer) { } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Initializes a new instance of the <see cref="SwiftHashSet{T}"/> class that is empty and has the default initial |
| | | 124 | | /// </summary> |
| | 71 | 125 | | public SwiftHashSet(int capacity, IEqualityComparer<T> comparer = null) |
| | 71 | 126 | | { |
| | 71 | 127 | | Initialize(capacity, comparer); |
| | 71 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Initializes a new instance of the <see cref="SwiftHashSet{T}"/> class that contains elements copied from the spe |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="collection">The collection whose elements are copied to the new set.</param> |
| | | 134 | | /// <param name="comparer">The comparer to use when comparing elements.</param> |
| | 32 | 135 | | public SwiftHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer = null) |
| | 32 | 136 | | { |
| | 32 | 137 | | SwiftThrowHelper.ThrowIfNull(collection, nameof(collection)); |
| | | 138 | | |
| | 32 | 139 | | int count = (collection as ICollection<T>)?.Count ?? DefaultCapacity; |
| | 32 | 140 | | int size = (int)(count / _LoadFactorThreshold); // Dynamic padding based on collision estimation |
| | 32 | 141 | | Initialize(size, comparer); |
| | | 142 | | |
| | 310 | 143 | | foreach (T item in collection) |
| | 107 | 144 | | InsertIfNotExists(item); |
| | 32 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Initializes a new instance of the <see cref="SwiftHashSet{T}"/> class with the specified <see cref="SwiftArrayS |
| | | 149 | | /// </summary> |
| | | 150 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 151 | | [MemoryPackConstructor] |
| | 6 | 152 | | public SwiftHashSet(SwiftArrayState<T> state) |
| | 6 | 153 | | { |
| | 6 | 154 | | State = state; |
| | 6 | 155 | | } |
| | | 156 | | |
| | | 157 | | #endregion |
| | | 158 | | |
| | | 159 | | #region Properties |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Gets the number of elements contained in the set. |
| | | 163 | | /// </summary> |
| | | 164 | | [JsonIgnore] |
| | | 165 | | [MemoryPackIgnore] |
| | 63 | 166 | | public int Count => _count; |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Gets the <see cref="IEqualityComparer{T}"/> object that is used to determine equality for the values in the set. |
| | | 170 | | /// </summary> |
| | | 171 | | [JsonIgnore] |
| | | 172 | | [MemoryPackIgnore] |
| | 13 | 173 | | public IEqualityComparer<T> Comparer => _comparer; |
| | | 174 | | |
| | | 175 | | [JsonIgnore] |
| | | 176 | | [MemoryPackIgnore] |
| | 1 | 177 | | bool ICollection<T>.IsReadOnly => false; |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Gets the stored value that matches the specified key. |
| | | 181 | | /// </summary> |
| | | 182 | | /// <param name="key">The lookup value used to find an equal element in the set.</param> |
| | | 183 | | /// <exception cref="KeyNotFoundException">No matching value exists in the set.</exception> |
| | | 184 | | [JsonIgnore] |
| | | 185 | | [MemoryPackIgnore] |
| | | 186 | | public T this[T key] |
| | | 187 | | { |
| | | 188 | | get |
| | 2 | 189 | | { |
| | 2 | 190 | | int index = FindEntry(key); |
| | 2 | 191 | | SwiftThrowHelper.ThrowIfKeyInvalid(index, key); |
| | 1 | 192 | | return _entries[index].Value; |
| | 1 | 193 | | } |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | [JsonInclude] |
| | | 197 | | [MemoryPackInclude] |
| | | 198 | | public SwiftArrayState<T> State |
| | | 199 | | { |
| | | 200 | | get |
| | 5 | 201 | | { |
| | 5 | 202 | | if (_count == 0) |
| | 0 | 203 | | return new SwiftArrayState<T>(Array.Empty<T>()); |
| | | 204 | | |
| | 5 | 205 | | T[] items = new T[_count]; |
| | 5 | 206 | | CopyTo(items, 0); |
| | | 207 | | |
| | 5 | 208 | | return new SwiftArrayState<T>(items); |
| | 5 | 209 | | } |
| | | 210 | | internal set |
| | 6 | 211 | | { |
| | 6 | 212 | | T[] items = value.Items; |
| | 6 | 213 | | int count = items?.Length ?? 0; |
| | | 214 | | |
| | 6 | 215 | | if (count == 0) |
| | 1 | 216 | | { |
| | 1 | 217 | | Initialize(DefaultCapacity); |
| | 1 | 218 | | _count = 0; |
| | 1 | 219 | | _version = 0; |
| | 1 | 220 | | return; |
| | | 221 | | } |
| | | 222 | | |
| | 5 | 223 | | int size = (int)(count / _LoadFactorThreshold); |
| | 5 | 224 | | Initialize(size); |
| | | 225 | | |
| | 59 | 226 | | foreach (T item in items) |
| | 22 | 227 | | if (item != null) |
| | 22 | 228 | | InsertIfNotExists(item); |
| | | 229 | | |
| | 5 | 230 | | _version = 0; |
| | 6 | 231 | | } |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | #endregion |
| | | 235 | | |
| | | 236 | | #region Collection Manipulation |
| | | 237 | | |
| | | 238 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 239 | | public bool Add(T item) |
| | 213522 | 240 | | { |
| | 213522 | 241 | | CheckLoadThreshold(); |
| | 213522 | 242 | | return InsertIfNotExists(item); |
| | 213521 | 243 | | } |
| | | 244 | | |
| | 1 | 245 | | void ICollection<T>.Add(T item) => Add(item); |
| | | 246 | | |
| | | 247 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 248 | | public void AddRange(IEnumerable<T> items) |
| | 3 | 249 | | { |
| | 3 | 250 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | | 251 | | |
| | 3 | 252 | | if (ReferenceEquals(this, items)) |
| | 0 | 253 | | return; |
| | | 254 | | |
| | 3 | 255 | | if (items is ICollection<T> collection) |
| | 1 | 256 | | { |
| | 1 | 257 | | EnsureCapacityForAddRange(collection.Count); |
| | | 258 | | |
| | 9 | 259 | | foreach (T item in collection) |
| | 6 | 260 | | if (item != null) InsertIfNotExists(item); |
| | | 261 | | |
| | 1 | 262 | | return; |
| | | 263 | | } |
| | | 264 | | |
| | 2 | 265 | | if (items is IReadOnlyCollection<T> readOnlyCollection) |
| | 1 | 266 | | { |
| | 1 | 267 | | EnsureCapacityForAddRange(readOnlyCollection.Count); |
| | | 268 | | |
| | 11 | 269 | | foreach (T item in readOnlyCollection) |
| | 8 | 270 | | if (item != null) InsertIfNotExists(item); |
| | | 271 | | |
| | 1 | 272 | | return; |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | // Preserve single-pass enumeration for lazy or one-shot sources. |
| | 9 | 276 | | foreach (T item in items) |
| | 6 | 277 | | if (item != null) Add(item); |
| | 3 | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Adds the specified element to the set if it's not already present. |
| | | 282 | | /// </summary> |
| | | 283 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 284 | | private bool InsertIfNotExists(T item) |
| | 213658 | 285 | | { |
| | 213658 | 286 | | SwiftThrowHelper.ThrowIfNull(item, nameof(item)); |
| | | 287 | | |
| | 213657 | 288 | | int hashCode = _comparer.GetHashCode(item) & 0x7FFFFFFF; |
| | 213657 | 289 | | int entryIndex = hashCode & _entryMask; |
| | | 290 | | |
| | 213657 | 291 | | int step = 1; |
| | 363170 | 292 | | while (_entries[entryIndex].IsUsed) |
| | 149525 | 293 | | { |
| | 149525 | 294 | | if (_entries[entryIndex].HashCode == hashCode && _comparer.Equals(_entries[entryIndex].Value, item)) |
| | 12 | 295 | | return false; // Item already exists |
| | | 296 | | |
| | 149513 | 297 | | entryIndex = (entryIndex + step * step) & _entryMask; // Quadratic probing |
| | 149513 | 298 | | step++; |
| | 149513 | 299 | | } |
| | | 300 | | |
| | 327167 | 301 | | if ((uint)entryIndex > (uint)_lastIndex) _lastIndex = entryIndex; |
| | | 302 | | |
| | 213645 | 303 | | _entries[entryIndex].HashCode = hashCode; |
| | 213645 | 304 | | _entries[entryIndex].Value = item; |
| | 213645 | 305 | | _entries[entryIndex].IsUsed = true; |
| | 213645 | 306 | | _count++; |
| | 213645 | 307 | | _version++; |
| | | 308 | | |
| | 213645 | 309 | | if ((uint)step > (uint)_maxStepCount) |
| | 201 | 310 | | { |
| | 201 | 311 | | _maxStepCount = step; |
| | 201 | 312 | | if (_comparer is not IRandomedEqualityComparer && _maxStepCount > 100) |
| | 1 | 313 | | SwitchToRandomizedComparer(); // Attempt to recompute hash code with potential randomization for better |
| | 201 | 314 | | } |
| | | 315 | | |
| | 213645 | 316 | | return true; |
| | 213657 | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// Removes the specified element from the set. |
| | | 321 | | /// </summary> |
| | | 322 | | /// <param name="item">The element to remove from the set.</param> |
| | | 323 | | /// <returns> |
| | | 324 | | /// True if the element is successfully found and removed; otherwise, false. |
| | | 325 | | /// </returns> |
| | | 326 | | public bool Remove(T item) |
| | 100565 | 327 | | { |
| | 100565 | 328 | | SwiftThrowHelper.ThrowIfNull(item, nameof(item)); |
| | | 329 | | |
| | 100564 | 330 | | int hashCode = _comparer.GetHashCode(item) & 0x7FFFFFFF; |
| | 100564 | 331 | | int entryIndex = hashCode & _entryMask; |
| | | 332 | | |
| | 100564 | 333 | | int step = 0; |
| | 150821 | 334 | | while ((uint)step <= (uint)_lastIndex) |
| | 150820 | 335 | | { |
| | 150820 | 336 | | ref Entry entry = ref _entries[entryIndex]; |
| | | 337 | | // Stop probing if an unused entry is found (not deleted) |
| | 150820 | 338 | | if (!entry.IsUsed && entry.HashCode != -1) |
| | 4 | 339 | | return false; |
| | 150816 | 340 | | if (entry.IsUsed && entry.HashCode == hashCode && _comparer.Equals(entry.Value, item)) |
| | 100559 | 341 | | { |
| | | 342 | | // Mark entry as deleted |
| | 100559 | 343 | | entry.IsUsed = false; |
| | 100559 | 344 | | entry.Value = default; |
| | 100559 | 345 | | entry.HashCode = -1; |
| | 100559 | 346 | | _count--; |
| | 100559 | 347 | | if ((uint)_count == 0) _lastIndex = 0; |
| | 100559 | 348 | | _version++; |
| | 100559 | 349 | | return true; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | // Entry not found in expected entry, it either doesn't exist or was moved via quadratic probing |
| | 50257 | 353 | | step++; |
| | 50257 | 354 | | entryIndex = (entryIndex + step * step) & _entryMask; |
| | 50257 | 355 | | } |
| | 1 | 356 | | return false; // Item not found after full loop |
| | 100564 | 357 | | } |
| | | 358 | | |
| | | 359 | | /// <summary> |
| | | 360 | | /// Removes all elements from the set. |
| | | 361 | | /// </summary> |
| | | 362 | | public void Clear() |
| | 7 | 363 | | { |
| | 8 | 364 | | if ((uint)_count == 0) return; |
| | | 365 | | |
| | 246 | 366 | | for (uint i = 0; i <= (uint)_lastIndex; i++) |
| | 117 | 367 | | { |
| | 117 | 368 | | _entries[i].HashCode = -1; |
| | 117 | 369 | | _entries[i].Value = default; |
| | 117 | 370 | | _entries[i].IsUsed = false; |
| | 117 | 371 | | } |
| | | 372 | | |
| | 6 | 373 | | _count = 0; |
| | 6 | 374 | | _lastIndex = 0; |
| | 6 | 375 | | _maxStepCount = 0; |
| | 6 | 376 | | _movingFillRate = 0; |
| | 6 | 377 | | _adaptiveResizeFactor = 4; |
| | | 378 | | |
| | 6 | 379 | | _version++; |
| | 7 | 380 | | } |
| | | 381 | | |
| | | 382 | | #endregion |
| | | 383 | | |
| | | 384 | | #region Capacity Management |
| | | 385 | | |
| | | 386 | | /// <summary> |
| | | 387 | | /// Ensures that the hash set is resized when the current load factor exceeds the predefined threshold. |
| | | 388 | | /// </summary> |
| | | 389 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 390 | | private void CheckLoadThreshold() |
| | 213522 | 391 | | { |
| | 213522 | 392 | | if ((uint)_count >= _nextResizeCount) |
| | 35 | 393 | | Resize(_entries.Length * _adaptiveResizeFactor); |
| | 213522 | 394 | | } |
| | | 395 | | |
| | | 396 | | /// <summary> |
| | | 397 | | /// Ensures there is enough room to add a batch of items without repeatedly checking the load threshold. |
| | | 398 | | /// </summary> |
| | | 399 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 400 | | private void EnsureCapacityForAddRange(int incomingCount) |
| | 2 | 401 | | { |
| | 2 | 402 | | if (incomingCount <= 0) |
| | 0 | 403 | | return; |
| | | 404 | | |
| | 2 | 405 | | long requiredCount = (long)_count + incomingCount; |
| | 2 | 406 | | if (requiredCount > int.MaxValue) |
| | 0 | 407 | | throw new InvalidOperationException("The collection is too large."); |
| | | 408 | | |
| | 2 | 409 | | double minimumCapacity = Math.Ceiling(requiredCount / (double)_LoadFactorThreshold); |
| | 2 | 410 | | EnsureCapacity(minimumCapacity >= int.MaxValue ? int.MaxValue : (int)minimumCapacity); |
| | 2 | 411 | | } |
| | | 412 | | |
| | | 413 | | /// <summary> |
| | | 414 | | /// Ensures that the set can hold up to the specified number of elements without resizing. |
| | | 415 | | /// </summary> |
| | | 416 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 417 | | public void EnsureCapacity(int capacity) |
| | 4 | 418 | | { |
| | 4 | 419 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); // Capacity must be a power of 2 for proper masking |
| | 4 | 420 | | if (capacity > _entries.Length) |
| | 2 | 421 | | Resize(capacity); |
| | 4 | 422 | | } |
| | | 423 | | |
| | | 424 | | /// <summary> |
| | | 425 | | /// Resizes the hash set to the specified capacity, redistributing all entries to maintain efficiency. |
| | | 426 | | /// </summary> |
| | | 427 | | /// <param name="newSize"></param> |
| | | 428 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 429 | | private void Resize(int newSize) |
| | 37 | 430 | | { |
| | 37 | 431 | | Entry[] newEntries = new Entry[newSize]; |
| | 37 | 432 | | int newMask = newSize - 1; |
| | | 433 | | |
| | 37 | 434 | | int lastIndex = 0; |
| | 184522 | 435 | | for (uint i = 0; i <= (uint)_lastIndex; i++) |
| | 92224 | 436 | | { |
| | 92224 | 437 | | if (_entries[i].IsUsed) |
| | 85668 | 438 | | { |
| | 85668 | 439 | | ref Entry oldEntry = ref _entries[i]; |
| | 85668 | 440 | | int newIndex = oldEntry.HashCode & newMask; |
| | | 441 | | // If current entry not available, perform Quadratic probing to find the next available entry |
| | 85668 | 442 | | int step = 1; |
| | 90635 | 443 | | while (newEntries[newIndex].IsUsed) |
| | 4967 | 444 | | { |
| | 4967 | 445 | | newIndex = (newIndex + step * step) & newMask; |
| | 4967 | 446 | | step++; |
| | 4967 | 447 | | } |
| | 85668 | 448 | | newEntries[newIndex] = oldEntry; |
| | 134240 | 449 | | if (newIndex > lastIndex) lastIndex = newIndex; |
| | 85668 | 450 | | } |
| | 92224 | 451 | | } |
| | | 452 | | |
| | 37 | 453 | | _lastIndex = lastIndex; |
| | | 454 | | |
| | 37 | 455 | | CalculateAdaptiveResizeFactors(newSize); |
| | | 456 | | |
| | 37 | 457 | | _entries = newEntries; |
| | 37 | 458 | | _entryMask = newMask; |
| | | 459 | | |
| | 37 | 460 | | _version++; |
| | 37 | 461 | | } |
| | | 462 | | |
| | | 463 | | /// <summary> |
| | | 464 | | /// Sets the capacity of a <see cref="SwiftHashSet{T}"/> to the actual |
| | | 465 | | /// number of elements it contains, rounded up to a nearby next power of 2 value. |
| | | 466 | | /// </summary> |
| | | 467 | | public void TrimExcess() |
| | 1 | 468 | | { |
| | 1 | 469 | | int newSize = _count <= DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(_count); |
| | 1 | 470 | | if (newSize >= _entries.Length) return; |
| | | 471 | | |
| | 1 | 472 | | Entry[] newEntries = new Entry[newSize]; |
| | 1 | 473 | | int newMask = newSize - 1; |
| | | 474 | | |
| | 1 | 475 | | int lastIndex = 0; |
| | 26 | 476 | | for (int i = 0; i <= (uint)_lastIndex; i++) |
| | 12 | 477 | | { |
| | 12 | 478 | | if (_entries[i].IsUsed) |
| | 12 | 479 | | { |
| | 12 | 480 | | ref Entry oldEntry = ref _entries[i]; |
| | 12 | 481 | | int newIndex = oldEntry.HashCode & newMask; |
| | | 482 | | // If current entry not available, perform quadratic probing to find the next available entry |
| | 12 | 483 | | int step = 1; |
| | 12 | 484 | | while (newEntries[newIndex].IsUsed) |
| | 0 | 485 | | { |
| | 0 | 486 | | newIndex = (newIndex + step * step) & newMask; |
| | 0 | 487 | | step++; |
| | 0 | 488 | | } |
| | 12 | 489 | | newEntries[newIndex] = oldEntry; |
| | 23 | 490 | | if (newIndex > lastIndex) lastIndex = newIndex; |
| | 12 | 491 | | } |
| | 12 | 492 | | } |
| | | 493 | | |
| | 1 | 494 | | _lastIndex = lastIndex; |
| | | 495 | | |
| | 1 | 496 | | CalculateAdaptiveResizeFactors(newSize); |
| | | 497 | | |
| | 1 | 498 | | _entryMask = newMask; |
| | 1 | 499 | | _entries = newEntries; |
| | | 500 | | |
| | 1 | 501 | | _version++; |
| | 1 | 502 | | } |
| | | 503 | | |
| | | 504 | | /// <summary> |
| | | 505 | | /// Updates adaptive resize parameters based on the current fill rate to balance memory usage and performance. |
| | | 506 | | /// </summary> |
| | | 507 | | /// <param name="newSize"></param> |
| | | 508 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 509 | | private void CalculateAdaptiveResizeFactors(int newSize) |
| | 38 | 510 | | { |
| | | 511 | | // Calculate current fill rate and update moving average |
| | 38 | 512 | | double currentFillRate = (double)_count / newSize; |
| | 38 | 513 | | _movingFillRate = _movingFillRate == 0 ? currentFillRate : (_movingFillRate * 0.7 + currentFillRate * 0.3); |
| | | 514 | | |
| | 38 | 515 | | if (_movingFillRate > 0.3f) |
| | 2 | 516 | | _adaptiveResizeFactor = 2; // Growth stabilizing |
| | 36 | 517 | | else if (_movingFillRate < 0.28f) |
| | 36 | 518 | | _adaptiveResizeFactor = 4; // Rapid growth |
| | | 519 | | |
| | | 520 | | // Reset the resize threshold based on the new size |
| | 38 | 521 | | _nextResizeCount = (uint)(newSize * _LoadFactorThreshold); |
| | 38 | 522 | | } |
| | | 523 | | |
| | | 524 | | #endregion |
| | | 525 | | |
| | | 526 | | #region Utility Methods |
| | | 527 | | |
| | | 528 | | /// <summary> |
| | | 529 | | /// Initializes the hash set with a given capacity, ensuring it starts with an optimal internal structure. |
| | | 530 | | /// </summary> |
| | | 531 | | /// <param name="capacity"></param> |
| | | 532 | | /// <param name="comparer"></param> |
| | | 533 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 534 | | private void Initialize(int capacity, IEqualityComparer<T> comparer = null) |
| | 109 | 535 | | { |
| | 109 | 536 | | _comparer = SwiftHashTools.GetDefaultEqualityComparer(comparer); |
| | | 537 | | |
| | 109 | 538 | | int size = capacity < DefaultCapacity ? DefaultCapacity : SwiftHashTools.NextPowerOfTwo(capacity); |
| | 109 | 539 | | _entries = new Entry[size]; |
| | 109 | 540 | | _entryMask = size - 1; |
| | | 541 | | |
| | 109 | 542 | | _nextResizeCount = (uint)(size * _LoadFactorThreshold); |
| | 109 | 543 | | _adaptiveResizeFactor = 4; // start agressive |
| | 109 | 544 | | _movingFillRate = 0.0; |
| | 109 | 545 | | } |
| | | 546 | | |
| | | 547 | | /// <summary> |
| | | 548 | | /// Determines whether the set contains the specified element. |
| | | 549 | | /// </summary> |
| | | 550 | | /// <param name="item">The element to locate in the set.</param> |
| | | 551 | | /// <returns> |
| | | 552 | | /// True if the set contains the specified element; otherwise, false. |
| | | 553 | | /// </returns> |
| | | 554 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1237 | 555 | | public bool Contains(T item) => FindEntry(item) >= 0; |
| | | 556 | | |
| | | 557 | | /// <summary> |
| | | 558 | | /// Determines whether the <see cref="SwiftHashSet{T}"/> contains an element that matches the conditions defined by |
| | | 559 | | /// </summary> |
| | | 560 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 561 | | /// <returns><c>true</c> if the <see cref="SwiftHashSet{T}"/> contains one or more elements that match the specified |
| | | 562 | | public bool Exists(Predicate<T> match) |
| | 3 | 563 | | { |
| | 3 | 564 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 565 | | |
| | 16 | 566 | | for (int i = 0; i <= _lastIndex; i++) |
| | 7 | 567 | | { |
| | 7 | 568 | | if (_entries[i].IsUsed && match(_entries[i].Value)) |
| | 1 | 569 | | return true; |
| | 6 | 570 | | } |
| | | 571 | | |
| | 1 | 572 | | return false; |
| | 2 | 573 | | } |
| | | 574 | | |
| | | 575 | | /// <summary> |
| | | 576 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first ma |
| | | 577 | | /// </summary> |
| | | 578 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 579 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 580 | | public T Find(Predicate<T> match) |
| | 2 | 581 | | { |
| | 2 | 582 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 583 | | |
| | 16 | 584 | | for (int i = 0; i <= _lastIndex; i++) |
| | 7 | 585 | | { |
| | 7 | 586 | | if (_entries[i].IsUsed && match(_entries[i].Value)) |
| | 1 | 587 | | return _entries[i].Value; |
| | 6 | 588 | | } |
| | | 589 | | |
| | 1 | 590 | | return default; |
| | 2 | 591 | | } |
| | | 592 | | |
| | | 593 | | /// <summary> |
| | | 594 | | /// Searches the set for a given value and returns the equal value it finds, if any. |
| | | 595 | | /// </summary> |
| | | 596 | | public bool TryGetValue(T expected, out T actual) |
| | 2 | 597 | | { |
| | 2 | 598 | | int index = FindEntry(expected); |
| | 2 | 599 | | if (index >= 0) |
| | 1 | 600 | | { |
| | 1 | 601 | | actual = _entries[index].Value; |
| | 1 | 602 | | return true; |
| | | 603 | | } |
| | 1 | 604 | | actual = default; |
| | 1 | 605 | | return false; |
| | 2 | 606 | | } |
| | | 607 | | |
| | | 608 | | /// <summary> |
| | | 609 | | /// Copies the elements of the set to an array, starting at the specified array index. |
| | | 610 | | /// </summary> |
| | | 611 | | public void CopyTo(T[] array, int arrayIndex) |
| | 11 | 612 | | { |
| | 11 | 613 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 12 | 614 | | if ((uint)arrayIndex > array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex)); |
| | 9 | 615 | | if (array.Length - arrayIndex < _count) throw new InvalidOperationException("The array is not large enough to ho |
| | | 616 | | |
| | 92 | 617 | | for (uint i = 0; i <= (uint)_lastIndex; i++) |
| | 39 | 618 | | { |
| | 39 | 619 | | if (_entries[i].IsUsed) |
| | 28 | 620 | | array[arrayIndex++] = _entries[i].Value; |
| | 39 | 621 | | } |
| | 7 | 622 | | } |
| | | 623 | | |
| | | 624 | | /// <summary> |
| | | 625 | | /// Switches the hash set's comparer and rehashes all entries |
| | | 626 | | /// using the new comparer to redistribute them across <see cref="_entries"/>. |
| | | 627 | | /// </summary> |
| | | 628 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 629 | | public void SetComparer(IEqualityComparer<T> comparer = null) |
| | 3 | 630 | | { |
| | 3 | 631 | | SwiftThrowHelper.ThrowIfNull(comparer, nameof(comparer)); |
| | 3 | 632 | | if (ReferenceEquals(comparer, _comparer)) |
| | 0 | 633 | | return; |
| | | 634 | | |
| | 3 | 635 | | _comparer = comparer; |
| | 3 | 636 | | RehashEntries(); |
| | 3 | 637 | | } |
| | | 638 | | |
| | | 639 | | /// <summary> |
| | | 640 | | /// Replaces the hash set's comparer with a randomized comparer to mitigate high collision rates. |
| | | 641 | | /// </summary> |
| | | 642 | | private void SwitchToRandomizedComparer() |
| | 1 | 643 | | { |
| | 1 | 644 | | if (SwiftHashTools.IsWellKnownEqualityComparer(_comparer)) |
| | 1 | 645 | | _comparer = (IEqualityComparer<T>)SwiftHashTools.GetSwiftEqualityComparer(_comparer); |
| | 0 | 646 | | else return; // nothing to do here |
| | | 647 | | |
| | 1 | 648 | | RehashEntries(); |
| | 1 | 649 | | _maxStepCount = 0; |
| | | 650 | | |
| | 1 | 651 | | _version++; |
| | 1 | 652 | | } |
| | | 653 | | |
| | | 654 | | /// <summary> |
| | | 655 | | /// Reconstructs the internal entry structure to align with updated hash codes, ensuring efficient access and storag |
| | | 656 | | /// </summary> |
| | | 657 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 658 | | private void RehashEntries() |
| | 4 | 659 | | { |
| | 4 | 660 | | Entry[] newEntries = new Entry[_entries.Length]; |
| | 4 | 661 | | int newMask = newEntries.Length - 1; |
| | | 662 | | |
| | 4 | 663 | | int lastIndex = 0; |
| | 584 | 664 | | for (uint i = 0; i <= (uint)_lastIndex; i++) |
| | 288 | 665 | | { |
| | 288 | 666 | | if (_entries[i].IsUsed) |
| | 101 | 667 | | { |
| | 101 | 668 | | ref Entry oldEntry = ref _entries[i]; |
| | 101 | 669 | | oldEntry.HashCode = _comparer.GetHashCode(oldEntry.Value) & 0x7FFFFFFF; |
| | 101 | 670 | | int newIndex = oldEntry.HashCode & newMask; |
| | 101 | 671 | | int step = 1; |
| | 123 | 672 | | while (newEntries[newIndex].IsUsed) |
| | 22 | 673 | | { |
| | 22 | 674 | | newIndex = (newIndex + step * step) & newMask; // Quadratic probing |
| | 22 | 675 | | step++; |
| | 22 | 676 | | } |
| | 101 | 677 | | newEntries[newIndex] = _entries[i]; |
| | 122 | 678 | | if (newIndex > lastIndex) lastIndex = newIndex; |
| | 101 | 679 | | } |
| | 288 | 680 | | } |
| | | 681 | | |
| | 4 | 682 | | _lastIndex = lastIndex; |
| | | 683 | | |
| | 4 | 684 | | _entryMask = newMask; |
| | 4 | 685 | | _entries = newEntries; |
| | | 686 | | |
| | 4 | 687 | | _version++; |
| | 4 | 688 | | } |
| | | 689 | | |
| | | 690 | | /// <summary> |
| | | 691 | | /// Searches for an entry in the hash set by following its probing sequence, returning its index if found. |
| | | 692 | | /// </summary> |
| | | 693 | | /// <param name="item"></param> |
| | | 694 | | /// <returns></returns> |
| | | 695 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 696 | | private int FindEntry(T item) |
| | 1241 | 697 | | { |
| | 1242 | 698 | | if (item == null) return -1; |
| | | 699 | | |
| | 1240 | 700 | | int hashCode = _comparer.GetHashCode(item) & 0x7FFFFFFF; |
| | 1240 | 701 | | int entryIndex = hashCode & _entryMask; |
| | | 702 | | |
| | 1240 | 703 | | int step = 0; |
| | 6722 | 704 | | while ((uint)step <= (uint)_lastIndex) |
| | 6719 | 705 | | { |
| | 6719 | 706 | | ref Entry entry = ref _entries[entryIndex]; |
| | | 707 | | // Stop probing if an unused entry is found (not deleted) |
| | 6719 | 708 | | if (!entry.IsUsed && entry.HashCode != -1) |
| | 522 | 709 | | return -1; |
| | 6197 | 710 | | if (entry.IsUsed && entry.HashCode == hashCode && _comparer.Equals(entry.Value, item)) |
| | 715 | 711 | | return entryIndex; // Match found |
| | | 712 | | |
| | | 713 | | // Perform quadratic probing to see if maybe the entry was shifted. |
| | 5482 | 714 | | step++; |
| | 5482 | 715 | | entryIndex = (entryIndex + step * step) & _entryMask; |
| | | 716 | | |
| | 5482 | 717 | | } |
| | 3 | 718 | | return -1; // Item not found, full loop completed |
| | 1241 | 719 | | } |
| | | 720 | | |
| | | 721 | | #endregion |
| | | 722 | | |
| | | 723 | | #region Enumerators |
| | | 724 | | |
| | | 725 | | /// <summary> |
| | | 726 | | /// Returns an enumerator that iterates through the set. |
| | | 727 | | /// </summary> |
| | 29 | 728 | | public SwiftHashSetEnumerator GetEnumerator() => new SwiftHashSetEnumerator(this); |
| | 12 | 729 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 1 | 730 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 731 | | |
| | | 732 | | /// <summary> |
| | | 733 | | /// Provides an enumerator for iterating through the elements of the hash set, ensuring consistency during enumerati |
| | | 734 | | /// </summary> |
| | | 735 | | public struct SwiftHashSetEnumerator : IEnumerator<T>, IEnumerator, IDisposable |
| | | 736 | | { |
| | | 737 | | private readonly SwiftHashSet<T> _set; |
| | | 738 | | private readonly Entry[] _entries; |
| | | 739 | | private readonly uint _version; |
| | | 740 | | private int _index; |
| | | 741 | | private T _current; |
| | | 742 | | |
| | | 743 | | internal SwiftHashSetEnumerator(SwiftHashSet<T> set) |
| | 29 | 744 | | { |
| | 29 | 745 | | _set = set; |
| | 29 | 746 | | _version = set._version; |
| | 29 | 747 | | _entries = set._entries; // Cache the entry array |
| | 29 | 748 | | _index = -1; |
| | 29 | 749 | | _current = default; |
| | 29 | 750 | | } |
| | | 751 | | |
| | 57 | 752 | | public readonly T Current => _current; |
| | | 753 | | |
| | | 754 | | readonly object IEnumerator.Current |
| | | 755 | | { |
| | | 756 | | get |
| | 1 | 757 | | { |
| | 1 | 758 | | if (_index > (uint)_set._lastIndex) throw new InvalidOperationException("Bad enumeration"); |
| | 1 | 759 | | return _current; |
| | 1 | 760 | | } |
| | | 761 | | } |
| | | 762 | | |
| | | 763 | | public bool MoveNext() |
| | 84 | 764 | | { |
| | 84 | 765 | | if (_version != _set._version) |
| | 1 | 766 | | throw new InvalidOperationException("Collection was modified during enumeration."); |
| | | 767 | | |
| | 83 | 768 | | uint last = (uint)_set._lastIndex; |
| | 124 | 769 | | while (++_index <= last) |
| | 99 | 770 | | { |
| | 99 | 771 | | if (_entries[_index].IsUsed) |
| | 58 | 772 | | { |
| | 58 | 773 | | _current = _entries[_index].Value; |
| | 58 | 774 | | return true; |
| | | 775 | | } |
| | 41 | 776 | | } |
| | | 777 | | |
| | 25 | 778 | | _current = default; |
| | 25 | 779 | | return false; |
| | 83 | 780 | | } |
| | | 781 | | |
| | | 782 | | public void Reset() |
| | 1 | 783 | | { |
| | 1 | 784 | | if (_version != _set._version) |
| | 0 | 785 | | throw new InvalidOperationException("Collection was modified during enumeration."); |
| | | 786 | | |
| | 1 | 787 | | _index = -1; |
| | 1 | 788 | | _current = default; |
| | 1 | 789 | | } |
| | | 790 | | |
| | 52 | 791 | | public readonly void Dispose() { } |
| | | 792 | | } |
| | | 793 | | |
| | | 794 | | #endregion |
| | | 795 | | |
| | | 796 | | #region ISet<T> Implementations |
| | | 797 | | |
| | | 798 | | public void ExceptWith(IEnumerable<T> other) |
| | 1 | 799 | | { |
| | 1 | 800 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 801 | | |
| | 1 | 802 | | if (ReferenceEquals(this, other)) |
| | 0 | 803 | | { |
| | 0 | 804 | | Clear(); |
| | 0 | 805 | | return; |
| | | 806 | | } |
| | | 807 | | |
| | 1 | 808 | | var otherSet = new SwiftHashSet<T>(other, _comparer); |
| | | 809 | | |
| | 9 | 810 | | foreach (var item in otherSet) |
| | 3 | 811 | | Remove(item); |
| | 1 | 812 | | } |
| | | 813 | | |
| | | 814 | | public void IntersectWith(IEnumerable<T> other) |
| | 1 | 815 | | { |
| | 1 | 816 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 817 | | |
| | 1 | 818 | | if (ReferenceEquals(this, other)) |
| | 0 | 819 | | return; |
| | | 820 | | |
| | 1 | 821 | | var otherSet = new SwiftHashSet<T>(other, _comparer); |
| | | 822 | | |
| | 12 | 823 | | for (int i = 0; i <= _lastIndex; i++) |
| | 5 | 824 | | { |
| | 5 | 825 | | if (_entries[i].IsUsed) |
| | 4 | 826 | | { |
| | 4 | 827 | | var value = _entries[i].Value; |
| | 4 | 828 | | if (!otherSet.Contains(value)) |
| | 2 | 829 | | Remove(value); |
| | 4 | 830 | | } |
| | 5 | 831 | | } |
| | 1 | 832 | | } |
| | | 833 | | |
| | | 834 | | public bool IsProperSubsetOf(IEnumerable<T> other) |
| | 2 | 835 | | { |
| | 2 | 836 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 837 | | |
| | 2 | 838 | | var otherSet = new SwiftHashSet<T>(other, _comparer); |
| | | 839 | | |
| | 2 | 840 | | if (Count >= otherSet.Count) |
| | 1 | 841 | | return false; |
| | | 842 | | |
| | 8 | 843 | | for (int i = 0; i <= _lastIndex; i++) |
| | 3 | 844 | | if (_entries[i].IsUsed && !otherSet.Contains(_entries[i].Value)) |
| | 0 | 845 | | return false; |
| | | 846 | | |
| | 1 | 847 | | return true; |
| | 2 | 848 | | } |
| | | 849 | | |
| | | 850 | | public bool IsProperSupersetOf(IEnumerable<T> other) |
| | 2 | 851 | | { |
| | 2 | 852 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 853 | | |
| | 2 | 854 | | var otherSet = new SwiftHashSet<T>(other, _comparer); |
| | | 855 | | |
| | 2 | 856 | | if (Count <= otherSet.Count) |
| | 1 | 857 | | return false; |
| | | 858 | | |
| | 5 | 859 | | foreach (var item in otherSet) |
| | 1 | 860 | | if (!Contains(item)) |
| | 0 | 861 | | return false; |
| | | 862 | | |
| | 1 | 863 | | return true; |
| | 2 | 864 | | } |
| | | 865 | | |
| | | 866 | | public bool IsSubsetOf(IEnumerable<T> other) |
| | 2 | 867 | | { |
| | 2 | 868 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 869 | | |
| | 2 | 870 | | var otherSet = new SwiftHashSet<T>(other, _comparer); |
| | | 871 | | |
| | 2 | 872 | | if (otherSet.Count < Count) |
| | 1 | 873 | | return false; |
| | | 874 | | |
| | 8 | 875 | | for (int i = 0; i <= _lastIndex; i++) |
| | 3 | 876 | | if (_entries[i].IsUsed && !otherSet.Contains(_entries[i].Value)) |
| | 0 | 877 | | return false; |
| | | 878 | | |
| | 1 | 879 | | return true; |
| | 2 | 880 | | } |
| | | 881 | | |
| | | 882 | | public bool IsSupersetOf(IEnumerable<T> other) |
| | 2 | 883 | | { |
| | 2 | 884 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 885 | | |
| | 15 | 886 | | foreach (var item in other) |
| | 5 | 887 | | { |
| | 5 | 888 | | if (!Contains(item)) |
| | 1 | 889 | | return false; |
| | 4 | 890 | | } |
| | | 891 | | |
| | 1 | 892 | | return true; |
| | 2 | 893 | | } |
| | | 894 | | |
| | | 895 | | public bool Overlaps(IEnumerable<T> other) |
| | 2 | 896 | | { |
| | 2 | 897 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 898 | | |
| | 11 | 899 | | foreach (var item in other) |
| | 3 | 900 | | if (Contains(item)) |
| | 1 | 901 | | return true; |
| | | 902 | | |
| | 1 | 903 | | return false; |
| | 2 | 904 | | } |
| | | 905 | | |
| | | 906 | | public bool SetEquals(IEnumerable<T> other) |
| | 5 | 907 | | { |
| | 5 | 908 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 909 | | |
| | 5 | 910 | | var otherSet = new SwiftHashSet<T>(other, _comparer); |
| | | 911 | | |
| | 5 | 912 | | if (otherSet.Count != Count) |
| | 0 | 913 | | return false; |
| | | 914 | | |
| | 50 | 915 | | foreach (var item in otherSet) |
| | 18 | 916 | | if (!Contains(item)) |
| | 1 | 917 | | return false; |
| | | 918 | | |
| | 4 | 919 | | return true; |
| | 5 | 920 | | } |
| | | 921 | | |
| | | 922 | | public void SymmetricExceptWith(IEnumerable<T> other) |
| | 2 | 923 | | { |
| | 2 | 924 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 925 | | |
| | 2 | 926 | | if (ReferenceEquals(this, other)) |
| | 1 | 927 | | { |
| | 1 | 928 | | Clear(); |
| | 1 | 929 | | return; |
| | | 930 | | } |
| | | 931 | | |
| | 1 | 932 | | var otherSet = new SwiftHashSet<T>(other, _comparer); |
| | | 933 | | |
| | 7 | 934 | | foreach (var item in otherSet) |
| | 2 | 935 | | { |
| | 2 | 936 | | if (!Remove(item)) |
| | 1 | 937 | | Add(item); |
| | 2 | 938 | | } |
| | 2 | 939 | | } |
| | | 940 | | |
| | | 941 | | public void UnionWith(IEnumerable<T> other) |
| | 1 | 942 | | { |
| | 1 | 943 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 944 | | |
| | 7 | 945 | | foreach (var item in other) |
| | 2 | 946 | | Add(item); |
| | 1 | 947 | | } |
| | | 948 | | |
| | | 949 | | #endregion |
| | | 950 | | } |