Skip to content

Commit

Permalink
Add IDictionary.TryPop overload with out parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Oct 30, 2023
1 parent 7d4b6ea commit bddcbc5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
26 changes: 26 additions & 0 deletions src/Aardvark.Base.FSharp/Utilities/Interop/Dictionary.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
namespace Aardvark.Base

[<AutoOpen>]
module CSharpCollectionExtensions =
open System
open System.Runtime.CompilerServices
open System.Collections.Generic
open System.Runtime.InteropServices

type public DictionaryExtensions =

[<Obsolete("Broken. Use IDictionary.TryPop instead.")>]
[<Extension>]
static member TryRemove(x : Dictionary<'a,'b>, k,[<Out>] r: byref<'b>) =
match x.TryGetValue k with
| (true,v) -> r <- v; true
| _ -> false

[<Obsolete("Use IDictionary.GetCreate instead.")>]
[<Extension>]
static member GetOrAdd(x : Dictionary<'a,'b>, k : 'a, creator : 'a -> 'b) =
match x.TryGetValue k with
| (true,v) -> v
| _ ->
let v = creator k
x.Add(k,v) |> ignore
v

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Dictionary =
open System.Collections.Generic
Expand Down
24 changes: 0 additions & 24 deletions src/Aardvark.Base.FSharp/Utilities/Interop/FSLibExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -337,30 +337,6 @@ module CSharpInterop =

static member Create<'a,'b,'c,'d> (func:System.Func<'a,'b,'c,'d>) = FSharpFuncUtil.ToFSharpFunc func

[<AutoOpen>]
module CSharpCollectionExtensions =

open System.Runtime.CompilerServices
open System.Collections.Generic
open System.Runtime.InteropServices

type public DictionaryExtensions =

[<Extension>]
static member TryRemove(x : Dictionary<'a,'b>, k,[<Out>] r: byref<'b>) =
match x.TryGetValue k with
| (true,v) -> r <- v; true
| _ -> false

[<Extension>]
static member GetOrAdd(x : Dictionary<'a,'b>, k : 'a, creator : 'a -> 'b) =
match x.TryGetValue k with
| (true,v) -> v
| _ ->
let v = creator k
x.Add(k,v) |> ignore
v

[<AutoOpen>]
module Threading =
open System.Threading
Expand Down
19 changes: 12 additions & 7 deletions src/Aardvark.Base/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,23 @@ public static TV Pop<TK, TV>(this IDictionary<TK, TV> self, TK key)
throw new KeyNotFoundException();
}

/// <summary>
/// Removes the value with the given key from the dictionary and passes it as out argument.
/// In case the value is not found default(T) will be returned.
/// </summary>
/// <return>true if the value was removed from the dictionary, false otherwise.</return>
public static bool TryPop<TK, TV>(this IDictionary<TK, TV> self, TK key, out TV value)
=> self.TryGetValue(key, out value) && self.Remove(key);

/// <summary>
/// Removes the value with the given key from the dictionary and passes it as return argument.
/// In case the value is not found default(T) will be return.
/// In case the value is not found default(T) will be returned.
/// </summary>
/// <return>The value removed from the dictionary or default(T)</return>
public static TV TryPop<TK, TV>(this IDictionary<TK, TV> self, TK key)
{
if (self.TryGetValue(key, out TV value))
self.Remove(key);
return value;
}

=> self.TryPop(key, out TV value) ? value : default;


public static T1v[] CopyToArray<Tk, Tv, T1v>(this IDictionary<Tk, Tv> self, Func<KeyValuePair<Tk, Tv>, T1v> fun)
{
var r = new T1v[self.Count];
Expand Down

0 comments on commit bddcbc5

Please sign in to comment.