Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
/*
Copyright (c) 2003-2010 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System.Reflection;
using System.Runtime.CompilerServices;
using System;
//
// 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("C5: Copenhagen Comprehensive Collection Classes for CLI")]
[assembly: AssemblyDescription("This is a build of release 1.1.1")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("(c) 2003-2010 Niels Kokholm and Peter Sestoft")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
//
// 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.1.1")]
//
// In order to sign your assembly you must specify a key to use. Refer to the
// Microsoft .NET Framework documentation for more information on assembly signing.
//
// Use the attributes below to control which key is used for signing.
//
// Notes:
// (*) If no key is specified, the assembly is not signed.
// (*) KeyName refers to a key that has been installed in the Crypto Service
// Provider (CSP) on your machine. KeyFile refers to a file which contains
// a key.
// (*) If the KeyFile and the KeyName values are both specified, the
// following processing occurs:
// (1) If the KeyName can be found in the CSP, that key is used.
// (2) If the KeyName does not exist and the KeyFile does exist, the key
// in the KeyFile is installed into the CSP and used.
// (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
// When specifying the KeyFile, the location of the KeyFile should be
// relative to the project output directory which is
// %Project Directory%\obj\<configuration>. For example, if your KeyFile is
// located in the project directory, you would specify the AssemblyKeyFile
// attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
// documentation for more information on this.
//
[assembly: AssemblyDelaySign(true)]
[assembly: AssemblyKeyFile("c5.pub")]
[assembly: CLSCompliant(true)]

View File

@@ -0,0 +1,48 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
/// <summary>
/// A custom attribute to mark methods and properties as being tested
/// sufficiently in the regression test suite.
/// </summary>
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public sealed class TestedAttribute : Attribute
{
/// <summary>
/// Optional reference to test case
/// </summary>
[Tested]
public string via;
/// <summary>
/// Pretty print attribute value
/// </summary>
/// <returns>"Tested via " + via</returns>
[Tested]
public override string ToString() { return "Tested via " + via; }
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,181 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using C5;
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
/// <summary>
/// A default item comparer for an item type that is either generic (IComparable&lt;T&gt;)
/// or ordinarily (System.IComparable) comparable.
/// </summary>
public static class Comparer<T>
{
readonly static Type naturalComparerO = typeof(NaturalComparerO<>);
readonly static Type naturalComparer = typeof(NaturalComparer<>);
static SCG.IComparer<T> cachedComparer = null;
/// <summary>
/// Create a default comparer.
/// <para>The IComparer[T] object is constructed when this class is initialised, i.e.
/// its static constructors called. Thus, the property will be the same object
/// for the duration of an invocation of the runtime, but a value serialized in
/// another invocation and deserialized here will not be the same object.</para>
/// </summary>
/// <exception cref="NotComparableException">If T is not comparable</exception>
/// <value>The comparer</value>
[Tested]
public static SCG.IComparer<T> Default
{
get
{
if (cachedComparer != null)
return cachedComparer;
Type t = typeof(T);
if (t.IsValueType)
{
if (t.Equals(typeof(char)))
return cachedComparer = (SCG.IComparer<T>)(new CharComparer());
if (t.Equals(typeof(sbyte)))
return cachedComparer = (SCG.IComparer<T>)(new SByteComparer());
if (t.Equals(typeof(byte)))
return cachedComparer = (SCG.IComparer<T>)(new ByteComparer());
if (t.Equals(typeof(short)))
return cachedComparer = (SCG.IComparer<T>)(new ShortComparer());
if (t.Equals(typeof(ushort)))
return cachedComparer = (SCG.IComparer<T>)(new UShortComparer());
if (t.Equals(typeof(int)))
return cachedComparer = (SCG.IComparer<T>)(new IntComparer());
if (t.Equals(typeof(uint)))
return cachedComparer = (SCG.IComparer<T>)(new UIntComparer());
if (t.Equals(typeof(long)))
return cachedComparer = (SCG.IComparer<T>)(new LongComparer());
if (t.Equals(typeof(ulong)))
return cachedComparer = (SCG.IComparer<T>)(new ULongComparer());
if (t.Equals(typeof(float)))
return cachedComparer = (SCG.IComparer<T>)(new FloatComparer());
if (t.Equals(typeof(double)))
return cachedComparer = (SCG.IComparer<T>)(new DoubleComparer());
if (t.Equals(typeof(decimal)))
return cachedComparer = (SCG.IComparer<T>)(new DecimalComparer());
}
if (typeof(IComparable<T>).IsAssignableFrom(t))
{
Type c = naturalComparer.MakeGenericType(new Type[] { t });
return cachedComparer = (SCG.IComparer<T>)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));
}
if (t.GetInterface("System.IComparable") != null)
{
Type c = naturalComparerO.MakeGenericType(new Type[] { t });
return cachedComparer = (SCG.IComparer<T>)(c.GetConstructor(System.Type.EmptyTypes).Invoke(null));
}
throw new NotComparableException(String.Format("Cannot make comparer for type {0}", t));
}
}
}
/// <summary>
/// A natural generic IComparer for an IComparable&lt;T&gt; item type
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable]
public class NaturalComparer<T> : SCG.IComparer<T>
where T : IComparable<T>
{
/// <summary>
/// Compare two items
/// </summary>
/// <param name="item1">First item</param>
/// <param name="item2">Second item</param>
/// <returns>item1 &lt;=&gt; item2</returns>
[Tested]
public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }
}
/// <summary>
/// A natural generic IComparer for a System.IComparable item type
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable]
public class NaturalComparerO<T> : SCG.IComparer<T> where T : System.IComparable
{
/// <summary>
/// Compare two items
/// </summary>
/// <param name="item1">First item</param>
/// <param name="item2">Second item</param>
/// <returns>item1 &lt;=&gt; item2</returns>
[Tested]
public int Compare(T item1, T item2) { return item1 != null ? item1.CompareTo(item2) : item2 != null ? -1 : 0; }
}
/// <summary>
/// A generic comparer for type T based on a Comparison[T] delegate
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable]
public class DelegateComparer<T> : SCG.IComparer<T>
{
readonly Comparison<T> cmp;
/// <summary>
///
/// </summary>
/// <param name="comparison"></param>
public DelegateComparer(Comparison<T> comparison)
{
if (comparison == null)
throw new NullReferenceException("Comparison cannot be null");
this.cmp = comparison;
}
/// <summary>
///
/// </summary>
/// <param name="item1">First item</param>
/// <param name="item2">Second item</param>
/// <returns>item1 &lt;=&gt; item2</returns>
public int Compare(T item1, T item2) { return cmp(item1, item2); }
}
}

View File

@@ -0,0 +1,131 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using C5;
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Diagnostics;
#pragma warning disable 1711
namespace C5
{
/// <summary>
///
/// </summary>
public delegate void Act();
/// <summary>
/// <para>
/// The type Act[T] corresponds to System.Action[T] in the .Net
/// Framework class library.
///</para>
/// </summary>
/// <typeparam name="A1"></typeparam>
/// <param name="x1"></param>
public delegate void Act<A1>(A1 x1);
/// <summary>
///
/// </summary>
/// <typeparam name="A1"></typeparam>
/// <typeparam name="A2"></typeparam>
/// <param name="x1"></param>
/// <param name="x2"></param>
public delegate void Act<A1, A2>(A1 x1, A2 x2);
/// <summary>
///
/// </summary>
/// <typeparam name="A1"></typeparam>
/// <typeparam name="A2"></typeparam>
/// <typeparam name="A3"></typeparam>
/// <param name="x1"></param>
/// <param name="x2"></param>
/// <param name="x3"></param>
public delegate void Act<A1, A2, A3>(A1 x1, A2 x2, A3 x3);
/// <summary>
///
/// </summary>
/// <typeparam name="A1"></typeparam>
/// <typeparam name="A2"></typeparam>
/// <typeparam name="A3"></typeparam>
/// <typeparam name="A4"></typeparam>
/// <param name="x1"></param>
/// <param name="x2"></param>
/// <param name="x3"></param>
/// <param name="x4"></param>
public delegate void Act<A1, A2, A3, A4>(A1 x1, A2 x2, A3 x3, A4 x4);
/// <summary>
///
/// </summary>
/// <typeparam name="R"></typeparam>
/// <returns></returns>
public delegate R Fun<R>();
/// <summary>
/// Delegate type Fun[A1,R] is the type of functions (methods) from A1
/// to R, used to compute some transformation for a given collection
/// item.
/// <para>
/// The type Fun[T,U] corresponds to System.Converter[T,U] in the .Net
/// Framework class library, and the type Fun[T,bool] corresponds
/// System.Predicate[T] in the .Net Framework class library.</para>
/// </summary>
/// <typeparam name="A1"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="x1"></param>
/// <returns></returns>
public delegate R Fun<A1, R>(A1 x1);
/// <summary>
///
/// </summary>
/// <typeparam name="A1"></typeparam>
/// <typeparam name="A2"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="x1"></param>
/// <param name="x2"></param>
/// <returns></returns>
public delegate R Fun<A1, A2, R>(A1 x1, A2 x2);
/// <summary>
///
/// </summary>
/// <typeparam name="A1"></typeparam>
/// <typeparam name="A2"></typeparam>
/// <typeparam name="A3"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="x1"></param>
/// <param name="x2"></param>
/// <param name="x3"></param>
/// <returns></returns>
public delegate R Fun<A1, A2, A3, R>(A1 x1, A2 x2, A3 x3);
/// <summary>
///
/// </summary>
/// <typeparam name="A1"></typeparam>
/// <typeparam name="A2"></typeparam>
/// <typeparam name="A3"></typeparam>
/// <typeparam name="A4"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="x1"></param>
/// <param name="x2"></param>
/// <param name="x3"></param>
/// <param name="x4"></param>
/// <returns></returns>
public delegate R Fun<A1, A2, A3, A4, R>(A1 x1, A2 x2, A3 x3, A4 x4);
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,98 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
/// <summary>
/// The symbolic characterization of the speed of lookups for a collection.
/// The values may refer to worst-case, amortized and/or expected asymtotic
/// complexity wrt. the collection size.
/// </summary>
public enum Speed : short
{
/// <summary>
/// Counting the collection with the <code>Count property</code> may not return
/// (for a synthetic and potentially infinite collection).
/// </summary>
PotentiallyInfinite = 1,
/// <summary>
/// Lookup operations like <code>Contains(T item)</code> or the <code>Count</code>
/// property may take time O(n),
/// where n is the size of the collection.
/// </summary>
Linear = 2,
/// <summary>
/// Lookup operations like <code>Contains(T item)</code> or the <code>Count</code>
/// property takes time O(log n),
/// where n is the size of the collection.
/// </summary>
Log = 3,
/// <summary>
/// Lookup operations like <code>Contains(T item)</code> or the <code>Count</code>
/// property takes time O(1),
/// where n is the size of the collection.
/// </summary>
Constant = 4
}
/*
/// <summary>
///
/// </summary>
public enum ItemEqualityTypeEnum
{
/// <summary>
/// Only an Equals(T,T)
/// </summary>
Equator,
/// <summary>
/// Equals(T,T) and GetHashCode(T)
/// </summary>
HashingEqualityComparer,
/// <summary>
/// Compare(T,T)
/// </summary>
Comparer,
/// <summary>
/// Compatible Compare(T,T) and GetHashCode(T)
/// </summary>
Both
}
*/
/// <summary>
/// Direction of enumeration order relative to original collection.
/// </summary>
public enum EnumerationDirection
{
/// <summary>
/// Same direction
/// </summary>
Forwards,
/// <summary>
/// Opposite direction
/// </summary>
Backwards
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,244 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
/// <summary>
/// An exception to throw from library code when an internal inconsistency is encountered.
/// </summary>
public class InternalException : Exception
{
internal InternalException(string message) : base(message) { }
}
/// <summary>
/// An exception thrown by an update operation on a Read-Only collection or dictionary.
/// <para>This exception will be thrown unconditionally when an update operation
/// (method or set property) is called. No check is made to see if the update operation,
/// if allowed, would actually change the collection. </para>
/// </summary>
[Serializable]
public class ReadOnlyCollectionException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public ReadOnlyCollectionException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public ReadOnlyCollectionException(string message) : base(message) { }
}
/// <summary>
///
/// </summary>
[Serializable]
public class FixedSizeCollectionException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public FixedSizeCollectionException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public FixedSizeCollectionException(string message) : base(message) { }
}
/// <summary>
///
/// </summary>
[Serializable]
public class UnlistenableEventException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public UnlistenableEventException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public UnlistenableEventException(string message) : base(message) { }
}
/// <summary>
/// An exception thrown by enumerators, range views etc. when accessed after
/// the underlying collection has been modified.
/// </summary>
[Serializable]
public class CollectionModifiedException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public CollectionModifiedException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public CollectionModifiedException(string message) : base(message) { }
}
/// <summary>
/// An excption thrown when trying to access a view (a list view on a <see cref="T:C5.IList`1"/> or
/// a snapshot on a <see cref="T:C5.IPersistentSorted`1"/>)
/// that has been invalidated by some earlier operation.
/// <para>
/// The typical scenario is a view on a list that hash been invalidated by a call to
/// Sort, Reverse or Shuffle on some other, overlapping view or the whole list.
/// </para>
/// </summary>
[Serializable]
public class ViewDisposedException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public ViewDisposedException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public ViewDisposedException(string message) : base(message) { }
}
/// <summary>
/// An exception thrown by a lookup or lookup with update operation that does not
/// find the lookup item and has no other means to communicate failure.
/// <para>The typical scenario is a lookup by key in a dictionary with an indexer,
/// see e.g. <see cref="P:C5.IDictionary`2.Item(`0)"/></para>
/// </summary>
[Serializable]
public class NoSuchItemException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public NoSuchItemException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public NoSuchItemException(string message) : base(message) { }
}
/// <summary>
/// An exception thrown by an operation on a list (<see cref="T:C5.IList`1"/>)
/// that only makes sense for a view, not for an underlying list.
/// </summary>
[Serializable]
public class NotAViewException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public NotAViewException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public NotAViewException(string message) : base(message) { }
}
/// <summary>
/// An exception thrown when an operation attempts to create a duplicate in a collection with set semantics
/// (<see cref="P:C5.IExtensible`1.AllowsDuplicates"/> is false) or attempts to create a duplicate key in a dictionary.
/// <para>With collections this can only happen with Insert operations on lists, since the Add operations will
/// not try to create duplictes and either ignore the failure or report it in a bool return value.
/// </para>
/// <para>With dictionaries this can happen with the <see cref="M:C5.IDictionary`2.Add(`0,`1)"/> metod.</para>
/// </summary>
[Serializable]
public class DuplicateNotAllowedException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public DuplicateNotAllowedException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public DuplicateNotAllowedException(string message) : base(message) { }
}
/// <summary>
///
/// </summary>
[Serializable]
public class InvalidPriorityQueueHandleException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public InvalidPriorityQueueHandleException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public InvalidPriorityQueueHandleException(string message) : base(message) { }
}
/// <summary>
/// An exception thrown by an operation that need to construct a natural
/// comparer for a type.
/// </summary>
[Serializable]
public class NotComparableException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public NotComparableException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public NotComparableException(string message) : base(message) { }
}
/// <summary>
/// An exception thrown by operations on a list that expects an argument
/// that is a view on the same underlying list.
/// </summary>
[Serializable]
public class IncompatibleViewException : Exception
{
/// <summary>
/// Create a simple exception with no further explanation.
/// </summary>
public IncompatibleViewException() : base() { }
/// <summary>
/// Create the exception with an explanation of the reason.
/// </summary>
/// <param name="message"></param>
public IncompatibleViewException(string message) : base(message) { }
}
}

View File

@@ -0,0 +1,249 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using C5;
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Diagnostics;
using System.Text;
namespace C5
{
/// <summary>
/// <i>(Describe usage of "L:300" format string.)</i>
/// </summary>
public interface IShowable : IFormattable
{
//TODO: wonder if we should use TextWriters instead of StringBuilders?
/// <summary>
/// Format <code>this</code> using at most approximately <code>rest</code> chars and
/// append the result, possibly truncated, to stringbuilder.
/// Subtract the actual number of used chars from <code>rest</code>.
/// </summary>
/// <param name="stringbuilder"></param>
/// <param name="rest"></param>
/// <param name="formatProvider"></param>
/// <returns>True if the appended formatted string was complete (not truncated).</returns>
bool Show(StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider);
}
// ------------------------------------------------------------
// Static helper methods for Showing collections
/// <summary>
///
/// </summary>
public static class Showing
{
/// <summary>
/// Show <code>Object obj</code> by appending it to <code>stringbuilder</code>
/// </summary>
/// <param name="obj"></param>
/// <param name="stringbuilder"></param>
/// <param name="rest"></param>
/// <param name="formatProvider"></param>
/// <returns>True if <code>obj</code> was shown completely.</returns>
public static bool Show(Object obj, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
{
IShowable showable;
if (rest <= 0)
return false;
else if ((showable = obj as IShowable) != null)
return showable.Show(stringbuilder, ref rest, formatProvider);
int oldLength = stringbuilder.Length;
stringbuilder.AppendFormat(formatProvider, "{0}", obj);
rest -= (stringbuilder.Length - oldLength);
return true;
}
/// <summary>
///
/// </summary>
/// <param name="showable"></param>
/// <param name="format"></param>
/// <param name="formatProvider"></param>
/// <returns></returns>
public static String ShowString(IShowable showable, String format, IFormatProvider formatProvider)
{
int rest = maxLength(format);
StringBuilder sb = new StringBuilder();
showable.Show(sb, ref rest, formatProvider);
return sb.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="format"></param>
/// <returns></returns>
static int maxLength(String format)
{
//TODO: validate format string
if (format == null)
return 80;
if (format.Length > 1 && format.StartsWith("L"))
{
return int.Parse(format.Substring(1));
}
else
return int.MaxValue;
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <param name="stringbuilder"></param>
/// <param name="rest"></param>
/// <param name="formatProvider"></param>
/// <returns>True if collection was shown completely</returns>
public static bool ShowCollectionValue<T>(ICollectionValue<T> items, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
{
string startdelim = "{ ", enddelim = " }";
bool showIndexes = false;
bool showMultiplicities = false;
//TODO: do not test here at run time, but select code at compile time
// perhaps by delivering the print type to this metod
IList<T> list;
ICollection<T> coll = items as ICollection<T>;
if ((list = items as IList<T>) != null)
{
startdelim = "[ ";
enddelim = " ]";
//TODO: should have been (items as IIndexed<T>).IndexingSpeed
showIndexes = list.IndexingSpeed == Speed.Constant;
}
else if (coll != null)
{
if (coll.AllowsDuplicates)
{
startdelim = "{{ ";
enddelim = " }}";
if (coll.DuplicatesByCounting)
showMultiplicities = true;
}
}
stringbuilder.Append(startdelim);
rest -= 2 * startdelim.Length;
bool first = true;
bool complete = true;
int index = 0;
if (showMultiplicities)
{
foreach (KeyValuePair<T, int> p in coll.ItemMultiplicities())
{
complete = false;
if (rest <= 0)
break;
if (first)
first = false;
else
{
stringbuilder.Append(", ");
rest -= 2;
}
if (complete = Showing.Show(p.Key, stringbuilder, ref rest, formatProvider))
{
string multiplicityString = string.Format("(*{0})", p.Value);
stringbuilder.Append(multiplicityString);
rest -= multiplicityString.Length;
}
}
}
else
{
foreach (T x in items)
{
complete = false;
if (rest <= 0)
break;
if (first)
first = false;
else
{
stringbuilder.Append(", ");
rest -= 2;
}
if (showIndexes)
{
string indexString = string.Format("{0}:", index++);
stringbuilder.Append(indexString);
rest -= indexString.Length;
}
complete = Showing.Show(x, stringbuilder, ref rest, formatProvider);
}
}
if (!complete)
{
stringbuilder.Append("...");
rest -= 3;
}
stringbuilder.Append(enddelim);
return complete;
}
/// <summary>
///
/// </summary>
/// <typeparam name="K"></typeparam>
/// <typeparam name="V"></typeparam>
///
/// <param name="dictionary"></param>
/// <param name="stringbuilder"></param>
/// <param name="formatProvider"></param>
/// <param name="rest"></param>
/// <returns></returns>
public static bool ShowDictionary<K, V>(IDictionary<K, V> dictionary, StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
{
bool sorted = dictionary is ISortedDictionary<K, V>;
stringbuilder.Append(sorted ? "[ " : "{ ");
rest -= 4; // Account for "( " and " )"
bool first = true;
bool complete = true;
foreach (KeyValuePair<K, V> p in dictionary)
{
complete = false;
if (rest <= 0)
break;
if (first)
first = false;
else
{
stringbuilder.Append(", ");
rest -= 2;
}
complete = Showing.Show(p, stringbuilder, ref rest, formatProvider);
}
if (!complete)
{
stringbuilder.Append("...");
rest -= 3;
}
stringbuilder.Append(sorted ? " ]" : " }");
return complete;
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,145 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
abstract class MappedDirectedCollectionValue<T, V> : DirectedCollectionValueBase<V>, IDirectedCollectionValue<V>
{
IDirectedCollectionValue<T> directedcollectionvalue;
abstract public V Map(T item);
public MappedDirectedCollectionValue(IDirectedCollectionValue<T> directedcollectionvalue)
{
this.directedcollectionvalue = directedcollectionvalue;
}
public override V Choose() { return Map(directedcollectionvalue.Choose()); }
public override bool IsEmpty { get { return directedcollectionvalue.IsEmpty; } }
public override int Count { get { return directedcollectionvalue.Count; } }
public override Speed CountSpeed { get { return directedcollectionvalue.CountSpeed; } }
public override IDirectedCollectionValue<V> Backwards()
{
MappedDirectedCollectionValue<T, V> retval = (MappedDirectedCollectionValue<T, V>)MemberwiseClone();
retval.directedcollectionvalue = directedcollectionvalue.Backwards();
return retval;
//If we made this classs non-abstract we could do
//return new MappedDirectedCollectionValue<T,V>(directedcollectionvalue.Backwards());;
}
public override SCG.IEnumerator<V> GetEnumerator()
{
foreach (T item in directedcollectionvalue)
yield return Map(item);
}
public override EnumerationDirection Direction
{
get { return directedcollectionvalue.Direction; }
}
IDirectedEnumerable<V> IDirectedEnumerable<V>.Backwards()
{
return Backwards();
}
}
abstract class MappedCollectionValue<T, V> : CollectionValueBase<V>, ICollectionValue<V>
{
ICollectionValue<T> collectionvalue;
abstract public V Map(T item);
public MappedCollectionValue(ICollectionValue<T> collectionvalue)
{
this.collectionvalue = collectionvalue;
}
public override V Choose() { return Map(collectionvalue.Choose()); }
public override bool IsEmpty { get { return collectionvalue.IsEmpty; } }
public override int Count { get { return collectionvalue.Count; } }
public override Speed CountSpeed { get { return collectionvalue.CountSpeed; } }
public override SCG.IEnumerator<V> GetEnumerator()
{
foreach (T item in collectionvalue)
yield return Map(item);
}
}
class MultiplicityOne<K> : MappedCollectionValue<K, KeyValuePair<K, int>>
{
public MultiplicityOne(ICollectionValue<K> coll):base(coll) { }
public override KeyValuePair<K, int> Map(K k) { return new KeyValuePair<K, int>(k, 1); }
}
class DropMultiplicity<K> : MappedCollectionValue<KeyValuePair<K, int>, K>
{
public DropMultiplicity(ICollectionValue<KeyValuePair<K, int>> coll):base(coll) { }
public override K Map(KeyValuePair<K, int> kvp) { return kvp.Key; }
}
abstract class MappedDirectedEnumerable<T, V> : EnumerableBase<V>, IDirectedEnumerable<V>
{
IDirectedEnumerable<T> directedenumerable;
abstract public V Map(T item);
public MappedDirectedEnumerable(IDirectedEnumerable<T> directedenumerable)
{
this.directedenumerable = directedenumerable;
}
public IDirectedEnumerable<V> Backwards()
{
MappedDirectedEnumerable<T, V> retval = (MappedDirectedEnumerable<T, V>)MemberwiseClone();
retval.directedenumerable = directedenumerable.Backwards();
return retval;
//If we made this classs non-abstract we could do
//return new MappedDirectedCollectionValue<T,V>(directedcollectionvalue.Backwards());;
}
public override SCG.IEnumerator<V> GetEnumerator()
{
foreach (T item in directedenumerable)
yield return Map(item);
}
public EnumerationDirection Direction
{
get { return directedenumerable.Direction; }
}
}
}

View File

@@ -0,0 +1,173 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
/// <summary>
/// A modern random number generator based on G. Marsaglia:
/// Seeds for Random Number Generators, Communications of the
/// ACM 46, 5 (May 2003) 90-93; and a posting by Marsaglia to
/// comp.lang.c on 2003-04-03.
/// </summary>
public class C5Random : Random
{
private uint[] Q = new uint[16];
private uint c = 362436, i = 15;
private uint Cmwc()
{
ulong t, a = 487198574UL;
uint x, r = 0xfffffffe;
i = (i + 1) & 15;
t = a * Q[i] + c;
c = (uint)(t >> 32);
x = (uint)(t + c);
if (x < c)
{
x++;
c++;
}
return Q[i] = r - x;
}
/// <summary>
/// Get a new random System.Double value
/// </summary>
/// <returns>The random double</returns>
public override double NextDouble()
{
return Cmwc() / 4294967296.0;
}
/// <summary>
/// Get a new random System.Double value
/// </summary>
/// <returns>The random double</returns>
protected override double Sample()
{
return NextDouble();
}
/// <summary>
/// Get a new random System.Int32 value
/// </summary>
/// <returns>The random int</returns>
public override int Next()
{
return (int)Cmwc();
}
/// <summary>
/// Get a random non-negative integer less than a given upper bound
/// </summary>
/// <exception cref="ArgumentException">If max is negative</exception>
/// <param name="max">The upper bound (exclusive)</param>
/// <returns></returns>
public override int Next(int max)
{
if (max < 0)
throw new ArgumentException("max must be non-negative");
return (int)(Cmwc() / 4294967296.0 * max);
}
/// <summary>
/// Get a random integer between two given bounds
/// </summary>
/// <exception cref="ArgumentException">If max is less than min</exception>
/// <param name="min">The lower bound (inclusive)</param>
/// <param name="max">The upper bound (exclusive)</param>
/// <returns></returns>
public override int Next(int min, int max)
{
if (min > max)
throw new ArgumentException("min must be less than or equal to max");
return min + (int)(Cmwc() / 4294967296.0 * (max - min));
}
/// <summary>
/// Fill a array of byte with random bytes
/// </summary>
/// <param name="buffer">The array to fill</param>
public override void NextBytes(byte[] buffer)
{
for (int i = 0, length = buffer.Length; i < length; i++)
buffer[i] = (byte)Cmwc();
}
/// <summary>
/// Create a random number generator seed by system time.
/// </summary>
public C5Random() : this(DateTime.Now.Ticks)
{
}
/// <summary>
/// Create a random number generator with a given seed
/// </summary>
/// <exception cref="ArgumentException">If seed is zero</exception>
/// <param name="seed">The seed</param>
public C5Random(long seed)
{
if (seed == 0)
throw new ArgumentException("Seed must be non-zero");
uint j = (uint)(seed & 0xFFFFFFFF);
for (int i = 0; i < 16; i++)
{
j ^= j << 13;
j ^= j >> 17;
j ^= j << 5;
Q[i] = j;
}
Q[15] = (uint)(seed ^ (seed >> 32));
}
/// <summary>
/// Create a random number generator with a specified internal start state.
/// </summary>
/// <exception cref="ArgumentException">If Q is not of length exactly 16</exception>
/// <param name="Q">The start state. Must be a collection of random bits given by an array of exactly 16 uints.</param>
[CLSCompliant(false)]
public C5Random(uint[] Q)
{
if (Q.Length != 16)
throw new ArgumentException("Q must have length 16, was " + Q.Length);
Array.Copy(Q, this.Q, 16);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,239 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
/// <summary>
/// A utility class with functions for sorting arrays with respect to an IComparer&lt;T&gt;
/// </summary>
public class Sorting
{
Sorting() { }
/// <summary>
/// Sort part of array in place using IntroSort
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If the <code>start</code>
/// and <code>count</code> arguments does not describe a valid range.</exception>
/// <param name="array">Array to sort</param>
/// <param name="start">Index of first position to sort</param>
/// <param name="count">Number of elements to sort</param>
/// <param name="comparer">IComparer&lt;T&gt; to sort by</param>
[Tested]
public static void IntroSort<T>(T[] array, int start, int count, SCG.IComparer<T> comparer)
{
if (start < 0 || count < 0 || start + count > array.Length)
throw new ArgumentOutOfRangeException();
new Sorter<T>(array, comparer).IntroSort(start, start + count);
}
/// <summary>
/// Sort an array in place using IntroSort and default comparer
/// </summary>
/// <exception cref="NotComparableException">If T is not comparable</exception>
/// <param name="array">Array to sort</param>
[Tested]
public static void IntroSort<T>(T[] array)
{
new Sorter<T>(array, Comparer<T>.Default).IntroSort(0, array.Length);
}
/// <summary>
/// Sort part of array in place using Insertion Sort
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If the <code>start</code>
/// and <code>count</code> arguments does not describe a valid range.</exception>
/// <param name="array">Array to sort</param>
/// <param name="start">Index of first position to sort</param>
/// <param name="count">Number of elements to sort</param>
/// <param name="comparer">IComparer&lt;T&gt; to sort by</param>
[Tested]
public static void InsertionSort<T>(T[] array, int start, int count, SCG.IComparer<T> comparer)
{
if (start < 0 || count < 0 || start + count > array.Length)
throw new ArgumentOutOfRangeException();
new Sorter<T>(array, comparer).InsertionSort(start, start + count);
}
/// <summary>
/// Sort part of array in place using Heap Sort
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">If the <code>start</code>
/// and <code>count</code> arguments does not describe a valid range.</exception>
/// <param name="array">Array to sort</param>
/// <param name="start">Index of first position to sort</param>
/// <param name="count">Number of elements to sort</param>
/// <param name="comparer">IComparer&lt;T&gt; to sort by</param>
[Tested]
public static void HeapSort<T>(T[] array, int start, int count, SCG.IComparer<T> comparer)
{
if (start < 0 || count < 0 || start + count > array.Length)
throw new ArgumentOutOfRangeException();
new Sorter<T>(array, comparer).HeapSort(start, start + count);
}
class Sorter<T>
{
T[] a;
SCG.IComparer<T> c;
internal Sorter(T[] a, SCG.IComparer<T> c) { this.a = a; this.c = c; }
internal void IntroSort(int f, int b)
{
if (b - f > 31)
{
int depth_limit = (int)Math.Floor(2.5 * Math.Log(b - f, 2));
introSort(f, b, depth_limit);
}
else
InsertionSort(f, b);
}
private void introSort(int f, int b, int depth_limit)
{
const int size_threshold = 14;//24;
if (depth_limit-- == 0)
HeapSort(f, b);
else if (b - f <= size_threshold)
InsertionSort(f, b);
else
{
int p = partition(f, b);
introSort(f, p, depth_limit);
introSort(p, b, depth_limit);
}
}
private int compare(T i1, T i2) { return c.Compare(i1, i2); }
private int partition(int f, int b)
{
int bot = f, mid = (b + f) / 2, top = b - 1;
T abot = a[bot], amid = a[mid], atop = a[top];
if (compare(abot, amid) < 0)
{
if (compare(atop, abot) < 0)//atop<abot<amid
{ a[top] = amid; amid = a[mid] = abot; a[bot] = atop; }
else if (compare(atop, amid) < 0) //abot<=atop<amid
{ a[top] = amid; amid = a[mid] = atop; }
//else abot<amid<=atop
}
else
{
if (compare(amid, atop) > 0) //atop<amid<=abot
{ a[bot] = atop; a[top] = abot; }
else if (compare(abot, atop) > 0) //amid<=atop<abot
{ a[bot] = amid; amid = a[mid] = atop; a[top] = abot; }
else //amid<=abot<=atop
{ a[bot] = amid; amid = a[mid] = abot; }
}
int i = bot, j = top;
while (true)
{
while (compare(a[++i], amid) < 0) ;
while (compare(amid, a[--j]) < 0) ;
if (i < j)
{
T tmp = a[i]; a[i] = a[j]; a[j] = tmp;
}
else
return i;
}
}
internal void InsertionSort(int f, int b)
{
for (int j = f + 1; j < b; j++)
{
T key = a[j], other;
int i = j - 1;
if (c.Compare(other = a[i], key) > 0)
{
a[j] = other;
while (i > f && c.Compare(other = a[i - 1], key) > 0) { a[i--] = other; }
a[i] = key;
}
}
}
internal void HeapSort(int f, int b)
{
for (int i = (b + f) / 2; i >= f; i--) heapify(f, b, i);
for (int i = b - 1; i > f; i--)
{
T tmp = a[f]; a[f] = a[i]; a[i] = tmp;
heapify(f, i, f);
}
}
private void heapify(int f, int b, int i)
{
T pv = a[i], lv, rv, max = pv;
int j = i, maxpt = j;
while (true)
{
int l = 2 * j - f + 1, r = l + 1;
if (l < b && compare(lv = a[l], max) > 0) { maxpt = l; max = lv; }
if (r < b && compare(rv = a[r], max) > 0) { maxpt = r; max = rv; }
if (maxpt == j)
break;
a[j] = max;
max = pv;
j = maxpt;
}
if (j > i)
a[j] = pv;
}
}
}
}

View File

@@ -0,0 +1,101 @@
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Diagnostics;
using SCG = System.Collections.Generic;
namespace C5
{
/// <summary>
/// Characterize the mutual position of some view B (other) relative to view A (this)
/// </summary>
enum MutualViewPosition
{
/// <summary>
/// B contains A(this)
/// </summary>
Contains,
/// <summary>
/// B is containd in A(this), but not vice versa
/// </summary>
ContainedIn,
/// <summary>
/// A and B does not overlap
/// </summary>
NonOverlapping,
/// <summary>
/// A and B overlap, but neither is contained in the other
/// </summary>
Overlapping
}
#region View List Nested class
/// <summary>
/// This class is shared between the linked list and array list implementations.
/// </summary>
/// <typeparam name="V"></typeparam>
[Serializable]
class WeakViewList<V> where V : class
{
Node start;
[Serializable]
internal class Node
{
internal WeakReference weakview; internal Node prev, next;
internal Node(V view) { weakview = new WeakReference(view); }
}
internal Node Add(V view)
{
Node newNode = new Node(view);
if (start != null) { start.prev = newNode; newNode.next = start; }
start = newNode;
return newNode;
}
internal void Remove(Node n)
{
if (n == start) { start = start.next; if (start != null) start.prev = null; }
else { n.prev.next = n.next; if (n.next != null) n.next.prev = n.prev; }
}
/// <summary>
/// Note that it is safe to call views.Remove(view.myWeakReference) if view
/// is the currently yielded object
/// </summary>
/// <returns></returns>
public SCG.IEnumerator<V> GetEnumerator()
{
Node n = start;
while (n != null)
{
//V view = n.weakview.Target as V; //This provokes a bug in the beta1 verifyer
object o = n.weakview.Target;
V view = o is V ? (V)o : null;
if (view == null)
Remove(n);
else
yield return view;
n = n.next;
}
}
}
#endregion
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More