2026-02-01 20:52:23 +01:00
|
|
|
|
2026-05-07 07:44:55 +02:00
|
|
|
namespace PersistentOrderedMap;
|
2026-02-01 20:52:23 +01:00
|
|
|
|
2026-05-21 13:13:22 +02:00
|
|
|
public sealed class PersistentOrderedMap<TK, TV, TStrategy> : BaseOrderedMap<TK, TV, TStrategy> where TStrategy : IKeyStrategy<TK>
|
2026-02-01 20:52:23 +01:00
|
|
|
{
|
2026-05-21 13:13:22 +02:00
|
|
|
internal PersistentOrderedMap(Node<TK> root, TStrategy strategy, int count)
|
2026-02-01 20:52:23 +01:00
|
|
|
: base(root, strategy, count) { }
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------
|
|
|
|
|
// Immutable Write API (Returns new Map)
|
|
|
|
|
// ---------------------------------------------------------
|
2026-05-21 13:13:22 +02:00
|
|
|
public PersistentOrderedMap<TK, TV, TStrategy> Set(TK key, TV value)
|
2026-02-01 20:52:23 +01:00
|
|
|
{
|
|
|
|
|
// OPTIMIZATION: Use OwnerId.None (0).
|
|
|
|
|
// This signals EnsureEditable to always copy the root path,
|
|
|
|
|
// producing a new tree of nodes that also have OwnerId.None.
|
2026-05-21 13:13:22 +02:00
|
|
|
var newRoot = BTreeFunctions.Set(Root, key, value, Strategy, OwnerId.None, out bool countChanged);
|
|
|
|
|
return new PersistentOrderedMap<TK, TV, TStrategy>(newRoot, Strategy, countChanged ? Count + 1 : Count);
|
2026-02-01 20:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-21 13:13:22 +02:00
|
|
|
public static PersistentOrderedMap<TK, TV, TStrategy> Empty(TStrategy strategy)
|
2026-02-01 20:52:23 +01:00
|
|
|
{
|
|
|
|
|
// Create an empty Leaf Node.
|
|
|
|
|
// 'default(OwnerId)' (usually 0) marks this node as Immutable/Persistent.
|
|
|
|
|
// This ensures that any subsequent Set/Remove will clone this node
|
|
|
|
|
// instead of modifying it in place.
|
2026-05-21 13:13:22 +02:00
|
|
|
var emptyRoot = new LeafNode<TK, TV>(default(OwnerId), strategy.UsesPrefixes);
|
2026-02-01 20:52:23 +01:00
|
|
|
|
2026-05-21 13:13:22 +02:00
|
|
|
return new PersistentOrderedMap<TK, TV, TStrategy>(emptyRoot, strategy, 0);
|
2026-02-01 20:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-21 13:13:22 +02:00
|
|
|
public PersistentOrderedMap<TK, TV, TStrategy> Remove(TK key)
|
2026-02-01 20:52:23 +01:00
|
|
|
{
|
2026-05-21 13:13:22 +02:00
|
|
|
var newRoot = BTreeFunctions.Remove<TK,TV, TStrategy>(Root, key, Strategy, OwnerId.None, out bool removed);
|
2026-02-11 12:56:48 +01:00
|
|
|
if (!removed) return this;
|
2026-05-21 13:13:22 +02:00
|
|
|
return new PersistentOrderedMap<TK, TV, TStrategy>(newRoot, Strategy, Count - 1);
|
2026-02-01 20:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-21 13:13:22 +02:00
|
|
|
public TransientOrderedMap<TK, TV, TStrategy> ToTransient()
|
2026-02-01 20:52:23 +01:00
|
|
|
{
|
2026-05-21 13:13:22 +02:00
|
|
|
return new TransientOrderedMap<TK, TV, TStrategy>(Root, Strategy, Count);
|
2026-02-01 20:52:23 +01:00
|
|
|
}
|
|
|
|
|
}
|