39 lines
No EOL
1.3 KiB
C#
39 lines
No EOL
1.3 KiB
C#
|
|
namespace PersistentOrderedMap;
|
|
|
|
public sealed class TransientOrderedMap<TK, TV, TStrategy> : BaseOrderedMap<TK, TV, TStrategy> where TStrategy : IKeyStrategy<TK>
|
|
{
|
|
// This is mutable, but we treat it as readonly for the ID generation logic usually.
|
|
private OwnerId _transactionId;
|
|
|
|
public TransientOrderedMap(Node<TK> root, TStrategy strategy, int count)
|
|
: base(root, strategy, count)
|
|
{
|
|
_transactionId = OwnerId.Next();
|
|
}
|
|
|
|
public void Set(TK key, TV value)
|
|
{
|
|
Root = BTreeFunctions.Set(Root, key, value, Strategy, _transactionId, out bool countChanged);
|
|
if (countChanged) Count++;
|
|
}
|
|
|
|
public void Remove(TK key)
|
|
{
|
|
Root = BTreeFunctions.Remove<TK,TV, TStrategy>(Root, key, Strategy, _transactionId, out bool removed);
|
|
if (removed) Count--;
|
|
}
|
|
|
|
public PersistentOrderedMap<TK, TV, TStrategy> ToPersistent()
|
|
{
|
|
// 1. Create the snapshot by copying all relevant information
|
|
|
|
var snapshot = new PersistentOrderedMap<TK, TV, TStrategy>(Root, Strategy, Count);
|
|
|
|
// 2. Protect the snapshot from THIS TransientOrderedMap by getting a new ownerId
|
|
// so that future edits will be done by CoW
|
|
_transactionId = OwnerId.Next();
|
|
|
|
return snapshot;
|
|
}
|
|
} |