Permalink
Browse files

first commit

  • Loading branch information...
richardhopton committed Nov 14, 2011
0 parents commit f3d91dd6fb3c3b1046a8a134bc35c4c7e4af47dd
@@ -0,0 +1,29 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoubleEndedQueue", "DoubleEndedQueue\DoubleEndedQueue.csproj", "{AA8C0A5C-EE25-4DA7-A607-D8810722FABC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoubleEndedQueueTest", "DoubleEndedQueueTest\DoubleEndedQueueTest.csproj", "{4826818F-5DB4-48D8-9B52-74A91F96E823}"
EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = DoubleEndedQueue.vsmdi
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AA8C0A5C-EE25-4DA7-A607-D8810722FABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA8C0A5C-EE25-4DA7-A607-D8810722FABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA8C0A5C-EE25-4DA7-A607-D8810722FABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA8C0A5C-EE25-4DA7-A607-D8810722FABC}.Release|Any CPU.Build.0 = Release|Any CPU
{4826818F-5DB4-48D8-9B52-74A91F96E823}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4826818F-5DB4-48D8-9B52-74A91F96E823}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4826818F-5DB4-48D8-9B52-74A91F96E823}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4826818F-5DB4-48D8-9B52-74A91F96E823}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace DoubleEndedQueue
{
public class Deque<T> : IDeque<T>
{
private static readonly T[] emptyArray = new T[0];
private Int32 startIndex = 0;
private Int32 count = 0;
private T[] items = emptyArray;
private enum HeadOrTail
{
Head,
Tail
}
public Int32 Capacity
{
get { return items.Length; }
}
public Int32 Count
{
get { return count; }
}
private void EnsureCapacity(HeadOrTail headOrTail)
{
var resizeCollection = (items.Length == 0) || ((count + 2) > items.Length);
var reorderCollection = resizeCollection;
if (!reorderCollection && (count > 0))
{
switch (headOrTail)
{
case Deque<T>.HeadOrTail.Head:
reorderCollection = (startIndex == 0);
break;
case Deque<T>.HeadOrTail.Tail:
reorderCollection = ((startIndex + count) == items.Length);
break;
}
}
if (reorderCollection)
{
var capacity = items.Length;
if (resizeCollection)
{
capacity = items.Length == 0 ? 4 : items.Length * 2;
}
var newItems = new T[capacity];
var newStartIndex = ((capacity - count) / 2);
if (count > 0)
{
Array.Copy(items, startIndex, newItems, newStartIndex, count);
}
items = newItems;
startIndex = newStartIndex;
}
}
public T PeekHead()
{
var result = default(T);
if (count > 0)
{
result = items[startIndex];
}
return result;
}
public T PeekTail()
{
var result = default(T);
if (count > 0)
{
var itemIndex = startIndex + (count - 1);
result = items[itemIndex];
}
return result;
}
public void EnqueueHead(T value)
{
EnsureCapacity(HeadOrTail.Head);
items[startIndex-1] = value;
startIndex--;
count++;
}
public void EnqueueTail(T value)
{
EnsureCapacity(HeadOrTail.Tail);
items[startIndex+count] = value;
count++;
}
public T DequeueHead()
{
var result = default(T);
if (count > 0)
{
result = items[startIndex];
items[startIndex] = default(T);
count--;
if (count > 0)
{
startIndex++;
}
else
{
startIndex = (items.Length / 2);
}
}
return result;
}
public T DequeueTail()
{
var result = default(T);
if (count > 0)
{
var itemIndex = startIndex + (count - 1);
result = items[itemIndex];
items[itemIndex] = default(T);
count--;
if (count == 0)
{
startIndex = (items.Length / 2);
}
}
return result;
}
public bool IsEmpty
{
get { return (count == 0); }
}
}
}
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{AA8C0A5C-EE25-4DA7-A607-D8810722FABC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DoubleEndedQueue</RootNamespace>
<AssemblyName>DoubleEndedQueue</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Deque.cs" />
<Compile Include="IDeque.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DoubleEndedQueue
{
public interface IDeque<T>
{
T PeekHead();
T PeekTail();
void EnqueueHead(T value);
void EnqueueTail(T value);
T DequeueHead();
T DequeueTail();
bool IsEmpty { get; }
}
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("DoubleEndedQueue")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DoubleEndedQueue")]
[assembly: AssemblyCopyright("Copyright © 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1a533f3a-c176-49d5-af40-ce27ffb7afdf")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Oops, something went wrong.

0 comments on commit f3d91dd

Please sign in to comment.