| 1 |
package main |
| 2 |
|
| 3 |
import "text/template" |
| 4 |
|
| 5 |
// level 1 aggregation (internal.E) templates |
| 6 |
|
| 7 |
const ( |
| 8 |
eArithRaw = `as := isScalar(a, t) |
| 9 |
bs := isScalar(b, t) |
| 10 |
{{$name := .Name}} |
| 11 |
switch t { |
| 12 |
{{range .Kinds -}} |
| 13 |
case {{reflectKind .}}: |
| 14 |
at := a.{{sliceOf .}} |
| 15 |
bt := b.{{sliceOf .}} |
| 16 |
{{$isDiv := eq $name "Div" -}} |
| 17 |
{{$p := panicsDiv0 . -}} |
| 18 |
switch { |
| 19 |
case as && bs: |
| 20 |
Vec{{$name}}{{short .}}(at, bt) |
| 21 |
case as && !bs: |
| 22 |
{{if and $isDiv $p}} err = {{end}} {{$name}}SV{{short .}}(at[0], bt) |
| 23 |
case !as && bs: |
| 24 |
{{if and $isDiv $p}} err = {{end}} {{$name}}VS{{short .}}(at, bt[0]) |
| 25 |
default: |
| 26 |
{{if and $isDiv $p}} err = {{end}} Vec{{$name}}{{short .}}(at, bt) |
| 27 |
} |
| 28 |
return |
| 29 |
{{end -}} |
| 30 |
default: |
| 31 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 32 |
} |
| 33 |
` |
| 34 |
|
| 35 |
eArithIncrRaw = `as := isScalar(a, t) |
| 36 |
bs := isScalar(b, t) |
| 37 |
is := isScalar(incr, t) |
| 38 |
if ((as && !bs) || (bs && !as)) && is { |
| 39 |
return errors.Errorf("Cannot increment on scalar increment. a: %d, b %d", a.TypedLen(t), b.TypedLen(t)) |
| 40 |
} |
| 41 |
{{$name := .Name}} |
| 42 |
switch t { |
| 43 |
{{range .Kinds -}} |
| 44 |
case {{reflectKind .}}: |
| 45 |
at := a.{{sliceOf .}} |
| 46 |
bt := b.{{sliceOf .}} |
| 47 |
it := incr.{{sliceOf .}} |
| 48 |
|
| 49 |
switch { |
| 50 |
case as && bs: |
| 51 |
Vec{{$name}}{{short .}}(at, bt) |
| 52 |
if !is { |
| 53 |
return e.Add(t, incr, a) |
| 54 |
} |
| 55 |
it[0]+= at[0] |
| 56 |
case as && !bs: |
| 57 |
{{$name}}IncrSV{{short .}}(at[0], bt, it) |
| 58 |
case !as && bs : |
| 59 |
{{$name}}IncrVS{{short .}}(at, bt[0], it) |
| 60 |
default: |
| 61 |
{{$name}}Incr{{short .}}(at, bt,it) |
| 62 |
} |
| 63 |
return |
| 64 |
{{end -}} |
| 65 |
default: |
| 66 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 67 |
} |
| 68 |
` |
| 69 |
eArithIterRaw = `as := isScalar(a, t) |
| 70 |
bs := isScalar(b, t) |
| 71 |
{{$name := .Name}} |
| 72 |
switch t { |
| 73 |
{{range .Kinds -}} |
| 74 |
case {{reflectKind .}}: |
| 75 |
at := a.{{sliceOf .}} |
| 76 |
bt := b.{{sliceOf .}} |
| 77 |
switch { |
| 78 |
case as && bs : |
| 79 |
Vec{{$name}}{{short .}}(at, bt) |
| 80 |
case as && !bs: |
| 81 |
{{$name}}IterSV{{short .}}(at[0], bt, bit) |
| 82 |
case !as && bs: |
| 83 |
{{$name}}IterVS{{short .}}(at, bt[0], ait) |
| 84 |
default: |
| 85 |
{{$name}}Iter{{short .}}(at, bt, ait, bit) |
| 86 |
} |
| 87 |
return |
| 88 |
{{end -}} |
| 89 |
default: |
| 90 |
return errors.Errorf("Unsupported type %v for {{$name}}Iter", t) |
| 91 |
} |
| 92 |
` |
| 93 |
|
| 94 |
eArithIterIncrRaw = `as :=isScalar(a, t) |
| 95 |
bs := isScalar(b, t) |
| 96 |
is := isScalar(incr, t) |
| 97 |
|
| 98 |
if ((as && !bs) || (bs && !as)) && is { |
| 99 |
return errors.Errorf("Cannot increment on a scalar increment. len(a): %d, len(b) %d", a.TypedLen(t), b.TypedLen(t)) |
| 100 |
} |
| 101 |
{{$name := .Name}} |
| 102 |
switch t { |
| 103 |
{{range .Kinds -}} |
| 104 |
case {{reflectKind .}}: |
| 105 |
at := a.{{sliceOf .}} |
| 106 |
bt := b.{{sliceOf .}} |
| 107 |
it := incr.{{sliceOf .}} |
| 108 |
switch { |
| 109 |
case as && bs: |
| 110 |
Vec{{$name}}{{short .}}(at, bt) |
| 111 |
if !is { |
| 112 |
return e.{{$name}}Iter(t, incr, a, iit, ait) |
| 113 |
} |
| 114 |
it[0] += at[0] |
| 115 |
return |
| 116 |
case as && !bs: |
| 117 |
return {{$name}}IterIncrSV{{short .}}(at[0], bt, it, bit, iit) |
| 118 |
case !as && bs: |
| 119 |
return {{$name}}IterIncrVS{{short .}}(at, bt[0], it, ait, iit) |
| 120 |
default: |
| 121 |
return {{$name}}IterIncr{{short .}}(at, bt, it, ait, bit, iit) |
| 122 |
} |
| 123 |
{{end -}} |
| 124 |
default: |
| 125 |
return errors.Errorf("Unsupported type %v for {{$name}}IterIncr", t) |
| 126 |
} |
| 127 |
` |
| 128 |
|
| 129 |
eArithRecvRaw = `as :=isScalar(a, t) |
| 130 |
bs := isScalar(b, t) |
| 131 |
rs := isScalar(recv, t) |
| 132 |
|
| 133 |
if ((as && !bs) || (bs && !as)) && rs { |
| 134 |
return errors.Errorf("Cannot increment on a scalar increment. len(a): %d, len(b) %d", a.TypedLen(t), b.TypedLen(t)) |
| 135 |
} |
| 136 |
|
| 137 |
{{$name := .Name}} |
| 138 |
switch t{ |
| 139 |
{{range .Kinds -}} |
| 140 |
case {{reflectKind .}}: |
| 141 |
at := a.{{sliceOf .}} |
| 142 |
bt := b.{{sliceOf .}} |
| 143 |
rt := recv.{{sliceOf .}} |
| 144 |
{{$name}}Recv{{short .}}(at, bt, rt) |
| 145 |
return |
| 146 |
{{end -}} |
| 147 |
default: |
| 148 |
return errors.Errorf("Unsupported type %v for {{$name}}Recv", t) |
| 149 |
} |
| 150 |
` |
| 151 |
|
| 152 |
eMapRaw = `as := isScalar(a, t) |
| 153 |
switch t { |
| 154 |
{{range .Kinds -}} |
| 155 |
case {{reflectKind .}}: |
| 156 |
var f0 {{template "fntype0" .}} |
| 157 |
var f1 {{template "fntype1" .}} |
| 158 |
|
| 159 |
switch f := fn.(type){ |
| 160 |
case {{template "fntype0" .}}: |
| 161 |
f0 = f |
| 162 |
case {{template "fntype1" .}}: |
| 163 |
f1 = f |
| 164 |
default: |
| 165 |
return errors.Errorf("Cannot map fn of %T to array", fn) |
| 166 |
} |
| 167 |
|
| 168 |
at := a.{{sliceOf .}} |
| 169 |
{{if isAddable . -}} |
| 170 |
switch{ |
| 171 |
case as && incr && f0 != nil: |
| 172 |
at[0] += f0(at[0]) |
| 173 |
case as && incr && f0 == nil: |
| 174 |
var tmp {{asType .}} |
| 175 |
if tmp, err= f1(at[0]); err != nil { |
| 176 |
return |
| 177 |
} |
| 178 |
at[0] += tmp |
| 179 |
case as && !incr && f0 != nil: |
| 180 |
at[0] = f0(at[0]) |
| 181 |
case as && !incr && f0 == nil: |
| 182 |
at[0], err = f1(at[0]) |
| 183 |
case !as && incr && f0 != nil: |
| 184 |
MapIncr{{short .}}(f0, at) |
| 185 |
case !as && incr && f0 == nil: |
| 186 |
err = MapIncrErr{{short .}}(f1, at) |
| 187 |
case !as && !incr && f0 == nil: |
| 188 |
err = MapErr{{short .}}(f1, at) |
| 189 |
default: |
| 190 |
Map{{short .}}(f0, at) |
| 191 |
} |
| 192 |
{{else -}} |
| 193 |
if incr { |
| 194 |
return errors.Errorf("Cannot perform increment on t of %v", t) |
| 195 |
} |
| 196 |
switch { |
| 197 |
case as && f0 != nil: |
| 198 |
at[0] = f0(at[0]) |
| 199 |
case as && f0 == nil: |
| 200 |
at[0], err = f1(at[0]) |
| 201 |
case !as && f0 == nil: |
| 202 |
err = MapErr{{short .}}(f1, at) |
| 203 |
default: |
| 204 |
Map{{short .}}(f0, at) |
| 205 |
} |
| 206 |
{{end -}} |
| 207 |
|
| 208 |
{{end -}} |
| 209 |
default: |
| 210 |
return errors.Errorf("Cannot map t of %v", t) |
| 211 |
|
| 212 |
} |
| 213 |
` |
| 214 |
|
| 215 |
eMapIterRaw = `switch t { |
| 216 |
{{range .Kinds -}} |
| 217 |
case {{reflectKind .}}: |
| 218 |
at := a.{{sliceOf .}} |
| 219 |
var f0 {{template "fntype0" .}} |
| 220 |
var f1 {{template "fntype1" .}} |
| 221 |
|
| 222 |
switch f := fn.(type){ |
| 223 |
case {{template "fntype0" .}}: |
| 224 |
f0 = f |
| 225 |
case {{template "fntype1" .}}: |
| 226 |
f1 = f |
| 227 |
default: |
| 228 |
return errors.Errorf("Cannot map fn of %T to array", fn) |
| 229 |
} |
| 230 |
|
| 231 |
{{if isAddable . -}} |
| 232 |
switch { |
| 233 |
case incr && f0 != nil: |
| 234 |
MapIterIncr{{short .}}(f0, at, ait) |
| 235 |
case incr && f0 == nil: |
| 236 |
err = MapIterIncrErr{{short .}}(f1, at, ait) |
| 237 |
case !incr && f0 == nil: |
| 238 |
err = MapIterErr{{short .}}(f1, at, ait) |
| 239 |
default: |
| 240 |
MapIter{{short .}}(f0, at, ait) |
| 241 |
} |
| 242 |
{{else -}} |
| 243 |
if incr { |
| 244 |
return errors.Errorf("Cannot perform increment on t of %v", t) |
| 245 |
} |
| 246 |
switch { |
| 247 |
case f0 == nil: |
| 248 |
err = MapIterErr{{short .}}(f1, at, ait) |
| 249 |
default: |
| 250 |
MapIter{{short .}}(f0, at, ait) |
| 251 |
} |
| 252 |
{{end -}} |
| 253 |
{{end -}} |
| 254 |
default: |
| 255 |
return errors.Errorf("Cannot map t of %v", t) |
| 256 |
} |
| 257 |
` |
| 258 |
|
| 259 |
eCmpSameRaw = `as := isScalar(a, t) |
| 260 |
bs := isScalar(b, t) |
| 261 |
{{$name := .Name}} |
| 262 |
switch t { |
| 263 |
{{range .Kinds -}} |
| 264 |
{{if isBoolRepr . -}} |
| 265 |
case {{reflectKind .}}: |
| 266 |
at := a.{{sliceOf .}} |
| 267 |
bt := b.{{sliceOf .}} |
| 268 |
switch { |
| 269 |
case as && bs: |
| 270 |
{{$name}}Same{{short .}}(at, bt) |
| 271 |
case as && !bs: |
| 272 |
{{$name}}SameSV{{short .}}(at[0], bt) |
| 273 |
case !as && bs: |
| 274 |
{{$name}}SameVS{{short .}}(at, bt[0]) |
| 275 |
default: |
| 276 |
{{$name}}Same{{short .}}(at, bt) |
| 277 |
} |
| 278 |
return |
| 279 |
{{end -}} |
| 280 |
{{end -}} |
| 281 |
default: |
| 282 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 283 |
}` |
| 284 |
|
| 285 |
eCmpBoolRaw = `as := isScalar(a, t) |
| 286 |
bs := isScalar(b, t) |
| 287 |
rs := isScalar(retVal, Bool) |
| 288 |
rt := retVal.Bools() |
| 289 |
|
| 290 |
if ((as && !bs) || (bs && !as)) && rs { |
| 291 |
return errors.Errorf("retVal is a scalar. a: %d, b %d", a.TypedLen(t), b.TypedLen(t)) |
| 292 |
} |
| 293 |
|
| 294 |
{{$name := .Name}} |
| 295 |
switch t { |
| 296 |
{{range .Kinds -}} |
| 297 |
case {{reflectKind .}}: |
| 298 |
at := a.{{sliceOf .}} |
| 299 |
bt := b.{{sliceOf .}} |
| 300 |
|
| 301 |
switch { |
| 302 |
case as && bs: |
| 303 |
{{$name}}{{short .}}(at, bt, rt) |
| 304 |
case as && !bs: |
| 305 |
{{$name}}SV{{short .}}(at[0], bt, rt) |
| 306 |
case !as && bs : |
| 307 |
{{$name}}VS{{short .}}(at, bt[0], rt) |
| 308 |
default: |
| 309 |
{{$name}}{{short .}}(at, bt, rt) |
| 310 |
} |
| 311 |
return |
| 312 |
{{end -}} |
| 313 |
default: |
| 314 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 315 |
} |
| 316 |
` |
| 317 |
|
| 318 |
eCmpSameIterRaw = `as := isScalar(a, t) |
| 319 |
bs := isScalar(b, t) |
| 320 |
{{$name := .Name}} |
| 321 |
switch t { |
| 322 |
{{range .Kinds -}} |
| 323 |
{{if isBoolRepr . -}} |
| 324 |
case {{reflectKind .}}: |
| 325 |
at := a.{{sliceOf .}} |
| 326 |
bt := b.{{sliceOf .}} |
| 327 |
switch { |
| 328 |
case as && bs : |
| 329 |
{{$name}}Same{{short .}}(at, bt) |
| 330 |
case as && !bs: |
| 331 |
{{$name}}SameIterSV{{short .}}(at[0], bt, bit) |
| 332 |
case !as && bs: |
| 333 |
{{$name}}SameIterVS{{short .}}(at, bt[0], ait) |
| 334 |
default: |
| 335 |
{{$name}}SameIter{{short .}}(at, bt, ait, bit) |
| 336 |
} |
| 337 |
return |
| 338 |
{{end -}} |
| 339 |
{{end -}} |
| 340 |
default: |
| 341 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 342 |
} |
| 343 |
` |
| 344 |
|
| 345 |
eCmpBoolIterRaw = `as :=isScalar(a, t) |
| 346 |
bs := isScalar(b, t) |
| 347 |
rs := isScalar(retVal, Bool) |
| 348 |
rt := retVal.Bools() |
| 349 |
|
| 350 |
if ((as && !bs) || (bs && !as)) && rs { |
| 351 |
return errors.Errorf("retVal is scalar while len(a): %d, len(b) %d", a.TypedLen(t), b.TypedLen(t)) |
| 352 |
} |
| 353 |
|
| 354 |
{{$name := .Name}} |
| 355 |
switch t { |
| 356 |
{{range .Kinds -}} |
| 357 |
case {{reflectKind .}}: |
| 358 |
at := a.{{sliceOf .}} |
| 359 |
bt := b.{{sliceOf .}} |
| 360 |
switch { |
| 361 |
case as && bs: |
| 362 |
{{$name}}{{short .}}(at, bt, rt) |
| 363 |
return |
| 364 |
case as && !bs: |
| 365 |
return {{$name}}IterSV{{short .}}(at[0], bt, rt, bit, rit) |
| 366 |
case !as && bs: |
| 367 |
return {{$name}}IterVS{{short .}}(at, bt[0], rt, ait, rit) |
| 368 |
default: |
| 369 |
return {{$name}}Iter{{short .}}(at, bt, rt, ait, bit, rit) |
| 370 |
} |
| 371 |
{{end -}} |
| 372 |
default: |
| 373 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 374 |
} |
| 375 |
` |
| 376 |
eMinMaxSameRaw = `as := isScalar(a, t) |
| 377 |
bs := isScalar(b, t) |
| 378 |
{{$name := .Name}} |
| 379 |
switch t { |
| 380 |
{{range .Kinds -}} |
| 381 |
{{if isOrd . -}} |
| 382 |
case {{reflectKind .}}: |
| 383 |
at := a.{{sliceOf .}} |
| 384 |
bt := b.{{sliceOf .}} |
| 385 |
switch { |
| 386 |
case as && bs: |
| 387 |
Vec{{$name}}{{short .}}(at, bt) |
| 388 |
case as && !bs: |
| 389 |
{{$name}}SV{{short .}}(at[0], bt) |
| 390 |
case !as && bs: |
| 391 |
{{$name}}VS{{short .}}(at, bt[0]) |
| 392 |
default: |
| 393 |
Vec{{$name}}{{short .}}(at, bt) |
| 394 |
} |
| 395 |
return |
| 396 |
{{end -}} |
| 397 |
{{end -}} |
| 398 |
default: |
| 399 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 400 |
} |
| 401 |
` |
| 402 |
|
| 403 |
eMinMaxSameIterRaw = `as := isScalar(a, t) |
| 404 |
bs := isScalar(b, t) |
| 405 |
{{$name := .Name}} |
| 406 |
switch t { |
| 407 |
{{range .Kinds -}} |
| 408 |
{{if isOrd . -}} |
| 409 |
case {{reflectKind .}}: |
| 410 |
at := a.{{sliceOf .}} |
| 411 |
bt := b.{{sliceOf .}} |
| 412 |
switch { |
| 413 |
case as && bs : |
| 414 |
Vec{{$name}}{{short .}}(at, bt) |
| 415 |
case as && !bs: |
| 416 |
{{$name}}IterSV{{short .}}(at[0], bt, bit) |
| 417 |
case !as && bs: |
| 418 |
{{$name}}IterVS{{short .}}(at, bt[0], ait) |
| 419 |
default: |
| 420 |
Vec{{$name}}Iter{{short .}}(at, bt, ait, bit) |
| 421 |
} |
| 422 |
return |
| 423 |
{{end -}} |
| 424 |
{{end -}} |
| 425 |
default: |
| 426 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 427 |
} |
| 428 |
` |
| 429 |
|
| 430 |
complexAbs = `{{if eq .Kind.String "complex64" -}} |
| 431 |
{{else if eq .Kind.String "complex128" -}} |
| 432 |
{{end -}} |
| 433 |
` |
| 434 |
|
| 435 |
eReduceFirstRaw = `{{$name := .Name -}} |
| 436 |
switch t { |
| 437 |
{{range .Kinds -}} |
| 438 |
case {{reflectKind .}}: |
| 439 |
dt := data.{{sliceOf .}} |
| 440 |
rt := retVal.{{sliceOf .}} |
| 441 |
switch f := fn.(type){ |
| 442 |
case func([]{{asType .}}, []{{asType .}}): |
| 443 |
{{$name | unexport}}{{short .}}(dt, rt, split, size, f) |
| 444 |
case func({{asType .}}, {{asType .}}) {{asType .}}: |
| 445 |
generic{{$name}}{{short .}}(dt, rt, split, size, f) |
| 446 |
default: |
| 447 |
return errors.Errorf(reductionErrMsg, fn) |
| 448 |
} |
| 449 |
return nil |
| 450 |
{{end -}} |
| 451 |
default: |
| 452 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 453 |
} |
| 454 |
` |
| 455 |
|
| 456 |
eReduceLastRaw = `var ok bool |
| 457 |
{{$name := .Name -}} |
| 458 |
switch t { |
| 459 |
{{range .Kinds -}} |
| 460 |
case {{reflectKind .}}: |
| 461 |
var def {{asType .}} |
| 462 |
|
| 463 |
if def, ok = defaultValue.({{asType .}}); !ok { |
| 464 |
return errors.Errorf(defaultValueErrMsg, def, defaultValue, defaultValue) |
| 465 |
} |
| 466 |
dt := data.{{sliceOf .}} |
| 467 |
rt := retVal.{{sliceOf .}} |
| 468 |
switch f := fn.(type){ |
| 469 |
case func([]{{asType .}}) {{asType .}}: |
| 470 |
{{$name | unexport}}{{short .}}(dt, rt, dimSize, def, f) |
| 471 |
case func({{asType .}}, {{asType .}}) {{asType .}}: |
| 472 |
generic{{$name}}{{short .}}(dt, rt, dimSize, def, f) |
| 473 |
default: |
| 474 |
return errors.Errorf(reductionErrMsg, fn) |
| 475 |
} |
| 476 |
return nil |
| 477 |
{{end -}} |
| 478 |
default: |
| 479 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 480 |
} |
| 481 |
` |
| 482 |
|
| 483 |
eReduceDefaultRaw = `var ok bool |
| 484 |
{{$name := .Name -}} |
| 485 |
switch t { |
| 486 |
{{range .Kinds -}} |
| 487 |
case {{reflectKind .}}: |
| 488 |
var f func({{asType .}}, {{asType .}}) {{asType .}} |
| 489 |
if f, ok = fn.(func({{asType .}}, {{asType .}}) {{asType .}}); !ok { |
| 490 |
return errors.Errorf(reductionErrMsg, fn) |
| 491 |
} |
| 492 |
dt := data.{{sliceOf .}} |
| 493 |
rt := retVal.{{sliceOf .}} |
| 494 |
{{$name | unexport}}{{short .}}(dt, rt, dim0, dimSize, outerStride, stride, expected, f) |
| 495 |
return nil |
| 496 |
{{end -}} |
| 497 |
default: |
| 498 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 499 |
} |
| 500 |
` |
| 501 |
|
| 502 |
eReduceRaw = `var ok bool |
| 503 |
switch t{ |
| 504 |
{{range .Kinds -}} |
| 505 |
case {{reflectKind .}}: |
| 506 |
var f func({{asType .}}, {{asType .}}) {{asType .}} |
| 507 |
var def {{asType .}} |
| 508 |
if f, ok = fn.(func({{asType .}}, {{asType .}}) {{asType .}}); !ok { |
| 509 |
return nil, errors.Errorf(reductionErrMsg, fn) |
| 510 |
} |
| 511 |
if def, ok = defaultValue.({{asType .}}); !ok { |
| 512 |
return nil, errors.Errorf(defaultValueErrMsg, def, defaultValue, defaultValue) |
| 513 |
} |
| 514 |
retVal = Reduce{{short .}}(f, def, a.{{sliceOf .}}...) |
| 515 |
return |
| 516 |
{{end -}} |
| 517 |
default: |
| 518 |
return nil, errors.Errorf("Unsupported type %v for Reduce", t) |
| 519 |
} |
| 520 |
` |
| 521 |
|
| 522 |
eUnaryRaw = `{{$name := .Name -}} |
| 523 |
switch t { |
| 524 |
{{range .Kinds -}} |
| 525 |
case {{reflectKind .}}: |
| 526 |
{{$name}}{{short .}}(a.{{sliceOf .}}) |
| 527 |
return nil |
| 528 |
{{end -}} |
| 529 |
default: |
| 530 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 531 |
} |
| 532 |
` |
| 533 |
|
| 534 |
eUnaryIterRaw = `{{$name := .Name -}} |
| 535 |
switch t { |
| 536 |
{{range .Kinds -}} |
| 537 |
case {{reflectKind .}}: |
| 538 |
return {{$name}}{{short .}}(a.{{sliceOf .}}, ait) |
| 539 |
{{end -}} |
| 540 |
default: |
| 541 |
return errors.Errorf("Unsupported type %v for {{$name}}", t) |
| 542 |
} |
| 543 |
` |
| 544 |
|
| 545 |
eUnaryClampRaw = `var ok bool |
| 546 |
switch t { |
| 547 |
{{range .Kinds -}} |
| 548 |
case {{reflectKind .}}: |
| 549 |
var min, max {{asType .}} |
| 550 |
if min, ok = minVal.({{asType .}}); !ok { |
| 551 |
return errors.Wrap(errors.Errorf(typeMismatch, min, minVal), "Clamp() min") |
| 552 |
} |
| 553 |
if max, ok = maxVal.({{asType .}}); !ok { |
| 554 |
return errors.Wrap(errors.Errorf(typeMismatch, max, maxVal), "Clamp() max") |
| 555 |
} |
| 556 |
Clamp{{short .}}(a.{{sliceOf .}}, min, max) |
| 557 |
return nil |
| 558 |
{{end -}} |
| 559 |
default: |
| 560 |
return errors.Errorf("Unsupported type %v for Clamp", t) |
| 561 |
} |
| 562 |
` |
| 563 |
|
| 564 |
eUnaryClampIterRaw = `var ok bool |
| 565 |
switch t { |
| 566 |
{{range .Kinds -}} |
| 567 |
case {{reflectKind .}}: |
| 568 |
var min, max {{asType .}} |
| 569 |
if min, ok = minVal.({{asType .}}); !ok { |
| 570 |
return errors.Wrap(errors.Errorf(typeMismatch, min, minVal), "Clamp() min") |
| 571 |
} |
| 572 |
if max, ok = maxVal.({{asType .}}); !ok { |
| 573 |
return errors.Wrap(errors.Errorf(typeMismatch, max, maxVal), "Clamp() max") |
| 574 |
} |
| 575 |
return ClampIter{{short .}}(a.{{sliceOf .}}, ait, min, max) |
| 576 |
{{end -}} |
| 577 |
default: |
| 578 |
return errors.Errorf("Unsupported type %v for Clamp", t) |
| 579 |
} |
| 580 |
` |
| 581 |
|
| 582 |
eArgmaxRaw = `var next int |
| 583 |
{{$name := .Name -}} |
| 584 |
switch t { |
| 585 |
{{range .Kinds -}} |
| 586 |
case {{reflectKind .}}: |
| 587 |
data := a.{{sliceOf .}} |
| 588 |
tmp := make([]{{asType .}}, 0, lastSize) |
| 589 |
for next, err = it.Next(); err == nil; next, err = it.Next() { |
| 590 |
tmp = append(tmp, data[next]) |
| 591 |
if len(tmp) == lastSize { |
| 592 |
am := Arg{{$name}}{{short .}}(tmp) |
| 593 |
indices = append(indices, am) |
| 594 |
|
| 595 |
// reset |
| 596 |
tmp = tmp[:0] |
| 597 |
} |
| 598 |
} |
| 599 |
if _, ok := err.(NoOpError); ok { |
| 600 |
err = nil |
| 601 |
} |
| 602 |
return |
| 603 |
{{end -}} |
| 604 |
default: |
| 605 |
return nil, errors.Errorf("Unsupported type %v for Arg{{.Name}}", t) |
| 606 |
} |
| 607 |
` |
| 608 |
|
| 609 |
eArgmaxMaskedRaw = `newMask := make([]bool, 0, lastSize) |
| 610 |
var next int |
| 611 |
{{$name := .Name -}} |
| 612 |
switch t { |
| 613 |
{{range .Kinds -}} |
| 614 |
case {{reflectKind .}}: |
| 615 |
data := a.{{sliceOf .}} |
| 616 |
tmp := make([]{{asType .}}, 0, lastSize) |
| 617 |
for next, err = it.Next(); err == nil; next, err = it.Next() { |
| 618 |
tmp = append(tmp, data[next]) |
| 619 |
newMask = append(newMask, mask[next]) |
| 620 |
if len(tmp) == lastSize { |
| 621 |
am := Arg{{$name}}Masked{{short .}}(tmp, mask) |
| 622 |
indices = append(indices, am) |
| 623 |
|
| 624 |
// reset |
| 625 |
tmp = tmp[:0] |
| 626 |
newMask = newMask[:0] |
| 627 |
} |
| 628 |
} |
| 629 |
if _, ok := err.(NoOpError); ok { |
| 630 |
err = nil |
| 631 |
} |
| 632 |
return |
| 633 |
{{end -}} |
| 634 |
default: |
| 635 |
return nil, errors.Errorf("Unsupported type %v for Arg{{.Name}}", t) |
| 636 |
} |
| 637 |
` |
| 638 |
|
| 639 |
eArgmaxFlatRaw = `switch t { |
| 640 |
{{$name := .Name -}} |
| 641 |
{{range .Kinds -}} |
| 642 |
case {{reflectKind .}}: |
| 643 |
return Arg{{$name}}{{short .}}(a.{{sliceOf .}}) |
| 644 |
{{end -}} |
| 645 |
default: |
| 646 |
return -1 |
| 647 |
} |
| 648 |
` |
| 649 |
|
| 650 |
eArgmaxFlatMaskedRaw = `switch t { |
| 651 |
{{$name := .Name -}} |
| 652 |
{{range .Kinds -}} |
| 653 |
case {{reflectKind .}}: |
| 654 |
return Arg{{$name}}Masked{{short .}}(a.{{sliceOf .}}, mask) |
| 655 |
{{end -}} |
| 656 |
default: |
| 657 |
return -1 |
| 658 |
} |
| 659 |
` |
| 660 |
) |
| 661 |
|
| 662 |
var ( |
| 663 |
eArith *template.Template |
| 664 |
eArithIncr *template.Template |
| 665 |
eArithIter *template.Template |
| 666 |
eArithIterIncr *template.Template |
| 667 |
eArithRecv *template.Template |
| 668 |
|
| 669 |
eMap *template.Template |
| 670 |
eMapIter *template.Template |
| 671 |
|
| 672 |
eCmpBool *template.Template |
| 673 |
eCmpSame *template.Template |
| 674 |
eCmpBoolIter *template.Template |
| 675 |
eCmpSameIter *template.Template |
| 676 |
|
| 677 |
eMinMaxSame *template.Template |
| 678 |
eMinMaxIter *template.Template |
| 679 |
|
| 680 |
eReduce *template.Template |
| 681 |
eReduceFirst *template.Template |
| 682 |
eReduceLast *template.Template |
| 683 |
eReduceDefault *template.Template |
| 684 |
|
| 685 |
eUnary *template.Template |
| 686 |
eUnaryIter *template.Template |
| 687 |
eUnaryClamp *template.Template |
| 688 |
eUnaryClampIter *template.Template |
| 689 |
|
| 690 |
eArgmax *template.Template |
| 691 |
eArgmaxMasked *template.Template |
| 692 |
eArgmaxFlat *template.Template |
| 693 |
eArgmaxFlatMasked *template.Template |
| 694 |
) |
| 695 |
|
| 696 |
func init() { |
| 697 |
eArith = template.Must(template.New("eArith").Funcs(funcs).Parse(eArithRaw)) |
| 698 |
eArithIncr = template.Must(template.New("eArithIncr").Funcs(funcs).Parse(eArithIncrRaw)) |
| 699 |
eArithIter = template.Must(template.New("eArithIter").Funcs(funcs).Parse(eArithIterRaw)) |
| 700 |
eArithIterIncr = template.Must(template.New("eArithIterIncr").Funcs(funcs).Parse(eArithIterIncrRaw)) |
| 701 |
eArithRecv = template.Must(template.New("eArithRecv").Funcs(funcs).Parse(eArithRecvRaw)) |
| 702 |
|
| 703 |
eMap = template.Must(template.New("eMap").Funcs(funcs).Parse(eMapRaw)) |
| 704 |
eMapIter = template.Must(template.New("eMapIter").Funcs(funcs).Parse(eMapIterRaw)) |
| 705 |
|
| 706 |
eCmpBool = template.Must(template.New("eCmpBool").Funcs(funcs).Parse(eCmpBoolRaw)) |
| 707 |
eCmpSame = template.Must(template.New("eCmpSame").Funcs(funcs).Parse(eCmpSameRaw)) |
| 708 |
eCmpBoolIter = template.Must(template.New("eCmpBoolIter").Funcs(funcs).Parse(eCmpBoolIterRaw)) |
| 709 |
eCmpSameIter = template.Must(template.New("eCmpSameIter").Funcs(funcs).Parse(eCmpSameIterRaw)) |
| 710 |
|
| 711 |
eMinMaxSame = template.Must(template.New("eMinMaxSame").Funcs(funcs).Parse(eMinMaxSameRaw)) |
| 712 |
eMinMaxIter = template.Must(template.New("eMinMaxSameIter").Funcs(funcs).Parse(eMinMaxSameIterRaw)) |
| 713 |
|
| 714 |
eReduce = template.Must(template.New("eReduce").Funcs(funcs).Parse(eReduceRaw)) |
| 715 |
eReduceFirst = template.Must(template.New("eReduceFirst").Funcs(funcs).Parse(eReduceFirstRaw)) |
| 716 |
eReduceLast = template.Must(template.New("eReduceLast").Funcs(funcs).Parse(eReduceLastRaw)) |
| 717 |
eReduceDefault = template.Must(template.New("eReduceDefault").Funcs(funcs).Parse(eReduceDefaultRaw)) |
| 718 |
|
| 719 |
eUnary = template.Must(template.New("eUnary").Funcs(funcs).Parse(eUnaryRaw)) |
| 720 |
eUnaryIter = template.Must(template.New("eUnaryIter").Funcs(funcs).Parse(eUnaryIterRaw)) |
| 721 |
eUnaryClamp = template.Must(template.New("eUnaryClamp").Funcs(funcs).Parse(eUnaryClampRaw)) |
| 722 |
eUnaryClampIter = template.Must(template.New("eUnaryClampIter").Funcs(funcs).Parse(eUnaryClampIterRaw)) |
| 723 |
|
| 724 |
eArgmax = template.Must(template.New("argmax").Funcs(funcs).Parse(eArgmaxRaw)) |
| 725 |
eArgmaxMasked = template.Must(template.New("argmaxMasked").Funcs(funcs).Parse(eArgmaxMaskedRaw)) |
| 726 |
eArgmaxFlat = template.Must(template.New("argmaxFlat").Funcs(funcs).Parse(eArgmaxFlatRaw)) |
| 727 |
eArgmaxFlatMasked = template.Must(template.New("argmaxFlatMasked").Funcs(funcs).Parse(eArgmaxFlatMaskedRaw)) |
| 728 |
} |