From a235fd3087eab6cf671c01c10ffc2b8ed20752aa Mon Sep 17 00:00:00 2001 From: muehlhaus Date: Mon, 31 Jul 2023 21:16:00 +0200 Subject: [PATCH] Add dictionary copy and deepCopy --- src/FSharpAux.Core/Dictionary.fs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/FSharpAux.Core/Dictionary.fs b/src/FSharpAux.Core/Dictionary.fs index c8c2a7a..24a6116 100644 --- a/src/FSharpAux.Core/Dictionary.fs +++ b/src/FSharpAux.Core/Dictionary.fs @@ -117,6 +117,20 @@ module Dictionary = | true -> Some v | false -> None + /// Performs a shallow copy of the input dictionary + /// The input dictionary. + /// A shallow copy of the dictionary. + let copy (table: IDictionary<'k,'v>) = new Dictionary<'k,'v>(table) + + /// Performs a deep copy of the input dictionary + /// The input dictionary. + /// A deep copy of the dictionary. + let deepCopy (table: IDictionary<'k,'v> when 'v :> ICloneable) = + let ret = new Dictionary<'k,'v>(table.Count) + for entry in table do + ret.Add(entry.Key, entry.Value.Clone() :?> 'v ) + ret + // /// Folds over the bindings in the dictionary // /// The function to update the state given the input key/value pairs.