< Summary

Information
Class: SwiftCollections.Pool.SwiftPackedSetPool<T>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftPackedSetPool.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 105
Line coverage: 100%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_Shared()100%11100%
Rent()100%11100%
Release(...)50%22100%
Clear()50%22100%
Dispose()100%22100%
Finalize()100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftPackedSetPool.cs

#LineLine coverage
 1using System;
 2using System.Threading;
 3
 4namespace 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>
 11public sealed class SwiftPackedSetPool<T> : SwiftCollectionPool<SwiftPackedSet<T>, T>, IDisposable
 12    where T : notnull
 13{
 14    #region Singleton Instance
 15
 16    /// <summary>
 17    /// Shared instance of the packed set pool, providing a globally accessible pool.
 18    /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal.
 19    /// </summary>
 120    private readonly static SwiftLazyDisposable<SwiftPackedSetPool<T>> _lazyInstance =
 221        new(() => new SwiftPackedSetPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 22
 23    /// <summary>
 24    /// Gets the shared instance of the pool.
 25    /// </summary>
 226    public static SwiftPackedSetPool<T> Shared => _lazyInstance.Value;
 27
 28    #endregion
 29
 30    #region Fields
 31
 32    /// <summary>
 33    /// Tracks whether the pool has been disposed.
 34    /// </summary>
 35    private volatile bool _disposed;
 36
 37    #endregion
 38
 39    #region Methods
 40
 41    /// <summary>
 42    /// Rents a packed set instance from the pool. If the pool is empty, a new packed set is created.
 43    /// </summary>
 44    /// <returns>A <see cref="SwiftPackedSet{T}"/> instance.</returns>
 45    public override SwiftPackedSet<T> Rent()
 46    {
 747        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftPackedSetPool<T>));
 48
 649        return base.Rent();
 50    }
 51
 52    /// <summary>
 53    /// Releases a packed set instance back to the pool for reuse.
 54    /// </summary>
 55    /// <param name="set">The packed set to release.</param>
 56    public override void Release(SwiftPackedSet<T> set)
 57    {
 358        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftPackedSetPool<T>));
 59
 360        if (set == null) return;
 61
 362        base.Release(set);
 363    }
 64
 65    /// <summary>
 66    /// Clears all pooled packed sets from the pool.
 67    /// </summary>
 68    public override void Clear()
 69    {
 570        if (_disposed) return;
 71
 572        base.Clear();
 573    }
 74
 75    #endregion
 76
 77    #region IDisposable Implementation
 78
 79    /// <summary>
 80    /// Releases all resources used by the SwiftPackedSetPool.
 81    /// </summary>
 82    public void Dispose()
 83    {
 584        if (_disposed)
 185            return;
 86
 487        Clear();
 488        base.Flush();
 89
 490        _disposed = true;
 91
 492        GC.SuppressFinalize(this);
 493    }
 94
 95    /// <summary>
 96    /// Releases the resources used by the SwiftPackedSetPool instance.
 97    /// </summary>
 98    /// <remarks>
 99    /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly.
 100    /// It is recommended to call Dispose to release resources deterministically.
 101    /// </remarks>
 3102    ~SwiftPackedSetPool() => Dispose();
 103
 104    #endregion
 105}