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

     1  package num
     2  
     3  import (
     4  	"gitee.com/quant1x/num/x64"
     5  )
     6  
     7  // Count 统计
     8  func Count[T Number | ~bool](x []T) int {
     9  	switch vs := any(x).(type) {
    10  	case []bool:
    11  		return x64.Count(vs)
    12  	case []int8:
    13  		return __count_go(vs)
    14  	case []uint8:
    15  		return __count_go(vs)
    16  	case []int16:
    17  		return __count_go(vs)
    18  	case []uint16:
    19  		return __count_go(vs)
    20  	case []int32:
    21  		return __count_go(vs)
    22  	case []uint32:
    23  		return __count_go(vs)
    24  	case []int64:
    25  		return __count_go(vs)
    26  	case []uint64:
    27  		return __count_go(vs)
    28  	case []int:
    29  		return __count_go(vs)
    30  	case []uint:
    31  		return __count_go(vs)
    32  	case []uintptr:
    33  		return __count_go(vs)
    34  	case []float32:
    35  		return __count_go(vs)
    36  	case []float64:
    37  		return __count_go(vs)
    38  	}
    39  	return 0
    40  }
    41  
    42  func __count_go[T Number](x []T) int {
    43  	cnt := 0
    44  	for i := 0; i < len(x); i++ {
    45  		if x[i] != 0 {
    46  			cnt += 1
    47  		}
    48  	}
    49  	return cnt
    50  }