vRp.CD2g_test/utils/sole/once.go

38 lines
829 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package sole
import "github.com/kercylan98/minotaur/utils/hash"
// NewOnce 创建一个用于数据取值去重的结构实例
func NewOnce[V any]() *Once[V] {
once := &Once[V]{
r: map[any]struct{}{},
}
return once
}
// Once 用于数据取值去重的结构体
type Once[V any] struct {
r map[any]struct{}
}
// Get 获取一个值,当该值已经被获取过的时候,返回 defaultValue否则返回 value
func (slf *Once[V]) Get(key any, value V, defaultValue V) V {
_, exist := slf.r[key]
if exist {
return defaultValue
}
slf.r[key] = struct{}{}
return value
}
// Reset 当 key 数量大于 0 时,将会重置对应 key 的记录,否则重置所有记录
func (slf *Once[V]) Reset(key ...any) {
if len(key) > 0 {
for _, k := range key {
delete(slf.r, k)
}
return
}
hash.Clear(slf.r)
}