feat: List AddSorted extension returns the insertion index

This commit is contained in:
Chris Cameron
2018-08-21 10:25:11 -04:00
parent 60a19fe7f8
commit 1c9d311422
2 changed files with 15 additions and 13 deletions

View File

@@ -13,10 +13,10 @@ namespace ICD.Common.Utils.Tests.Extensions
{
List<int> testList = new List<int>();
testList.AddSorted(2);
testList.AddSorted(3);
testList.AddSorted(1);
testList.AddSorted(2);
Assert.AreEqual(0, testList.AddSorted(2));
Assert.AreEqual(1, testList.AddSorted(3));
Assert.AreEqual(0, testList.AddSorted(1));
Assert.AreEqual(1, testList.AddSorted(2));
Assert.AreEqual(4, testList.Count);
Assert.AreEqual(1, testList[0]);
@@ -31,10 +31,10 @@ namespace ICD.Common.Utils.Tests.Extensions
List<int> testList = new List<int>();
IComparer<int> comparer = new InverseComparer();
testList.AddSorted(2, comparer);
testList.AddSorted(3, comparer);
testList.AddSorted(1, comparer);
testList.AddSorted(2, comparer);
Assert.AreEqual(0, testList.AddSorted(2, comparer));
Assert.AreEqual(0, testList.AddSorted(3, comparer));
Assert.AreEqual(2, testList.AddSorted(1, comparer));
Assert.AreEqual(1, testList.AddSorted(2, comparer));
Assert.AreEqual(4, testList.Count);
Assert.AreEqual(3, testList[0]);

View File

@@ -82,12 +82,12 @@ namespace ICD.Common.Utils.Extensions
/// <param name="extends"></param>
/// <param name="item"></param>
[PublicAPI]
public static void AddSorted<T>(this List<T> extends, T item)
public static int AddSorted<T>(this List<T> extends, T item)
{
if (extends == null)
throw new ArgumentNullException("extends");
extends.AddSorted(item, Comparer<T>.Default);
return extends.AddSorted(item, Comparer<T>.Default);
}
/// <summary>
@@ -98,7 +98,7 @@ namespace ICD.Common.Utils.Extensions
/// <param name="item"></param>
/// <param name="comparer"></param>
[PublicAPI]
public static void AddSorted<T>(this List<T> extends, T item, IComparer<T> comparer)
public static int AddSorted<T>(this List<T> extends, T item, IComparer<T> comparer)
{
if (extends == null)
throw new ArgumentNullException("extends");
@@ -111,6 +111,8 @@ namespace ICD.Common.Utils.Extensions
index = ~index;
extends.Insert(index, item);
return index;
}
/// <summary>
@@ -122,7 +124,7 @@ namespace ICD.Common.Utils.Extensions
/// <param name="item"></param>
/// <param name="predicate"></param>
[PublicAPI]
public static void AddSorted<T, TProp>(this List<T> extends, T item, Func<T, TProp> predicate)
public static int AddSorted<T, TProp>(this List<T> extends, T item, Func<T, TProp> predicate)
{
if (extends == null)
throw new ArgumentNullException("extends");
@@ -131,7 +133,7 @@ namespace ICD.Common.Utils.Extensions
throw new ArgumentNullException("predicate");
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
extends.AddSorted(item, comparer);
return extends.AddSorted(item, comparer);
}
/// <summary>