Documentation ¶
Index ¶
- func Clamp[T constraints.Ordered](x, lo, hi T) T
- func ClampFunc[T any](x, lo, hi T, less LessFn[T]) T
- func Compare[T any](a, b T, less LessFn[T]) int
- func Equals[T comparable](a, b T) bool
- func HashBytes(b []byte) uint64
- func HashInt(i int) uint64
- func HashString(s string) uint64
- func HashUint64(u uint64) uint64
- func Less[T constraints.Ordered](a, b T) bool
- func Max[T constraints.Ordered](a, b T) T
- func MaxFunc[T any](a, b T, less LessFn[T]) T
- func Min[T constraints.Ordered](a, b T) T
- func MinFunc[T any](a, b T, less LessFn[T]) T
- type EqualsFn
- type HashFn
- type LessFn
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clamp ¶
func Clamp[T constraints.Ordered](x, lo, hi T) T
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.
Example ¶
package main import ( "fmt" "time" "github.com/fufuok/utils/generic" ) func main() { fmt.Println(generic.Clamp(500, 400, 600)) fmt.Println(generic.Clamp(200, 400, 600)) fmt.Println(generic.Clamp(800, 400, 600)) fmt.Println(generic.Clamp(5*time.Second, 4*time.Second, 6*time.Second).Milliseconds()) fmt.Println(generic.Clamp(2*time.Second, 4*time.Second, 6*time.Second).Milliseconds()) fmt.Println(generic.Clamp(8*time.Second, 4*time.Second, 6*time.Second).Milliseconds()) fmt.Println(generic.Clamp(1.5, 1.4, 1.8)) fmt.Println(generic.Clamp(1.5, 1.8, 1.8)) fmt.Println(generic.Clamp(1.5, 2.1, 1.9)) }
Output: 500 400 600 5000 4000 6000 1.5 1.8 2.1
func ClampFunc ¶
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.
Example ¶
package main import ( "fmt" "math" "github.com/fufuok/utils/generic" ) func lessMagnitude(a, b float64) bool { return math.Abs(a) < math.Abs(b) } func main() { fmt.Println(generic.ClampFunc(1.5, 1.4, 1.8, lessMagnitude)) fmt.Println(generic.ClampFunc(1.5, 1.8, 1.8, lessMagnitude)) fmt.Println(generic.ClampFunc(1.5, 2.1, 1.9, lessMagnitude)) fmt.Println(generic.ClampFunc(-1.5, -1.4, -1.8, lessMagnitude)) fmt.Println(generic.ClampFunc(-1.5, -1.8, -1.8, lessMagnitude)) fmt.Println(generic.ClampFunc(-1.5, -2.1, -1.9, lessMagnitude)) fmt.Println(generic.ClampFunc(1.5, -1.5, -1.5, lessMagnitude)) }
Output: 1.5 1.8 2.1 -1.5 -1.8 -2.1 1.5
func Compare ¶
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 Equals ¶
func Equals[T comparable](a, b T) bool
Equals wraps the '==' operator for comparable types.
func HashString ¶
func HashUint64 ¶
func Less ¶
func Less[T constraints.Ordered](a, b T) bool
Less wraps the '<' operator for ordered types.
func Max ¶
func Max[T constraints.Ordered](a, b T) T
Max returns the max of a and b.
Example ¶
package main import ( "fmt" "time" "github.com/fufuok/utils/generic" ) func main() { fmt.Println(generic.Max(7, 3)) fmt.Println(generic.Max(2*time.Second, 3*time.Second).Milliseconds()) }
Output: 7 3000
func MaxFunc ¶
MaxFunc returns the max of a and b using the less func.
Example ¶
package main import ( "fmt" "math" "github.com/fufuok/utils/generic" ) func lessMagnitude(a, b float64) bool { return math.Abs(a) < math.Abs(b) } func main() { fmt.Println(generic.MaxFunc(2.5, -3.1, lessMagnitude)) }
Output: -3.1
func Min ¶
func Min[T constraints.Ordered](a, b T) T
Min returns the min of a and b.
Example ¶
package main import ( "fmt" "time" "github.com/fufuok/utils/generic" ) func main() { fmt.Println(generic.Min(7, 3)) fmt.Println(generic.Min(2*time.Second, 3*time.Second).Milliseconds()) }
Output: 3 2000
func MinFunc ¶
MinFunc returns the min of a and b using the less func.
Example ¶
package main import ( "fmt" "math" "github.com/fufuok/utils/generic" ) func lessMagnitude(a, b float64) bool { return math.Abs(a) < math.Abs(b) } func main() { fmt.Println(generic.MinFunc(2.5, -3.1, lessMagnitude)) }
Output: 2.5
Types ¶
Directories ¶
Path | Synopsis |
---|---|
Package avl provides an implementation of an AVL tree.
|
Package avl provides an implementation of an AVL tree. |
Package bimap provides an implementation of a bi-directional map.
|
Package bimap provides an implementation of a bi-directional map. |
Package btree provides an implementation of a B-tree.
|
Package btree provides an implementation of a B-tree. |
Package cache provides an implementation of a key-value store with a maximum size.
|
Package cache provides an implementation of a key-value store with a maximum size. |
Package constraints defines a set of useful constraints to be used with type parameters.
|
Package constraints defines a set of useful constraints to be used with type parameters. |
Package deepcopy implements the proposal https://go.dev/issue/51520.
|
Package deepcopy implements the proposal https://go.dev/issue/51520. |
Package hashmap provides an implementation of a hashmap.
|
Package hashmap provides an implementation of a hashmap. |
Package hashset provides an implementation of a hashset.
|
Package hashset provides an implementation of a hashset. |
Package heap provides an implementation of a binary heap.
|
Package heap provides an implementation of a binary heap. |
Package interval provides an implementation of an interval tree built using an augmented AVL tree.
|
Package interval provides an implementation of an interval tree built using an augmented AVL tree. |
Package list provides an implementation of a doubly-linked list with a front and back.
|
Package list provides an implementation of a doubly-linked list with a front and back. |
Package maps defines various functions useful with maps of any type.
|
Package maps defines various functions useful with maps of any type. |
Package mapset provides an implementation of a set using the built-in map.
|
Package mapset provides an implementation of a set using the built-in map. |
Package multimap provides an associative container that permits multiple entries with the same key.
|
Package multimap provides an associative container that permits multiple entries with the same key. |
Package queue provides an implementation of a First In First Out (FIFO) queue.
|
Package queue provides an implementation of a First In First Out (FIFO) queue. |
Package rope provides an implementation of a rope data structure.
|
Package rope provides an implementation of a rope data structure. |
Package slices defines various functions useful with slices of any type.
|
Package slices defines various functions useful with slices of any type. |
Package stack provides an implementation of a LIFO stack built using a resizing array.
|
Package stack provides an implementation of a LIFO stack built using a resizing array. |
Package trie provides an implementation of a ternary search trie.
|
Package trie provides an implementation of a ternary search trie. |