gitee.com/quant1x/num@v0.3.2/max.go (about)

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/x32"
     5  	"gitee.com/quant1x/num/x64"
     6  )
     7  
     8  // Max 纵向计算x最大值
     9  func Max[T Number](x []T) T {
    10  	return UnaryOperations1[T](x, x32.Max, x64.Max, __go_max[T])
    11  }
    12  
    13  func __go_max[T Number | ~string](x []T) T {
    14  	maxValue := x[0]
    15  	for _, v := range x[1:] {
    16  		if v > maxValue {
    17  			maxValue = v
    18  		}
    19  	}
    20  	return maxValue
    21  }
    22  
    23  func Max2[T BaseType](x []T) T {
    24  	var d any
    25  	switch vs := any(x).(type) {
    26  	case []float32:
    27  		d = Max(vs)
    28  	case []float64:
    29  		d = Max(vs)
    30  	case []int:
    31  		d = Max(vs)
    32  	case []int8:
    33  		d = Max(vs)
    34  	case []int16:
    35  		d = Max(vs)
    36  	case []int32:
    37  		d = Max(vs)
    38  	case []int64:
    39  		d = Max(vs)
    40  	case []uint:
    41  		d = Max(vs)
    42  	case []uint8:
    43  		d = Max(vs)
    44  	case []uint16:
    45  		d = Max(vs)
    46  	case []uint32:
    47  		d = Max(vs)
    48  	case []uint64:
    49  		d = Max(vs)
    50  	case []uintptr:
    51  		d = Max(vs)
    52  	case []string:
    53  		d = __go_max(vs)
    54  	default:
    55  		// 其它类型原样返回
    56  		panic(TypeError(any(x)))
    57  	}
    58  
    59  	return d.(T)
    60  }