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

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