/// A double-ended queue (deque), which provides O(1) indexed access, O(1) removals from the front and back, amortized O(1) insertions to the front and back, and O(N) insertions and removals anywhere else (with the operations getting slower as the index approaches the middle).
/// </summary>
/// <typeparam name="T">The type of elements contained in the deque.</typeparam>
/// Determines whether this list contains a specific value.
/// </summary>
/// <param name="item">The object to locate in this list.</param>
/// <returns>
/// true if <paramref name="item"/> is found in this list; otherwise, false.
/// </returns>
boolICollection<T>.Contains(Titem)
{
returnthis.Contains(item,null);
}
/// <summary>
/// Copies the elements of this list to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
/// </summary>
/// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from this slice. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
/// <paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.
/// -or-
/// The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.
/// </exception>
voidICollection<T>.CopyTo(T[]array,intarrayIndex)
{
if(array==null)
thrownewArgumentNullException("array","Array is null");
/// Removes the first occurrence of a specific object from this list.
/// </summary>
/// <param name="item">The object to remove from this list.</param>
/// <returns>
/// true if <paramref name="item"/> was successfully removed from this list; otherwise, false. This method also returns false if <paramref name="item"/> is not found in this list.
thrownewArgumentOutOfRangeException("index","Invalid existing index "+index+" for source length "+sourceLength);
}
}
/// <summary>
/// Checks the <paramref name="offset"/> and <paramref name="count"/> arguments for validity when applied to a source of a given length. Allows 0-element ranges, including a 0-element range at the end of the source.
/// </summary>
/// <param name="sourceLength">The length of the source. This parameter is not checked for validity.</param>
/// <param name="offset">The index into source at which the range begins.</param>
/// <param name="count">The number of elements in the range.</param>
/// <exception cref="ArgumentOutOfRangeException">Either <paramref name="offset"/> or <paramref name="count"/> is less than 0.</exception>
/// <exception cref="ArgumentException">The range [offset, offset + count) is not within the range [0, sourceLength).</exception>
/// Gets a value indicating whether this instance is empty.
/// </summary>
privateboolIsEmpty
{
get{returnCount==0;}
}
/// <summary>
/// Gets a value indicating whether this instance is at full capacity.
/// </summary>
privateboolIsFull
{
get{returnCount==Capacity;}
}
/// <summary>
/// Gets a value indicating whether the buffer is "split" (meaning the beginning of the view is at a later index in <see cref="buffer"/> than the end).
/// </summary>
privateboolIsSplit
{
get
{
// Overflow-safe version of "(offset + Count) > Capacity"
returnoffset>(Capacity-Count);
}
}
/// <summary>
/// Gets or sets the capacity for this deque. This value must always be greater than zero, and this property cannot be set to a value less than <see cref="Count"/>.
/// </summary>
/// <exception cref="InvalidOperationException"><c>Capacity</c> cannot be set to a value less than <see cref="Count"/>.</exception>
publicintCapacity
{
get
{
returnbuffer.Length;
}
set
{
if(value<1)
thrownewArgumentOutOfRangeException("value","Capacity must be greater than 0.");
if(value<Count)
thrownewInvalidOperationException("Capacity cannot be set to a value less than Count");
if(value==buffer.Length)
return;
// Create the new buffer and copy our existing range.
T[]newBuffer=newT[value];
if(IsSplit)
{
// The existing buffer is split, so we have to copy it in parts
/// Gets the number of elements contained in this deque.
/// </summary>
/// <returns>The number of elements contained in this deque.</returns>
publicintCount{get;privateset;}
/// <summary>
/// Applies the offset to <paramref name="index"/>, resulting in a buffer index.
/// </summary>
/// <param name="index">The deque index.</param>
/// <returns>The buffer index.</returns>
privateintDequeIndexToBufferIndex(intindex)
{
return(index+offset)%Capacity;
}
/// <summary>
/// Gets an element at the specified view index.
/// </summary>
/// <param name="index">The zero-based view index of the element to get. This index is guaranteed to be valid.</param>
/// <returns>The element at the specified index.</returns>
privateTDoGetItem(intindex)
{
returnbuffer[DequeIndexToBufferIndex(index)];
}
/// <summary>
/// Sets an element at the specified view index.
/// </summary>
/// <param name="index">The zero-based view index of the element to get. This index is guaranteed to be valid.</param>
/// <param name="item">The element to store in the list.</param>
privatevoidDoSetItem(intindex,Titem)
{
buffer[DequeIndexToBufferIndex(index)]=item;
}
/// <summary>
/// Inserts an element at the specified view index.
/// </summary>
/// <param name="index">The zero-based view index at which the element should be inserted. This index is guaranteed to be valid.</param>
/// <param name="item">The element to store in the list.</param>
privatevoidDoInsert(intindex,Titem)
{
EnsureCapacityForOneElement();
if(index==0)
{
DoAddToFront(item);
return;
}
elseif(index==Count)
{
DoAddToBack(item);
return;
}
DoInsertRange(index,new[]{item},1);
}
/// <summary>
/// Removes an element at the specified view index.
/// </summary>
/// <param name="index">The zero-based view index of the element to remove. This index is guaranteed to be valid.</param>
privatevoidDoRemoveAt(intindex)
{
if(index==0)
{
DoRemoveFromFront();
return;
}
elseif(index==Count-1)
{
DoRemoveFromBack();
return;
}
DoRemoveRange(index,1);
}
/// <summary>
/// Increments <see cref="offset"/> by <paramref name="value"/> using modulo-<see cref="Capacity"/> arithmetic.
/// </summary>
/// <param name="value">The value by which to increase <see cref="offset"/>. May not be negative.</param>
/// <returns>The value of <see cref="offset"/> after it was incremented.</returns>
privateintPostIncrement(intvalue)
{
intret=offset;
offset+=value;
offset%=Capacity;
returnret;
}
/// <summary>
/// Decrements <see cref="offset"/> by <paramref name="value"/> using modulo-<see cref="Capacity"/> arithmetic.
/// </summary>
/// <param name="value">The value by which to reduce <see cref="offset"/>. May not be negative or greater than <see cref="Capacity"/>.</param>
/// <returns>The value of <see cref="offset"/> before it was decremented.</returns>
privateintPreDecrement(intvalue)
{
offset-=value;
if(offset<0)
offset+=Capacity;
returnoffset;
}
/// <summary>
/// Inserts a single element to the back of the view. <see cref="IsFull"/> must be false when this method is called.
/// </summary>
/// <param name="value">The element to insert.</param>
privatevoidDoAddToBack(Tvalue)
{
buffer[DequeIndexToBufferIndex(Count)]=value;
++Count;
}
/// <summary>
/// Inserts a single element to the front of the view. <see cref="IsFull"/> must be false when this method is called.
/// </summary>
/// <param name="value">The element to insert.</param>
privatevoidDoAddToFront(Tvalue)
{
buffer[PreDecrement(1)]=value;
++Count;
}
/// <summary>
/// Removes and returns the last element in the view. <see cref="IsEmpty"/> must be false when this method is called.
/// </summary>
/// <returns>The former last element.</returns>
privateTDoRemoveFromBack()
{
Tret=buffer[DequeIndexToBufferIndex(Count-1)];
--Count;
returnret;
}
/// <summary>
/// Removes and returns the first element in the view. <see cref="IsEmpty"/> must be false when this method is called.
/// </summary>
/// <returns>The former first element.</returns>
privateTDoRemoveFromFront()
{
--Count;
returnbuffer[PostIncrement(1)];
}
/// <summary>
/// Inserts a range of elements into the view.
/// </summary>
/// <param name="index">The index into the view at which the elements are to be inserted.</param>
/// <param name="collection">The elements to insert.</param>
/// <param name="collectionCount">The number of elements in <paramref name="collection"/>. Must be greater than zero, and the sum of <paramref name="collectionCount"/> and <see cref="Count"/> must be less than or equal to <see cref="Capacity"/>.</param>
/// <param name="index">The index into the view at which the range begins.</param>
/// <param name="collectionCount">The number of elements in the range. This must be greater than 0 and less than or equal to <see cref="Count"/>.</param>
/// <param name="offset">The index into the deque at which the range begins.</param>
/// <param name="count">The number of elements to remove.</param>
/// <exception cref="ArgumentOutOfRangeException">Either <paramref name="offset"/> or <paramref name="count"/> is less than 0.</exception>
/// <exception cref="ArgumentException">The range [<paramref name="offset"/>, <paramref name="offset"/> + <paramref name="count"/>) is not within the range [0, <see cref="Count"/>).</exception>
publicvoidRemoveRange(intoffset,intcount)
{
CheckRangeArguments(Count,offset,count);
if(count==0)
{
return;
}
this.DoRemoveRange(offset,count);
}
/// <summary>
/// Removes and returns the last element of this deque.
/// </summary>
/// <returns>The former last element.</returns>
/// <exception cref="InvalidOperationException">The deque is empty.</exception>
publicTRemoveFromBack()
{
if(this.IsEmpty)
thrownewInvalidOperationException("The deque is empty.");
returnthis.DoRemoveFromBack();
}
/// <summary>
/// Removes and returns the first element of this deque.
/// </summary>
/// <returns>The former first element.</returns>
/// <exception cref="InvalidOperationException">The deque is empty.</exception>
publicTRemoveFromFront()
{
if(this.IsEmpty)
thrownewInvalidOperationException("The deque is empty.");