代码拉取完成,页面将自动刷新
同步操作将从 Gitee 极速下载/generic 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package generic
import (
"golang.org/x/exp/constraints"
"github.com/segmentio/fasthash/fnv1a"
)
// EqualsFn is a function that returns whether 'a' and 'b' are equal.
type EqualsFn[T any] func(a, b T) bool
// LessFn is a function that returns whether 'a' is less than 'b'.
type LessFn[T any] func(a, b T) bool
// HashFn is a function that returns the hash of 't'.
type HashFn[T any] func(t T) uint64
// Equals wraps the '==' operator for comparable types.
func Equals[T comparable](a, b T) bool {
return a == b
}
// Less wraps the '<' operator for ordered types.
func Less[T constraints.Ordered](a, b T) bool {
return a < b
}
// Compare uses a less function to determine the ordering of 'a' and 'b'. It returns:
//
// * -1 if a < b
//
// * 1 if a > b
//
// * 0 if a == b
func Compare[T any](a, b T, less LessFn[T]) int {
if less(a, b) {
return -1
} else if less(b, a) {
return 1
}
return 0
}
// Max returns the max of a and b.
func Max[T constraints.Ordered](a, b T) T {
if a > b {
return a
}
return b
}
// Min returns the min of a and b.
func Min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
// Clamp returns x constrained within [lo:hi] range.
// If x compares less than lo, returns lo; otherwise if hi compares less than x, returns hi; otherwise returns v.
func Clamp[T constraints.Ordered](x, lo, hi T) T {
return Max(lo, Min(hi, x))
}
// MaxFunc returns the max of a and b using the less func.
func MaxFunc[T any](a, b T, less LessFn[T]) T {
if less(b, a) {
return a
}
return b
}
// MinFunc returns the min of a and b using the less func.
func MinFunc[T any](a, b T, less LessFn[T]) T {
if less(a, b) {
return a
}
return b
}
// ClampFunc returns x constrained within [lo:hi] range using the less func.
// If x compares less than lo, returns lo; otherwise if hi compares less than x, returns hi; otherwise returns v.
func ClampFunc[T any](x, lo, hi T, less LessFn[T]) T {
return MaxFunc(lo, MinFunc(hi, x, less), less)
}
func HashUint64(u uint64) uint64 {
return hash(u)
}
func HashUint32(u uint32) uint64 {
return hash(uint64(u))
}
func HashUint16(u uint16) uint64 {
return hash(uint64(u))
}
func HashUint8(u uint8) uint64 {
return hash(uint64(u))
}
func HashInt64(i int64) uint64 {
return hash(uint64(i))
}
func HashInt32(i int32) uint64 {
return hash(uint64(i))
}
func HashInt16(i int16) uint64 {
return hash(uint64(i))
}
func HashInt8(i int8) uint64 {
return hash(uint64(i))
}
func HashInt(i int) uint64 {
return hash(uint64(i))
}
func HashUint(i uint) uint64 {
return hash(uint64(i))
}
func HashString(s string) uint64 {
return fnv1a.HashString64(s)
}
func HashBytes(b []byte) uint64 {
return fnv1a.HashBytes64(b)
}
func hash(u uint64) uint64 {
u ^= u >> 33
u *= 0xff51afd7ed558ccd
u ^= u >> 33
u *= 0xc4ceb9fe1a85ec53
u ^= u >> 33
return u
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。