/// <summary> /// Creates a new instance of <see cref="PooledIntegerSet"/>. /// </summary> /// <param name="pool">A <see cref="DynamicArrayPool{Int32}"/> instance from which the memory /// for this set should be allocated.</param> /// <param name="capacity">The initial capacity to allocate for this set.</param> public PooledIntegerSet(DynamicArrayPool <int> pool, int capacity = 0) { m_pool = pool; m_token = pool.allocate((capacity > 0) ? DataStructureUtil.nextPowerOf2(capacity - 1) : 4); m_pool.getSpan(m_token).Fill(EMPTY_SLOT); m_count = 0; }
/// <summary> /// Releases any memory being used by the set back to its array pool. Once this method /// is called the set can no longer be used. /// </summary> /// <remarks> /// This method has no effect when called on the default value of <see cref="PooledIntegerSet"/>. /// </remarks> public void free() { if (m_pool != null) { m_pool.free(m_token); m_token = default; } }
/// <summary> /// Returns a span that can be used to access an array allocated with the /// <see cref="allocate(Int32)"/> method. /// </summary> /// /// <param name="token">A <see cref="DynamicArrayPoolToken{T}"/> that was obtained from /// a call to <see cref="allocate(Int32)"/>. If this is the default value of /// <see cref="DynamicArrayPoolToken{T}"/>, an empty span is returned.</param> /// <returns>A <see cref="Span{T}"/> that provides access to the allocated memory.</returns> /// /// <remarks> /// The returned span is guaranteed to refer to the memory associated with the token /// <paramref name="token"/> as long as the <see cref="allocate(Int32)"/>, /// <see cref="resize(DynamicArrayPoolToken{T}, Int32)"/> and <see cref="append(DynamicArrayPoolToken{T}, T)"/> /// methods are not called on this <see cref="DynamicArrayPool{T}"/> instance. These methods may move /// the data associated with the token to new location in memory, and this method must then be called /// again to get the span for the new backing memory. /// </remarks> /// <remarks> /// If the token was obtained from a different <see cref="DynamicArrayPoolToken{T}"/> /// instance, or if the memory backed by the token was released by calling <see cref="free"/> /// or <see cref="clear"/>, the returned span may refer to the memory of some other allocated /// array in the pool or an exception may be thrown. /// </remarks> public Span <T> getSpan(DynamicArrayPoolToken <T> token) { int id = token.m_id; if (id == 0) { return(Span <T> .Empty); } ref Segment segment = ref m_segments[id - 1];