mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
feat: List AddSorted extension returns the insertion index
This commit is contained in:
@@ -13,10 +13,10 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
{
|
{
|
||||||
List<int> testList = new List<int>();
|
List<int> testList = new List<int>();
|
||||||
|
|
||||||
testList.AddSorted(2);
|
Assert.AreEqual(0, testList.AddSorted(2));
|
||||||
testList.AddSorted(3);
|
Assert.AreEqual(1, testList.AddSorted(3));
|
||||||
testList.AddSorted(1);
|
Assert.AreEqual(0, testList.AddSorted(1));
|
||||||
testList.AddSorted(2);
|
Assert.AreEqual(1, testList.AddSorted(2));
|
||||||
|
|
||||||
Assert.AreEqual(4, testList.Count);
|
Assert.AreEqual(4, testList.Count);
|
||||||
Assert.AreEqual(1, testList[0]);
|
Assert.AreEqual(1, testList[0]);
|
||||||
@@ -31,10 +31,10 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
List<int> testList = new List<int>();
|
List<int> testList = new List<int>();
|
||||||
IComparer<int> comparer = new InverseComparer();
|
IComparer<int> comparer = new InverseComparer();
|
||||||
|
|
||||||
testList.AddSorted(2, comparer);
|
Assert.AreEqual(0, testList.AddSorted(2, comparer));
|
||||||
testList.AddSorted(3, comparer);
|
Assert.AreEqual(0, testList.AddSorted(3, comparer));
|
||||||
testList.AddSorted(1, comparer);
|
Assert.AreEqual(2, testList.AddSorted(1, comparer));
|
||||||
testList.AddSorted(2, comparer);
|
Assert.AreEqual(1, testList.AddSorted(2, comparer));
|
||||||
|
|
||||||
Assert.AreEqual(4, testList.Count);
|
Assert.AreEqual(4, testList.Count);
|
||||||
Assert.AreEqual(3, testList[0]);
|
Assert.AreEqual(3, testList[0]);
|
||||||
|
|||||||
@@ -82,12 +82,12 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <param name="item"></param>
|
/// <param name="item"></param>
|
||||||
[PublicAPI]
|
[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)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
extends.AddSorted(item, Comparer<T>.Default);
|
return extends.AddSorted(item, Comparer<T>.Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -98,7 +98,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="item"></param>
|
/// <param name="item"></param>
|
||||||
/// <param name="comparer"></param>
|
/// <param name="comparer"></param>
|
||||||
[PublicAPI]
|
[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)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
@@ -111,6 +111,8 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
index = ~index;
|
index = ~index;
|
||||||
|
|
||||||
extends.Insert(index, item);
|
extends.Insert(index, item);
|
||||||
|
|
||||||
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -122,7 +124,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="item"></param>
|
/// <param name="item"></param>
|
||||||
/// <param name="predicate"></param>
|
/// <param name="predicate"></param>
|
||||||
[PublicAPI]
|
[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)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
@@ -131,7 +133,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
throw new ArgumentNullException("predicate");
|
throw new ArgumentNullException("predicate");
|
||||||
|
|
||||||
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
|
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
|
||||||
extends.AddSorted(item, comparer);
|
return extends.AddSorted(item, comparer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user