diff --git a/src/fable-library-py/fable_library/MutableMap.fs b/src/fable-library-py/fable_library/MutableMap.fs index 502a283cc8..d3344d27e1 100644 --- a/src/fable-library-py/fable_library/MutableMap.fs +++ b/src/fable-library-py/fable_library/MutableMap.fs @@ -72,6 +72,10 @@ type MutableMap<'Key, 'Value when 'Key: equality> match this.TryFindIndex(k) with | true, h, i when i > -1 -> hashMap.[h].RemoveAt(i) + + if hashMap.[h].Count = 0 then + hashMap.Remove(h) |> ignore + true | _, _, _ -> false diff --git a/src/fable-library-py/fable_library/MutableSet.fs b/src/fable-library-py/fable_library/MutableSet.fs index c0d329a001..9a886e2bd4 100644 --- a/src/fable-library-py/fable_library/MutableSet.fs +++ b/src/fable-library-py/fable_library/MutableSet.fs @@ -60,6 +60,10 @@ type MutableSet<'T when 'T: equality>(items: 'T seq, comparer: IEqualityComparer match this.TryFindIndex(k) with | true, h, i when i > -1 -> hashMap.[h].RemoveAt(i) + + if hashMap.[h].Count = 0 then + hashMap.Remove(h) |> ignore + true | _, _, _ -> false diff --git a/src/fable-library-ts/MutableMap.fs b/src/fable-library-ts/MutableMap.fs index 12fb5aee85..04b1633556 100644 --- a/src/fable-library-ts/MutableMap.fs +++ b/src/fable-library-ts/MutableMap.fs @@ -74,6 +74,10 @@ type MutableMap<'Key, 'Value when 'Key: equality> match this.TryFindIndex(k) with | true, h, i when i > -1 -> hashMap.[h].RemoveAt(i) + + if hashMap.[h].Count = 0 then + hashMap.Remove(h) |> ignore + true | _, _, _ -> false diff --git a/src/fable-library-ts/MutableSet.fs b/src/fable-library-ts/MutableSet.fs index db04abb6a6..100eeb520a 100644 --- a/src/fable-library-ts/MutableSet.fs +++ b/src/fable-library-ts/MutableSet.fs @@ -61,6 +61,10 @@ type MutableSet<'T when 'T: equality>(items: 'T seq, comparer: IEqualityComparer match this.TryFindIndex(k) with | true, h, i when i > -1 -> hashMap.[h].RemoveAt(i) + + if hashMap.[h].Count = 0 then + hashMap.Remove(h) |> ignore + true | _, _, _ -> false diff --git a/tests/Beam/DictionaryTests.fs b/tests/Beam/DictionaryTests.fs index 0694e9ae13..d7cd601aa7 100644 --- a/tests/Beam/DictionaryTests.fs +++ b/tests/Beam/DictionaryTests.fs @@ -5,6 +5,8 @@ open System.Collections.Generic open Fable.Tests.Util open Util.Testing +type MyRecord = { a: int } + // --- Construction --- [] @@ -74,6 +76,28 @@ let ``test Dictionary.Remove works`` () = dic.Remove("C") |> equal false dic.Count |> equal 1 +[] +let ``test Dictionary.Remove with complex keys works`` () = + let dic = Dictionary<_, _>() + dic.Add((0,"A"), "Hello") + dic.Add((1,"B"), "World!") + dic.Remove((0,"A")) |> equal true + dic.Remove((1,"B")) |> equal true + dic.Count |> equal 0 + +[] +let ``test Dictionary.Remove with records as keys works`` () = + let x1 = { a = 5 } + let x2 = { a = 5 } + let x3 = { a = 10 } + let x4 = { a = 15 } + let dict = Dictionary<_, _>() + dict.Add(x1, "Hello") + dict.Add(x3, "World!") + dict.Remove(x2) |> equal true + dict.Remove(x4) |> equal false + dict.Count |> equal 1 + // --- TryGetValue --- [] diff --git a/tests/Dart/src/DictionaryTests.fs b/tests/Dart/src/DictionaryTests.fs index 6addc9e00d..544d1197f6 100644 --- a/tests/Dart/src/DictionaryTests.fs +++ b/tests/Dart/src/DictionaryTests.fs @@ -186,6 +186,15 @@ let tests() = dic.Add("B", "World!") dic.Remove("A") |> equal true dic.Remove("C") |> equal false + dic.Count |> equal 1 + + testCase "Dictionary.Remove with complex keys works" <| fun () -> + let dic = Dictionary<_, _>() + dic.Add((0,"A"), "Hello") + dic.Add((1,"B"), "World!") + dic.Remove((0,"A")) |> equal true + dic.Remove((1,"B")) |> equal true + dic.Count |> equal 0 testCase "Dictionary.Remove with records as keys works" <| fun () -> let x1 = { a = 5 } @@ -197,6 +206,7 @@ let tests() = dic.Add(x3, "World!") dic.Remove(x2) |> equal true dic.Remove(x4) |> equal false + dic.Count |> equal 1 testCase "Interface IDictionary.Count works" <| fun () -> let dic = dict <| seq { for i in 1. .. 10. -> i, i*i } diff --git a/tests/Js/Main/DictionaryTests.fs b/tests/Js/Main/DictionaryTests.fs index 31037ee512..ea58422792 100644 --- a/tests/Js/Main/DictionaryTests.fs +++ b/tests/Js/Main/DictionaryTests.fs @@ -194,6 +194,15 @@ let tests = dic.Add("B", "World!") dic.Remove("A") |> equal true dic.Remove("C") |> equal false + dic.Count |> equal 1 + + testCase "Dictionary.Remove with complex keys works" <| fun () -> + let dic = Dictionary<_, _>() + dic.Add((0,"A"), "Hello") + dic.Add((1,"B"), "World!") + dic.Remove((0,"A")) |> equal true + dic.Remove((1,"B")) |> equal true + dic.Count |> equal 0 testCase "Dictionary.Remove with records as keys works" <| fun () -> let x1 = { a = 5 } @@ -205,6 +214,7 @@ let tests = dic.Add(x3, "World!") dic.Remove(x2) |> equal true dic.Remove(x4) |> equal false + dic.Count |> equal 1 testCase "Interface IDictionary.Count works" <| fun () -> let dic = dict <| seq { for i in 1. .. 10. -> i.ToString(), i*i } diff --git a/tests/Python/TestDictionary.fs b/tests/Python/TestDictionary.fs index 878f652fb8..8f28123c47 100644 --- a/tests/Python/TestDictionary.fs +++ b/tests/Python/TestDictionary.fs @@ -226,16 +226,39 @@ let ``test Dictionary.ContainsValue works`` () = dic.ContainsValue("Everybody!") |> equal false [] -let ``test Dictionary.Delete works`` () = +let ``test Dictionary.Remove works`` () = let dic = Dictionary<_, _>() dic.Add("A", "Hello") dic.Add("B", "World!") dic.ContainsValue("Hello") |> equal true dic.Remove("A") |> equal true dic.ContainsValue("Hello") |> equal false + dic.Count |> equal 1 [] -let ``test Dictionary.Delete works with tuples as keys`` () = +let ``test Dictionary.Remove with complex keys works`` () = + let dic = Dictionary<_, _>() + dic.Add((0,"A"), "Hello") + dic.Add((1,"B"), "World!") + dic.Remove((0,"A")) |> equal true + dic.Remove((1,"B")) |> equal true + dic.Count |> equal 0 + +[] +let ``test Dictionary.Remove with records as keys works`` () = + let x1 = { a = 5 } + let x2 = { a = 5 } + let x3 = { a = 10 } + let x4 = { a = 15 } + let dict = Dictionary<_, _>() + dict.Add(x1, "Hello") + dict.Add(x3, "World!") + dict.Remove(x2) |> equal true + dict.Remove(x4) |> equal false + dict.Count |> equal 1 + +[] +let ``test Dictionary.Remove works with tuples as keys`` () = let my_dict = Dictionary((Map [for i in 0..10 do yield (i,i), sprintf "Number: %i" i])) my_dict.ContainsKey((0,0)) |> equal true my_dict.Remove((0,0)) |> equal true diff --git a/tests/Rust/tests/src/DictionaryTests.fs b/tests/Rust/tests/src/DictionaryTests.fs index 11458d3126..ccd124d79f 100644 --- a/tests/Rust/tests/src/DictionaryTests.fs +++ b/tests/Rust/tests/src/DictionaryTests.fs @@ -271,6 +271,15 @@ let ``Dictionary.Remove works`` () = dict.Remove(3) |> equal false dict.Count |> equal 1 +[] +let ``Dictionary.Remove with complex keys works`` () = + let dic = Dictionary<_, _>() + dic.Add((0,"A"), "Hello") + dic.Add((1,"B"), "World!") + dic.Remove((0,"A")) |> equal true + dic.Remove((1,"B")) |> equal true + dic.Count |> equal 0 + [] let ``Dictionary.Remove with records as keys works`` () = let x1 = { a = 5 }