From f6873bd5dc59af9ec2997029ba30063d18b15238 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Wed, 2 Aug 2023 15:35:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20combination=20=E5=8C=85=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20Validator=20=E6=A0=A1=E9=AA=8C=E5=99=A8=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=A0=A1=E9=AA=8C=E7=BB=84=E5=90=88=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=8C=B9=E9=85=8D=EF=BC=8C=E5=8F=96=E4=BB=A3=20poker.?= =?UTF-8?q?Rule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/combination/validator.go | 25 ++++ utils/combination/validator_options.go | 193 +++++++++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 utils/combination/validator.go create mode 100644 utils/combination/validator_options.go diff --git a/utils/combination/validator.go b/utils/combination/validator.go new file mode 100644 index 0000000..9ab5777 --- /dev/null +++ b/utils/combination/validator.go @@ -0,0 +1,25 @@ +package combination + +// NewValidator 创建一个新的校验器 +func NewValidator[T Item](options ...ValidatorOption[T]) *Validator[T] { + var validator = &Validator[T]{} + for _, option := range options { + option(validator) + } + return validator +} + +// Validator 用于对组合进行验证的校验器 +type Validator[T Item] struct { + vh []func(items []T) bool // 用于对组合进行验证的函数 +} + +// Validate 校验组合是否符合要求 +func (slf *Validator[T]) Validate(items []T) bool { + for _, handle := range slf.vh { + if !handle(items) { + return false + } + } + return true +} diff --git a/utils/combination/validator_options.go b/utils/combination/validator_options.go new file mode 100644 index 0000000..669e309 --- /dev/null +++ b/utils/combination/validator_options.go @@ -0,0 +1,193 @@ +package combination + +import ( + "github.com/kercylan98/minotaur/utils/generic" + "github.com/kercylan98/minotaur/utils/maths" +) + +type ValidatorOption[T Item] func(validator *Validator[T]) + +// WithValidatorHandle 通过特定的验证函数对组合进行验证 +func WithValidatorHandle[T Item](handle func(items []T) bool) ValidatorOption[T] { + return func(validator *Validator[T]) { + validator.vh = append(validator.vh, handle) + } +} + +// WithValidatorHandleLength 校验组合的长度是否符合要求 +func WithValidatorHandleLength[T Item](length int) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + return len(items) == length + }) +} + +// WithValidatorHandleLengthRange 校验组合的长度是否在指定的范围内 +func WithValidatorHandleLengthRange[T Item](min, max int) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + return len(items) >= min && len(items) <= max + }) +} + +// WithValidatorHandleLengthMin 校验组合的长度是否大于等于指定的最小值 +func WithValidatorHandleLengthMin[T Item](min int) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + return len(items) >= min + }) +} + +// WithValidatorHandleLengthMax 校验组合的长度是否小于等于指定的最大值 +func WithValidatorHandleLengthMax[T Item](max int) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + return len(items) <= max + }) +} + +// WithValidatorHandleLengthNot 校验组合的长度是否不等于指定的值 +func WithValidatorHandleLengthNot[T Item](length int) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + return len(items) != length + }) +} + +// WithValidatorHandleTypeLength 校验组合成员类型数量是否为指定的值 +func WithValidatorHandleTypeLength[T Item, E generic.Ordered](length int, getType func(item T) E) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + var types = make(map[E]bool) + for _, item := range items { + types[getType(item)] = true + if len(types) > length { + return false + } + } + return len(items) == length + }) +} + +// WithValidatorHandleTypeLengthRange 校验组合成员类型数量是否在指定的范围内 +func WithValidatorHandleTypeLengthRange[T Item, E generic.Ordered](min, max int, getType func(item T) E) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + var types = make(map[E]bool) + for _, item := range items { + types[getType(item)] = true + if len(types) > max { + return false + } + } + return len(types) >= min && len(types) <= max + }) +} + +// WithValidatorHandleTypeLengthMin 校验组合成员类型数量是否大于等于指定的最小值 +func WithValidatorHandleTypeLengthMin[T Item, E generic.Ordered](min int, getType func(item T) E) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + var types = make(map[E]bool) + for _, item := range items { + types[getType(item)] = true + if len(types) > min { + return false + } + } + return len(types) >= min + }) +} + +// WithValidatorHandleTypeLengthMax 校验组合成员类型数量是否小于等于指定的最大值 +func WithValidatorHandleTypeLengthMax[T Item, E generic.Ordered](max int, getType func(item T) E) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + var types = make(map[E]bool) + for _, item := range items { + types[getType(item)] = true + if len(types) > max { + return false + } + } + return len(types) <= max + }) +} + +// WithValidatorHandleTypeLengthNot 校验组合成员类型数量是否不等于指定的值 +func WithValidatorHandleTypeLengthNot[T Item, E generic.Ordered](length int, getType func(item T) E) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + var types = make(map[E]bool) + for _, item := range items { + types[getType(item)] = true + if len(types) > length { + return false + } + } + return len(types) != length + }) +} + +// WithValidatorHandleContinuous 校验组合成员是否连续 +func WithValidatorHandleContinuous[T Item, Index generic.Integer](getIndex func(item T) Index) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + index := make([]Index, len(items)) + for i, item := range items { + index[i] = getIndex(item) + } + return maths.IsContinuityWithSort(index) + }) +} + +// WithValidatorHandleContinuousNot 校验组合成员是否不连续 +func WithValidatorHandleContinuousNot[T Item, Index generic.Integer](getIndex func(item T) Index) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + index := make([]Index, len(items)) + for i, item := range items { + index[i] = getIndex(item) + } + return !maths.IsContinuityWithSort(index) + }) +} + +// WithValidatorHandleGroupContinuous 校验组合成员是否能够按类型分组并且连续 +func WithValidatorHandleGroupContinuous[T Item, E generic.Ordered, Index generic.Integer](getType func(item T) E, getIndex func(item T) Index) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + var types = make(map[E]int) + var index = make([]Index, len(types)) + for _, item := range items { + e := getType(item) + types[e]++ + if types[e] == 1 { + index = append(index, getIndex(item)) + } + } + var n = -1 + for _, i := range types { + if n == -1 { + n = i + } else if n != i { + return false + } + } + return maths.IsContinuityWithSort(index) + }) +} + +// WithValidatorHandleGroupContinuousN 校验组合成员是否能够按分组为 n 组类型并且连续 +func WithValidatorHandleGroupContinuousN[T Item, E generic.Ordered, Index generic.Integer](n int, getType func(item T) E, getIndex func(item T) Index) ValidatorOption[T] { + return WithValidatorHandle[T](func(items []T) bool { + var types = make(map[E]int) + var index = make([]Index, len(types)) + for _, item := range items { + e := getType(item) + types[e]++ + if types[e] == 1 { + index = append(index, getIndex(item)) + } + } + if n != len(types) { + return false + } + var n = -1 + for _, i := range types { + if n == -1 { + n = i + } else if n != i { + return false + } + } + return maths.IsContinuityWithSort(index) + }) +}