Ram Search - use binary search to grealtly speed up preview when scrolling deep into the list

This commit is contained in:
adelikat 2013-10-07 18:32:53 +00:00
parent 4a1d9ee576
commit 0645a216b7
1 changed files with 44 additions and 1 deletions

View File

@ -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<T, TKey>(this IList<T> list, Func<T, TKey> keySelector, TKey key)
where TKey : IComparable<TKey>
{
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<IMiniWatch>() { _watchList.FirstOrDefault(x => x.Address == address) };
var listOfOne = new List<IMiniWatch>
{
_watchList.BinarySearch(x => x.Address, address)
};
switch (_compareTo)
{
default: