fix: combination.WithValidatorHandleNCarryM 修复 M 允许类型不同的问题

This commit is contained in:
kercylan98 2023-08-02 18:04:31 +08:00
parent faac7b27bb
commit 0db1e5c30b
2 changed files with 76 additions and 34 deletions

View File

@ -197,6 +197,40 @@ func WithValidatorHandleGroupContinuousN[T Item, E generic.Ordered, Index generi
// - m: 组合中元素的数量表示需要匹配的组合数量m 的类型需要全部相同 // - m: 组合中元素的数量表示需要匹配的组合数量m 的类型需要全部相同
// - getType: 用于获取组合中元素的类型,用于判断是否相同 // - getType: 用于获取组合中元素的类型,用于判断是否相同
func WithValidatorHandleNCarryM[T Item, E generic.Ordered](n, m int, getType func(item T) E) ValidatorOption[T] { 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]++
}
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
})
}
// 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 { return WithValidatorHandle[T](func(items []T) bool {
if len(items) != n+m { if len(items) != n+m {
return false // 总数量不符合 n + m return false // 总数量不符合 n + m
@ -230,37 +264,3 @@ func WithValidatorHandleNCarryM[T Item, E generic.Ordered](n, m int, getType fun
return true // 符合 N 带 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
})
}

View File

@ -0,0 +1,42 @@
package combination_test
import (
"fmt"
"github.com/kercylan98/minotaur/utils/combination"
"github.com/kercylan98/minotaur/utils/super"
"testing"
)
type Card struct {
Point string
Color string
}
func TestValidator_Validate(t *testing.T) {
v := combination.NewValidator[*Card](
combination.WithValidatorHandleContinuous[*Card, int](func(item *Card) int {
switch item.Point {
case "A":
return 1
case "2", "3", "4", "5", "6", "7", "8", "9", "10":
return super.StringToInt(item.Point)
case "J":
return 11
case "Q":
return 12
case "K":
return 13
}
return -1
}),
combination.WithValidatorHandleLength[*Card](3),
)
cards := []*Card{
{Point: "2", Color: "Spade"},
{Point: "4", Color: "Heart"},
{Point: "3", Color: "Diamond"},
}
fmt.Println(v.Validate(cards))
}