diff --git a/src/ZoneTree/Core/ZoneTree.ReadWrite.cs b/src/ZoneTree/Core/ZoneTree.ReadWrite.cs index 23530c7..a4ccde0 100644 --- a/src/ZoneTree/Core/ZoneTree.ReadWrite.cs +++ b/src/ZoneTree/Core/ZoneTree.ReadWrite.cs @@ -61,6 +61,14 @@ public bool TryGet(in TKey key, out TValue value) return TryGetFromReadonlySegments(in key, out value); } + public bool TryAdd(in TKey key, in TValue value) + { + if (ContainsKey(key)) + return false; + Upsert(in key, in value); + return true; + } + public bool TryGetAndUpdate(in TKey key, out TValue value, ValueUpdaterDelegate valueUpdater) { if (IsReadOnly) diff --git a/src/ZoneTree/IZoneTree.cs b/src/ZoneTree/IZoneTree.cs index bea1f6a..097f85f 100644 --- a/src/ZoneTree/IZoneTree.cs +++ b/src/ZoneTree/IZoneTree.cs @@ -34,13 +34,21 @@ public interface IZoneTree : IDisposable bool ContainsKey(in TKey key); /// - /// Tries to get the value of the given key. + /// Tries to retrieve the value associated with the specified key. /// - /// The key of the element. + /// The key of the element to retrieve. /// The value of the element associated with the key. /// true if the key is found; otherwise, false bool TryGet(in TKey key, out TValue value); + /// + /// Tries to add the specified key and value to the collection. + /// + /// The key of the element to add. + /// The value of the element to add. + /// true if the key and value were added successfully; otherwise, false if the key already exists. + bool TryAdd(in TKey key, in TValue value); + /// /// Tries to get the value of the given key and /// updates the value using value updater if found any.