move a search extension out of RamSearchEngine into the extensions class in BizHawk.Common
This commit is contained in:
parent
be547db4a1
commit
bc5807a07c
|
@ -7,43 +7,6 @@ using BizHawk.Emulation.Common;
|
||||||
|
|
||||||
namespace BizHawk.Client.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<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 class RamSearchEngine
|
||||||
{
|
{
|
||||||
public enum ComparisonOperator { Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, NotEqual, DifferentBy };
|
public enum ComparisonOperator { Equal, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, NotEqual, DifferentBy };
|
||||||
|
|
|
@ -71,6 +71,39 @@ namespace BizHawk.Common
|
||||||
return string.Format("{0:X" + numdigits + "}", n);
|
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<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 static void CopyTo(this Stream src, Stream dest)
|
public static void CopyTo(this Stream src, Stream dest)
|
||||||
{
|
{
|
||||||
int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000;
|
int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000;
|
||||||
|
|
Loading…
Reference in New Issue