From 87a1ca90bd80f9a2ff4ef06d56d3e7c0ce77a4b3 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Wed, 2 Aug 2023 15:43:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20combination=20=E5=8C=85=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20WithValidatorHandleNCarryM=E3=80=81WithValidatorHan?= =?UTF-8?q?dleNCarryIndependentM=20=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/combination/validator_options.go | 73 ++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/utils/combination/validator_options.go b/utils/combination/validator_options.go index 669e309..3be1105 100644 --- a/utils/combination/validator_options.go +++ b/utils/combination/validator_options.go @@ -191,3 +191,76 @@ func WithValidatorHandleGroupContinuousN[T Item, E generic.Ordered, Index generi return maths.IsContinuityWithSort(index) }) } + +// WithValidatorHandleNCarryM 校验组合成员是否匹配 N 携带相同的 M 的组合 +// - n: 组合中元素的数量,表示需要匹配的组合数量,n 的类型需要全部相同 +// - m: 组合中元素的数量,表示需要匹配的组合数量,m 的类型需要全部相同 +// - getType: 用于获取组合中元素的类型,用于判断是否相同 +func WithValidatorHandleNCarryM[T Item, E generic.Ordered](n, m int, getType func(item T) E) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + if len(items) != n+m { + return false // 总数量不符合 n + m + } + + typeCount := make(map[E]int) + for _, item := range items { + t := getType(item) + typeCount[t]++ + } + + // 检查是否有 n 个相同类型的元素 + foundN := false + for _, count := range typeCount { + if count == n { + foundN = true + break + } + } + + if !foundN { + return false // 没有找到 n 个相同类型的元素 + } + + // 检查剩余的元素数量是否为 m + remaining := len(items) - n + if remaining != m { + return false // 剩余元素数量不为 m + } + + return true // 符合 N 带 M 的条件 + }) +} + +// WithValidatorHandleNCarryIndependentM 校验组合成员是否匹配 N 携带独立的 M 的组合 +// - n: 组合中元素的数量,表示需要匹配的组合数量,n 的类型需要全部相同 +// - m: 组合中元素的数量,表示需要匹配的组合数量,m 的类型无需全部相同 +// - getType: 用于获取组合中元素的类型,用于判断是否相同 +func WithValidatorHandleNCarryIndependentM[T Item, E generic.Ordered](n, m int, getType func(item T) E) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + if len(items) != n+m { + return false // 总数量不符合 n + m + } + + typeCount := make(map[E]int) + for _, item := range items { + t := getType(item) + typeCount[t]++ + } + + if len(typeCount) != 2 { + return false + } + + foundN := false + foundM := false + for _, count := range typeCount { + if count == n { + foundN = true + } else if count == m { + foundM = true + } + } + + return foundN && foundM + }) +}