runtime: implement growing hashmaps

Add support for growing hashmaps beyond their initial size.
This commit is contained in:
Ayke van Laethem
2019-05-13 20:18:31 +02:00
committed by Ron Evans
parent 55fc7b904a
commit 763b9d7d10
3 changed files with 67 additions and 19 deletions
+27 -8
View File
@@ -50,15 +50,15 @@ func main() {
// test preallocated map
squares := make(map[int]int, 200)
for i := 0; i < 100; i++ {
squares[i] = i*i
for j := 0; j <= i; j++ {
if v := squares[j]; v != j*j {
println("unexpected value read back from squares map:", j, v)
}
}
}
testBigMap(squares, 100)
println("tested preallocated map")
// test growing maps
squares = make(map[int]int, 0)
testBigMap(squares, 10)
squares = make(map[int]int, 20)
testBigMap(squares, 40)
println("tested growing of a map")
}
func readMap(m map[string]int, key string) {
@@ -73,3 +73,22 @@ func lookup(m map[string]int, key string) {
value, ok := m[key]
println("lookup with comma-ok:", key, value, ok)
}
func testBigMap(squares map[int]int, n int) {
for i := 0; i < n; i++ {
if len(squares) != i {
println("unexpected length:", len(squares), "at i =", i)
}
squares[i] = i*i
for j := 0; j <= i; j++ {
if v, ok := squares[j]; !ok || v != j*j {
if !ok {
println("key not found in squares map:", j)
} else {
println("unexpected value read back from squares map:", j, v)
}
return
}
}
}
}
+1
View File
@@ -55,3 +55,4 @@ true false 0
4321
5555
tested preallocated map
tested growing of a map