vRp.CD2g_test/utils/combination/README.md

17 KiB
Raw Blame History

Combination

Go doc

combination 包提供了一些实用的组合函数。

目录导航

列出了该 package 下所有的函数及类型定义,可通过目录导航进行快捷跳转 ❤️

展开 / 折叠目录导航

包级函数定义

函数名称 描述
NewCombination 创建一个新的组合器
WithEvaluation 设置组合评估函数
NewMatcher 创建一个新的匹配器
WithMatcherEvaluation 设置匹配器评估函数
WithMatcherLeastLength 通过匹配最小长度的组合创建匹配器
WithMatcherLength 通过匹配长度的组合创建匹配器
WithMatcherMostLength 通过匹配最大长度的组合创建匹配器
WithMatcherIntervalLength 通过匹配长度区间的组合创建匹配器
WithMatcherContinuity 通过匹配连续的组合创建匹配器
WithMatcherSame 通过匹配相同的组合创建匹配器
WithMatcherNCarryM 通过匹配 N 携带 M 的组合创建匹配器
WithMatcherNCarryIndependentM 通过匹配 N 携带独立 M 的组合创建匹配器
NewValidator 创建一个新的校验器
WithValidatorHandle 通过特定的验证函数对组合进行验证
WithValidatorHandleLength 校验组合的长度是否符合要求
WithValidatorHandleLengthRange 校验组合的长度是否在指定的范围内
WithValidatorHandleLengthMin 校验组合的长度是否大于等于指定的最小值
WithValidatorHandleLengthMax 校验组合的长度是否小于等于指定的最大值
WithValidatorHandleLengthNot 校验组合的长度是否不等于指定的值
WithValidatorHandleTypeLength 校验组合成员类型数量是否为指定的值
WithValidatorHandleTypeLengthRange 校验组合成员类型数量是否在指定的范围内
WithValidatorHandleTypeLengthMin 校验组合成员类型数量是否大于等于指定的最小值
WithValidatorHandleTypeLengthMax 校验组合成员类型数量是否小于等于指定的最大值
WithValidatorHandleTypeLengthNot 校验组合成员类型数量是否不等于指定的值
WithValidatorHandleContinuous 校验组合成员是否连续
WithValidatorHandleContinuousNot 校验组合成员是否不连续
WithValidatorHandleGroupContinuous 校验组合成员是否能够按类型分组并且连续
WithValidatorHandleGroupContinuousN 校验组合成员是否能够按分组为 n 组类型并且连续
WithValidatorHandleNCarryM 校验组合成员是否匹配 N 携带相同的 M 的组合
WithValidatorHandleNCarryIndependentM 校验组合成员是否匹配 N 携带独立的 M 的组合

类型定义

类型 名称 描述
STRUCT Combination 用于从多个匹配器内提取组合的数据结构
STRUCT Option 组合器选项
INTERFACE Item 暂无描述...
STRUCT Matcher 用于从一组数据内提取组合的数据结构
STRUCT MatcherOption 匹配器选项
STRUCT Validator 用于对组合进行验证的校验器
STRUCT ValidatorOption 暂无描述...

详情信息

func NewCombination[T Item](options ...Option[T]) *Combination[T]

创建一个新的组合器


func WithEvaluation[T Item](evaluate func (items []T) float64) Option[T]

设置组合评估函数

  • 用于对组合进行评估,返回一个分值的评价函数
  • 通过该选项将设置所有匹配器的默认评估函数为该函数
  • 通过匹配器选项 WithMatcherEvaluation 可以覆盖该默认评估函数
  • 默认的评估函数将返回一个随机数

func NewMatcher[T Item](options ...MatcherOption[T]) *Matcher[T]

创建一个新的匹配器


func WithMatcherEvaluation[T Item](evaluate func (items []T) float64) MatcherOption[T]

设置匹配器评估函数

  • 用于对组合进行评估,返回一个分值的评价函数
  • 通过该选项将覆盖匹配器的默认(WithEvaluation)评估函数

func WithMatcherLeastLength[T Item](length int) MatcherOption[T]

通过匹配最小长度的组合创建匹配器

  • length: 组合的长度,表示需要匹配的组合最小数量

func WithMatcherLength[T Item](length int) MatcherOption[T]

通过匹配长度的组合创建匹配器

  • length: 组合的长度,表示需要匹配的组合数量

func WithMatcherMostLength[T Item](length int) MatcherOption[T]

通过匹配最大长度的组合创建匹配器

  • length: 组合的长度,表示需要匹配的组合最大数量

func WithMatcherIntervalLength[T Item](min int, max int) MatcherOption[T]

通过匹配长度区间的组合创建匹配器

  • min: 组合的最小长度,表示需要匹配的组合最小数量
  • max: 组合的最大长度,表示需要匹配的组合最大数量

func WithMatcherContinuity[T Item, Index generic.Number](getIndex func (item T) Index) MatcherOption[T]

通过匹配连续的组合创建匹配器

  • index: 用于获取组合中元素的索引值,用于判断是否连续

func WithMatcherSame[T Item, E generic.Ordered](count int, getType func (item T) E) MatcherOption[T]

通过匹配相同的组合创建匹配器

  • count: 组合中相同元素的数量,当 count <= 0 时,表示相同元素的数量不限
  • getType: 用于获取组合中元素的类型,用于判断是否相同

func WithMatcherNCarryM[T Item, E generic.Ordered](n int, m int, getType func (item T) E) MatcherOption[T]

通过匹配 N 携带 M 的组合创建匹配器

  • n: 组合中元素的数量表示需要匹配的组合数量n 的类型需要全部相同
  • m: 组合中元素的数量表示需要匹配的组合数量m 的类型需要全部相同
  • getType: 用于获取组合中元素的类型,用于判断是否相同

func WithMatcherNCarryIndependentM[T Item, E generic.Ordered](n int, m int, getType func (item T) E) MatcherOption[T]

通过匹配 N 携带独立 M 的组合创建匹配器

  • n: 组合中元素的数量表示需要匹配的组合数量n 的类型需要全部相同
  • m: 组合中元素的数量表示需要匹配的组合数量m 的类型无需全部相同
  • getType: 用于获取组合中元素的类型,用于判断是否相同

func NewValidator[T Item](options ...ValidatorOption[T]) *Validator[T]

创建一个新的校验器


func WithValidatorHandle[T Item](handle func (items []T) bool) ValidatorOption[T]

通过特定的验证函数对组合进行验证


func WithValidatorHandleLength[T Item](length int) ValidatorOption[T]

校验组合的长度是否符合要求


func WithValidatorHandleLengthRange[T Item](min int, max int) ValidatorOption[T]

校验组合的长度是否在指定的范围内


func WithValidatorHandleLengthMin[T Item](min int) ValidatorOption[T]

校验组合的长度是否大于等于指定的最小值


func WithValidatorHandleLengthMax[T Item](max int) ValidatorOption[T]

校验组合的长度是否小于等于指定的最大值


func WithValidatorHandleLengthNot[T Item](length int) ValidatorOption[T]

校验组合的长度是否不等于指定的值


func WithValidatorHandleTypeLength[T Item, E generic.Ordered](length int, getType func (item T) E) ValidatorOption[T]

校验组合成员类型数量是否为指定的值


func WithValidatorHandleTypeLengthRange[T Item, E generic.Ordered](min int, max int, getType func (item T) E) ValidatorOption[T]

校验组合成员类型数量是否在指定的范围内


func WithValidatorHandleTypeLengthMin[T Item, E generic.Ordered](min int, getType func (item T) E) ValidatorOption[T]

校验组合成员类型数量是否大于等于指定的最小值


func WithValidatorHandleTypeLengthMax[T Item, E generic.Ordered](max int, getType func (item T) E) ValidatorOption[T]

校验组合成员类型数量是否小于等于指定的最大值


func WithValidatorHandleTypeLengthNot[T Item, E generic.Ordered](length int, getType func (item T) E) ValidatorOption[T]

校验组合成员类型数量是否不等于指定的值


func WithValidatorHandleContinuous[T Item, Index generic.Integer](getIndex func (item T) Index) ValidatorOption[T]

校验组合成员是否连续


func WithValidatorHandleContinuousNot[T Item, Index generic.Integer](getIndex func (item T) Index) ValidatorOption[T]

校验组合成员是否不连续


func WithValidatorHandleGroupContinuous[T Item, E generic.Ordered, Index generic.Integer](getType func (item T) E, getIndex func (item T) Index) ValidatorOption[T]

校验组合成员是否能够按类型分组并且连续


func WithValidatorHandleGroupContinuousN[T Item, E generic.Ordered, Index generic.Integer](n int, getType func (item T) E, getIndex func (item T) Index) ValidatorOption[T]

校验组合成员是否能够按分组为 n 组类型并且连续


func WithValidatorHandleNCarryM[T Item, E generic.Ordered](n int, m int, getType func (item T) E) ValidatorOption[T]

校验组合成员是否匹配 N 携带相同的 M 的组合

  • n: 组合中元素的数量表示需要匹配的组合数量n 的类型需要全部相同
  • m: 组合中元素的数量表示需要匹配的组合数量m 的类型需要全部相同
  • getType: 用于获取组合中元素的类型,用于判断是否相同

func WithValidatorHandleNCarryIndependentM[T Item, E generic.Ordered](n int, m int, getType func (item T) E) ValidatorOption[T]

校验组合成员是否匹配 N 携带独立的 M 的组合

  • n: 组合中元素的数量表示需要匹配的组合数量n 的类型需要全部相同
  • m: 组合中元素的数量表示需要匹配的组合数量m 的类型无需全部相同
  • getType: 用于获取组合中元素的类型,用于判断是否相同

Combination STRUCT

用于从多个匹配器内提取组合的数据结构

type Combination[T Item] struct {
	evaluate func([]T) float64
	matchers map[string]*Matcher[T]
	priority []string
}

func (*Combination) NewMatcher(name string, options ...MatcherOption[T]) *Combination[T]

添加一个新的匹配器


func (*Combination) AddMatcher(name string, matcher *Matcher[T]) *Combination[T]

添加一个匹配器


func (*Combination) RemoveMatcher(name string) *Combination[T]

移除一个匹配器


func (*Combination) Combinations(items []T) (result [][]T)

从一组数据中提取所有符合匹配器规则的组合


func (*Combination) CombinationsToName(items []T) (result map[string][][]T)

从一组数据中提取所有符合匹配器规则的组合,并返回匹配器名称


func (*Combination) Best(items []T) (name string, result []T)

从一组数据中提取符合匹配器规则的最佳组合

查看 / 收起单元测试

func TestCombination_Best(t *testing.T) {
	combine := combination.NewCombination(combination.WithEvaluation(func(items []*Poker) float64 {
		var total float64
		for _, item := range items {
			total += float64(item.Point)
		}
		return total
	}))
	combine.NewMatcher("炸弹", combination.WithMatcherSame[*Poker, int](4, func(item *Poker) int {
		return item.Point
	})).NewMatcher("三带一", combination.WithMatcherNCarryM[*Poker, int](3, 1, func(item *Poker) int {
		return item.Point
	}))
	var cards = []*Poker{{Point: 2, Color: 1}, {Point: 2, Color: 2}, {Point: 2, Color: 3}, {Point: 3, Color: 4}, {Point: 4, Color: 1}, {Point: 4, Color: 2}, {Point: 5, Color: 3}, {Point: 6, Color: 4}, {Point: 7, Color: 1}, {Point: 8, Color: 2}, {Point: 9, Color: 3}, {Point: 10, Color: 4}, {Point: 11, Color: 1}, {Point: 12, Color: 2}, {Point: 13, Color: 3}, {Point: 10, Color: 3}, {Point: 11, Color: 2}, {Point: 12, Color: 1}, {Point: 13, Color: 4}, {Point: 10, Color: 2}}
	name, result := combine.Worst(cards)
	fmt.Println("best:", name)
	for _, item := range result {
		fmt.Println(item)
	}
}


func (*Combination) Worst(items []T) (name string, result []T)

从一组数据中提取符合匹配器规则的最差组合


Option STRUCT

组合器选项

type Option[T Item] func(*Combination[T])

Item INTERFACE

type Item interface{}

Matcher STRUCT

用于从一组数据内提取组合的数据结构

type Matcher[T Item] struct {
	evaluate func(items []T) float64
	filter   []func(items []T) [][]T
}

func (*Matcher) AddFilter(filter func (items []T) [][]T)

添加一个筛选器

  • 筛选器用于对组合进行筛选,返回一个二维数组,每个数组内的元素都是一个组合

func (*Matcher) Combinations(items []T) [][]T

从一组数据中提取所有符合筛选器规则的组合


func (*Matcher) Best(items []T) []T

从一组数据中提取符筛选器规则的最佳组合


func (*Matcher) Worst(items []T) []T

从一组数据中提取符筛选器规则的最差组合


MatcherOption STRUCT

匹配器选项

type MatcherOption[T Item] func(matcher *Matcher[T])

Validator STRUCT

用于对组合进行验证的校验器

type Validator[T Item] struct {
	vh []func(items []T) bool
}

func (*Validator) Validate(items []T) bool

校验组合是否符合要求

查看 / 收起单元测试

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))
}


ValidatorOption STRUCT

type ValidatorOption[T Item] func(validator *Validator[T])