From aa17f293057039374f52d1741012ee3942658759 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 13 Sep 2019 16:35:38 -0400 Subject: [PATCH] feat: Adding BinarySearch extension method for ILists --- ICD.Common.Utils/Extensions/ListExtensions.cs | 58 +++++++++++++++---- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/ICD.Common.Utils/Extensions/ListExtensions.cs b/ICD.Common.Utils/Extensions/ListExtensions.cs index 04d32b1..b7d8298 100644 --- a/ICD.Common.Utils/Extensions/ListExtensions.cs +++ b/ICD.Common.Utils/Extensions/ListExtensions.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using ICD.Common.Properties; using ICD.Common.Utils.Comparers; @@ -18,7 +17,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void AddSorted(this List extends, IEnumerable items) + public static void AddSorted(this IList extends, IEnumerable items) { if (extends == null) throw new ArgumentNullException("extends"); @@ -37,7 +36,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void AddSorted(this List extends, IEnumerable items, IComparer comparer) + public static void AddSorted(this IList extends, IEnumerable items, IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -60,7 +59,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void AddSorted(this List extends, IEnumerable items, Func predicate) + public static void AddSorted(this IList extends, IEnumerable items, Func predicate) { if (extends == null) throw new ArgumentNullException("extends"); @@ -82,7 +81,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static int AddSorted(this List extends, T item) + public static int AddSorted(this IList extends, T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -98,7 +97,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static int AddSorted(this List extends, T item, IComparer comparer) + public static int AddSorted(this IList extends, T item, IComparer comparer) { if (extends == null) throw new ArgumentNullException("extends"); @@ -124,7 +123,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static int AddSorted(this List extends, T item, Func predicate) + public static int AddSorted(this IList extends, T item, Func predicate) { if (extends == null) throw new ArgumentNullException("extends"); @@ -143,7 +142,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void PadRight(this List extends, int totalLength) + public static void PadRight(this IList extends, int totalLength) { if (extends == null) throw new ArgumentNullException("extends"); @@ -162,7 +161,7 @@ namespace ICD.Common.Utils.Extensions /// /// [PublicAPI] - public static void PadRight(this List extends, int totalLength, T item) + public static void PadRight(this IList extends, int totalLength, T item) { if (extends == null) throw new ArgumentNullException("extends"); @@ -171,10 +170,45 @@ namespace ICD.Common.Utils.Extensions throw new ArgumentOutOfRangeException("totalLength", "totalLength must be greater or equal to 0"); int pad = totalLength - extends.Count; - if (pad <= 0) - return; + for (int index = 0; index < pad; index++) + extends.Add(item); + } - extends.AddRange(Enumerable.Repeat(item, pad)); + /// + /// Returns the index of the item in the list. + /// + /// + /// + /// + /// + /// + [PublicAPI] + public static int BinarySearch(this IList extends, T item, IComparer comparer) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (comparer == null) + throw new ArgumentNullException("comparer"); + + int lo = 0; + int hi = extends.Count - 1; + + while (lo <= hi) + { + int i = lo + ((hi - lo) >> 1); + int order = comparer.Compare(extends[i], item); + + if (order == 0) + return i; + + if (order < 0) + lo = i + 1; + else + hi = i - 1; + } + + return ~lo; } } }