< 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: 114
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/SwiftPackedSetPool.cs

#LineLine coverage
 1//=======================================================================
 2// SwiftPackedSetPool.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="SwiftPackedSet{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 packed set.</typeparam>
 20public sealed class SwiftPackedSetPool<T> : SwiftCollectionPool<SwiftPackedSet<T>, T>, IDisposable
 21    where T : notnull
 22{
 23    #region Singleton Instance
 24
 25    /// <summary>
 26    /// Shared instance of the packed 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<SwiftPackedSetPool<T>> _lazyInstance =
 130        new(() => new SwiftPackedSetPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 31
 32    /// <summary>
 33    /// Gets the shared instance of the pool.
 34    /// </summary>
 235    public static SwiftPackedSetPool<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 packed set instance from the pool. If the pool is empty, a new packed set is created.
 52    /// </summary>
 53    /// <returns>A <see cref="SwiftPackedSet{T}"/> instance.</returns>
 54    public override SwiftPackedSet<T> Rent()
 55    {
 756        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftPackedSetPool<T>));
 57
 658        return base.Rent();
 59    }
 60
 61    /// <summary>
 62    /// Releases a packed set instance back to the pool for reuse.
 63    /// </summary>
 64    /// <param name="set">The packed set to release.</param>
 65    public override void Release(SwiftPackedSet<T> set)
 66    {
 567        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftPackedSetPool<T>));
 68
 569        if (set == null) return;
 70
 371        base.Release(set);
 372    }
 73
 74    /// <summary>
 75    /// Clears all pooled packed sets from the pool.
 76    /// </summary>
 77    public override void Clear()
 78    {
 879        if (_disposed) return;
 80
 681        base.Clear();
 682    }
 83
 84    #endregion
 85
 86    #region IDisposable Implementation
 87
 88    /// <summary>
 89    /// Releases all resources used by the SwiftPackedSetPool.
 90    /// </summary>
 91    public void Dispose()
 92    {
 693        if (_disposed)
 194            return;
 95
 596        Clear();
 597        base.Flush();
 98
 599        _disposed = true;
 100
 5101        GC.SuppressFinalize(this);
 5102    }
 103
 104    /// <summary>
 105    /// Releases the resources used by the SwiftPackedSetPool instance.
 106    /// </summary>
 107    /// <remarks>
 108    /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly.
 109    /// It is recommended to call Dispose to release resources deterministically.
 110    /// </remarks>
 3111    ~SwiftPackedSetPool() => Dispose();
 112
 113    #endregion
 114}