git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1297 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
(no author)
2007-07-17 18:20:50 +00:00
parent 5bb5fc32fe
commit 33abcb1063
13 changed files with 252 additions and 9 deletions

View File

@@ -0,0 +1,48 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{2F9E7A23-56C0-4286-9C8E-1060A9B2F073}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OpenRa.DataStructures</RootNamespace>
<AssemblyName>OpenRa.DataStructures</AssemblyName>
</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.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PriorityQueue.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tuple.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\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>

View File

@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace OpenRa.DataStructures
{
class PriorityQueue<T>
where T : IComparable<T>
{
List<T[]> items = new List<T[]>();
int level;
int index;
public PriorityQueue()
{
items.Add( new T[ 1 ] );
}
public void Add( T item )
{
int addLevel = level;
int addIndex = index;
while( addLevel >= 1 && Above( addLevel, addIndex ).CompareTo( item ) > 0 )
{
items[ addLevel ][ addIndex ] = Above( addLevel, addIndex );
--addLevel;
addIndex >>= 1;
}
items[ addLevel ][ addIndex ] = item;
++index;
if( index >= ( 1 << level ) )
{
index = 0;
++level;
items.Add( new T[ 1 << level ] );
}
}
T At( int level, int index )
{
return items[ level ][ index ];
}
T Above( int level, int index )
{
return items[ level - 1 ][ index >> 1 ];
}
T Last()
{
int lastLevel = level;
int lastIndex = index;
--lastIndex;
if( lastIndex < 0 )
{
--lastLevel;
lastIndex = ( 1 << lastLevel ) - 1;
}
return At( lastLevel, lastIndex );
}
public T Pop()
{
if( level == 0 && index == 0 )
throw new InvalidOperationException( "Attempting to pop empty PriorityQueue" );
T ret = At( 0, 0 );
BubbleInto( 0, 0, Last() );
--index;
if( index < 0 )
{
--level;
index = ( 1 << level ) - 1;
}
return ret;
}
public void BubbleInto( int intoLevel, int intoIndex, T val )
{
int downLevel = intoLevel + 1;
int downIndex = intoIndex << 1;
if( downLevel > level || ( downLevel == level && downIndex >= index ))
{
items[ intoLevel ][ intoIndex ] = val;
return;
}
if( downLevel == level && downIndex == index - 1 )
{
//Log.Write( "one-option bubble" );
}
else if( At( downLevel, downIndex ).CompareTo( At( downLevel, downIndex + 1 ) ) < 0 )
{
//Log.Write( "left bubble" );
}
else
{
//Log.Write( "right bubble" );
++downIndex;
}
if( val.CompareTo( At( downLevel, downIndex ) ) <= 0 )
{
items[ intoLevel ][ intoIndex ] = val;
return;
}
items[ intoLevel ][ intoIndex ] = At( downLevel, downIndex );
BubbleInto( downLevel, downIndex, val );
}
//void Invariant()
//{
// for( int i = 1 ; i < level ; i++ )
// for( int j = 0 ; j < RowLength( i ) ; j++ )
// if( At( i, j ).CompareTo( Above( i, j ) ) < 0 )
// System.Diagnostics.Debug.Assert( At( i, j ).CompareTo( Above( i, j ) ) < 0, "At( i, j ) > Above( i, j )" );
//}
private int RowLength( int i )
{
if( i == level )
return index;
return ( 1 << i );
}
}
}

View File

@@ -0,0 +1,35 @@
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( "OpenRa.DataStructures" )]
[assembly: AssemblyDescription( "" )]
[assembly: AssemblyConfiguration( "" )]
[assembly: AssemblyCompany( "" )]
[assembly: AssemblyProduct( "OpenRa.DataStructures" )]
[assembly: AssemblyCopyright( "Copyright © 2007" )]
[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( "fdbd08da-dde2-4eaf-9a8c-4bc63ac5b074" )]
// 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 Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion( "1.0.0.0" )]
[assembly: AssemblyFileVersion( "1.0.0.0" )]

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenRa.DataStructures
{
public class Tuple<A>
{
public A a;
public Tuple(A a) { this.a = a; }
}
public class Tuple<A, B>
{
public A a;
public B b;
public Tuple(A a, B b) { this.a = a; this.b = b; }
}
public class Tuple<A, B, C>
{
public A a;
public B b;
public C c;
public Tuple(A a, B b, C c) { this.a = a; this.b = b; this.c = c; }
}
public class Tuple<A, B, C, D>
{
public A a;
public B b;
public C c;
public D d;
public Tuple(A a, B b, C c, D d) { this.a = a; this.b = b; this.c = c; this.d = d; }
}
}