PersistentMap/PersistentOrderedMap/KeyStrategies/StandardStrategy.cs

54 lines
1.8 KiB
C#
Raw Normal View History

2026-05-07 07:44:55 +02:00
namespace PersistentOrderedMap;
2026-04-21 08:41:59 +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>
public readonly struct StandardStrategy<K> : IKeyStrategy<K>
{
private readonly IComparer<K> _comparer;
// If no comparer is provided, it defaults to Comparer<K>.Default
// which automatically uses IComparable<K> if the type implements it.
public StandardStrategy()
2026-04-21 08:41:59 +02:00
{
_comparer = Comparer<K>.Default;
2026-04-21 08:41:59 +02:00
}
public StandardStrategy(IComparer<K>? comparer)
{
_comparer = comparer ?? Comparer<K>.Default;
}
2026-04-21 08:41:59 +02:00
// Tell the B-Tree to skip SIMD routing and just use LinearSearch
public bool UsesPrefixes => false;
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)]
public long GetPrefix(K key) => 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(K x, K y)
{
return _comparer.Compare(x, y);
}
}
public readonly struct StandardStrategy2<K, TComparer> : IKeyStrategy<K>
where TComparer : struct, IComparer<K>
{
private readonly TComparer _comparer;
public StandardStrategy2(TComparer comparer) => _comparer = comparer;
public bool UsesPrefixes => false;
public bool UseBinarySearch => true;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(K x, K y) => _comparer.Compare(x, y);
public long GetPrefix(K key) => 0;
}