< Summary

Information
Class: SwiftCollections.Pool.SwiftHashSetPool<T>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftHashSetPool.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 111
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/SwiftHashSetPool.cs

#LineLine coverage
 1using System;
 2using System.Threading;
 3
 4namespace 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>
 11public sealed class SwiftHashSetPool<T> : SwiftCollectionPool<SwiftHashSet<T>, T>, IDisposable
 12    where T : notnull
 13{
 14    #region Singleton Instance
 15
 16    /// <summary>
 17    /// Shared instance of the hash 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<SwiftHashSetPool<T>> _lazyInstance =
 221        new(() => new SwiftHashSetPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 22
 23    /// <summary>
 24    /// Gets the shared instance of the pool.
 25    /// </summary>
 226    public static SwiftHashSetPool<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 hash set instance from the pool. If the pool is empty, a new hash set is created.
 43    /// </summary>
 44    /// <returns>A <see cref="SwiftHashSet{T}"/> instance.</returns>
 45    public override SwiftHashSet<T> Rent()
 46    {
 747        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftHashSetPool<T>));
 48
 649        return base.Rent();
 50    }
 51
 52    /// <summary>
 53    /// Releases a hash set instance back to the pool for reuse.
 54    /// </summary>
 55    /// <param name="set">The hash set to release.</param>
 56    /// <remarks>
 57    /// The hash set will be cleared before being returned to the pool to ensure it contains no stale data.
 58    /// </remarks>
 59    public override void Release(SwiftHashSet<T> set)
 60    {
 361        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftHashSetPool<T>));
 62
 363        if (set == null) return;
 64
 65        // SwiftCollectionPool clears pool before releasing
 366        base.Release(set);
 367    }
 68
 69    /// <summary>
 70    /// Clears all pooled hash sets from the pool.
 71    /// </summary>
 72    public override void Clear()
 73    {
 574        if (_disposed) return;
 75
 576        base.Clear();
 577    }
 78
 79    #endregion
 80
 81    #region IDisposable Implementation
 82
 83    /// <summary>
 84    /// Releases all resources used by the SwiftHashSetPool.
 85    /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks.
 86    /// </summary>
 87    public void Dispose()
 88    {
 589        if (_disposed)
 190            return;
 91
 492        Clear();
 493        base.Flush();
 94
 495        _disposed = true;
 96
 97        // Suppress finalization to prevent unnecessary GC overhead
 498        GC.SuppressFinalize(this);
 499    }
 100
 101    /// <summary>
 102    /// Releases the resources used by the SwiftHashSetPool instance.
 103    /// </summary>
 104    /// <remarks>
 105    /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly.
 106    /// It is recommended to call Dispose to release resources deterministically.
 107    /// </remarks>
 3108    ~SwiftHashSetPool() => Dispose();
 109
 110    #endregion
 111}