diff --git a/BizHawk.MultiClient/tools/Watch/RamSearchEngine.cs b/BizHawk.MultiClient/tools/Watch/RamSearchEngine.cs index 9658c5e795..dafbf9fe61 100644 --- a/BizHawk.MultiClient/tools/Watch/RamSearchEngine.cs +++ b/BizHawk.MultiClient/tools/Watch/RamSearchEngine.cs @@ -8,8 +8,47 @@ using System.Threading.Tasks; namespace BizHawk.MultiClient { + //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 }; public enum Compare { Previous, SpecificValue, SpecificAddress, Changes, Difference } @@ -175,7 +214,11 @@ namespace BizHawk.MultiClient public bool Preview(int address) { - var listOfOne = new List() { _watchList.FirstOrDefault(x => x.Address == address) }; + var listOfOne = new List + { + _watchList.BinarySearch(x => x.Address, address) + }; + switch (_compareTo) { default: