Add remove() to Map.

This commit is contained in:
Bob Nystrom
2015-01-25 17:42:36 -08:00
parent 0e6a90443e
commit 4579171afa
7 changed files with 142 additions and 46 deletions
@@ -1,4 +1,5 @@
// Make sure it can grow to some size.
// This brute force test basically validates that the map can grow and shrink
// its capacity while still behaving correctly.
var fishes = [
"Aeneus corydoras", "African glass catfish", "African lungfish",
@@ -65,9 +66,16 @@ for (fish in fishes) {
IO.print(map.count) // expect: 249
// Re-add some keys.
for (n in 20..50) {
map[fishes[n]] = n
for (n in 0...150) {
map.remove(fishes[n])
}
IO.print(map.count) // expect: 249
IO.print(map.count) // expect: 99
// Make sure we can still find all of the remaining ones.
var contained = 0
for (n in 150...249) {
if (map.containsKey(fishes[n])) contained = contained + 1
}
IO.print(contained) // expect: 99
+18
View File
@@ -0,0 +1,18 @@
var map = {
"one": 1,
"two": 2,
"three": 3
}
IO.print(map.count) // expect: 3
IO.print(map.remove("two")) // expect: 2
IO.print(map.count) // expect: 2
IO.print(map.remove("three")) // expect: 3
IO.print(map.count) // expect: 1
// Remove an already removed entry.
IO.print(map.remove("two")) // expect: null
IO.print(map.count) // expect: 1
IO.print(map.remove("one")) // expect: 1
IO.print(map.count) // expect: 0
+1
View File
@@ -0,0 +1 @@
var result = {}.remove([]) // expect runtime error: Key must be a value type.