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