mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-12 13:24:09 +02:00
Allow method reference for Sort comparer
This commit is contained in:
parent
1c310e1bec
commit
bdd034952a
4 changed files with 54 additions and 10 deletions
|
@ -159,19 +159,40 @@ namespace System
|
||||||
|
|
||||||
public static void Sort<T>(T[] array, Comparison<T> comp)
|
public static void Sort<T>(T[] array, Comparison<T> comp)
|
||||||
{
|
{
|
||||||
var sorter = Sorter<T, void>(&array.[Friend]mFirstElement, null, array.[Friend]mLength, comp);
|
var sorter = Sorter<T, void, Comparison<T>>(&array.[Friend]mFirstElement, null, array.[Friend]mLength, comp);
|
||||||
|
sorter.[Friend]Sort(0, array.[Friend]mLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Sort<T, TComparer>(T[] array, TComparer comp)
|
||||||
|
where TComparer : Comparison<T>
|
||||||
|
{
|
||||||
|
var sorter = Sorter<T, void, TComparer>(&array.[Friend]mFirstElement, null, array.[Friend]mLength, comp);
|
||||||
sorter.[Friend]Sort(0, array.[Friend]mLength);
|
sorter.[Friend]Sort(0, array.[Friend]mLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Sort<T, T2>(T[] keys, T2[] items, Comparison<T> comp)
|
public static void Sort<T, T2>(T[] keys, T2[] items, Comparison<T> comp)
|
||||||
{
|
{
|
||||||
var sorter = Sorter<T, T2>(&keys.[Friend]mFirstElement, &items.[Friend]mFirstElement, keys.[Friend]mLength, comp);
|
var sorter = Sorter<T, T2, Comparison<T>>(&keys.[Friend]mFirstElement, &items.[Friend]mFirstElement, keys.[Friend]mLength, comp);
|
||||||
|
sorter.[Friend]Sort(0, keys.[Friend]mLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Sort<T, T2, TComparer>(T[] keys, T2[] items, Comparison<T> comp)
|
||||||
|
where TComparer : Comparison<T>
|
||||||
|
{
|
||||||
|
var sorter = Sorter<T, T2, TComparer>(&keys.[Friend]mFirstElement, &items.[Friend]mFirstElement, keys.[Friend]mLength, comp);
|
||||||
sorter.[Friend]Sort(0, keys.[Friend]mLength);
|
sorter.[Friend]Sort(0, keys.[Friend]mLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Sort<T>(T[] array, int index, int count, Comparison<T> comp)
|
public static void Sort<T>(T[] array, int index, int count, Comparison<T> comp)
|
||||||
{
|
{
|
||||||
var sorter = Sorter<T, void>(&array.[Friend]mFirstElement, null, array.[Friend]mLength, comp);
|
var sorter = Sorter<T, void, Comparison<T>>(&array.[Friend]mFirstElement, null, array.[Friend]mLength, comp);
|
||||||
|
sorter.[Friend]Sort(index, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Sort<T, TComparer>(T[] array, int index, int count, Comparison<T> comp)
|
||||||
|
where TComparer : Comparison<T>
|
||||||
|
{
|
||||||
|
var sorter = Sorter<T, void, TComparer>(&array.[Friend]mFirstElement, null, array.[Friend]mLength, comp);
|
||||||
sorter.[Friend]Sort(index, count);
|
sorter.[Friend]Sort(index, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -789,14 +789,29 @@ namespace System.Collections
|
||||||
|
|
||||||
public void Sort(Comparison<T> comp)
|
public void Sort(Comparison<T> comp)
|
||||||
{
|
{
|
||||||
var sorter = Sorter<T, void>(mItems, null, mSize, comp);
|
var sorter = Sorter<T, void, Comparison<T>>(mItems, null, mSize, comp);
|
||||||
|
sorter.[Friend]Sort(0, mSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Sort<TComparer>(TComparer comp)
|
||||||
|
where TComparer : Comparison<T>
|
||||||
|
{
|
||||||
|
var sorter = Sorter<T, void, TComparer>(mItems, null, mSize, comp);
|
||||||
sorter.[Friend]Sort(0, mSize);
|
sorter.[Friend]Sort(0, mSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Sort(Comparison<T> comp, int index, int count)
|
public void Sort(Comparison<T> comp, int index, int count)
|
||||||
{
|
{
|
||||||
Debug.Assert((uint)index + (uint)count <= (uint)mSize);
|
Debug.Assert((uint)index + (uint)count <= (uint)mSize);
|
||||||
var sorter = Sorter<T, void>(mItems, null, mSize, comp);
|
var sorter = Sorter<T, void, Comparison<T>>(mItems, null, mSize, comp);
|
||||||
|
sorter.[Friend]Sort(index, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Sort<TComparer>(TComparer comp, int index, int count)
|
||||||
|
where TComparer : Comparison<T>
|
||||||
|
{
|
||||||
|
Debug.Assert((uint)index + (uint)count <= (uint)mSize);
|
||||||
|
var sorter = Sorter<T, void, TComparer>(mItems, null, mSize, comp);
|
||||||
sorter.[Friend]Sort(index, count);
|
sorter.[Friend]Sort(index, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1137,7 +1152,7 @@ namespace System.Collections
|
||||||
|
|
||||||
public void Sort()
|
public void Sort()
|
||||||
{
|
{
|
||||||
Sort(scope (lhs, rhs) => lhs <=> rhs);
|
Sort((lhs, rhs) => lhs <=> rhs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
|
|
||||||
namespace System.Collections
|
namespace System.Collections
|
||||||
{
|
{
|
||||||
struct Sorter<T, T2>
|
struct Sorter<T, T2, TComparer>
|
||||||
|
where TComparer : Comparison<T>
|
||||||
{
|
{
|
||||||
// This is the threshold where Introspective sort switches to Insertion sort.
|
// This is the threshold where Introspective sort switches to Insertion sort.
|
||||||
// Empirically, 16 seems to speed up most cases without slowing down others, at least for integers.
|
// Empirically, 16 seems to speed up most cases without slowing down others, at least for integers.
|
||||||
|
@ -14,9 +15,9 @@ namespace System.Collections
|
||||||
private T* keys;
|
private T* keys;
|
||||||
private T2* items;
|
private T2* items;
|
||||||
private int mCount;
|
private int mCount;
|
||||||
private Comparison<T> comparer;
|
private TComparer comparer;
|
||||||
|
|
||||||
public this(T* keys, T2* items, int count, Comparison<T> comparer)
|
public this(T* keys, T2* items, int count, TComparer comparer)
|
||||||
{
|
{
|
||||||
this.keys = keys;
|
this.keys = keys;
|
||||||
this.items = items;
|
this.items = items;
|
||||||
|
|
|
@ -301,7 +301,14 @@ namespace System
|
||||||
|
|
||||||
public void Sort(Comparison<T> comp)
|
public void Sort(Comparison<T> comp)
|
||||||
{
|
{
|
||||||
var sorter = Sorter<T, void>(Ptr, null, Length, comp);
|
var sorter = Sorter<T, void, Comparison<T>>(Ptr, null, Length, comp);
|
||||||
|
sorter.[Friend]Sort(0, Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Sort<TComparer>(TComparer comp)
|
||||||
|
where TComparer : Comparison<T>
|
||||||
|
{
|
||||||
|
var sorter = Sorter<T, void, TComparer>(Ptr, null, Length, comp);
|
||||||
sorter.[Friend]Sort(0, Length);
|
sorter.[Friend]Sort(0, Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue