mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-11 18:54:55 +00:00
Adding list extension methods for adding items to a sorted list
This commit is contained in:
54
ICD.Common.Utils.Tests/Extensions/ListExtensionsTest.cs
Normal file
54
ICD.Common.Utils.Tests/Extensions/ListExtensionsTest.cs
Normal file
@@ -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<int> testList = new List<int>();
|
||||
|
||||
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<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(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<int>
|
||||
{
|
||||
public int Compare(int x, int y)
|
||||
{
|
||||
return y.CompareTo(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
68
ICD.Common.Utils/Extensions/ListExtensions.cs
Normal file
68
ICD.Common.Utils/Extensions/ListExtensions.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for working with Lists.
|
||||
/// </summary>
|
||||
public static class ListExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds the item into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
[PublicAPI]
|
||||
public static void AddSorted<T>(this List<T> extends, T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
extends.AddSorted(item, Comparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the item into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="comparer"></param>
|
||||
[PublicAPI]
|
||||
public static void AddSorted<T>(this List<T> extends, T item, IComparer<T> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,7 @@
|
||||
<Compile Include="EventArguments\XmlRecursionEventArgs.cs" />
|
||||
<None Include="ObfuscationSettings.cs" />
|
||||
<Compile Include="Extensions\ByteExtensions.cs" />
|
||||
<Compile Include="Extensions\ListExtensions.cs" />
|
||||
<Compile Include="Properties\Annotations.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Services\Logging\ILoggerService.cs" />
|
||||
|
||||
Reference in New Issue
Block a user