diff --git a/OpenRa.DataStructures/OpenRa.DataStructures.csproj b/OpenRa.DataStructures/OpenRa.DataStructures.csproj new file mode 100644 index 0000000000..f0fddf98c3 --- /dev/null +++ b/OpenRa.DataStructures/OpenRa.DataStructures.csproj @@ -0,0 +1,48 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {2F9E7A23-56C0-4286-9C8E-1060A9B2F073} + Library + Properties + OpenRa.DataStructures + OpenRa.DataStructures + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OpenRa.DataStructures/PriorityQueue.cs b/OpenRa.DataStructures/PriorityQueue.cs new file mode 100644 index 0000000000..fcb20425d8 --- /dev/null +++ b/OpenRa.DataStructures/PriorityQueue.cs @@ -0,0 +1,133 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Diagnostics; + +namespace OpenRa.DataStructures +{ + class PriorityQueue + where T : IComparable + { + List items = new List(); + 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 ); + } + } +} diff --git a/OpenRa.DataStructures/Properties/AssemblyInfo.cs b/OpenRa.DataStructures/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..cf7fc180e0 --- /dev/null +++ b/OpenRa.DataStructures/Properties/AssemblyInfo.cs @@ -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" )] diff --git a/OpenRa.FileFormats/Tuple.cs b/OpenRa.DataStructures/Tuple.cs similarity index 89% rename from OpenRa.FileFormats/Tuple.cs rename to OpenRa.DataStructures/Tuple.cs index f20cd086bd..17233884c8 100644 --- a/OpenRa.FileFormats/Tuple.cs +++ b/OpenRa.DataStructures/Tuple.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Text; -namespace OpenRa.FileFormats +namespace OpenRa.DataStructures { public class Tuple { diff --git a/OpenRa.FileFormats/OpenRa.FileFormats.csproj b/OpenRa.FileFormats/OpenRa.FileFormats.csproj index a5d6eb0fa8..97df2ae56e 100644 --- a/OpenRa.FileFormats/OpenRa.FileFormats.csproj +++ b/OpenRa.FileFormats/OpenRa.FileFormats.csproj @@ -52,7 +52,6 @@ -