go/types: add testdata/linalg.go2 (not yet type-checked)

Also, added missing copyright notices in a few places.

Change-Id: Iaa39c10a1821a189cef604d2442e6f7ed7ac68a8
This commit is contained in:
Robert Griesemer 2019-10-10 16:51:00 -07:00
parent 660f7c38d1
commit 34f753b9d1
5 changed files with 100 additions and 0 deletions

84
src/go/types/testdata/linalg.go2 vendored Normal file
View File

@ -0,0 +1,84 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// TODO(gri) add this file to the test suite once it type-checks.
package linalg
import "math"
// Numeric is a contract that matches any numeric type.
// It would likely be in a contracts package in the standard library.
contract Numeric(T) {
T int; T int8; T int16; T int32; T int64
T uint; T uint8; T uint16; T uint32; T uint64; T uintptr
T float32; T float64
T complex64; T complex128
}
func DotProduct(type T Numeric)(s1, s2 []T) T {
if len(s1) != len(s2) {
panic("DotProduct: slices of unequal length")
}
var r T
for i := range s1 {
r += s1[i] * s2[i]
}
return r
}
// NumericAbs matches numeric types with an Abs method.
contract NumericAbs(T) {
Numeric(T)
T Abs() T
}
// AbsDifference computes the absolute value of the difference of
// a and b, where the absolute value is determined by the Abs method.
func AbsDifference(type T NumericAbs)(a, b T) T {
d := a - b
return d.Abs()
}
// OrderedNumeric matches numeric types that support the < operator.
contract OrderedNumeric(T) {
T int; T int8; T int16; T int32; T int64
T uint; T uint8; T uint16; T uint32; T uint64; T uintptr
T float32; T float64
}
// Complex matches the two complex types, which do not have a < operator.
contract Complex(T) {
T complex64; T complex128
}
// OrderedAbs is a helper type that defines an Abs method for
// ordered numeric types.
type OrderedAbs(type T OrderedNumeric) T
func (a OrderedAbs(T)) Abs() T {
if a < 0 {
return -a
}
return a
}
// ComplexAbs is a helper type that defines an Abs method for
// complex types.
type ComplexAbs(type T Complex) T
func (a ComplexAbs(T)) Abs() T {
r := float64(real(a))
i := float64(imag(a))
d := math.Sqrt(r * r + i * i)
return T(complex(d, 0))
}
func OrderedAbsDifference(type T OrderedNumeric)(a, b T) T {
return T(AbsDifference(OrderedAbs(T)(a), OrderedAbs(T)(b)))
}
func ComplexAbsDifference(type T Complex)(a, b T) T {
return T(AbsDifference(ComplexAbs(T)(a), ComplexAbs(T)(b)))
}

View File

@ -1,3 +1,7 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package orderedmap provides an ordered map, implemented as a binary tree.
package orderedmap

View File

@ -1,3 +1,7 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file is like map.go2, but instead if importing chans, it contains
// the necessary functionality at the end of the file.

View File

@ -1,3 +1,7 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package slices implements various slice algorithms.
package slices

View File

@ -1,3 +1,7 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
type Pair(type K) struct {