Combination
combination 包提供了一些实用的组合函数。
目录
列出了该 package 下所有的函数,可通过目录进行快捷跳转 ❤️
展开 / 折叠目录
包级函数定义
结构体定义
| 结构体 | 描述 |
|---|---|
| Combination | 用于从多个匹配器内提取组合的数据结构 |
| Option | 组合器选项 |
| Item | 暂无描述... |
| Matcher | 用于从一组数据内提取组合的数据结构 |
| MatcherOption | 匹配器选项 |
| Validator | 用于对组合进行验证的校验器 |
| ValidatorOption | 暂无描述... |
func NewCombination(options ...Option[T]) *Combination[T]
创建一个新的组合器
func WithEvaluation(evaluate func (items []T) float64) Option[T]
设置组合评估函数
- 用于对组合进行评估,返回一个分值的评价函数
- 通过该选项将设置所有匹配器的默认评估函数为该函数
- 通过匹配器选项 WithMatcherEvaluation 可以覆盖该默认评估函数
- 默认的评估函数将返回一个随机数
func NewMatcher(options ...MatcherOption[T]) *Matcher[T]
创建一个新的匹配器
func WithMatcherEvaluation(evaluate func (items []T) float64) MatcherOption[T]
设置匹配器评估函数
- 用于对组合进行评估,返回一个分值的评价函数
- 通过该选项将覆盖匹配器的默认(WithEvaluation)评估函数
func WithMatcherLeastLength(length int) MatcherOption[T]
通过匹配最小长度的组合创建匹配器
- length: 组合的长度,表示需要匹配的组合最小数量
func WithMatcherLength(length int) MatcherOption[T]
通过匹配长度的组合创建匹配器
- length: 组合的长度,表示需要匹配的组合数量
func WithMatcherMostLength(length int) MatcherOption[T]
通过匹配最大长度的组合创建匹配器
- length: 组合的长度,表示需要匹配的组合最大数量
func WithMatcherIntervalLength(min int, max int) MatcherOption[T]
通过匹配长度区间的组合创建匹配器
- min: 组合的最小长度,表示需要匹配的组合最小数量
- max: 组合的最大长度,表示需要匹配的组合最大数量
func WithMatcherContinuity(getIndex func (item T) Index) MatcherOption[T]
通过匹配连续的组合创建匹配器
- index: 用于获取组合中元素的索引值,用于判断是否连续
func WithMatcherSame(count int, getType func (item T) E) MatcherOption[T]
通过匹配相同的组合创建匹配器
- count: 组合中相同元素的数量,当 count <= 0 时,表示相同元素的数量不限
- getType: 用于获取组合中元素的类型,用于判断是否相同
func WithMatcherNCarryM(n int, m int, getType func (item T) E) MatcherOption[T]
通过匹配 N 携带 M 的组合创建匹配器
- n: 组合中元素的数量,表示需要匹配的组合数量,n 的类型需要全部相同
- m: 组合中元素的数量,表示需要匹配的组合数量,m 的类型需要全部相同
- getType: 用于获取组合中元素的类型,用于判断是否相同
func WithMatcherNCarryIndependentM(n int, m int, getType func (item T) E) MatcherOption[T]
通过匹配 N 携带独立 M 的组合创建匹配器
- n: 组合中元素的数量,表示需要匹配的组合数量,n 的类型需要全部相同
- m: 组合中元素的数量,表示需要匹配的组合数量,m 的类型无需全部相同
- getType: 用于获取组合中元素的类型,用于判断是否相同
func NewValidator(options ...ValidatorOption[T]) *Validator[T]
创建一个新的校验器
func WithValidatorHandle(handle func (items []T) bool) ValidatorOption[T]
通过特定的验证函数对组合进行验证
func WithValidatorHandleLength(length int) ValidatorOption[T]
校验组合的长度是否符合要求
func WithValidatorHandleLengthRange(min int, max int) ValidatorOption[T]
校验组合的长度是否在指定的范围内
func WithValidatorHandleLengthMin(min int) ValidatorOption[T]
校验组合的长度是否大于等于指定的最小值
func WithValidatorHandleLengthMax(max int) ValidatorOption[T]
校验组合的长度是否小于等于指定的最大值
func WithValidatorHandleLengthNot(length int) ValidatorOption[T]
校验组合的长度是否不等于指定的值
func WithValidatorHandleTypeLength(length int, getType func (item T) E) ValidatorOption[T]
校验组合成员类型数量是否为指定的值
func WithValidatorHandleTypeLengthRange(min int, max int, getType func (item T) E) ValidatorOption[T]
校验组合成员类型数量是否在指定的范围内
func WithValidatorHandleTypeLengthMin(min int, getType func (item T) E) ValidatorOption[T]
校验组合成员类型数量是否大于等于指定的最小值
func WithValidatorHandleTypeLengthMax(max int, getType func (item T) E) ValidatorOption[T]
校验组合成员类型数量是否小于等于指定的最大值
func WithValidatorHandleTypeLengthNot(length int, getType func (item T) E) ValidatorOption[T]
校验组合成员类型数量是否不等于指定的值
func WithValidatorHandleContinuous(getIndex func (item T) Index) ValidatorOption[T]
校验组合成员是否连续
func WithValidatorHandleContinuousNot(getIndex func (item T) Index) ValidatorOption[T]
校验组合成员是否不连续
func WithValidatorHandleGroupContinuous(getType func (item T) E, getIndex func (item T) Index) ValidatorOption[T]
校验组合成员是否能够按类型分组并且连续
func WithValidatorHandleGroupContinuousN(n int, getType func (item T) E, getIndex func (item T) Index) ValidatorOption[T]
校验组合成员是否能够按分组为 n 组类型并且连续
func WithValidatorHandleNCarryM(n int, m int, getType func (item T) E) ValidatorOption[T]
校验组合成员是否匹配 N 携带相同的 M 的组合
- n: 组合中元素的数量,表示需要匹配的组合数量,n 的类型需要全部相同
- m: 组合中元素的数量,表示需要匹配的组合数量,m 的类型需要全部相同
- getType: 用于获取组合中元素的类型,用于判断是否相同
func WithValidatorHandleNCarryIndependentM(n int, m int, getType func (item T) E) ValidatorOption[T]
校验组合成员是否匹配 N 携带独立的 M 的组合
- n: 组合中元素的数量,表示需要匹配的组合数量,n 的类型需要全部相同
- m: 组合中元素的数量,表示需要匹配的组合数量,m 的类型无需全部相同
- getType: 用于获取组合中元素的类型,用于判断是否相同
Combination
用于从多个匹配器内提取组合的数据结构
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 (*Combination) Worst(items []T) (name string, result []T)
从一组数据中提取符合匹配器规则的最差组合
Option
组合器选项
type Option[T Item] struct{}
Item
type Item struct{}
Matcher
用于从一组数据内提取组合的数据结构
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
匹配器选项
type MatcherOption[T Item] struct{}
Validator
用于对组合进行验证的校验器
type Validator[T Item] struct {
vh []func(items []T) bool
}
func (*Validator) Validate(items []T) bool
校验组合是否符合要求
ValidatorOption
type ValidatorOption[T Item] struct{}