Skip to content

Commit aea61b5

Browse files
authored
fix(js/ts): Fix Dictionary.Remove and HashSet.Remove to remove empty buckets (#4985)
1 parent 50a37f7 commit aea61b5

9 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎src/fable-library-py/fable_library/MutableMap.fs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type MutableMap<'Key, 'Value when 'Key: equality>
7272
match this.TryFindIndex(k) with
7373
| true, h, i when i > -1 ->
7474
hashMap.[h].RemoveAt(i)
75+
76+
if hashMap.[h].Count = 0 then
77+
hashMap.Remove(h) |> ignore
78+
7579
true
7680
| _, _, _ -> false
7781

‎src/fable-library-py/fable_library/MutableSet.fs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ type MutableSet<'T when 'T: equality>(items: 'T seq, comparer: IEqualityComparer
6060
match this.TryFindIndex(k) with
6161
| true, h, i when i > -1 ->
6262
hashMap.[h].RemoveAt(i)
63+
64+
if hashMap.[h].Count = 0 then
65+
hashMap.Remove(h) |> ignore
66+
6367
true
6468
| _, _, _ -> false
6569

‎src/fable-library-ts/MutableMap.fs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ type MutableMap<'Key, 'Value when 'Key: equality>
7474
match this.TryFindIndex(k) with
7575
| true, h, i when i > -1 ->
7676
hashMap.[h].RemoveAt(i)
77+
78+
if hashMap.[h].Count = 0 then
79+
hashMap.Remove(h) |> ignore
80+
7781
true
7882
| _, _, _ -> false
7983

‎src/fable-library-ts/MutableSet.fs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ type MutableSet<'T when 'T: equality>(items: 'T seq, comparer: IEqualityComparer
6161
match this.TryFindIndex(k) with
6262
| true, h, i when i > -1 ->
6363
hashMap.[h].RemoveAt(i)
64+
65+
if hashMap.[h].Count = 0 then
66+
hashMap.Remove(h) |> ignore
67+
6468
true
6569
| _, _, _ -> false
6670

‎tests/Beam/DictionaryTests.fs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ open System.Collections.Generic
55
open Fable.Tests.Util
66
open Util.Testing
77

8+
type MyRecord = { a: int }
9+
810
// --- Construction ---
911

1012
[<Fact>]
@@ -74,6 +76,28 @@ let ``test Dictionary.Remove works`` () =
7476
dic.Remove("C") |> equal false
7577
dic.Count |> equal 1
7678

79+
[<Fact>]
80+
let ``test Dictionary.Remove with complex keys works`` () =
81+
let dic = Dictionary<_, _>()
82+
dic.Add((0,"A"), "Hello")
83+
dic.Add((1,"B"), "World!")
84+
dic.Remove((0,"A")) |> equal true
85+
dic.Remove((1,"B")) |> equal true
86+
dic.Count |> equal 0
87+
88+
[<Fact>]
89+
let ``test Dictionary.Remove with records as keys works`` () =
90+
let x1 = { a = 5 }
91+
let x2 = { a = 5 }
92+
let x3 = { a = 10 }
93+
let x4 = { a = 15 }
94+
let dict = Dictionary<_, _>()
95+
dict.Add(x1, "Hello")
96+
dict.Add(x3, "World!")
97+
dict.Remove(x2) |> equal true
98+
dict.Remove(x4) |> equal false
99+
dict.Count |> equal 1
100+
77101
// --- TryGetValue ---
78102

79103
[<Fact>]

‎tests/Dart/src/DictionaryTests.fs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ let tests() =
186186
dic.Add("B", "World!")
187187
dic.Remove("A") |> equal true
188188
dic.Remove("C") |> equal false
189+
dic.Count |> equal 1
190+
191+
testCase "Dictionary.Remove with complex keys works" <| fun () ->
192+
let dic = Dictionary<_, _>()
193+
dic.Add((0,"A"), "Hello")
194+
dic.Add((1,"B"), "World!")
195+
dic.Remove((0,"A")) |> equal true
196+
dic.Remove((1,"B")) |> equal true
197+
dic.Count |> equal 0
189198

190199
testCase "Dictionary.Remove with records as keys works" <| fun () ->
191200
let x1 = { a = 5 }
@@ -197,6 +206,7 @@ let tests() =
197206
dic.Add(x3, "World!")
198207
dic.Remove(x2) |> equal true
199208
dic.Remove(x4) |> equal false
209+
dic.Count |> equal 1
200210

201211
testCase "Interface IDictionary.Count works" <| fun () ->
202212
let dic = dict <| seq { for i in 1. .. 10. -> i, i*i }

‎tests/Js/Main/DictionaryTests.fs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ let tests =
194194
dic.Add("B", "World!")
195195
dic.Remove("A") |> equal true
196196
dic.Remove("C") |> equal false
197+
dic.Count |> equal 1
198+
199+
testCase "Dictionary.Remove with complex keys works" <| fun () ->
200+
let dic = Dictionary<_, _>()
201+
dic.Add((0,"A"), "Hello")
202+
dic.Add((1,"B"), "World!")
203+
dic.Remove((0,"A")) |> equal true
204+
dic.Remove((1,"B")) |> equal true
205+
dic.Count |> equal 0
197206

198207
testCase "Dictionary.Remove with records as keys works" <| fun () ->
199208
let x1 = { a = 5 }
@@ -205,6 +214,7 @@ let tests =
205214
dic.Add(x3, "World!")
206215
dic.Remove(x2) |> equal true
207216
dic.Remove(x4) |> equal false
217+
dic.Count |> equal 1
208218

209219
testCase "Interface IDictionary.Count works" <| fun () ->
210220
let dic = dict <| seq { for i in 1. .. 10. -> i.ToString(), i*i }

‎tests/Python/TestDictionary.fs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,39 @@ let ``test Dictionary.ContainsValue works`` () =
226226
dic.ContainsValue("Everybody!") |> equal false
227227

228228
[<Fact>]
229-
let ``test Dictionary.Delete works`` () =
229+
let ``test Dictionary.Remove works`` () =
230230
let dic = Dictionary<_, _>()
231231
dic.Add("A", "Hello")
232232
dic.Add("B", "World!")
233233
dic.ContainsValue("Hello") |> equal true
234234
dic.Remove("A") |> equal true
235235
dic.ContainsValue("Hello") |> equal false
236+
dic.Count |> equal 1
236237

237238
[<Fact>]
238-
let ``test Dictionary.Delete works with tuples as keys`` () =
239+
let ``test Dictionary.Remove with complex keys works`` () =
240+
let dic = Dictionary<_, _>()
241+
dic.Add((0,"A"), "Hello")
242+
dic.Add((1,"B"), "World!")
243+
dic.Remove((0,"A")) |> equal true
244+
dic.Remove((1,"B")) |> equal true
245+
dic.Count |> equal 0
246+
247+
[<Fact>]
248+
let ``test Dictionary.Remove with records as keys works`` () =
249+
let x1 = { a = 5 }
250+
let x2 = { a = 5 }
251+
let x3 = { a = 10 }
252+
let x4 = { a = 15 }
253+
let dict = Dictionary<_, _>()
254+
dict.Add(x1, "Hello")
255+
dict.Add(x3, "World!")
256+
dict.Remove(x2) |> equal true
257+
dict.Remove(x4) |> equal false
258+
dict.Count |> equal 1
259+
260+
[<Fact>]
261+
let ``test Dictionary.Remove works with tuples as keys`` () =
239262
let my_dict = Dictionary((Map [for i in 0..10 do yield (i,i), sprintf "Number: %i" i]))
240263
my_dict.ContainsKey((0,0)) |> equal true
241264
my_dict.Remove((0,0)) |> equal true

‎tests/Rust/tests/src/DictionaryTests.fs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ let ``Dictionary.Remove works`` () =
271271
dict.Remove(3) |> equal false
272272
dict.Count |> equal 1
273273

274+
[<Fact>]
275+
let ``Dictionary.Remove with complex keys works`` () =
276+
let dic = Dictionary<_, _>()
277+
dic.Add((0,"A"), "Hello")
278+
dic.Add((1,"B"), "World!")
279+
dic.Remove((0,"A")) |> equal true
280+
dic.Remove((1,"B")) |> equal true
281+
dic.Count |> equal 0
282+
274283
[<Fact>]
275284
let ``Dictionary.Remove with records as keys works`` () =
276285
let x1 = { a = 5 }

0 commit comments

Comments
 (0)