diff --git a/ICD.Common.Utils.Tests/Extensions/ListExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/ListExtensionsTest.cs new file mode 100644 index 0000000..f05539c --- /dev/null +++ b/ICD.Common.Utils.Tests/Extensions/ListExtensionsTest.cs @@ -0,0 +1,54 @@ +using ICD.Common.Utils.Extensions; +using NUnit.Framework; +using System.Collections.Generic; +using System; + +namespace ICD.Common.Utils.Tests_NetStandard.Extensions +{ + [TestFixture] + public sealed class ListExtensionsTest + { + [Test] + public void AddSortedTest() + { + List testList = new List(); + + testList.AddSorted(2); + testList.AddSorted(3); + testList.AddSorted(1); + testList.AddSorted(2); + + Assert.AreEqual(4, testList.Count); + Assert.AreEqual(1, testList[0]); + Assert.AreEqual(2, testList[1]); + Assert.AreEqual(2, testList[2]); + Assert.AreEqual(3, testList[3]); + } + + [Test] + public void AddSortedComparerTest() + { + List testList = new List(); + IComparer comparer = new InverseComparer(); + + testList.AddSorted(2, comparer); + testList.AddSorted(3, comparer); + testList.AddSorted(1, comparer); + testList.AddSorted(2, comparer); + + Assert.AreEqual(4, testList.Count); + Assert.AreEqual(3, testList[0]); + Assert.AreEqual(2, testList[1]); + Assert.AreEqual(2, testList[2]); + Assert.AreEqual(1, testList[3]); + } + + internal class InverseComparer : IComparer + { + public int Compare(int x, int y) + { + return y.CompareTo(x); + } + } + } +} diff --git a/ICD.Common.Utils/Extensions/ListExtensions.cs b/ICD.Common.Utils/Extensions/ListExtensions.cs new file mode 100644 index 0000000..6cfe6ed --- /dev/null +++ b/ICD.Common.Utils/Extensions/ListExtensions.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using ICD.Common.Properties; + +namespace ICD.Common.Utils.Extensions +{ + /// + /// Extension methods for working with Lists. + /// + public static class ListExtensions + { + /// + /// Adds the item into a sorted list. + /// + /// + /// + /// + [PublicAPI] + public static void AddSorted(this List extends, T item) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + extends.AddSorted(item, Comparer.Default); + } + + /// + /// Adds the item into a sorted list. + /// + /// + /// + /// + /// + [PublicAPI] + public static void AddSorted(this List extends, T item, IComparer comparer) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (comparer == null) + throw new ArgumentNullException("comparer"); + + if (extends.Count == 0) + { + extends.Add(item); + return; + } + + if (comparer.Compare(extends[extends.Count - 1], item) <= 0) + { + extends.Add(item); + return; + } + + if (comparer.Compare(extends[0], item) >= 0) + { + extends.Insert(0, item); + return; + } + + int index = extends.BinarySearch(item); + if (index < 0) + index = ~index; + + extends.Insert(index, item); + } + } +} diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index c39b3ae..b6944e9 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -85,6 +85,7 @@ +