gitee.com/quant1x/num@v0.3.2/logic.go (about) 1 package num 2 3 import ( 4 "gitee.com/quant1x/num/x32" 5 "gitee.com/quant1x/num/x64" 6 "reflect" 7 ) 8 9 func __compare[T ~[]E, E any](x T, y any, c int, comparator func(f1, f2 DType) bool) []bool { 10 if __y, ok := y.(Array); ok { 11 y = __y.Values() 12 } 13 var d = []bool{} 14 switch Y := y.(type) { 15 case nil, int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint, float32, float64, bool, string: 16 f2 := Any2DType(Y) 17 d = __compare_dtype(x, f2, c, comparator) 18 case []float32: 19 d = __compare_slice(x, Y, c, comparator) 20 case []float64: 21 d = __compare_slice(x, Y, c, comparator) 22 case []int: 23 d = __compare_slice(x, Y, c, comparator) 24 case []int8: 25 d = __compare_slice(x, Y, c, comparator) 26 case []int16: 27 d = __compare_slice(x, Y, c, comparator) 28 case []int32: 29 d = __compare_slice(x, Y, c, comparator) 30 case []int64: 31 d = __compare_slice(x, Y, c, comparator) 32 case []uint: 33 d = __compare_slice(x, Y, c, comparator) 34 case []uint8: 35 d = __compare_slice(x, Y, c, comparator) 36 case []uint16: 37 d = __compare_slice(x, Y, c, comparator) 38 case []uint32: 39 d = __compare_slice(x, Y, c, comparator) 40 case []uint64: 41 d = __compare_slice(x, Y, c, comparator) 42 case []uintptr: 43 d = __compare_slice(x, Y, c, comparator) 44 case []string: 45 d = __compare_slice(x, Y, c, comparator) 46 case []bool: 47 d = __compare_slice(x, Y, c, comparator) 48 default: 49 // 其它未知类型抛异常 50 panic(TypeError(y)) 51 } 52 return d 53 } 54 55 // 切片和dtype对比, 不用考虑slice长度对齐的问题 56 func __compare_dtype[T ~[]E, E any](x T, y DType, c int, comparator func(f1, f2 DType) bool) []bool { 57 var bs = []bool{} 58 xLen := len(x) 59 bs = make([]bool, xLen) 60 61 kind := CheckoutRawType(x) 62 if kind == reflect.Float64 && c == __k_compare_gt { 63 return x64.GtNumber_Into(bs, any(x).([]float64), y) 64 } else if kind == reflect.Float64 && c == __k_compare_gte { 65 return x64.GteNumber_Into(bs, any(x).([]float64), y) 66 } else if kind == reflect.Float64 && c == __k_compare_lt { 67 return x64.LtNumber_Into(bs, any(x).([]float64), y) 68 } else if kind == reflect.Float64 && c == __k_compare_lte { 69 return x64.LteNumber_Into(bs, any(x).([]float64), y) 70 } else if kind == reflect.Float32 && c == __k_compare_gt { 71 return x32.GtNumber_Into(bs, any(x).([]float32), float32(y)) 72 } else if kind == reflect.Float32 && c == __k_compare_gte { 73 return x32.GteNumber_Into(bs, any(x).([]float32), float32(y)) 74 } else if kind == reflect.Float32 && c == __k_compare_lt { 75 return x32.LtNumber_Into(bs, any(x).([]float32), float32(y)) 76 } else if kind == reflect.Float32 && c == __k_compare_lte { 77 return x32.LteNumber_Into(bs, any(x).([]float32), float32(y)) 78 } else { 79 b := y 80 for i := 0; i < xLen; i++ { 81 a := Any2DType(x[i]) 82 bs[i] = comparator(a, b) 83 } 84 } 85 return bs 86 } 87 88 // 切片和切片对比 89 func __compare_slice[T ~[]E, E any, T2 ~[]E2, E2 any](x T, y T2, c int, comparator func(f1, f2 DType) bool) []bool { 90 var bs = []bool{} 91 xLen := len(x) 92 yLen := len(y) 93 xKind := CheckoutRawType(x) 94 yKind := CheckoutRawType(y) 95 96 if xLen >= yLen { 97 bs = make([]bool, xLen) 98 if xKind == reflect.Float64 && xKind == yKind && c == __k_compare_gt { 99 x64.Gt_Into(bs[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen]) 100 } else if xKind == reflect.Float64 && xKind == yKind && c == __k_compare_gte { 101 x64.Gte_Into(bs[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen]) 102 } else if xKind == reflect.Float64 && xKind == yKind && c == __k_compare_lt { 103 x64.Lt_Into(bs[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen]) 104 } else if xKind == reflect.Float64 && xKind == yKind && c == __k_compare_lte { 105 x64.Lte_Into(bs[:yLen], any(x).([]float64)[:yLen], any(y).([]float64)[:yLen]) 106 } else if xKind == reflect.Float32 && xKind == yKind && c == __k_compare_gt { 107 x32.Gt_Into(bs[:yLen], any(x).([]float32)[:yLen], any(y).([]float32)[:yLen]) 108 } else if xKind == reflect.Float32 && xKind == yKind && c == __k_compare_gte { 109 x32.Gte_Into(bs[:yLen], any(x).([]float32)[:yLen], any(y).([]float32)[:yLen]) 110 } else if xKind == reflect.Float32 && xKind == yKind && c == __k_compare_lt { 111 x32.Lt_Into(bs[:yLen], any(x).([]float32)[:yLen], any(y).([]float32)[:yLen]) 112 } else if xKind == reflect.Float32 && xKind == yKind && c == __k_compare_lte { 113 x32.Lte_Into(bs[:yLen], any(x).([]float32)[:yLen], any(y).([]float32)[:yLen]) 114 } else if xKind == reflect.Bool && xKind == yKind && c == __k_compare_and { 115 x64.And_Into(bs[:yLen], any(x).([]bool)[:yLen], any(y).([]bool)[:yLen]) 116 } else if xKind == reflect.Bool && xKind == yKind && c == __k_compare_or { 117 x64.Or_Into(bs[:yLen], any(x).([]bool)[:yLen], any(y).([]bool)[:yLen]) 118 } else { 119 for i := 0; i < yLen; i++ { 120 f1 := Any2DType(x[i]) 121 f2 := Any2DType(y[i]) 122 bs[i] = comparator(f1, f2) 123 } 124 } 125 for i := yLen; i < xLen; i++ { 126 f1 := Any2DType(x[i]) 127 f2 := DType(0) 128 bs[i] = comparator(f1, f2) 129 } 130 } else { 131 bs = make([]bool, yLen) 132 if xKind == reflect.Float64 && xKind == yKind && c == __k_compare_gt { 133 x64.Gt_Into(bs[:xLen], any(x).([]float64)[:xLen], any(y).([]float64)[:xLen]) 134 } else if xKind == reflect.Float64 && xKind == yKind && c == __k_compare_gte { 135 x64.Gte_Into(bs[:xLen], any(x).([]float64)[:xLen], any(y).([]float64)[:xLen]) 136 } else if xKind == reflect.Float64 && xKind == yKind && c == __k_compare_lt { 137 x64.Lt_Into(bs[:xLen], any(x).([]float64)[:xLen], any(y).([]float64)[:xLen]) 138 } else if xKind == reflect.Float64 && xKind == yKind && c == __k_compare_lte { 139 x64.Lte_Into(bs[:xLen], any(x).([]float64)[:xLen], any(y).([]float64)[:xLen]) 140 } else if xKind == reflect.Float32 && xKind == yKind && c == __k_compare_gt { 141 x32.Gt_Into(bs[:xLen], any(x).([]float32)[:xLen], any(y).([]float32)[:xLen]) 142 } else if xKind == reflect.Float32 && xKind == yKind && c == __k_compare_gte { 143 x32.Gte_Into(bs[:xLen], any(x).([]float32)[:xLen], any(y).([]float32)[:xLen]) 144 } else if xKind == reflect.Float32 && xKind == yKind && c == __k_compare_lt { 145 x32.Lt_Into(bs[:xLen], any(x).([]float32)[:xLen], any(y).([]float32)[:xLen]) 146 } else if xKind == reflect.Float32 && xKind == yKind && c == __k_compare_lte { 147 x32.Lte_Into(bs[:xLen], any(x).([]float32)[:xLen], any(y).([]float32)[:xLen]) 148 } else if xKind == reflect.Bool && xKind == yKind && c == __k_compare_and { 149 x64.And_Into(bs[:xLen], any(x).([]bool)[:xLen], any(y).([]bool)[:xLen]) 150 } else if xKind == reflect.Bool && xKind == yKind && c == __k_compare_or { 151 x64.Or_Into(bs[:xLen], any(x).([]bool)[:xLen], any(y).([]bool)[:xLen]) 152 } else { 153 for i := 0; i < xLen; i++ { 154 f1 := Any2DType(x[i]) 155 f2 := Any2DType(y[i]) 156 bs[i] = comparator(f1, f2) 157 } 158 } 159 for i := xLen; i < yLen; i++ { 160 f1 := DType(0) 161 f2 := Any2DType(y[i]) 162 bs[i] = comparator(f1, f2) 163 } 164 } 165 return bs 166 } 167 168 const ( 169 __k_compare_gt = 1 170 __k_compare_gte = 2 171 __k_compare_lt = 3 172 __k_compare_lte = 4 173 __k_compare_and = 5 174 __k_compare_or = 6 175 ) 176 177 var ( 178 // 大于 179 __logic_gt = func(f1, f2 DType) bool { 180 return f1 > f2 181 } 182 // 大于等于 183 __logic_gte = func(f1, f2 DType) bool { 184 return f1 >= f2 185 } 186 // 小于 187 __logic_lt = func(f1, f2 DType) bool { 188 return f1 < f2 189 } 190 // 小于等于 191 __logic_lte = func(f1, f2 DType) bool { 192 return f1 <= f2 193 } 194 // AND 195 __logic_and = func(f1, f2 DType) bool { 196 return f1 != 0 && f2 != 0 197 } 198 // OR 199 __logic_or = func(f1, f2 DType) bool { 200 return f1 != 0 || f2 != 0 201 } 202 ) 203 204 // Gt 比较 v > x 205 func Gt[S ~[]E, E any](v S, x any) []bool { 206 return __compare(v, x, __k_compare_gt, __logic_gt) 207 } 208 209 // Gte 比较 v >= x 210 func Gte[S ~[]E, E any](v S, x any) []bool { 211 return __compare(v, x, __k_compare_gte, __logic_gte) 212 } 213 214 // Lt 比较 v < x 215 func Lt[S ~[]E, E any](v S, x any) []bool { 216 return __compare(v, x, __k_compare_lt, __logic_lt) 217 } 218 219 // Lte 比较 v <= x 220 func Lte[S ~[]E, E any](v S, x any) []bool { 221 return __compare(v, x, __k_compare_lte, __logic_lte) 222 } 223 224 // And 比较 v && x 225 func And[S ~[]E, E any](v S, x any) []bool { 226 return __compare(v, x, __k_compare_and, __logic_and) 227 } 228 229 // Or 比较 v || x 230 func Or[S ~[]E, E any](v S, x any) []bool { 231 return __compare(v, x, __k_compare_or, __logic_or) 232 } 233 234 // Not 非 235 func Not[S ~[]E, E any](v S) []bool { 236 x := SliceToBool(v) 237 return x64.Not(x) 238 }