Skip to content

Commit

Permalink
Merge pull request #76 from koculu/75-enhancement-add-tryadd-method
Browse files Browse the repository at this point in the history
Add TryAdd method.
  • Loading branch information
koculu authored Aug 16, 2024
2 parents b2357da + 140e702 commit 6a3f96f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/ZoneTree/Core/ZoneTree.ReadWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TValue> valueUpdater)
{
if (IsReadOnly)
Expand Down
12 changes: 10 additions & 2 deletions src/ZoneTree/IZoneTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@ public interface IZoneTree<TKey, TValue> : IDisposable
bool ContainsKey(in TKey key);

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

/// <summary>
/// Tries to add the specified key and value to the collection.
/// </summary>
/// <param name="key">The key of the element to add.</param>
/// <param name="value">The value of the element to add.</param>
/// <returns>true if the key and value were added successfully; otherwise, false if the key already exists.</returns>
bool TryAdd(in TKey key, in TValue value);

/// <summary>
/// Tries to get the value of the given key and
/// updates the value using value updater if found any.
Expand Down

0 comments on commit 6a3f96f

Please sign in to comment.