2009-07-14 21:32:44 +00:00
|
|
|
|
#region CPL License
|
|
|
|
|
/*
|
|
|
|
|
Nuclex Framework
|
2013-04-25 13:11:59 +00:00
|
|
|
|
Copyright (C) 2002-2013 Nuclex Development Labs
|
2009-07-14 21:32:44 +00:00
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
|
License along with this library
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
2009-07-10 18:42:23 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections;
|
2012-03-17 13:03:40 +00:00
|
|
|
|
using System.Reflection;
|
2009-07-10 18:42:23 +00:00
|
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Collections {
|
|
|
|
|
|
|
|
|
|
/// <summary>A double-ended queue that allocates memory in blocks</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
/// <typeparam name="TItem">Type of the items being stored in the queue</typeparam>
|
2009-07-10 18:42:23 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The double-ended queue allows items to be appended to either side of the queue
|
|
|
|
|
/// without a hefty toll on performance. Like its namesake in C++, it is implemented
|
|
|
|
|
/// using multiple arrays.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Therefore, it's not only good at coping with lists that are modified at their
|
|
|
|
|
/// beginning, but also at handling huge data sets since enlarging the deque doesn't
|
2011-08-14 09:33:10 +00:00
|
|
|
|
/// require items to be copied around and it still can be accessed by index.
|
2009-07-10 18:42:23 +00:00
|
|
|
|
/// </para>
|
|
|
|
|
/// </remarks>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public partial class Deque<TItem> : IList<TItem>, IList {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
|
|
|
|
|
#region class Enumerator
|
|
|
|
|
|
|
|
|
|
/// <summary>Enumerates over the items in a deque</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
private class Enumerator : IEnumerator<TItem>, IEnumerator {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new deque enumerator</summary>
|
|
|
|
|
/// <param name="deque">Deque whose items will be enumerated</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public Enumerator(Deque<TItem> deque) {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
this.deque = deque;
|
|
|
|
|
this.blockSize = this.deque.blockSize;
|
|
|
|
|
this.lastBlock = this.deque.blocks.Count - 1;
|
2009-07-14 21:08:10 +00:00
|
|
|
|
this.lastBlockEndIndex = this.deque.lastBlockEndIndex - 1;
|
2009-07-14 20:15:34 +00:00
|
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Immediately releases all resources owned by the instance</summary>
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
this.deque = null;
|
|
|
|
|
this.currentBlock = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>The item at the enumerator's current position</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public TItem Current {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
get {
|
2009-07-14 21:52:40 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
checkVersion();
|
|
|
|
|
#endif
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (this.currentBlock == null) {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
throw new InvalidOperationException("Enumerator is not on a valid position");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.currentBlock[this.subIndex];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Advances the enumerator to the next item</summary>
|
|
|
|
|
/// <returns>True if there was a next item</returns>
|
|
|
|
|
public bool MoveNext() {
|
|
|
|
|
|
2009-07-14 21:52:40 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
checkVersion();
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-07-14 20:15:34 +00:00
|
|
|
|
// If we haven't reached the last block yet
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (this.currentBlockIndex < this.lastBlock) {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
|
|
|
|
|
// Advance to the next item. If the end of the current block is reached,
|
|
|
|
|
// go to the next block's first item
|
|
|
|
|
++this.subIndex;
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (this.subIndex >= this.blockSize) {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
++this.currentBlockIndex;
|
|
|
|
|
this.currentBlock = this.deque.blocks[this.currentBlockIndex];
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (this.currentBlockIndex == 0) {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
this.subIndex = this.deque.firstBlockStartIndex;
|
|
|
|
|
} else {
|
|
|
|
|
this.subIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Item found. If the current block wasn't the last block, an item *has*
|
|
|
|
|
// to follow since otherwise, no further blocks would exist!
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
} else { // We in or beyond the last block
|
|
|
|
|
|
|
|
|
|
// Are there any items left to advance to?
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (this.subIndex < this.lastBlockEndIndex) {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
++this.subIndex;
|
|
|
|
|
return true;
|
|
|
|
|
} else { // Nope, we've reached the end of the deque
|
|
|
|
|
this.currentBlock = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Resets the enumerator to its initial position</summary>
|
|
|
|
|
public void Reset() {
|
|
|
|
|
this.currentBlock = null;
|
|
|
|
|
this.currentBlockIndex = -1;
|
|
|
|
|
this.subIndex = this.deque.blockSize - 1;
|
2009-07-21 20:42:07 +00:00
|
|
|
|
#if DEBUG
|
2009-07-14 21:52:40 +00:00
|
|
|
|
this.expectedVersion = this.deque.version;
|
2009-07-21 20:42:07 +00:00
|
|
|
|
#endif
|
2009-07-14 20:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>The item at the enumerator's current position</summary>
|
|
|
|
|
object IEnumerator.Current {
|
|
|
|
|
get { return Current; }
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-14 21:52:40 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
/// <summary>Ensures that the deque has not changed</summary>
|
|
|
|
|
private void checkVersion() {
|
|
|
|
|
if(this.expectedVersion != this.deque.version)
|
|
|
|
|
throw new InvalidOperationException("Deque has been modified");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-07-14 20:15:34 +00:00
|
|
|
|
/// <summary>Deque the enumerator belongs to</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
private Deque<TItem> deque;
|
2009-07-14 20:15:34 +00:00
|
|
|
|
/// <summary>Size of the blocks in the deque</summary>
|
|
|
|
|
private int blockSize;
|
|
|
|
|
/// <summary>Index of the last block in the deque</summary>
|
|
|
|
|
private int lastBlock;
|
|
|
|
|
/// <summary>End index of the items in the deque's last block</summary>
|
|
|
|
|
private int lastBlockEndIndex;
|
|
|
|
|
|
|
|
|
|
/// <summary>Index of the block the enumerator currently is in</summary>
|
|
|
|
|
private int currentBlockIndex;
|
|
|
|
|
/// <summary>Reference to the block being enumerated</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
private TItem[] currentBlock;
|
2009-07-14 20:15:34 +00:00
|
|
|
|
/// <summary>Index in the current block</summary>
|
|
|
|
|
private int subIndex;
|
2009-07-21 20:42:07 +00:00
|
|
|
|
#if DEBUG
|
2009-07-14 21:52:40 +00:00
|
|
|
|
/// <summary>Version the deque is expected to have</summary>
|
|
|
|
|
private int expectedVersion;
|
2009-07-21 20:42:07 +00:00
|
|
|
|
#endif
|
2009-07-14 20:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion // class Enumerator
|
2009-07-10 18:42:23 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new deque</summary>
|
|
|
|
|
public Deque() : this(512) { }
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new deque using the specified block size</summary>
|
|
|
|
|
/// <param name="blockSize">Size of the individual memory blocks used</param>
|
|
|
|
|
public Deque(int blockSize) {
|
|
|
|
|
this.blockSize = blockSize;
|
|
|
|
|
|
2012-02-29 16:27:43 +00:00
|
|
|
|
this.blocks = new List<TItem[]>();
|
|
|
|
|
this.blocks.Add(new TItem[this.blockSize]);
|
2009-07-10 18:42:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Number of items contained in the double ended queue</summary>
|
|
|
|
|
public int Count {
|
|
|
|
|
get { return this.count; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Accesses an item by its index</summary>
|
|
|
|
|
/// <param name="index">Index of the item that will be accessed</param>
|
|
|
|
|
/// <returns>The item at the specified index</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public TItem this[int index] {
|
2009-07-10 18:42:23 +00:00
|
|
|
|
get {
|
|
|
|
|
int blockIndex, subIndex;
|
|
|
|
|
findIndex(index, out blockIndex, out subIndex);
|
|
|
|
|
|
|
|
|
|
return this.blocks[blockIndex][subIndex];
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
int blockIndex, subIndex;
|
|
|
|
|
findIndex(index, out blockIndex, out subIndex);
|
|
|
|
|
|
|
|
|
|
this.blocks[blockIndex][subIndex] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>The first item in the double-ended queue</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public TItem First {
|
2009-07-10 18:42:23 +00:00
|
|
|
|
get {
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (this.count == 0) {
|
2009-07-10 18:42:23 +00:00
|
|
|
|
throw new InvalidOperationException("The deque is empty");
|
|
|
|
|
}
|
|
|
|
|
return this.blocks[0][this.firstBlockStartIndex];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>The last item in the double-ended queue</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public TItem Last {
|
2009-07-10 18:42:23 +00:00
|
|
|
|
get {
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (this.count == 0) {
|
2009-07-10 18:42:23 +00:00
|
|
|
|
throw new InvalidOperationException("The deque is empty");
|
|
|
|
|
}
|
2009-07-14 21:08:10 +00:00
|
|
|
|
return this.blocks[this.blocks.Count - 1][this.lastBlockEndIndex - 1];
|
2009-07-10 18:42:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-14 20:15:34 +00:00
|
|
|
|
/// <summary>Determines whether the deque contains the specified item</summary>
|
|
|
|
|
/// <param name="item">Item the deque will be scanned for</param>
|
|
|
|
|
/// <returns>True if the deque contains the item, false otherwise</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public bool Contains(TItem item) {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
return (IndexOf(item) != -1);
|
|
|
|
|
}
|
2009-07-11 19:55:59 +00:00
|
|
|
|
|
2009-07-14 20:15:34 +00:00
|
|
|
|
/// <summary>Copies the contents of the deque into an array</summary>
|
|
|
|
|
/// <param name="array">Array the contents of the deque will be copied into</param>
|
|
|
|
|
/// <param name="arrayIndex">Array index the deque contents will begin at</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public void CopyTo(TItem[] array, int arrayIndex) {
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (this.count > (array.Length - arrayIndex)) {
|
2009-07-14 21:08:10 +00:00
|
|
|
|
throw new ArgumentException(
|
|
|
|
|
"Array too small to hold the collection items starting at the specified index"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (this.blocks.Count == 1) { // Does only one block exist?
|
2009-07-14 21:08:10 +00:00
|
|
|
|
|
|
|
|
|
// Copy the one and only block there is
|
|
|
|
|
Array.Copy(
|
|
|
|
|
this.blocks[0], this.firstBlockStartIndex,
|
|
|
|
|
array, arrayIndex,
|
|
|
|
|
this.lastBlockEndIndex - this.firstBlockStartIndex
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
} else { // Multiple blocks exist
|
|
|
|
|
|
|
|
|
|
// Copy the first block which is filled from the start index to its end
|
|
|
|
|
int length = this.blockSize - this.firstBlockStartIndex;
|
|
|
|
|
Array.Copy(
|
|
|
|
|
this.blocks[0], this.firstBlockStartIndex,
|
|
|
|
|
array, arrayIndex,
|
|
|
|
|
length
|
|
|
|
|
);
|
|
|
|
|
arrayIndex += length;
|
|
|
|
|
|
|
|
|
|
// Copy all intermediate blocks (if there are any). These are completely filled
|
|
|
|
|
int lastBlock = this.blocks.Count - 1;
|
2010-12-24 01:02:56 +00:00
|
|
|
|
for (int index = 1; index < lastBlock; ++index) {
|
2009-07-14 21:08:10 +00:00
|
|
|
|
Array.Copy(
|
|
|
|
|
this.blocks[index], 0,
|
|
|
|
|
array, arrayIndex,
|
|
|
|
|
this.blockSize
|
|
|
|
|
);
|
|
|
|
|
arrayIndex += this.blockSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy the final block which is filled from the beginning to the end index
|
|
|
|
|
Array.Copy(
|
|
|
|
|
this.blocks[lastBlock], 0,
|
|
|
|
|
array, arrayIndex,
|
|
|
|
|
this.lastBlockEndIndex
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
}
|
2009-07-14 20:15:34 +00:00
|
|
|
|
}
|
2009-07-13 20:22:59 +00:00
|
|
|
|
|
2009-07-14 20:15:34 +00:00
|
|
|
|
/// <summary>Obtains a new enumerator for the contents of the deque</summary>
|
|
|
|
|
/// <returns>The new enumerator</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public IEnumerator<TItem> GetEnumerator() {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
return new Enumerator(this);
|
2009-07-11 19:55:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-10 18:42:23 +00:00
|
|
|
|
/// <summary>Calculates the block index and local sub index of an entry</summary>
|
|
|
|
|
/// <param name="index">Index of the entry that will be located</param>
|
|
|
|
|
/// <param name="blockIndex">Index of the block the entry is contained in</param>
|
|
|
|
|
/// <param name="subIndex">Local sub index of the entry within the block</param>
|
|
|
|
|
private void findIndex(int index, out int blockIndex, out int subIndex) {
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if ((index < 0) || (index >= this.count)) {
|
2009-07-10 18:42:23 +00:00
|
|
|
|
throw new ArgumentOutOfRangeException("Index out of range", "index");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index += this.firstBlockStartIndex;
|
2010-12-24 01:02:56 +00:00
|
|
|
|
#if WINDOWS
|
|
|
|
|
blockIndex = Math.DivRem(index, this.blockSize, out subIndex);
|
|
|
|
|
#else
|
2009-07-21 20:47:32 +00:00
|
|
|
|
blockIndex = index / this.blockSize;
|
|
|
|
|
subIndex = index % this.blockSize;
|
|
|
|
|
#endif
|
2009-07-10 18:42:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-14 20:15:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether the provided object can be placed in the deque
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">Value that will be checked for compatibility</param>
|
|
|
|
|
/// <returns>True if the value can be placed in the deque</returns>
|
|
|
|
|
private static bool isCompatibleObject(object value) {
|
2012-03-17 13:03:40 +00:00
|
|
|
|
#if WINRT
|
|
|
|
|
return ((value is TItem) || ((value == null) && !typeof(TItem).GetTypeInfo().IsValueType));
|
|
|
|
|
#else
|
2012-02-29 16:27:43 +00:00
|
|
|
|
return ((value is TItem) || ((value == null) && !typeof(TItem).IsValueType));
|
2012-03-17 13:03:40 +00:00
|
|
|
|
#endif
|
2009-07-14 20:15:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Verifies that the provided object matches the deque's type</summary>
|
|
|
|
|
/// <param name="value">Value that will be checked for compatibility</param>
|
|
|
|
|
private static void verifyCompatibleObject(object value) {
|
2010-12-24 01:02:56 +00:00
|
|
|
|
if (!isCompatibleObject(value)) {
|
2009-07-14 20:15:34 +00:00
|
|
|
|
throw new ArgumentException("Value does not match the deque's type", "value");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-10 18:42:23 +00:00
|
|
|
|
/// <summary>Number if items currently stored in the deque</summary>
|
|
|
|
|
private int count;
|
|
|
|
|
/// <summary>Size of a single deque block</summary>
|
|
|
|
|
private int blockSize;
|
|
|
|
|
/// <summary>Memory blocks being used to store the deque's data</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
private List<TItem[]> blocks;
|
2009-07-10 18:42:23 +00:00
|
|
|
|
/// <summary>Starting index of data in the first block</summary>
|
|
|
|
|
private int firstBlockStartIndex;
|
|
|
|
|
/// <summary>End index of data in the last block</summary>
|
2009-07-14 21:08:10 +00:00
|
|
|
|
private int lastBlockEndIndex;
|
2009-07-14 21:52:40 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
/// <summary>Used to detect when enumerators go out of sync</summary>
|
|
|
|
|
private int version;
|
|
|
|
|
#endif
|
2009-07-10 18:42:23 +00:00
|
|
|
|
|
|
|
|
|
}
|
2009-07-11 19:55:59 +00:00
|
|
|
|
|
2009-07-10 18:42:23 +00:00
|
|
|
|
} // namespace Nuclex.Support.Collections
|