A 2D slice is a slice of slices. In your function you allocate one slice to hold the others, then for each slice you allocate memory to hold that line of data. To copy this, you need to copy all of these data lines plus the overall slice.
When you say copy(origins, mapArray)
, what you really do is make a copy of a slice of pointers to the original data. You do not however copy the original data.
I would suggest that instead of doing a nested for-loop for copying the sub-slices, instead just use a one-dimensional slice and create wrapper functions to index into it. This is more memory efficient and you can use the built-in copy
.
Here is a sample of what I would do instead:
package main
import "fmt"
type squareMat struct {
size int
data []float32
}
func newSquareMat(size int) *squareMat {
return &squareMat{
size: size,
data: make([]float32, size*size),
}
}
func (s *squareMat) get(i, j int) float32 {
return s.data[i+j*s.size]
}
func (s *squareMat) set(i, j int, to float32) {
s.data[i+j*s.size] = to
}
func (s *squareMat) copy() *squareMat {
c := newSquareMat(s.size)
copy(c.data, s.data)
return c
}
func main() {
m := newSquareMat(5)
m.set(2, 3, 1.5)
n := m.copy()
n.set(2, 3, 99)
fmt.Println(m.get(2, 3))
fmt.Println(n.get(2, 3))
}
If you instead insist on using 2D float32 arrays, here is how to copy that:
package main
import "fmt"
func copy2D(x [][]float32) [][]float32 {
c := make([][]float32, len(x))
for i := range c {
c[i] = make([]float32, len(x[i]))
copy(c[i], x[i])
}
return c
}
func main() {
a := [][]float32{
[]float32{1, 2, 3},
[]float32{4, 5, 6},
[]float32{7, 8, 9},
}
b := copy2D(a)
b[1][1] = 99
fmt.Println(a)
fmt.Println(b)
}