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