Began implementing a block-allocating Deque class similar to C++'s std::deque - insertion and removal at both ends already working
git-svn-id: file:///srv/devel/repo-conversion/nusu@158 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
74bf351727
commit
ff44edcdf1
|
@ -1,4 +1,4 @@
|
||||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<ProjectGuid>{DFFEAB70-51B8-4714-BCA6-79B733BBC520}</ProjectGuid>
|
<ProjectGuid>{DFFEAB70-51B8-4714-BCA6-79B733BBC520}</ProjectGuid>
|
||||||
<ProjectTypeGuids>{2DF5C3F4-5A5F-47a9-8E94-23B4456F55E2};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
<ProjectTypeGuids>{2DF5C3F4-5A5F-47a9-8E94-23B4456F55E2};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||||
|
@ -69,6 +69,10 @@
|
||||||
<Compile Include="Source\AssertHelper.Test.cs">
|
<Compile Include="Source\AssertHelper.Test.cs">
|
||||||
<DependentUpon>AssertHelper.cs</DependentUpon>
|
<DependentUpon>AssertHelper.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Source\Collections\Deque.cs" />
|
||||||
|
<Compile Include="Source\Collections\Deque.Test.cs">
|
||||||
|
<DependentUpon>Deque.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Collections\IObservableCollection.cs" />
|
<Compile Include="Source\Collections\IObservableCollection.cs" />
|
||||||
<Compile Include="Source\Collections\ItemEventArgs.cs" />
|
<Compile Include="Source\Collections\ItemEventArgs.cs" />
|
||||||
<Compile Include="Source\Collections\ItemEventArgs.Test.cs">
|
<Compile Include="Source\Collections\ItemEventArgs.Test.cs">
|
||||||
|
|
|
@ -51,6 +51,10 @@
|
||||||
<Compile Include="Source\AssertHelper.Test.cs">
|
<Compile Include="Source\AssertHelper.Test.cs">
|
||||||
<DependentUpon>AssertHelper.cs</DependentUpon>
|
<DependentUpon>AssertHelper.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Source\Collections\Deque.cs" />
|
||||||
|
<Compile Include="Source\Collections\Deque.Test.cs">
|
||||||
|
<DependentUpon>Deque.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Collections\IObservableCollection.cs" />
|
<Compile Include="Source\Collections\IObservableCollection.cs" />
|
||||||
<Compile Include="Source\Collections\ItemEventArgs.cs" />
|
<Compile Include="Source\Collections\ItemEventArgs.cs" />
|
||||||
<Compile Include="Source\Collections\ItemEventArgs.Test.cs">
|
<Compile Include="Source\Collections\ItemEventArgs.Test.cs">
|
||||||
|
|
101
Source/Collections/Deque.Test.cs
Normal file
101
Source/Collections/Deque.Test.cs
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2009 Nuclex Development Labs
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NMock2;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
|
#if false
|
||||||
|
/// <summary>Unit Test for the double ended queue</summary>
|
||||||
|
[TestFixture]
|
||||||
|
public class DequeTest {
|
||||||
|
|
||||||
|
/// <summary>Verifies that the AddLast() method of the deque is working</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestAddLast() {
|
||||||
|
Deque<int> intDeque = new Deque<int>(16);
|
||||||
|
for(int item = 0; item < 48; ++item) {
|
||||||
|
intDeque.AddLast(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int item = 0; item < 48; ++item) {
|
||||||
|
Assert.AreEqual(item, intDeque[item]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Verifies that the AddFirst() method of the deque is working</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestAddFirst() {
|
||||||
|
Deque<int> intDeque = new Deque<int>(16);
|
||||||
|
for(int item = 0; item < 48; ++item) {
|
||||||
|
intDeque.AddFirst(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int item = 0; item < 48; ++item) {
|
||||||
|
Assert.AreEqual(47 - item, intDeque[item]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the RemoveFirst() method of the deque is working
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestRemoveFirst() {
|
||||||
|
Deque<int> intDeque = new Deque<int>(16);
|
||||||
|
for(int item = 0; item < 48; ++item) {
|
||||||
|
intDeque.AddLast(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int item = 0; item < 48; ++item) {
|
||||||
|
Assert.AreEqual(item, intDeque.First);
|
||||||
|
Assert.AreEqual(48 - item, intDeque.Count);
|
||||||
|
intDeque.RemoveFirst();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the RemoveLast() method of the deque is working
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestRemoveLast() {
|
||||||
|
Deque<int> intDeque = new Deque<int>(16);
|
||||||
|
for(int item = 0; item < 48; ++item) {
|
||||||
|
intDeque.AddLast(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int item = 0; item < 48; ++item) {
|
||||||
|
Assert.AreEqual(47 - item, intDeque.Last);
|
||||||
|
Assert.AreEqual(48 - item, intDeque.Count);
|
||||||
|
intDeque.RemoveLast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Collections
|
||||||
|
|
||||||
|
#endif // UNITTEST
|
244
Source/Collections/Deque.cs
Normal file
244
Source/Collections/Deque.cs
Normal file
|
@ -0,0 +1,244 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
|
#if false
|
||||||
|
/// <summary>A double-ended queue that allocates memory in blocks</summary>
|
||||||
|
/// <typeparam name="ItemType">Type of the items being stored in the queue</typeparam>
|
||||||
|
/// <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
|
||||||
|
/// require items to be copied around and still can be accessed by index.
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
public class Deque<ItemType> /*: IList<ItemType>, IList*/ {
|
||||||
|
|
||||||
|
/// <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;
|
||||||
|
|
||||||
|
this.blocks = new List<ItemType[]>();
|
||||||
|
this.blocks.Add(new ItemType[this.blockSize]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public ItemType this[int index] {
|
||||||
|
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>
|
||||||
|
public ItemType First {
|
||||||
|
get {
|
||||||
|
if(this.lastBlockEndIndex <= this.firstBlockStartIndex) {
|
||||||
|
throw new InvalidOperationException("The deque is empty");
|
||||||
|
}
|
||||||
|
return this.blocks[0][this.firstBlockStartIndex];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>The last item in the double-ended queue</summary>
|
||||||
|
public ItemType Last {
|
||||||
|
get {
|
||||||
|
if(this.lastBlockEndIndex <= this.firstBlockStartIndex) {
|
||||||
|
throw new InvalidOperationException("The deque is empty");
|
||||||
|
}
|
||||||
|
return this.blocks[this.blocks.Count - 1][this.lastBlockEndIndex - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Removes the first item in the double-ended queue</summary>
|
||||||
|
public void RemoveFirst() {
|
||||||
|
if(this.count == 0) {
|
||||||
|
throw new InvalidOperationException("Cannot remove items from empty deque");
|
||||||
|
}
|
||||||
|
|
||||||
|
++this.firstBlockStartIndex;
|
||||||
|
if(this.firstBlockStartIndex >= this.blockSize) { // Block became empty
|
||||||
|
if(this.count > 1) { // Still more blocks in queue, remove block
|
||||||
|
this.blocks.RemoveAt(0);
|
||||||
|
this.firstBlockStartIndex = 0;
|
||||||
|
} else { // Last block - do not remove
|
||||||
|
this.firstBlockStartIndex = 0;
|
||||||
|
this.lastBlockEndIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
--this.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Removes the last item in the double-ended queue</summary>
|
||||||
|
public void RemoveLast() {
|
||||||
|
if(this.count == 0) {
|
||||||
|
throw new InvalidOperationException("Cannot remove items from empty deque");
|
||||||
|
}
|
||||||
|
|
||||||
|
--this.lastBlockEndIndex;
|
||||||
|
if(this.lastBlockEndIndex == 0) { // Block became empty
|
||||||
|
if(this.count > 1) {
|
||||||
|
this.blocks.RemoveAt(this.blocks.Count - 1);
|
||||||
|
this.lastBlockEndIndex = this.blockSize;
|
||||||
|
} else { // Last block - do not remove
|
||||||
|
this.firstBlockStartIndex = 0;
|
||||||
|
this.lastBlockEndIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
--this.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Inserts an item at the beginning of the double-ended queue</summary>
|
||||||
|
/// <param name="item">Item that will be inserted into the queue</param>
|
||||||
|
public void AddFirst(ItemType item) {
|
||||||
|
if(this.firstBlockStartIndex > 0) {
|
||||||
|
--this.firstBlockStartIndex;
|
||||||
|
} else { // Need to allocate a new block
|
||||||
|
this.blocks.Insert(0, new ItemType[this.blockSize]);
|
||||||
|
this.firstBlockStartIndex = this.blockSize - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.blocks[0][this.firstBlockStartIndex] = item;
|
||||||
|
++this.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Appends an item to the end of the double-ended queue</summary>
|
||||||
|
/// <param name="item">Item that will be appended to the queue</param>
|
||||||
|
public void AddLast(ItemType item) {
|
||||||
|
if(this.lastBlockEndIndex < this.blockSize) {
|
||||||
|
++this.lastBlockEndIndex;
|
||||||
|
} else { // Need to allocate a new block
|
||||||
|
this.blocks.Add(new ItemType[this.blockSize]);
|
||||||
|
this.lastBlockEndIndex = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.blocks[this.blocks.Count - 1][this.lastBlockEndIndex - 1] = item;
|
||||||
|
++this.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Insert(int index, ItemType item) {
|
||||||
|
if(index == this.count) {
|
||||||
|
AddLast(item);
|
||||||
|
} else {
|
||||||
|
int blockIndex, subIndex;
|
||||||
|
findIndex(index, out blockIndex, out subIndex);
|
||||||
|
|
||||||
|
//for(int block = this.blocks.Count - 1; block >
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// http://www.codeproject.com/KB/dotnet/ccnetsandcastle.aspx
|
||||||
|
/*
|
||||||
|
public int IndexOf(ItemType item) {
|
||||||
|
switch(this.blocks.Count) {
|
||||||
|
|
||||||
|
// No block exist, so the item cannot be found
|
||||||
|
case 0: {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only one block exists, start index and end index apply to the same block
|
||||||
|
case 1: {
|
||||||
|
int count = this.lastBlockEndIndex - this.firstBlockStartIndex;
|
||||||
|
int index = Array.IndexOf<ItemType>(
|
||||||
|
this.blocks[0], item, this.firstBlockStartIndex, count
|
||||||
|
);
|
||||||
|
|
||||||
|
// If we found something, we need to adjust its index so the first item in
|
||||||
|
// the deque always appears at index 0 to the user
|
||||||
|
if(index != -1) {
|
||||||
|
return (index - this.firstBlockStartIndex);
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Two blocks exist, start index is in first block, end index in second block
|
||||||
|
case 2: {
|
||||||
|
|
||||||
|
// Scan the first block for the item and if found, return the index
|
||||||
|
int count = this.blockSize - this.firstBlockStartIndex;
|
||||||
|
int index = Array.IndexOf<ItemType>(
|
||||||
|
this.blocks[0], item, this.firstBlockStartIndex, this.blockSize
|
||||||
|
);
|
||||||
|
|
||||||
|
// If we found something, we need to adjust its index
|
||||||
|
if(index != -1) {
|
||||||
|
return (index - this.firstBlockStartIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing found, continue the search in the
|
||||||
|
index = Array.IndexOf<ItemType>(
|
||||||
|
this.blocks[1], item, 0, this.lastBlockEndIndex
|
||||||
|
);
|
||||||
|
if(index == -1) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
return (index - this.firstBlockStartIndex + this.blockSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default: {
|
||||||
|
int count = this.blockSize - this.firstBlockStartIndex;
|
||||||
|
int index = Array.IndexOf<ItemType>(
|
||||||
|
this.blocks[0], item, this.firstBlockStartIndex, this.blockSize
|
||||||
|
);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
/// <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) {
|
||||||
|
if((index < 0) || (index >= this.count)) {
|
||||||
|
throw new ArgumentOutOfRangeException("Index out of range", "index");
|
||||||
|
}
|
||||||
|
|
||||||
|
index += this.firstBlockStartIndex;
|
||||||
|
blockIndex = Math.DivRem(index, this.blockSize, out subIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
private List<ItemType[]> blocks;
|
||||||
|
/// <summary>Starting index of data in the first block</summary>
|
||||||
|
private int firstBlockStartIndex;
|
||||||
|
/// <summary>End index of data in the last block</summary>
|
||||||
|
private int lastBlockEndIndex;
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Collections
|
|
@ -18,11 +18,11 @@ License along with this library
|
||||||
*/
|
*/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
#if UNITTEST
|
|
||||||
|
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using NMock2;
|
using NMock2;
|
||||||
|
|
||||||
|
|
|
@ -329,7 +329,6 @@ namespace Nuclex.Support.IO {
|
||||||
|
|
||||||
Assert.AreEqual(10, bytesRead);
|
Assert.AreEqual(10, bytesRead);
|
||||||
Assert.AreEqual(20, partialStream.Position);
|
Assert.AreEqual(20, partialStream.Position);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user