mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 12:32:20 +02:00
Allow binary search with alt key
This commit is contained in:
parent
5851653a19
commit
85279be033
2 changed files with 24 additions and 0 deletions
|
@ -92,6 +92,25 @@ namespace System
|
|||
return ~lo;
|
||||
}
|
||||
|
||||
public static int BinarySearchAlt<T, TAlt>(T* arr, int length, TAlt value, delegate int(T lhs, TAlt rhs) comp)
|
||||
{
|
||||
int lo = 0;
|
||||
int hi = length - 1;
|
||||
|
||||
while (lo <= hi)
|
||||
{
|
||||
int i = (lo + hi) / 2;
|
||||
T midVal = arr[i];
|
||||
int c = comp(midVal, value);
|
||||
if (c == 0) return i;
|
||||
if (c < 0)
|
||||
lo = i + 1;
|
||||
else
|
||||
hi = i - 1;
|
||||
}
|
||||
return ~lo;
|
||||
}
|
||||
|
||||
public static int BinarySearch<T>(T[] arr, T value, delegate int(T lhs, T rhs) comp)
|
||||
{
|
||||
return BinarySearch(&arr.[Friend]GetRef(0), arr.mLength, value, comp);
|
||||
|
|
|
@ -689,6 +689,11 @@ namespace System.Collections
|
|||
return Array.BinarySearch(mItems, Count, item, comparer);
|
||||
}
|
||||
|
||||
public int BinarySearchAlt<TAlt>(TAlt item, delegate int(T lhs, TAlt rhs) comparer)
|
||||
{
|
||||
return Array.BinarySearchAlt(mItems, Count, item, comparer);
|
||||
}
|
||||
|
||||
public int BinarySearch(int index, int count, T item, delegate int(T lhs, T rhs) comparer)
|
||||
{
|
||||
Debug.Assert((uint)index <= (uint)mSize);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue