[Agate-svn-commit] SF.net SVN: agate:[1417] trunk
Status: Alpha
Brought to you by:
kanato
From: <ka...@us...> - 2014-07-26 14:38:45
|
Revision: 1417 http://sourceforge.net/p/agate/code/1417 Author: kanato Date: 2014-07-26 14:38:36 +0000 (Sat, 26 Jul 2014) Log Message: ----------- Add generic list sort method. Modified Paths: -------------- trunk/AgateLib/Extensions/Collections/Generic/ListSorting.cs trunk/AgateLib/Serialization/Xle/XleSerializationInfo.cs trunk/AgateLib/Serialization/Xle/XleSerializer.cs trunk/UnitTests/UnitTests.csproj Added Paths: ----------- trunk/UnitTests/Extensions/ trunk/UnitTests/Extensions/ListExtensions.cs Modified: trunk/AgateLib/Extensions/Collections/Generic/ListSorting.cs =================================================================== --- trunk/AgateLib/Extensions/Collections/Generic/ListSorting.cs 2014-07-26 07:09:55 UTC (rev 1416) +++ trunk/AgateLib/Extensions/Collections/Generic/ListSorting.cs 2014-07-26 14:38:36 UTC (rev 1417) @@ -19,16 +19,21 @@ { if (list == null) throw new ArgumentNullException("list"); - Comparison<T> comp; + Comparison<T> comp = null; if (typeof(IComparable<T>).IsAssignableFrom(typeof(T))) { - InsertionSort(list, (x, y) => ((IComparable<T>)x).CompareTo(y)); + comp = (x, y) => ((IComparable<T>)x).CompareTo(y); } else if (typeof(IComparable).IsAssignableFrom(typeof(T))) { - InsertionSort(list, (x, y) => ((IComparable)x).CompareTo(y)); + comp = (x, y) => ((IComparable)x).CompareTo(y); } + + if (comp == null) + throw new InvalidOperationException("No comparison method available for " + typeof(T).FullName); + + InsertionSort(list, comp); } /// <summary> /// Provides a sort method for IList<T> objects which @@ -37,6 +42,19 @@ /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <param name="comparison"></param> + public static void InsertionSort<T>(this IList<T> list, IComparer<T> comparer) + { + if (list == null) throw new ArgumentNullException("list"); + + InsertionSort(list, (x, y) => comparer.Compare(x, y)); + } + /// <summary> + /// Provides a sort method for IList<T> objects which + /// is stable, unlike the List<T>.Sort() method. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="list"></param> + /// <param name="comparison"></param> public static void InsertionSort<T>(this IList<T> list, Comparison<T> comparison) { if (list == null) throw new ArgumentNullException("list"); Modified: trunk/AgateLib/Serialization/Xle/XleSerializationInfo.cs =================================================================== --- trunk/AgateLib/Serialization/Xle/XleSerializationInfo.cs 2014-07-26 07:09:55 UTC (rev 1416) +++ trunk/AgateLib/Serialization/Xle/XleSerializationInfo.cs 2014-07-26 14:38:36 UTC (rev 1417) @@ -1591,7 +1591,7 @@ try { - obj = (IXleSerializable)Activator.CreateInstance(type, true); + obj = (IXleSerializable)Activator.CreateInstance(type); } catch (Exception e) { Modified: trunk/AgateLib/Serialization/Xle/XleSerializer.cs =================================================================== --- trunk/AgateLib/Serialization/Xle/XleSerializer.cs 2014-07-26 07:09:55 UTC (rev 1416) +++ trunk/AgateLib/Serialization/Xle/XleSerializer.cs 2014-07-26 14:38:36 UTC (rev 1417) @@ -21,6 +21,7 @@ using System.IO; using System.Text; using System.Reflection; +using System.Linq; using System.Xml.Linq; using System.Xml; @@ -54,7 +55,7 @@ /// <param name="objectType">The type of the object to serialize.</param> public XleSerializer(Type objectType) { - if (typeof(IXleSerializable).IsAssignableFrom(objectType)) + if (objectType.GetInterfaces().Contains(typeof(IXleSerializable)) == false) throw new ArgumentException("Object type is not IXleSerializable."); var typeBinder = new TypeBinder(); Added: trunk/UnitTests/Extensions/ListExtensions.cs =================================================================== --- trunk/UnitTests/Extensions/ListExtensions.cs (rev 0) +++ trunk/UnitTests/Extensions/ListExtensions.cs 2014-07-26 14:38:36 UTC (rev 1417) @@ -0,0 +1,24 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using AgateLib.Extensions.Collections.Generic; + +namespace AgateLib.UnitTests.Extensions +{ + [TestClass] + public class ListExtensions + { + [TestMethod] + public void SortPrimitives() + { + List<int> li = new List<int> { 1, 6, 2, 3, 8, 10, 9, 7, 4, 5 }; + + Assert.AreEqual(10, li.Count); + + li.InsertionSort(); + + for (int i = 0; i < li.Count; i++) + Assert.AreEqual(i + 1, li[i]); + } + } +} Modified: trunk/UnitTests/UnitTests.csproj =================================================================== --- trunk/UnitTests/UnitTests.csproj 2014-07-26 07:09:55 UTC (rev 1416) +++ trunk/UnitTests/UnitTests.csproj 2014-07-26 14:38:36 UTC (rev 1417) @@ -89,6 +89,7 @@ <ItemGroup> <Compile Include="Core\ConsoleTests.cs" /> <Compile Include="Display\DisplayTests.cs" /> + <Compile Include="Extensions\ListExtensions.cs" /> <Compile Include="Fakes\FakeDisplayDriver.cs" /> <Compile Include="Fakes\FakeDisplayWindow.cs" /> <Compile Include="Fakes\FakeFrameBuffer.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |