Skip to content

Commit 6a3f96f

Browse files
authored
Merge pull request #76 from koculu/75-enhancement-add-tryadd-method
Add TryAdd method.
2 parents b2357da + 140e702 commit 6a3f96f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/ZoneTree/Core/ZoneTree.ReadWrite.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public bool TryGet(in TKey key, out TValue value)
6161
return TryGetFromReadonlySegments(in key, out value);
6262
}
6363

64+
public bool TryAdd(in TKey key, in TValue value)
65+
{
66+
if (ContainsKey(key))
67+
return false;
68+
Upsert(in key, in value);
69+
return true;
70+
}
71+
6472
public bool TryGetAndUpdate(in TKey key, out TValue value, ValueUpdaterDelegate<TValue> valueUpdater)
6573
{
6674
if (IsReadOnly)

src/ZoneTree/IZoneTree.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,21 @@ public interface IZoneTree<TKey, TValue> : IDisposable
3434
bool ContainsKey(in TKey key);
3535

3636
/// <summary>
37-
/// Tries to get the value of the given key.
37+
/// Tries to retrieve the value associated with the specified key.
3838
/// </summary>
39-
/// <param name="key">The key of the element.</param>
39+
/// <param name="key">The key of the element to retrieve.</param>
4040
/// <param name="value">The value of the element associated with the key.</param>
4141
/// <returns>true if the key is found; otherwise, false</returns>
4242
bool TryGet(in TKey key, out TValue value);
4343

44+
/// <summary>
45+
/// Tries to add the specified key and value to the collection.
46+
/// </summary>
47+
/// <param name="key">The key of the element to add.</param>
48+
/// <param name="value">The value of the element to add.</param>
49+
/// <returns>true if the key and value were added successfully; otherwise, false if the key already exists.</returns>
50+
bool TryAdd(in TKey key, in TValue value);
51+
4452
/// <summary>
4553
/// Tries to get the value of the given key and
4654
/// updates the value using value updater if found any.

0 commit comments

Comments
 (0)