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