| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | |
| | | 4 | | namespace SwiftCollections.Pool; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A specialized pool for managing <see cref="SwiftHashSet{T}"/> instances, |
| | | 8 | | /// providing efficient reuse to minimize memory allocations and improve performance. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <typeparam name="T">The type of elements in the hash set.</typeparam> |
| | | 11 | | public sealed class SwiftHashSetPool<T> : SwiftCollectionPool<SwiftHashSet<T>, T>, IDisposable |
| | | 12 | | { |
| | | 13 | | #region Singleton Instance |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Shared instance of the hash set pool, providing a globally accessible pool. |
| | | 17 | | /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal. |
| | | 18 | | /// </summary> |
| | 1 | 19 | | private readonly static SwiftLazyDisposable<SwiftHashSetPool<T>> _lazyInstance = |
| | 2 | 20 | | new(() => new SwiftHashSetPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the shared instance of the pool. |
| | | 24 | | /// </summary> |
| | 2 | 25 | | public static SwiftHashSetPool<T> Shared => _lazyInstance.Value; |
| | | 26 | | |
| | | 27 | | #endregion |
| | | 28 | | |
| | | 29 | | #region Fields |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Tracks whether the pool has been disposed. |
| | | 33 | | /// </summary> |
| | | 34 | | private volatile bool _disposed; |
| | | 35 | | |
| | | 36 | | #endregion |
| | | 37 | | |
| | | 38 | | #region Methods |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Rents a hash set instance from the pool. If the pool is empty, a new hash set is created. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <returns>A <see cref="SwiftHashSet{T}"/> instance.</returns> |
| | | 44 | | public override SwiftHashSet<T> Rent() |
| | 7 | 45 | | { |
| | 7 | 46 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftHashSetPool<T>)); |
| | | 47 | | |
| | 6 | 48 | | return base.Rent(); |
| | 6 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Releases a hash set instance back to the pool for reuse. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="set">The hash set to release.</param> |
| | | 55 | | /// <remarks> |
| | | 56 | | /// The hash set will be cleared before being returned to the pool to ensure it contains no stale data. |
| | | 57 | | /// </remarks> |
| | | 58 | | public override void Release(SwiftHashSet<T> set) |
| | 3 | 59 | | { |
| | 3 | 60 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftHashSetPool<T>)); |
| | | 61 | | |
| | 3 | 62 | | if (set == null) return; |
| | | 63 | | |
| | | 64 | | // SwiftCollectionPool clears pool before releasing |
| | 3 | 65 | | base.Release(set); |
| | 3 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Clears all pooled hash sets from the pool. |
| | | 70 | | /// </summary> |
| | | 71 | | public override void Clear() |
| | 5 | 72 | | { |
| | 5 | 73 | | if (_disposed) return; |
| | | 74 | | |
| | 5 | 75 | | base.Clear(); |
| | 5 | 76 | | } |
| | | 77 | | |
| | | 78 | | #endregion |
| | | 79 | | |
| | | 80 | | #region IDisposable Implementation |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Releases all resources used by the SwiftHashSetPool. |
| | | 84 | | /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks. |
| | | 85 | | /// </summary> |
| | | 86 | | public void Dispose() |
| | 5 | 87 | | { |
| | 5 | 88 | | if (_disposed) |
| | 1 | 89 | | return; |
| | | 90 | | |
| | 4 | 91 | | Clear(); |
| | 4 | 92 | | base.Flush(); |
| | | 93 | | |
| | 4 | 94 | | _disposed = true; |
| | | 95 | | |
| | | 96 | | // Suppress finalization to prevent unnecessary GC overhead |
| | 4 | 97 | | GC.SuppressFinalize(this); |
| | 5 | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | ~SwiftHashSetPool() => Dispose(); |
| | | 101 | | |
| | | 102 | | #endregion |
| | | 103 | | } |