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(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(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; } }