| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Text.Json.Serialization; |
| | | 7 | | |
| | | 8 | | namespace SwiftCollections; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents a high-performance set that stores unique values in a densely packed array |
| | | 12 | | /// while providing O(1) lookups via an internal hash map. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// <para> |
| | | 16 | | /// <see cref="SwiftPackedSet{T}"/> maintains values in a contiguous array for extremely |
| | | 17 | | /// cache-friendly iteration while using a hash-based lookup table to guarantee fast |
| | | 18 | | /// membership tests and removals. |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// Removal uses a swap-back strategy that keeps the dense storage contiguous but does not |
| | | 22 | | /// preserve ordering. As a result, iteration order is not guaranteed to remain stable. |
| | | 23 | | /// </para> |
| | | 24 | | /// <para> |
| | | 25 | | /// This structure is commonly used in high-performance systems such as ECS (Entity Component Systems) |
| | | 26 | | /// where dense iteration speed is critical. |
| | | 27 | | /// </para> |
| | | 28 | | /// </remarks> |
| | | 29 | | /// <typeparam name="T">The type of elements contained in the set.</typeparam> |
| | | 30 | | [Serializable] |
| | | 31 | | [JsonConverter(typeof(SwiftStateJsonConverterFactory))] |
| | | 32 | | [MemoryPackable] |
| | | 33 | | public sealed partial class SwiftPackedSet<T> : |
| | | 34 | | ISwiftCloneable<T>, |
| | | 35 | | ISet<T>, |
| | | 36 | | IEnumerable<T>, |
| | | 37 | | IEnumerable |
| | | 38 | | { |
| | | 39 | | #region Constants |
| | | 40 | | |
| | | 41 | | public const int DefaultCapacity = 8; |
| | 2 | 42 | | private static readonly bool _clearReleasedSlots = RuntimeHelpers.IsReferenceOrContainsReferences<T>(); |
| | | 43 | | |
| | | 44 | | #endregion |
| | | 45 | | |
| | | 46 | | #region Fields |
| | | 47 | | |
| | | 48 | | private T[] _dense; |
| | | 49 | | private SwiftDictionary<T, int> _lookup; |
| | | 50 | | private int _count; |
| | | 51 | | |
| | | 52 | | [NonSerialized] |
| | | 53 | | private uint _version; |
| | | 54 | | |
| | | 55 | | [NonSerialized] |
| | | 56 | | private object _syncRoot; |
| | | 57 | | |
| | | 58 | | #endregion |
| | | 59 | | |
| | | 60 | | #region Constructors |
| | | 61 | | |
| | 138 | 62 | | public SwiftPackedSet() : this(DefaultCapacity) { } |
| | | 63 | | |
| | 47 | 64 | | public SwiftPackedSet(int capacity) |
| | 47 | 65 | | { |
| | 47 | 66 | | capacity = capacity <= DefaultCapacity |
| | 47 | 67 | | ? DefaultCapacity |
| | 47 | 68 | | : SwiftHashTools.NextPowerOfTwo(capacity); |
| | | 69 | | |
| | 47 | 70 | | _dense = new T[capacity]; |
| | 47 | 71 | | _lookup = new SwiftDictionary<T, int>(capacity); |
| | 47 | 72 | | } |
| | | 73 | | |
| | | 74 | | [MemoryPackConstructor] |
| | 7 | 75 | | public SwiftPackedSet(SwiftArrayState<T> state) |
| | 7 | 76 | | { |
| | 7 | 77 | | State = state; |
| | 7 | 78 | | } |
| | | 79 | | |
| | | 80 | | #endregion |
| | | 81 | | |
| | | 82 | | #region Properties |
| | | 83 | | |
| | | 84 | | [JsonIgnore] |
| | | 85 | | [MemoryPackIgnore] |
| | 19 | 86 | | public int Count => _count; |
| | | 87 | | |
| | | 88 | | [JsonIgnore] |
| | | 89 | | [MemoryPackIgnore] |
| | 4 | 90 | | public int Capacity => _dense.Length; |
| | | 91 | | |
| | | 92 | | [JsonIgnore] |
| | | 93 | | [MemoryPackIgnore] |
| | 1 | 94 | | public bool IsSynchronized => false; |
| | | 95 | | |
| | | 96 | | [JsonIgnore] |
| | | 97 | | [MemoryPackIgnore] |
| | 1 | 98 | | public object SyncRoot => _syncRoot ??= new object(); |
| | | 99 | | |
| | | 100 | | [JsonIgnore] |
| | | 101 | | [MemoryPackIgnore] |
| | 8 | 102 | | public T[] Dense => _dense; |
| | | 103 | | |
| | | 104 | | [JsonIgnore] |
| | | 105 | | [MemoryPackIgnore] |
| | 1 | 106 | | public bool IsReadOnly => false; |
| | | 107 | | |
| | | 108 | | [JsonInclude] |
| | | 109 | | [MemoryPackInclude] |
| | | 110 | | public SwiftArrayState<T> State |
| | | 111 | | { |
| | | 112 | | get |
| | 6 | 113 | | { |
| | 6 | 114 | | var values = new T[_count]; |
| | 6 | 115 | | Array.Copy(_dense, values, _count); |
| | | 116 | | |
| | 6 | 117 | | return new SwiftArrayState<T>(values); |
| | 6 | 118 | | } |
| | | 119 | | internal set |
| | 7 | 120 | | { |
| | 7 | 121 | | var values = value.Items ?? Array.Empty<T>(); |
| | | 122 | | |
| | 7 | 123 | | int n = values.Length; |
| | 7 | 124 | | int newCapacity = n < DefaultCapacity |
| | 7 | 125 | | ? DefaultCapacity |
| | 7 | 126 | | : SwiftHashTools.NextPowerOfTwo(n); |
| | | 127 | | |
| | 7 | 128 | | _dense = new T[newCapacity]; |
| | 7 | 129 | | _lookup = new SwiftDictionary<T, int>(newCapacity); |
| | | 130 | | |
| | 7 | 131 | | if (n > 0) |
| | 4 | 132 | | { |
| | 4 | 133 | | Array.Copy(values, _dense, n); |
| | | 134 | | |
| | 2022 | 135 | | for (int i = 0; i < n; i++) |
| | 1007 | 136 | | _lookup.Add(values[i], i); |
| | 4 | 137 | | } |
| | | 138 | | |
| | 7 | 139 | | _count = n; |
| | 7 | 140 | | _version++; |
| | 7 | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | #endregion |
| | | 145 | | |
| | | 146 | | #region Core Operations |
| | | 147 | | |
| | | 148 | | public bool Contains(T value) |
| | 1035 | 149 | | => _lookup.ContainsKey(value); |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Returns a read-only span over the populated dense portion of the set. |
| | | 153 | | /// </summary> |
| | 1 | 154 | | public ReadOnlySpan<T> AsReadOnlySpan() => _dense.AsSpan(0, _count); |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Determines whether the <see cref="SwiftPackedSet{T}"/> contains an element that matches the conditions defined b |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 160 | | /// <returns><c>true</c> if the <see cref="SwiftPackedSet{T}"/> contains one or more elements that match the specifi |
| | | 161 | | public bool Exists(Predicate<T> match) |
| | 3 | 162 | | { |
| | 3 | 163 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 164 | | |
| | 10 | 165 | | for (int i = 0; i < _count; i++) |
| | 4 | 166 | | { |
| | 4 | 167 | | if (match(_dense[i])) |
| | 1 | 168 | | return true; |
| | 3 | 169 | | } |
| | | 170 | | |
| | 1 | 171 | | return false; |
| | 2 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first ma |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 178 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 179 | | public T Find(Predicate<T> match) |
| | 2 | 180 | | { |
| | 2 | 181 | | SwiftThrowHelper.ThrowIfNull(match, nameof(match)); |
| | | 182 | | |
| | 10 | 183 | | for (int i = 0; i < _count; i++) |
| | 4 | 184 | | { |
| | 4 | 185 | | if (match(_dense[i])) |
| | 1 | 186 | | return _dense[i]; |
| | 3 | 187 | | } |
| | | 188 | | |
| | 1 | 189 | | return default; |
| | 2 | 190 | | } |
| | | 191 | | |
| | | 192 | | public bool Add(T value) |
| | 11096 | 193 | | { |
| | 11096 | 194 | | if (_lookup.ContainsKey(value)) |
| | 1 | 195 | | return false; |
| | | 196 | | |
| | 11095 | 197 | | EnsureCapacity(_count + 1); |
| | | 198 | | |
| | 11095 | 199 | | _dense[_count] = value; |
| | 11095 | 200 | | _lookup.Add(value, _count); |
| | | 201 | | |
| | 11095 | 202 | | _count++; |
| | 11095 | 203 | | _version++; |
| | | 204 | | |
| | 11095 | 205 | | return true; |
| | 11096 | 206 | | } |
| | | 207 | | |
| | 1 | 208 | | void ICollection<T>.Add(T item) => Add(item); |
| | | 209 | | |
| | | 210 | | public bool Remove(T value) |
| | 5013 | 211 | | { |
| | 5013 | 212 | | if (!_lookup.TryGetValue(value, out int index)) |
| | 3 | 213 | | return false; |
| | | 214 | | |
| | 5010 | 215 | | int last = --_count; |
| | | 216 | | |
| | 5010 | 217 | | if (index != last) |
| | 5006 | 218 | | { |
| | 5006 | 219 | | T moved = _dense[last]; |
| | | 220 | | |
| | 5006 | 221 | | _dense[index] = moved; |
| | 5006 | 222 | | _lookup[moved] = index; |
| | 5006 | 223 | | } |
| | | 224 | | |
| | 5010 | 225 | | if (_clearReleasedSlots) |
| | 3 | 226 | | _dense[last] = default; |
| | 5010 | 227 | | _lookup.Remove(value); |
| | | 228 | | |
| | 5010 | 229 | | _version++; |
| | 5010 | 230 | | return true; |
| | 5013 | 231 | | } |
| | | 232 | | |
| | | 233 | | public void Clear() |
| | 7 | 234 | | { |
| | 8 | 235 | | if (_count == 0) return; |
| | | 236 | | |
| | 6 | 237 | | if (_clearReleasedSlots) |
| | 1 | 238 | | Array.Clear(_dense, 0, _count); |
| | 6 | 239 | | _lookup.Clear(); |
| | | 240 | | |
| | 6 | 241 | | _count = 0; |
| | 6 | 242 | | _version++; |
| | 7 | 243 | | } |
| | | 244 | | |
| | | 245 | | #endregion |
| | | 246 | | |
| | | 247 | | #region Capacity |
| | | 248 | | |
| | | 249 | | public void EnsureCapacity(int capacity) |
| | 11096 | 250 | | { |
| | 11096 | 251 | | int newCapacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 11096 | 252 | | if (newCapacity <= _dense.Length) |
| | 11078 | 253 | | return; |
| | | 254 | | |
| | 18 | 255 | | var newArray = new T[newCapacity]; |
| | 18 | 256 | | Array.Copy(_dense, newArray, _count); |
| | | 257 | | |
| | 18 | 258 | | _dense = newArray; |
| | 11096 | 259 | | } |
| | | 260 | | |
| | | 261 | | #endregion |
| | | 262 | | |
| | | 263 | | #region Enumeration |
| | | 264 | | |
| | 15 | 265 | | public Enumerator GetEnumerator() => new Enumerator(this); |
| | | 266 | | |
| | 12 | 267 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 1 | 268 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 269 | | |
| | | 270 | | public struct Enumerator : IEnumerator<T> |
| | | 271 | | { |
| | | 272 | | private readonly SwiftPackedSet<T> _set; |
| | | 273 | | private readonly uint _version; |
| | | 274 | | private int _index; |
| | | 275 | | |
| | | 276 | | internal Enumerator(SwiftPackedSet<T> set) |
| | 15 | 277 | | { |
| | 15 | 278 | | _set = set; |
| | 15 | 279 | | _version = set._version; |
| | 15 | 280 | | _index = -1; |
| | 15 | 281 | | Current = default; |
| | 15 | 282 | | } |
| | | 283 | | |
| | 44 | 284 | | public T Current { get; private set; } |
| | | 285 | | |
| | 2 | 286 | | object IEnumerator.Current => Current; |
| | | 287 | | |
| | | 288 | | public bool MoveNext() |
| | 19 | 289 | | { |
| | 19 | 290 | | if (_version != _set._version) |
| | 1 | 291 | | throw new InvalidOperationException("Collection modified during enumeration"); |
| | | 292 | | |
| | 18 | 293 | | int next = _index + 1; |
| | 18 | 294 | | if (next >= _set._count) |
| | 12 | 295 | | { |
| | 12 | 296 | | Current = default; |
| | 12 | 297 | | return false; |
| | | 298 | | } |
| | | 299 | | |
| | 6 | 300 | | _index = next; |
| | 6 | 301 | | Current = _set._dense[next]; |
| | 6 | 302 | | return true; |
| | 18 | 303 | | } |
| | | 304 | | |
| | | 305 | | public void Reset() |
| | 2 | 306 | | { |
| | 2 | 307 | | if (_version != _set._version) |
| | 1 | 308 | | throw new InvalidOperationException("Collection modified during enumeration"); |
| | | 309 | | |
| | 1 | 310 | | _index = -1; |
| | 1 | 311 | | Current = default; |
| | 1 | 312 | | } |
| | | 313 | | |
| | 24 | 314 | | public void Dispose() { } |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | #endregion |
| | | 318 | | |
| | | 319 | | #region Clone |
| | | 320 | | |
| | | 321 | | public void CloneTo(ICollection<T> output) |
| | 1 | 322 | | { |
| | 1 | 323 | | SwiftThrowHelper.ThrowIfNull(output, nameof(output)); |
| | | 324 | | |
| | 1 | 325 | | output.Clear(); |
| | | 326 | | |
| | 6 | 327 | | for (int i = 0; i < _count; i++) |
| | 2 | 328 | | output.Add(_dense[i]); |
| | 1 | 329 | | } |
| | | 330 | | |
| | | 331 | | public void CopyTo(T[] array, int arrayIndex) |
| | 3 | 332 | | { |
| | 3 | 333 | | SwiftThrowHelper.ThrowIfNull(array, nameof(array)); |
| | 4 | 334 | | if ((uint)arrayIndex > array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex)); |
| | 3 | 335 | | if (array.Length - arrayIndex < _count) throw new ArgumentException("Destination array is not long enough.", nam |
| | | 336 | | |
| | 1 | 337 | | Array.Copy(_dense, 0, array, arrayIndex, _count); |
| | 1 | 338 | | } |
| | | 339 | | |
| | | 340 | | #endregion |
| | | 341 | | |
| | | 342 | | #region ISet<T> Implementations |
| | | 343 | | |
| | | 344 | | public void ExceptWith(IEnumerable<T> other) |
| | 2 | 345 | | { |
| | 2 | 346 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 347 | | |
| | 2 | 348 | | if (ReferenceEquals(this, other)) |
| | 1 | 349 | | { |
| | 1 | 350 | | Clear(); |
| | 1 | 351 | | return; |
| | | 352 | | } |
| | | 353 | | |
| | 1 | 354 | | var otherSet = new SwiftHashSet<T>(other); |
| | | 355 | | |
| | 9 | 356 | | foreach (var item in otherSet) |
| | 3 | 357 | | Remove(item); |
| | 2 | 358 | | } |
| | | 359 | | |
| | | 360 | | public void IntersectWith(IEnumerable<T> other) |
| | 2 | 361 | | { |
| | 2 | 362 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 363 | | |
| | 2 | 364 | | if (ReferenceEquals(this, other)) |
| | 1 | 365 | | return; |
| | | 366 | | |
| | 1 | 367 | | var otherSet = new SwiftHashSet<T>(other); |
| | | 368 | | |
| | 10 | 369 | | for (int i = _count - 1; i >= 0; i--) |
| | 4 | 370 | | { |
| | 4 | 371 | | var value = _dense[i]; |
| | 4 | 372 | | if (!otherSet.Contains(value)) |
| | 2 | 373 | | Remove(value); |
| | 4 | 374 | | } |
| | 2 | 375 | | } |
| | | 376 | | |
| | | 377 | | public bool IsProperSubsetOf(IEnumerable<T> other) |
| | 2 | 378 | | { |
| | 2 | 379 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 380 | | |
| | 2 | 381 | | var set = new SwiftHashSet<T>(other); |
| | | 382 | | |
| | 2 | 383 | | if (_count >= set.Count) |
| | 1 | 384 | | return false; |
| | | 385 | | |
| | 8 | 386 | | for (int i = 0; i < _count; i++) |
| | 3 | 387 | | if (!set.Contains(_dense[i])) |
| | 0 | 388 | | return false; |
| | | 389 | | |
| | 1 | 390 | | return true; |
| | 2 | 391 | | } |
| | | 392 | | |
| | | 393 | | public bool IsProperSupersetOf(IEnumerable<T> other) |
| | 3 | 394 | | { |
| | 3 | 395 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 396 | | |
| | 3 | 397 | | var set = new SwiftHashSet<T>(other); |
| | | 398 | | |
| | 3 | 399 | | if (_count <= set.Count) |
| | 1 | 400 | | return false; |
| | | 401 | | |
| | 11 | 402 | | foreach (var item in set) |
| | 3 | 403 | | if (!Contains(item)) |
| | 1 | 404 | | return false; |
| | | 405 | | |
| | 1 | 406 | | return true; |
| | 3 | 407 | | } |
| | | 408 | | |
| | | 409 | | public bool IsSubsetOf(IEnumerable<T> other) |
| | 3 | 410 | | { |
| | 3 | 411 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 412 | | |
| | 3 | 413 | | var set = new SwiftHashSet<T>(other); |
| | | 414 | | |
| | 3 | 415 | | if (_count > set.Count) |
| | 1 | 416 | | return false; |
| | | 417 | | |
| | 14 | 418 | | for (int i = 0; i < _count; i++) |
| | 6 | 419 | | if (!set.Contains(_dense[i])) |
| | 1 | 420 | | return false; |
| | | 421 | | |
| | 1 | 422 | | return true; |
| | 3 | 423 | | } |
| | | 424 | | |
| | | 425 | | public bool IsSupersetOf(IEnumerable<T> other) |
| | 2 | 426 | | { |
| | 2 | 427 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 428 | | |
| | 15 | 429 | | foreach (var item in other) |
| | 5 | 430 | | if (!Contains(item)) |
| | 1 | 431 | | return false; |
| | | 432 | | |
| | 1 | 433 | | return true; |
| | 2 | 434 | | } |
| | | 435 | | |
| | | 436 | | public bool Overlaps(IEnumerable<T> other) |
| | 2 | 437 | | { |
| | 2 | 438 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 439 | | |
| | 11 | 440 | | foreach (var item in other) |
| | 3 | 441 | | if (Contains(item)) |
| | 1 | 442 | | return true; |
| | | 443 | | |
| | 1 | 444 | | return false; |
| | 2 | 445 | | } |
| | | 446 | | |
| | | 447 | | public bool SetEquals(IEnumerable<T> other) |
| | 6 | 448 | | { |
| | 6 | 449 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 450 | | |
| | 6 | 451 | | var set = new SwiftHashSet<T>(other); |
| | | 452 | | |
| | 6 | 453 | | if (_count != set.Count) |
| | 1 | 454 | | return false; |
| | | 455 | | |
| | 38 | 456 | | for (int i = 0; i < _count; i++) |
| | 15 | 457 | | if (!set.Contains(_dense[i])) |
| | 1 | 458 | | return false; |
| | | 459 | | |
| | 4 | 460 | | return true; |
| | 6 | 461 | | } |
| | | 462 | | |
| | | 463 | | public void SymmetricExceptWith(IEnumerable<T> other) |
| | 2 | 464 | | { |
| | 2 | 465 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 466 | | |
| | 2 | 467 | | if (ReferenceEquals(this, other)) |
| | 1 | 468 | | { |
| | 1 | 469 | | Clear(); |
| | 1 | 470 | | return; |
| | | 471 | | } |
| | | 472 | | |
| | 1 | 473 | | var set = new SwiftHashSet<T>(other); |
| | | 474 | | |
| | 7 | 475 | | foreach (var item in set) |
| | 2 | 476 | | { |
| | 2 | 477 | | if (!Remove(item)) |
| | 1 | 478 | | Add(item); |
| | 2 | 479 | | } |
| | 2 | 480 | | } |
| | | 481 | | |
| | | 482 | | public void UnionWith(IEnumerable<T> other) |
| | 1 | 483 | | { |
| | 1 | 484 | | SwiftThrowHelper.ThrowIfNull(other, nameof(other)); |
| | | 485 | | |
| | 7 | 486 | | foreach (var item in other) |
| | 2 | 487 | | Add(item); |
| | 1 | 488 | | } |
| | | 489 | | |
| | | 490 | | #endregion |
| | | 491 | | } |