From 767e8ceaf542a28868aa171654a46ea5cb75d8ac Mon Sep 17 00:00:00 2001 From: wangfeng Date: Mon, 13 Feb 2023 12:04:44 +0800 Subject: [PATCH] =?UTF-8?q?#I6ENFJ=20=E5=AE=9E=E7=8E=B0All=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stat/all.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ stat/all_test.go | 19 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 stat/all.go create mode 100644 stat/all_test.go diff --git a/stat/all.go b/stat/all.go new file mode 100644 index 0000000..bc2a429 --- /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 0000000..cddf081 --- /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)) +} -- Gitee