Beispiel #1
0
        /// <summary>
        ///     Get a free element from the <see cref="Pool{T}" />.
        /// </summary>
        /// <param name="killExistingObjectIfFull">
        ///     <c>true</c> to forcibly kill an existing, in use, element in the <see cref="Pool{T}" /> if <see cref="Count" /> is
        ///     equal to <see cref="Capacity" />; otherwise, <c>false</c>.
        /// </param>
        /// <returns>A free <see cref="T" /> element from the <see cref="Pool{T}" />.</returns>
        /// <remarks>
        ///     <para>This method is an O(1) operation.</para>
        /// </remarks>
        public T Request(bool killExistingObjectIfFull = false)
        {
            while (true)
            {
                T poolable;

                if (_freeItems.RemoveFromFront(out poolable))
                {
                    poolable.Initialize(Return);
                    _usedItems.AddToBack(poolable);
                    return(poolable);
                }

                if (!killExistingObjectIfFull)
                {
                    return(null);
                }
                if (!_usedItems.GetFront(out poolable))
                {
                    return(null);
                }

                poolable.Return();
                killExistingObjectIfFull = false;
            }
        }
Beispiel #2
0
 /// <summary>
 ///     Returns all in use elements back to the <see cref="Pool{T}" />.
 /// </summary>
 /// <remarks>
 ///     <para><see cref="Count" /> is set to 0.</para>
 ///     <para><see cref="Capacity" /> remains unchanged.</para>
 ///     <para>
 ///         The order which elements are returned is the same as the order they were requested using
 ///         <see cref="Request" />.
 ///     </para>
 ///     <para>This method is an O(n) operation where n is <see cref="Count" />.</para>
 /// </remarks>
 public void Clear()
 {
     while (_usedItems.Count > 0)
     {
         T item;
         _usedItems.GetFront(out item);
         item.Return();
     }
 }
 public void Deque_Get_Front(int count)
 {
     var elements = new TestDequeElement[count];
     for (var i = 0; i < count; i++)
     {
         elements[i] = new TestDequeElement
         {
             Value = i
         };
     }
     var deque = new Deque<TestDequeElement>(elements);
     var indices = Enumerable.Range(0, count);
     foreach (var index in indices)
     {
         TestDequeElement element;
         deque.GetFront(out element);
         deque.RemoveFromFront();
         Assert.IsTrue(element.Value == index);
     }
 }