| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftPackedSet.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 that stores unique values in a densely packed array |
| | | 23 | | /// while providing O(1) lookups via an internal hash map. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <remarks> |
| | | 26 | | /// <para> |
| | | 27 | | /// <see cref="SwiftPackedSet{T}"/> maintains values in a contiguous array for extremely |
| | | 28 | | /// cache-friendly iteration while using a hash-based lookup table to guarantee fast |
| | | 29 | | /// membership tests and removals. |
| | | 30 | | /// </para> |
| | | 31 | | /// <para> |
| | | 32 | | /// Removal uses a swap-back strategy that keeps the dense storage contiguous but does not |
| | | 33 | | /// preserve ordering. As a result, iteration order is not guaranteed to remain stable. |
| | | 34 | | /// </para> |
| | | 35 | | /// <para> |
| | | 36 | | /// This structure is commonly used in high-performance systems such as ECS (Entity Component Systems) |
| | | 37 | | /// where dense iteration speed is critical. |
| | | 38 | | /// </para> |
| | | 39 | | /// </remarks> |
| | | 40 | | /// <typeparam name="T">The type of elements contained in the set.</typeparam> |
| | | 41 | | [Serializable] |
| | | 42 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 43 | | [MemoryPackable] |
| | | 44 | | public sealed partial class SwiftPackedSet<T> : IStateBacked<SwiftArrayState<T>>, ISwiftCloneable<T>, ISet<T>, IEnumerab |
| | | 45 | | where T : notnull |
| | | 46 | | { |
| | | 47 | | #region Constants |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Represents the default initial capacity value used when no specific capacity is provided. |
| | | 51 | | /// </summary> |
| | | 52 | | public const int DefaultCapacity = 8; |
| | | 53 | | |
| | 2 | 54 | | private static readonly bool _clearReleasedSlots = RuntimeHelpers.IsReferenceOrContainsReferences<T>(); |
| | | 55 | | |
| | | 56 | | #endregion |
| | | 57 | | |
| | | 58 | | #region Fields |
| | | 59 | | |
| | | 60 | | private T[] _dense; |
| | | 61 | | private SwiftDictionary<T, int> _lookup; |
| | | 62 | | private int _count; |
| | | 63 | | |
| | | 64 | | [NonSerialized] |
| | | 65 | | private uint _version; |
| | | 66 | | |
| | | 67 | | [NonSerialized] |
| | | 68 | | private object? _syncRoot; |
| | | 69 | | |
| | | 70 | | #endregion |
| | | 71 | | |
| | | 72 | | #region Constructors |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Initializes a new instance of the SwiftPackedSet class with the default capacity. |
| | | 76 | | /// </summary> |
| | 144 | 77 | | public SwiftPackedSet() : this(DefaultCapacity) { } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Initializes a new instance of the SwiftPackedSet class with the specified initial capacity. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <remarks> |
| | | 83 | | /// The actual capacity is rounded up to the next power of two greater than or equal to the specified value, |
| | | 84 | | /// unless the specified value is less than or equal to the default capacity. |
| | | 85 | | /// </remarks> |
| | | 86 | | /// <param name="capacity"> |
| | | 87 | | /// The initial number of elements that the set can contain before resizing. |
| | | 88 | | /// If less than or equal to the default capacity, the default capacity is used. |
| | | 89 | | /// Must be non-negative. |
| | | 90 | | /// </param> |
| | 75 | 91 | | public SwiftPackedSet(int capacity) |
| | | 92 | | { |
| | 75 | 93 | | capacity = capacity <= DefaultCapacity |
| | 75 | 94 | | ? DefaultCapacity |
| | 75 | 95 | | : SwiftHashTools.NextPowerOfTwo(capacity); |
| | | 96 | | |
| | 75 | 97 | | _dense = new T[capacity]; |
| | 75 | 98 | | _lookup = new SwiftDictionary<T, int>(capacity); |
| | 75 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Initializes a new instance of the SwiftPackedSet class with the specified array state. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="state">The state object that provides the initial data and configuration for the set. Cannot be nul |
| | | 105 | | [MemoryPackConstructor] |
| | 8 | 106 | | public SwiftPackedSet(SwiftArrayState<T> state) |
| | | 107 | | { |
| | 8 | 108 | | State = state; |
| | | 109 | | |
| | 8 | 110 | | ChronicleGuard.ThrowIfNull(_dense, nameof(_dense)); |
| | 8 | 111 | | ChronicleGuard.ThrowIfNull(_lookup, nameof(_lookup)); |
| | 8 | 112 | | } |
| | | 113 | | |
| | | 114 | | #endregion |
| | | 115 | | |
| | | 116 | | #region Properties |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the number of elements contained in the collection. |
| | | 120 | | /// </summary> |
| | | 121 | | [JsonIgnore] |
| | | 122 | | [MemoryPackIgnore] |
| | 21 | 123 | | public int Count => _count; |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Gets the total number of elements that the collection can hold without resizing. |
| | | 127 | | /// </summary> |
| | | 128 | | [JsonIgnore] |
| | | 129 | | [MemoryPackIgnore] |
| | 5 | 130 | | public int Capacity => _dense.Length; |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Gets a value indicating whether access to the collection is synchronized (thread safe). |
| | | 134 | | /// </summary> |
| | | 135 | | [JsonIgnore] |
| | | 136 | | [MemoryPackIgnore] |
| | 1 | 137 | | public bool IsSynchronized => false; |
| | | 138 | | |
| | | 139 | | /// <inheritdoc/> |
| | | 140 | | [JsonIgnore] |
| | | 141 | | [MemoryPackIgnore] |
| | 3 | 142 | | public object SyncRoot => _syncRoot ??= new object(); |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Gets the underlying dense array of elements. |
| | | 146 | | /// </summary> |
| | | 147 | | [JsonIgnore] |
| | | 148 | | [MemoryPackIgnore] |
| | 12 | 149 | | public T[] Dense => _dense; |
| | | 150 | | |
| | | 151 | | /// <inheritdoc/> |
| | | 152 | | [JsonIgnore] |
| | | 153 | | [MemoryPackIgnore] |
| | 1 | 154 | | public bool IsReadOnly => false; |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets or sets the current state of the array, including its items and order. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <remarks> |
| | | 160 | | /// Use this property to capture or restore the array's contents and structure. |
| | | 161 | | /// Setting this property replaces the entire array with the provided state. |
| | | 162 | | /// </remarks> |
| | | 163 | | [JsonInclude] |
| | | 164 | | [MemoryPackInclude] |
| | | 165 | | public SwiftArrayState<T> State |
| | | 166 | | { |
| | | 167 | | get |
| | | 168 | | { |
| | 6 | 169 | | var values = new T[_count]; |
| | 6 | 170 | | Array.Copy(_dense, values, _count); |
| | | 171 | | |
| | 6 | 172 | | return new SwiftArrayState<T>(values); |
| | | 173 | | } |
| | | 174 | | internal set |
| | | 175 | | { |
| | 8 | 176 | | ChronicleGuard.ThrowIfNull(value.Items, nameof(value.Items)); |
| | | 177 | | |
| | 8 | 178 | | var values = value.Items; |
| | | 179 | | |
| | 8 | 180 | | int n = values.Length; |
| | 8 | 181 | | int newCapacity = n < DefaultCapacity |
| | 8 | 182 | | ? DefaultCapacity |
| | 8 | 183 | | : SwiftHashTools.NextPowerOfTwo(n); |
| | | 184 | | |
| | 8 | 185 | | _dense = new T[newCapacity]; |
| | 8 | 186 | | _lookup = new SwiftDictionary<T, int>(newCapacity); |
| | | 187 | | |
| | 8 | 188 | | if (n > 0) |
| | | 189 | | { |
| | 5 | 190 | | Array.Copy(values, _dense, n); |
| | | 191 | | |
| | 2042 | 192 | | for (int i = 0; i < n; i++) |
| | 1016 | 193 | | _lookup.Add(values[i], i); |
| | | 194 | | } |
| | | 195 | | |
| | 8 | 196 | | _count = n; |
| | 8 | 197 | | _version++; |
| | 8 | 198 | | } |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | #endregion |
| | | 202 | | |
| | | 203 | | #region Core Operations |
| | | 204 | | |
| | | 205 | | /// <inheritdoc/> |
| | | 206 | | public bool Contains(T value) |
| | 1040 | 207 | | => _lookup.ContainsKey(value); |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Returns a read-only span over the populated dense portion of the set. |
| | | 211 | | /// </summary> |
| | 1 | 212 | | public ReadOnlySpan<T> AsReadOnlySpan() => _dense.AsSpan(0, _count); |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Determines whether the <see cref="SwiftPackedSet{T}"/> contains an element that matches the conditions defined b |
| | | 216 | | /// </summary> |
| | | 217 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 218 | | /// <returns><c>true</c> if the <see cref="SwiftPackedSet{T}"/> contains one or more elements that match the specifi |
| | | 219 | | public bool Exists(Predicate<T> match) |
| | | 220 | | { |
| | 3 | 221 | | ChronicleGuard.ThrowIfNull(match, nameof(match)); |
| | | 222 | | |
| | 10 | 223 | | for (int i = 0; i < _count; i++) |
| | | 224 | | { |
| | 4 | 225 | | if (match(_dense[i])) |
| | 1 | 226 | | return true; |
| | | 227 | | } |
| | | 228 | | |
| | 1 | 229 | | return false; |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first ma |
| | | 234 | | /// </summary> |
| | | 235 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 236 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 237 | | public T Find(Predicate<T> match) |
| | | 238 | | { |
| | 2 | 239 | | ChronicleGuard.ThrowIfNull(match, nameof(match)); |
| | | 240 | | |
| | 10 | 241 | | for (int i = 0; i < _count; i++) |
| | | 242 | | { |
| | 4 | 243 | | if (match(_dense[i])) |
| | 1 | 244 | | return _dense[i]; |
| | | 245 | | } |
| | | 246 | | |
| | 1 | 247 | | return default!; |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | /// <inheritdoc/> |
| | | 251 | | public bool Add(T value) |
| | | 252 | | { |
| | 11263 | 253 | | if (_lookup.ContainsKey(value)) |
| | 4 | 254 | | return false; |
| | | 255 | | |
| | 11259 | 256 | | EnsureCapacity(_count + 1); |
| | | 257 | | |
| | 11259 | 258 | | _dense[_count] = value; |
| | 11259 | 259 | | _lookup.Add(value, _count); |
| | | 260 | | |
| | 11259 | 261 | | _count++; |
| | 11259 | 262 | | _version++; |
| | | 263 | | |
| | 11259 | 264 | | return true; |
| | | 265 | | } |
| | | 266 | | |
| | 1 | 267 | | void ICollection<T>.Add(T item) => Add(item); |
| | | 268 | | |
| | | 269 | | /// <inheritdoc/> |
| | | 270 | | public bool Remove(T value) |
| | | 271 | | { |
| | 5014 | 272 | | if (!_lookup.TryGetValue(value, out int index)) |
| | 3 | 273 | | return false; |
| | | 274 | | |
| | 5011 | 275 | | int last = --_count; |
| | | 276 | | |
| | 5011 | 277 | | if (index != last) |
| | | 278 | | { |
| | 5007 | 279 | | T moved = _dense[last]; |
| | | 280 | | |
| | 5007 | 281 | | _dense[index] = moved; |
| | 5007 | 282 | | _lookup[moved] = index; |
| | | 283 | | } |
| | | 284 | | |
| | 5011 | 285 | | if (_clearReleasedSlots) |
| | 3 | 286 | | _dense[last] = default!; |
| | 5011 | 287 | | _lookup.Remove(value); |
| | | 288 | | |
| | 5011 | 289 | | _version++; |
| | 5011 | 290 | | return true; |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | /// <inheritdoc/> |
| | | 294 | | public void Clear() |
| | | 295 | | { |
| | 149 | 296 | | if (_count == 0) return; |
| | | 297 | | |
| | 139 | 298 | | if (_clearReleasedSlots) |
| | 131 | 299 | | Array.Clear(_dense, 0, _count); |
| | 139 | 300 | | _lookup.Clear(); |
| | | 301 | | |
| | 139 | 302 | | _count = 0; |
| | 139 | 303 | | _version++; |
| | 139 | 304 | | } |
| | | 305 | | |
| | | 306 | | /// <summary> |
| | | 307 | | /// Removes all elements, optionally releasing references throughout retained backing storage. |
| | | 308 | | /// </summary> |
| | | 309 | | /// <param name="clearRetainedReferences">If true, also releases references outside the live range; otherwise uses o |
| | | 310 | | /// <remarks>Capacity is preserved. Full reference cleanup can take time proportional to retained capacity.</remarks |
| | | 311 | | public void Clear(bool clearRetainedReferences) |
| | | 312 | | { |
| | 136 | 313 | | if (clearRetainedReferences && _clearReleasedSlots) |
| | | 314 | | { |
| | 130 | 315 | | Array.Clear(_dense, _count, _dense.Length - _count); |
| | 130 | 316 | | _lookup.Clear(clearRetainedReferences: true); |
| | | 317 | | } |
| | 136 | 318 | | Clear(); |
| | 136 | 319 | | } |
| | | 320 | | |
| | | 321 | | #endregion |
| | | 322 | | |
| | | 323 | | #region Capacity |
| | | 324 | | |
| | | 325 | | /// <summary> |
| | | 326 | | /// Ensures that the internal storage has at least the specified capacity, expanding it if necessary. |
| | | 327 | | /// </summary> |
| | | 328 | | /// <remarks> |
| | | 329 | | /// If the current capacity is less than the specified value, the internal storage is resized to |
| | | 330 | | /// the next power of two greater than or equal to the specified capacity. |
| | | 331 | | /// Existing elements are preserved. |
| | | 332 | | /// </remarks> |
| | | 333 | | /// <param name="capacity">The minimum number of elements that the internal storage should be able to hold. Must be |
| | | 334 | | public void EnsureCapacity(int capacity) |
| | | 335 | | { |
| | 11260 | 336 | | int newCapacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 11260 | 337 | | if (newCapacity <= _dense.Length) |
| | 11242 | 338 | | return; |
| | | 339 | | |
| | 18 | 340 | | var newArray = new T[newCapacity]; |
| | 18 | 341 | | Array.Copy(_dense, newArray, _count); |
| | | 342 | | |
| | 18 | 343 | | _dense = newArray; |
| | 18 | 344 | | } |
| | | 345 | | |
| | | 346 | | #endregion |
| | | 347 | | |
| | | 348 | | #region Enumeration |
| | | 349 | | |
| | | 350 | | /// <inheritdoc cref="IEnumerable.GetEnumerator()"/> |
| | 55 | 351 | | public SwiftPackedSetEnumerator GetEnumerator() => new(this); |
| | 34 | 352 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 19 | 353 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 354 | | |
| | | 355 | | |
| | | 356 | | |
| | | 357 | | #endregion |
| | | 358 | | |
| | | 359 | | #region Clone |
| | | 360 | | |
| | | 361 | | /// <inheritdoc/> |
| | | 362 | | public void CloneTo(ICollection<T> output) |
| | | 363 | | { |
| | 1 | 364 | | ChronicleGuard.ThrowIfNull(output, nameof(output)); |
| | | 365 | | |
| | 1 | 366 | | output.Clear(); |
| | | 367 | | |
| | 6 | 368 | | for (int i = 0; i < _count; i++) |
| | 2 | 369 | | output.Add(_dense[i]); |
| | 1 | 370 | | } |
| | | 371 | | |
| | | 372 | | /// <inheritdoc/> |
| | | 373 | | public void CopyTo(T[] array, int arrayIndex) |
| | | 374 | | { |
| | 4 | 375 | | ChronicleGuard.ThrowIfNull(array, nameof(array)); |
| | 4 | 376 | | ChronicleGuard.ThrowIfArrayIndexInvalid(arrayIndex, array.Length); |
| | 3 | 377 | | ChronicleGuard.ThrowIfArgument(array.Length - arrayIndex < _count, nameof(array), "Destination array is not long |
| | | 378 | | |
| | 2 | 379 | | Array.Copy(_dense, 0, array, arrayIndex, _count); |
| | 2 | 380 | | } |
| | | 381 | | |
| | | 382 | | #endregion |
| | | 383 | | |
| | | 384 | | #region ISet<T> Implementations |
| | | 385 | | |
| | | 386 | | /// <inheritdoc/> |
| | | 387 | | public void ExceptWith(IEnumerable<T> other) |
| | | 388 | | { |
| | 2 | 389 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 390 | | |
| | 2 | 391 | | if (ReferenceEquals(this, other)) |
| | | 392 | | { |
| | 1 | 393 | | Clear(); |
| | 1 | 394 | | return; |
| | | 395 | | } |
| | | 396 | | |
| | 1 | 397 | | var otherSet = new SwiftHashSet<T>(other); |
| | | 398 | | |
| | 8 | 399 | | foreach (var item in otherSet) |
| | 3 | 400 | | Remove(item); |
| | 1 | 401 | | } |
| | | 402 | | |
| | | 403 | | /// <inheritdoc/> |
| | | 404 | | public void IntersectWith(IEnumerable<T> other) |
| | | 405 | | { |
| | 2 | 406 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 407 | | |
| | 2 | 408 | | if (ReferenceEquals(this, other)) |
| | 1 | 409 | | return; |
| | | 410 | | |
| | 1 | 411 | | var otherSet = new SwiftHashSet<T>(other); |
| | | 412 | | |
| | 10 | 413 | | for (int i = _count - 1; i >= 0; i--) |
| | | 414 | | { |
| | 4 | 415 | | var value = _dense[i]; |
| | 4 | 416 | | if (!otherSet.Contains(value)) |
| | 2 | 417 | | Remove(value); |
| | | 418 | | } |
| | 1 | 419 | | } |
| | | 420 | | |
| | | 421 | | /// <inheritdoc/> |
| | | 422 | | public bool IsProperSubsetOf(IEnumerable<T> other) |
| | | 423 | | { |
| | 3 | 424 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 425 | | |
| | 3 | 426 | | var set = new SwiftHashSet<T>(other); |
| | | 427 | | |
| | 3 | 428 | | if (_count >= set.Count) |
| | 1 | 429 | | return false; |
| | | 430 | | |
| | 14 | 431 | | for (int i = 0; i < _count; i++) |
| | 6 | 432 | | if (!set.Contains(_dense[i])) |
| | 1 | 433 | | return false; |
| | | 434 | | |
| | 1 | 435 | | return true; |
| | | 436 | | } |
| | | 437 | | |
| | | 438 | | /// <inheritdoc/> |
| | | 439 | | public bool IsProperSupersetOf(IEnumerable<T> other) |
| | | 440 | | { |
| | 3 | 441 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 442 | | |
| | 3 | 443 | | var set = new SwiftHashSet<T>(other); |
| | | 444 | | |
| | 3 | 445 | | if (_count <= set.Count) |
| | 1 | 446 | | return false; |
| | | 447 | | |
| | 9 | 448 | | foreach (var item in set) |
| | 3 | 449 | | if (!Contains(item)) |
| | 1 | 450 | | return false; |
| | | 451 | | |
| | 1 | 452 | | return true; |
| | 1 | 453 | | } |
| | | 454 | | |
| | | 455 | | /// <inheritdoc/> |
| | | 456 | | public bool IsSubsetOf(IEnumerable<T> other) |
| | | 457 | | { |
| | 3 | 458 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 459 | | |
| | 3 | 460 | | var set = new SwiftHashSet<T>(other); |
| | | 461 | | |
| | 3 | 462 | | if (_count > set.Count) |
| | 1 | 463 | | return false; |
| | | 464 | | |
| | 14 | 465 | | for (int i = 0; i < _count; i++) |
| | 6 | 466 | | if (!set.Contains(_dense[i])) |
| | 1 | 467 | | return false; |
| | | 468 | | |
| | 1 | 469 | | return true; |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | /// <inheritdoc/> |
| | | 473 | | public bool IsSupersetOf(IEnumerable<T> other) |
| | | 474 | | { |
| | 2 | 475 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 476 | | |
| | 13 | 477 | | foreach (var item in other) |
| | 5 | 478 | | if (!Contains(item)) |
| | 1 | 479 | | return false; |
| | | 480 | | |
| | 1 | 481 | | return true; |
| | 1 | 482 | | } |
| | | 483 | | |
| | | 484 | | /// <inheritdoc/> |
| | | 485 | | public bool Overlaps(IEnumerable<T> other) |
| | | 486 | | { |
| | 2 | 487 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 488 | | |
| | 9 | 489 | | foreach (var item in other) |
| | 3 | 490 | | if (Contains(item)) |
| | 1 | 491 | | return true; |
| | | 492 | | |
| | 1 | 493 | | return false; |
| | 1 | 494 | | } |
| | | 495 | | |
| | | 496 | | /// <inheritdoc/> |
| | | 497 | | public bool SetEquals(IEnumerable<T> other) |
| | | 498 | | { |
| | 6 | 499 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 500 | | |
| | 6 | 501 | | var set = new SwiftHashSet<T>(other); |
| | | 502 | | |
| | 6 | 503 | | if (_count != set.Count) |
| | 1 | 504 | | return false; |
| | | 505 | | |
| | 38 | 506 | | for (int i = 0; i < _count; i++) |
| | 15 | 507 | | if (!set.Contains(_dense[i])) |
| | 1 | 508 | | return false; |
| | | 509 | | |
| | 4 | 510 | | return true; |
| | | 511 | | } |
| | | 512 | | |
| | | 513 | | /// <inheritdoc/> |
| | | 514 | | public void SymmetricExceptWith(IEnumerable<T> other) |
| | | 515 | | { |
| | 2 | 516 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 517 | | |
| | 2 | 518 | | if (ReferenceEquals(this, other)) |
| | | 519 | | { |
| | 1 | 520 | | Clear(); |
| | 1 | 521 | | return; |
| | | 522 | | } |
| | | 523 | | |
| | 1 | 524 | | var set = new SwiftHashSet<T>(other); |
| | | 525 | | |
| | 6 | 526 | | foreach (var item in set) |
| | | 527 | | { |
| | 2 | 528 | | if (!Remove(item)) |
| | 1 | 529 | | Add(item); |
| | | 530 | | } |
| | 1 | 531 | | } |
| | | 532 | | |
| | | 533 | | /// <inheritdoc/> |
| | | 534 | | public void UnionWith(IEnumerable<T> other) |
| | | 535 | | { |
| | 1 | 536 | | ChronicleGuard.ThrowIfNull(other, nameof(other)); |
| | | 537 | | |
| | 6 | 538 | | foreach (var item in other) |
| | 2 | 539 | | Add(item); |
| | 1 | 540 | | } |
| | | 541 | | |
| | | 542 | | #endregion |
| | | 543 | | } |