Last active
October 23, 2017 20:58
-
-
Save prl900/90cd1ee1e313f0c59c654a82931d4469 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Array struct { | |
Data []byte | |
Shape []int | |
Offset []int | |
} | |
func (a Array) String() string { | |
out := "" | |
for k := 0; k < a.Shape[2]; k++ { | |
for j := 0; j < a.Shape[1]; j++ { | |
out += fmt.Sprintf("%v\n", a.Data[j*a.Shape[0]:(j+1)*a.Shape[0]]) | |
} | |
out += fmt.Sprintf("\n\n") | |
} | |
return out | |
} | |
func (a *Array) GetStrides() []int { | |
out := make([]int, len(a.Shape)) | |
for i := range out { | |
out[i] = 1 | |
} | |
for i := 1; i < len(a.Shape); i++ { | |
for j := i; j < len(a.Shape); j++ { | |
out[j] *= a.Shape[i-1] | |
} | |
} | |
return out | |
} | |
func (a *Array) Burn(b Array) { | |
aStrd := a.GetStrides() | |
bStrd := b.GetStrides() | |
for k0 := 0; k0 < b.Shape[2]; k0++ { | |
for j0 := 0; j0 < b.Shape[1]; j0++ { | |
for i0 := 0; i0 < b.Shape[0]; i0++ { | |
a.Data[(b.Offset[2]+k0)*aStrd[2]+(b.Offset[1]+j0)*aStrd[1]+(b.Offset[0]*aStrd[0]+i0)] = b.Data[k0*bStrd[2]+j0*bStrd[1]+i0*bStrd[0]] | |
} | |
} | |
} | |
} | |
func main() { | |
data := make([]byte, 7*5*2) | |
for i := range data { | |
data[i] = 1 | |
} | |
a := Array{Data: data, Shape: []int{7, 5, 2}, Offset: []int{4, 2, 0}} | |
fmt.Println(a.GetStrides()) | |
canvas := Array{Data: make([]byte, 20*20*2), Shape: []int{20, 20, 2}, Offset: []int{0, 0, 0}} | |
canvas.Burn(a) | |
fmt.Println(canvas) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment