1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
package main
import (
"fmt"
"io"
)
type DenseBinOp struct {
MethodName string
Name string
Scalar bool
}
func (fn *DenseBinOp) Write(w io.Writer) {
type tmp struct {
Left, Right string
}
var ds tmp
ds.Left = "t"
ds.Right = "other"
name := fn.MethodName
if fn.Scalar {
name += "Scalar"
}
if tmpl, ok := arithDocStrings[name]; ok {
tmpl.Execute(w, ds)
}
if tmpl, ok := cmpDocStrings[name]; ok {
tmpl.Execute(w, ds)
}
if fn.Scalar {
fmt.Fprintf(w, "func (t *Dense) %sScalar(other interface{}, leftTensor bool, opts ...FuncOpt) (retVal *Dense, err error) {\n", fn.MethodName)
denseArithScalarBody.Execute(w, fn)
} else {
fmt.Fprintf(w, "func (t *Dense) %s(other *Dense, opts ...FuncOpt) (retVal *Dense, err error) {\n", fn.MethodName)
denseArithBody.Execute(w, fn)
}
w.Write([]byte("}\n\n"))
}
func generateDenseArith(f io.Writer, ak Kinds) {
var methods []*DenseBinOp
for _, bo := range arithBinOps {
meth := &DenseBinOp{
MethodName: bo.Name(),
Name: bo.Name(),
}
methods = append(methods, meth)
}
for _, meth := range methods {
meth.Write(f)
meth.Scalar = true
}
for _, meth := range methods {
meth.Write(f)
}
}
func generateDenseCmp(f io.Writer, ak Kinds) {
var methods []*DenseBinOp
for _, cbo := range cmpBinOps {
methName := cbo.Name()
if methName == "Eq" || methName == "Ne" {
methName = "El" + cbo.Name()
}
meth := &DenseBinOp{
MethodName: methName,
Name: cbo.Name(),
}
methods = append(methods, meth)
}
for _, meth := range methods {
meth.Write(f)
meth.Scalar = true
}
for _, meth := range methods {
meth.Write(f)
}
}
|