| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | |
| | | 4 | | namespace SwiftCollections.Pool; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A specialized pool for managing <see cref="SwiftPackedSet{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 packed set.</typeparam> |
| | | 11 | | public sealed class SwiftPackedSetPool<T> : SwiftCollectionPool<SwiftPackedSet<T>, T>, IDisposable |
| | | 12 | | { |
| | | 13 | | #region Singleton Instance |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Shared instance of the packed 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<SwiftPackedSetPool<T>> _lazyInstance = |
| | 2 | 20 | | new(() => new SwiftPackedSetPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the shared instance of the pool. |
| | | 24 | | /// </summary> |
| | 2 | 25 | | public static SwiftPackedSetPool<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 packed set instance from the pool. If the pool is empty, a new packed set is created. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <returns>A <see cref="SwiftPackedSet{T}"/> instance.</returns> |
| | | 44 | | public override SwiftPackedSet<T> Rent() |
| | 7 | 45 | | { |
| | 7 | 46 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftPackedSetPool<T>)); |
| | | 47 | | |
| | 6 | 48 | | return base.Rent(); |
| | 6 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Releases a packed set instance back to the pool for reuse. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="set">The packed set to release.</param> |
| | | 55 | | public override void Release(SwiftPackedSet<T> set) |
| | 3 | 56 | | { |
| | 3 | 57 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftPackedSetPool<T>)); |
| | | 58 | | |
| | 3 | 59 | | if (set == null) return; |
| | | 60 | | |
| | 3 | 61 | | base.Release(set); |
| | 3 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Clears all pooled packed sets from the pool. |
| | | 66 | | /// </summary> |
| | | 67 | | public override void Clear() |
| | 5 | 68 | | { |
| | 5 | 69 | | if (_disposed) return; |
| | | 70 | | |
| | 5 | 71 | | base.Clear(); |
| | 5 | 72 | | } |
| | | 73 | | |
| | | 74 | | #endregion |
| | | 75 | | |
| | | 76 | | #region IDisposable Implementation |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Releases all resources used by the SwiftPackedSetPool. |
| | | 80 | | /// </summary> |
| | | 81 | | public void Dispose() |
| | 5 | 82 | | { |
| | 5 | 83 | | if (_disposed) |
| | 1 | 84 | | return; |
| | | 85 | | |
| | 4 | 86 | | Clear(); |
| | 4 | 87 | | base.Flush(); |
| | | 88 | | |
| | 4 | 89 | | _disposed = true; |
| | | 90 | | |
| | 4 | 91 | | GC.SuppressFinalize(this); |
| | 5 | 92 | | } |
| | | 93 | | |
| | 3 | 94 | | ~SwiftPackedSetPool() => Dispose(); |
| | | 95 | | |
| | | 96 | | #endregion |
| | | 97 | | } |