using System.Collections; namespace PersistentOrderedMap; public sealed class TransientOrderedMap : BaseOrderedMap where TStrategy : IKeyStrategy { // This is mutable, but we treat it as readonly for the ID generation logic usually. private OwnerId _transactionId; public TransientOrderedMap(Node 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(_root, key, _strategy, _transactionId, out bool removed); if (removed) Count--; } public PersistentOrderedMap ToPersistent() { // 1. Create the snapshot by copying all relevant information var snapshot = new PersistentOrderedMap(_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; } }