From bc5807a07c14ed338e3f83b91cedc422fae6463c Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 4 Nov 2013 02:18:40 +0000 Subject: [PATCH] move a search extension out of RamSearchEngine into the extensions class in BizHawk.Common --- .../tools/RamSearchEngine.cs | 37 ------------------- BizHawk.Common/Extensions.cs | 33 +++++++++++++++++ 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/BizHawk.Client.Common/tools/RamSearchEngine.cs b/BizHawk.Client.Common/tools/RamSearchEngine.cs index 6a1f4bf070..b7855ee10f 100644 --- a/BizHawk.Client.Common/tools/RamSearchEngine.cs +++ b/BizHawk.Client.Common/tools/RamSearchEngine.cs @@ -7,43 +7,6 @@ using BizHawk.Emulation.Common; namespace BizHawk.Client.Common { - //TODO: move me - //http://stackoverflow.com/questions/1766328/can-linq-use-binary-search-when-the-collection-is-ordered - public static class BinarySearchClass - { - public static T BinarySearch(this IList list, Func keySelector, TKey key) - where TKey : IComparable - { - int min = 0; - int max = list.Count; - while (min < max) - { - int mid = (max + min) / 2; - T midItem = list[mid]; - TKey midKey = keySelector(midItem); - int comp = midKey.CompareTo(key); - if (comp < 0) - { - min = mid + 1; - } - else if (comp > 0) - { - max = mid - 1; - } - else - { - return midItem; - } - } - if (min == max && - keySelector(list[min]).CompareTo(key) == 0) - { - return list[min]; - } - throw new InvalidOperationException("Item not found"); - } - } - public class RamSearchEngine { public enum ComparisonOperator { Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, NotEqual, DifferentBy }; diff --git a/BizHawk.Common/Extensions.cs b/BizHawk.Common/Extensions.cs index af95b77589..4ce0922a7b 100644 --- a/BizHawk.Common/Extensions.cs +++ b/BizHawk.Common/Extensions.cs @@ -71,6 +71,39 @@ namespace BizHawk.Common return string.Format("{0:X" + numdigits + "}", n); } + //http://stackoverflow.com/questions/1766328/can-linq-use-binary-search-when-the-collection-is-ordered + public static T BinarySearch(this IList list, Func keySelector, TKey key) + where TKey : IComparable + { + int min = 0; + int max = list.Count; + while (min < max) + { + int mid = (max + min) / 2; + T midItem = list[mid]; + TKey midKey = keySelector(midItem); + int comp = midKey.CompareTo(key); + if (comp < 0) + { + min = mid + 1; + } + else if (comp > 0) + { + max = mid - 1; + } + else + { + return midItem; + } + } + if (min == max && + keySelector(list[min]).CompareTo(key) == 0) + { + return list[min]; + } + throw new InvalidOperationException("Item not found"); + } + public static void CopyTo(this Stream src, Stream dest) { int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000;