Generic 2D array
- zero-alloc
- 100% test coverage
import (
"fmt"
"github.com/s0rg/array2d"
)
func main() {
// create new 2D array with given type and dimensions (width x height)
a := array2d.New[int](15, 20)
// fill with ones
a.Fill(func() (value int) {
return 1
})
// set value at given coords
a.Set(1, 2, 100)
// get values
t1, ok := a.Get(1, 1)
if !ok {
panic("nothing at 1x1")
}
t2, ok := a.Get(1, 2)
if !ok {
panic("nothing at 1x2")
}
fmt.Println(t1, t2) // should print: 1, 100
}
goos: linux
goarch: amd64
pkg: github.com/s0rg/array2d
cpu: AMD Ryzen 5 5500U with Radeon Graphics
BenchmarkArray/Set-12 1000000000 1.002 ns/op 0 B/op 0 allocs/op
BenchmarkArray/Get-12 927178446 1.250 ns/op 0 B/op 0 allocs/op
BenchmarkArray/Iter-12 39491674 30.68 ns/op 0 B/op 0 allocs/op
BenchmarkArray/Fill-12 20840200 56.84 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/s0rg/array2d 4.897s