mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 04:23:41 +00:00
runtime: implement growing hashmaps
Add support for growing hashmaps beyond their initial size.
This commit is contained in:
committed by
Ron Evans
parent
55fc7b904a
commit
763b9d7d10
Vendored
+27
-8
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
@@ -55,3 +55,4 @@ true false 0
|
||||
4321
|
||||
5555
|
||||
tested preallocated map
|
||||
tested growing of a map
|
||||
|
||||
Reference in New Issue
Block a user