40 lines
No EOL
1.3 KiB
C#
40 lines
No EOL
1.3 KiB
C#
using System.Collections;
|
|
|
|
namespace PersistentOrderedMap;
|
|
|
|
public sealed class TransientOrderedMap<K, V, TStrategy> : BaseOrderedMap<K, V, TStrategy> where TStrategy : IKeyStrategy<K>
|
|
{
|
|
// This is mutable, but we treat it as readonly for the ID generation logic usually.
|
|
private OwnerId _transactionId;
|
|
|
|
public TransientOrderedMap(Node<K> root, TStrategy strategy, int count)
|
|
: base(root, strategy, count)
|
|
{
|
|
_transactionId = OwnerId.Next();
|
|
}
|
|
|
|
public void Set(K key, V value)
|
|
{
|
|
_root = BTreeFunctions.Set(_root, key, value, _strategy, _transactionId, out bool countChanged);
|
|
if (countChanged) Count++;
|
|
}
|
|
|
|
public void Remove(K key)
|
|
{
|
|
_root = BTreeFunctions.Remove<K,V, TStrategy>(_root, key, _strategy, _transactionId, out bool removed);
|
|
if (removed) Count--;
|
|
}
|
|
|
|
public PersistentOrderedMap<K, V, TStrategy> ToPersistent()
|
|
{
|
|
// 1. Create the snapshot by copying all relevant information
|
|
|
|
var snapshot = new PersistentOrderedMap<K, V, 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;
|
|
}
|
|
} |