Skip to content

Commit

Permalink
Add TryAdd method.
Browse files Browse the repository at this point in the history
  • Loading branch information
koculu committed Aug 16, 2024
1 parent b2357da commit 140e702
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 140e702

Please sign in to comment.