feat: Util methods for inserting items into a sorted list by a predicate

This commit is contained in:
Chris Cameron
2018-04-17 14:05:32 -04:00
parent 23047c1c2c
commit 9e715d3790

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using ICD.Common.Properties;
using ICD.Common.Utils.Comparers;
namespace ICD.Common.Utils.Extensions
{
@@ -50,6 +51,30 @@ namespace ICD.Common.Utils.Extensions
items.ForEach(i => extends.AddSorted(i, comparer));
}
/// <summary>
/// Adds the items into a sorted list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TProp"></typeparam>
/// <param name="extends"></param>
/// <param name="items"></param>
/// <param name="predicate"></param>
[PublicAPI]
public static void AddSorted<T, TProp>(this List<T> extends, IEnumerable<T> items, Func<T, TProp> predicate)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (items == null)
throw new ArgumentNullException("items");
if (predicate == null)
throw new ArgumentNullException("predicate");
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
extends.AddSorted(items, comparer);
}
/// <summary>
/// Adds the item into a sorted list.
/// </summary>
@@ -106,6 +131,27 @@ namespace ICD.Common.Utils.Extensions
extends.Insert(index, item);
}
/// <summary>
/// Adds the item into a sorted list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TProp"></typeparam>
/// <param name="extends"></param>
/// <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)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (predicate == null)
throw new ArgumentNullException("predicate");
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
extends.AddSorted(item, comparer);
}
/// <summary>
/// Pads the list to the given total length.
/// </summary>