53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
namespace PersistentOrderedMap;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
/// <summary>
|
|
/// A universal key strategy for any type that relies on standard comparisons
|
|
/// (IComparable, IComparer, or custom StringComparers) without SIMD prefixes.
|
|
/// </summary>
|
|
public readonly struct StandardStrategy<TK> : IKeyStrategy<TK>
|
|
{
|
|
private readonly IComparer<TK> _comparer;
|
|
|
|
// If no comparer is provided, it defaults to Comparer<K>.Default
|
|
// which automatically uses IComparable<K> if the type implements it.
|
|
|
|
public StandardStrategy()
|
|
{
|
|
_comparer = Comparer<TK>.Default;
|
|
}
|
|
|
|
public StandardStrategy(IComparer<TK>? comparer)
|
|
{
|
|
_comparer = comparer ?? Comparer<TK>.Default;
|
|
}
|
|
// Tell the B-Tree to skip SIMD routing and just use LinearSearch
|
|
public bool UsesPrefixes => false;
|
|
public bool UseBinarySearch => true;
|
|
// This will never be called because UsesPrefixes is false,
|
|
// but we must satisfy the interface.
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public long GetPrefix(TK key) => 0;
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public int Compare(TK x, TK y)
|
|
{
|
|
return _comparer.Compare(x, y);
|
|
}
|
|
}
|
|
public readonly struct StandardStrategy2<TK, TComparer> : IKeyStrategy<TK>
|
|
where TComparer : struct, IComparer<TK>
|
|
{
|
|
private readonly TComparer _comparer;
|
|
|
|
public StandardStrategy2(TComparer comparer) => _comparer = comparer;
|
|
|
|
public bool UsesPrefixes => false;
|
|
public bool UseBinarySearch => true;
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public int Compare(TK x, TK y) => _comparer.Compare(x, y);
|
|
|
|
public long GetPrefix(TK key) => 0;
|
|
}
|
|
|