diff --git a/stat/all.go b/stat/all.go new file mode 100644 index 0000000000000000000000000000000000000000..bc2a429f9aae0cd3b7e6ace4d0172483d538093d --- /dev/null +++ b/stat/all.go @@ -0,0 +1,47 @@ +package stat + +import "github.com/viterin/vek" + +// All 全部为真 +func All[T Number | ~bool](x []T) bool { + switch vs := any(x).(type) { + case []bool: + return vek.All(vs) + case []int8: + return __all_go(vs) + case []uint8: + return __all_go(vs) + case []int16: + return __all_go(vs) + case []uint16: + return __all_go(vs) + case []int32: + return __all_go(vs) + case []uint32: + return __all_go(vs) + case []int64: + return __all_go(vs) + case []uint64: + return __all_go(vs) + case []int: + return __all_go(vs) + case []uint: + return __all_go(vs) + case []uintptr: + return __all_go(vs) + case []float32: + return __all_go(vs) + case []float64: + return __all_go(vs) + } + return false +} + +func __all_go[T Number](x []T) bool { + for i := 0; i < len(x); i++ { + if x[i] == 0 { + return false + } + } + return true +} diff --git a/stat/all_test.go b/stat/all_test.go new file mode 100644 index 0000000000000000000000000000000000000000..cddf08150af3b55d665431e0fdcbceb61e3ee3ed --- /dev/null +++ b/stat/all_test.go @@ -0,0 +1,19 @@ +package stat + +import ( + "fmt" + "testing" +) + +func TestAll(t *testing.T) { + d1 := []bool{true, true} + d2 := []bool{true, false} + d3 := []uintptr{0, 1} + d4 := []int{1, 1} + d5 := []float64{1.0, 0} + fmt.Println(All(d1)) + fmt.Println(All(d2)) + fmt.Println(All(d3)) + fmt.Println(All(d4)) + fmt.Println(All(d5)) +}