Add a standard key strategy for maps without prefixe
Remove the stupid "next" field
This commit is contained in:
parent
7bea233edc
commit
4d87e30b40
3 changed files with 48 additions and 18 deletions
|
|
@ -21,6 +21,37 @@ public interface IKeyStrategy<K>
|
|||
}
|
||||
|
||||
|
||||
/// <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(IComparer<K>? comparer = null)
|
||||
{
|
||||
_comparer = comparer ?? Comparer<K>.Default;
|
||||
}
|
||||
|
||||
// Tell the B-Tree to skip SIMD routing and just use LinearSearch
|
||||
public bool UsesPrefixes => false;
|
||||
|
||||
// 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 struct UnicodeStrategy : IKeyStrategy<string>
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
|
@ -120,7 +151,7 @@ public static class PrefixScanner
|
|||
//if (targetPrefix == long.MinValue)
|
||||
//{
|
||||
// return 0;
|
||||
//}
|
||||
//}
|
||||
|
||||
// Fallback for short arrays or unsupported hardware
|
||||
if (!Avx2.IsSupported || prefixes.Length < 4)
|
||||
|
|
|
|||
|
|
@ -91,7 +91,6 @@ public sealed class LeafNode<K, V> : Node<K>
|
|||
|
||||
public K[]? Keys;
|
||||
public V[] Values;
|
||||
public LeafNode<K, V>? Next;
|
||||
|
||||
internal long[]? _prefixes;
|
||||
|
||||
|
|
@ -111,9 +110,7 @@ public sealed class LeafNode<K, V> : Node<K>
|
|||
{
|
||||
Keys = new K[Capacity];
|
||||
Values = new V[Capacity];
|
||||
Header.Count = original.Header.Count;
|
||||
Next = original.Next;
|
||||
_prefixes = new long[Capacity];
|
||||
Header.Count = original.Header.Count; _prefixes = new long[Capacity];
|
||||
|
||||
// Copy data
|
||||
Array.Copy(original.Keys, Keys, original.Header.Count);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
* NiceBtree (PersistentMap)
|
||||
* PersistentMap
|
||||
|
||||
A high-performance, persistent (Copy-on-Write) B+ Tree implemented in C#.
|
||||
|
||||
|
|
@ -14,6 +14,8 @@ It is designed for zero-overhead reads, SIMD-accelerated key routing, and alloca
|
|||
** When should I use this?
|
||||
Never, probably. This was just a fun little project. If you want a really fast immutable sorted map you should consider it. Despite this map being faster than LanguageExt.HashMap for some key types, you should definitely use that if you don't need a sorted collection. It is well tested and does not have any problems key collisions, which will slow this map down by a lot.
|
||||
|
||||
The general version of this, using =StandardStrategy<K>= does not benefit from the prefix optimization.
|
||||
|
||||
** Quick Start
|
||||
|
||||
*** 1. Basic Immutable Usage
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue