github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bcomparator/comparator.go (about) 1 package bcomparator 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/songzhibin97/go-baseutils/base/bmath" 9 "github.com/songzhibin97/go-baseutils/base/bternaryexpr" 10 "github.com/songzhibin97/go-baseutils/base/btype" 11 ) 12 13 type Comparator[T any] func(a, b T) int 14 15 func (c Comparator[T]) Marshal(t T) ([]byte, error) { 16 return json.Marshal(t) 17 } 18 19 type temporary[T any] struct { 20 Key T `json:"key"` 21 } 22 23 func (c Comparator[T]) Unmarshal(data []byte, v *T) error { 24 err := json.Unmarshal(data, v) 25 if err != nil { 26 switch err.(type) { 27 case *json.SyntaxError: 28 var temp temporary[T] 29 data = []byte(fmt.Sprintf(`{"key": "%s"}`, string(data))) 30 err = json.Unmarshal(data, &temp) 31 if err != nil { 32 return err 33 } 34 *v = temp.Key 35 default: 36 return err 37 } 38 } 39 return nil 40 } 41 42 func OrderedComparator[T btype.Ordered]() Comparator[T] { 43 return func(a T, b T) int { 44 return bternaryexpr.TernaryExpr[int](a == b, 0, bternaryexpr.TernaryExpr[int](a > b, 1, -1)) 45 } 46 } 47 48 func ReverseComparator[T any](comparator Comparator[T]) Comparator[T] { 49 return func(a T, b T) int { 50 return comparator(a, b) * -1 51 } 52 } 53 54 // ------------------------------------------------ --------------------------------------------------------------------- 55 56 // StringComparator 比较字符串 57 func StringComparator() Comparator[string] { 58 return strings.Compare 59 } 60 61 // ------------------------------------------------ --------------------------------------------------------------------- 62 63 func IntComparator() Comparator[int] { 64 return OrderedComparator[int]() 65 } 66 67 func Int8Comparator() Comparator[int8] { 68 return OrderedComparator[int8]() 69 } 70 71 func Int16Comparator() Comparator[int16] { 72 return OrderedComparator[int16]() 73 } 74 75 func Int32Comparator() Comparator[int32] { 76 return OrderedComparator[int32]() 77 } 78 79 func Int64Comparator() Comparator[int64] { 80 return OrderedComparator[int64]() 81 } 82 83 // ------------------------------------------------ --------------------------------------------------------------------- 84 85 func UintComparator() Comparator[uint] { 86 return OrderedComparator[uint]() 87 } 88 89 func Uint8Comparator() Comparator[uint8] { 90 return OrderedComparator[uint8]() 91 } 92 93 func Uint16Comparator() Comparator[uint16] { 94 return OrderedComparator[uint16]() 95 } 96 97 func Uint32Comparator() Comparator[uint32] { 98 return OrderedComparator[uint32]() 99 } 100 101 func Uint64Comparator() Comparator[uint64] { 102 return OrderedComparator[uint64]() 103 } 104 105 // ------------------------------------------------ --------------------------------------------------------------------- 106 107 func Float32Comparator() Comparator[float32] { 108 return func(a float32, b float32) int { 109 return bternaryexpr.TernaryExpr[int](bmath.Abs(float64(a-b)) < 0.0000001, 0, bternaryexpr.TernaryExpr[int](a > b, 1, -1)) 110 } 111 } 112 113 func Float64Comparator() Comparator[float64] { 114 return func(a float64, b float64) int { 115 return bternaryexpr.TernaryExpr[int](bmath.Abs(a-b) < 0.0000001, 0, bternaryexpr.TernaryExpr[int](a > b, 1, -1)) 116 } 117 } 118 119 // ------------------------------------------------ --------------------------------------------------------------------- 120 121 // BoolComparator 布尔类型的比较器,默认是让false排在前面 122 func BoolComparator() Comparator[bool] { 123 return func(a bool, b bool) int { 124 return bternaryexpr.TernaryExpr[int](a == b, 0, bternaryexpr.TernaryExpr[int](!a && b, -1, 1)) 125 } 126 }