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