Source file test/typeparam/sliceimp.dir/a.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package a
     6  
     7  type Ordered interface {
     8  	~int | ~int8 | ~int16 | ~int32 | ~int64 |
     9  		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
    10  		~float32 | ~float64 |
    11  		~string
    12  }
    13  
    14  // Max returns the maximum of two values of some ordered type.
    15  func Max[T Ordered](a, b T) T {
    16  	if a > b {
    17  		return a
    18  	}
    19  	return b
    20  }
    21  
    22  // Min returns the minimum of two values of some ordered type.
    23  func Min[T Ordered](a, b T) T {
    24  	if a < b {
    25  		return a
    26  	}
    27  	return b
    28  }
    29  
    30  // Equal reports whether two slices are equal: the same length and all
    31  // elements equal. All floating point NaNs are considered equal.
    32  func Equal[Elem comparable](s1, s2 []Elem) bool {
    33  	if len(s1) != len(s2) {
    34  		return false
    35  	}
    36  	for i, v1 := range s1 {
    37  		v2 := s2[i]
    38  		if v1 != v2 {
    39  			isNaN := func(f Elem) bool { return f != f }
    40  			if !isNaN(v1) || !isNaN(v2) {
    41  				return false
    42  			}
    43  		}
    44  	}
    45  	return true
    46  }
    47  
    48  // EqualFn reports whether two slices are equal using a comparison
    49  // function on each element.
    50  func EqualFn[Elem any](s1, s2 []Elem, eq func(Elem, Elem) bool) bool {
    51  	if len(s1) != len(s2) {
    52  		return false
    53  	}
    54  	for i, v1 := range s1 {
    55  		v2 := s2[i]
    56  		if !eq(v1, v2) {
    57  			return false
    58  		}
    59  	}
    60  	return true
    61  }
    62  
    63  // Map turns a []Elem1 to a []Elem2 using a mapping function.
    64  func Map[Elem1, Elem2 any](s []Elem1, f func(Elem1) Elem2) []Elem2 {
    65  	r := make([]Elem2, len(s))
    66  	for i, v := range s {
    67  		r[i] = f(v)
    68  	}
    69  	return r
    70  }
    71  
    72  // Reduce reduces a []Elem1 to a single value of type Elem2 using
    73  // a reduction function.
    74  func Reduce[Elem1, Elem2 any](s []Elem1, initializer Elem2, f func(Elem2, Elem1) Elem2) Elem2 {
    75  	r := initializer
    76  	for _, v := range s {
    77  		r = f(r, v)
    78  	}
    79  	return r
    80  }
    81  
    82  // Filter filters values from a slice using a filter function.
    83  func Filter[Elem any](s []Elem, f func(Elem) bool) []Elem {
    84  	var r []Elem
    85  	for _, v := range s {
    86  		if f(v) {
    87  			r = append(r, v)
    88  		}
    89  	}
    90  	return r
    91  }
    92  
    93  // Max returns the maximum element in a slice of some ordered type.
    94  // If the slice is empty it returns the zero value of the element type.
    95  func SliceMax[Elem Ordered](s []Elem) Elem {
    96  	if len(s) == 0 {
    97  		var zero Elem
    98  		return zero
    99  	}
   100  	return Reduce(s[1:], s[0], Max[Elem])
   101  }
   102  
   103  // Min returns the minimum element in a slice of some ordered type.
   104  // If the slice is empty it returns the zero value of the element type.
   105  func SliceMin[Elem Ordered](s []Elem) Elem {
   106  	if len(s) == 0 {
   107  		var zero Elem
   108  		return zero
   109  	}
   110  	return Reduce(s[1:], s[0], Min[Elem])
   111  }
   112  
   113  // Append adds values to the end of a slice, returning a new slice.
   114  // This is like the predeclared append function; it's an example
   115  // of how to write it using generics. We used to write code like
   116  // this before append was added to the language, but we had to write
   117  // a separate copy for each type.
   118  func Append[T any](s []T, t ...T) []T {
   119  	lens := len(s)
   120  	tot := lens + len(t)
   121  	if tot <= cap(s) {
   122  		s = s[:tot]
   123  	} else {
   124  		news := make([]T, tot, tot+tot/2)
   125  		Copy(news, s)
   126  		s = news
   127  	}
   128  	Copy(s[lens:tot], t)
   129  	return s
   130  }
   131  
   132  // Copy copies values from t to s, stopping when either slice is full,
   133  // returning the number of values copied. This is like the predeclared
   134  // copy function; it's an example of how to write it using generics.
   135  func Copy[T any](s, t []T) int {
   136  	i := 0
   137  	for ; i < len(s) && i < len(t); i++ {
   138  		s[i] = t[i]
   139  	}
   140  	return i
   141  }
   142  

View as plain text