< 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: 120
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
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(...)100%22100%
Clear()100%22100%
Dispose()100%22100%
Finalize()100%11100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// SwiftHashSetPool.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024–present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using SwiftCollections.Diagnostics;
 9using SwiftCollections.Lazy;
 10using System;
 11using System.Threading;
 12
 13namespace SwiftCollections.Pool;
 14
 15/// <summary>
 16/// A specialized pool for managing <see cref="SwiftHashSet{T}"/> instances,
 17/// providing efficient reuse to minimize memory allocations and improve performance.
 18/// </summary>
 19/// <typeparam name="T">The type of elements in the hash set.</typeparam>
 20public sealed class SwiftHashSetPool<T> : SwiftCollectionPool<SwiftHashSet<T>, T>, IDisposable
 21    where T : notnull
 22{
 23    #region Singleton Instance
 24
 25    /// <summary>
 26    /// Shared instance of the hash set pool, providing a globally accessible pool.
 27    /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal.
 28    /// </summary>
 129    private readonly static SwiftLazyDisposable<SwiftHashSetPool<T>> _lazyInstance =
 130        new(() => new SwiftHashSetPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 31
 32    /// <summary>
 33    /// Gets the shared instance of the pool.
 34    /// </summary>
 235    public static SwiftHashSetPool<T> Shared => _lazyInstance.Value;
 36
 37    #endregion
 38
 39    #region Fields
 40
 41    /// <summary>
 42    /// Tracks whether the pool has been disposed.
 43    /// </summary>
 44    private volatile bool _disposed;
 45
 46    #endregion
 47
 48    #region Methods
 49
 50    /// <summary>
 51    /// Rents a hash set instance from the pool. If the pool is empty, a new hash set is created.
 52    /// </summary>
 53    /// <returns>A <see cref="SwiftHashSet{T}"/> instance.</returns>
 54    public override SwiftHashSet<T> Rent()
 55    {
 756        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftHashSetPool<T>));
 57
 658        return base.Rent();
 59    }
 60
 61    /// <summary>
 62    /// Releases a hash set instance back to the pool for reuse.
 63    /// </summary>
 64    /// <param name="set">The hash set to release.</param>
 65    /// <remarks>
 66    /// The hash set will be cleared before being returned to the pool to ensure it contains no stale data.
 67    /// </remarks>
 68    public override void Release(SwiftHashSet<T> set)
 69    {
 570        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftHashSetPool<T>));
 71
 572        if (set == null) return;
 73
 74        // SwiftCollectionPool clears pool before releasing
 375        base.Release(set);
 376    }
 77
 78    /// <summary>
 79    /// Clears all pooled hash sets from the pool.
 80    /// </summary>
 81    public override void Clear()
 82    {
 883        if (_disposed) return;
 84
 685        base.Clear();
 686    }
 87
 88    #endregion
 89
 90    #region IDisposable Implementation
 91
 92    /// <summary>
 93    /// Releases all resources used by the SwiftHashSetPool.
 94    /// It is important to call Dispose() to release pooled objects, preventing potential memory leaks.
 95    /// </summary>
 96    public void Dispose()
 97    {
 698        if (_disposed)
 199            return;
 100
 5101        Clear();
 5102        base.Flush();
 103
 5104        _disposed = true;
 105
 106        // Suppress finalization to prevent unnecessary GC overhead
 5107        GC.SuppressFinalize(this);
 5108    }
 109
 110    /// <summary>
 111    /// Releases the resources used by the SwiftHashSetPool instance.
 112    /// </summary>
 113    /// <remarks>
 114    /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly.
 115    /// It is recommended to call Dispose to release resources deterministically.
 116    /// </remarks>
 3117    ~SwiftHashSetPool() => Dispose();
 118
 119    #endregion
 120}