gitee.com/quant1x/num@v0.3.2/vectors.go (about) 1 package num 2 3 // AnyToSlice any转切片 4 // 5 // 如果a是基础类型, 就是repeat 6 // 如果a是切片, 就做对齐处理, 截断或者填充 7 func AnyToSlice[T BaseType](A any, N int) []T { 8 var d any 9 switch v := A.(type) { 10 case nil: 11 d = Repeat[T](TypeDefault[T](), N) 12 case int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint, float32, float64, bool, string: 13 // 基础数据类型, 复制N次 14 //d = Repeat[T](v.(T), n) 15 x := AnyToGeneric[T](v) 16 d = Repeat[T](x, N) 17 case *int8, *uint8, *int16, *uint16, *int32, *uint32, *int64, *uint64, *int, *uint, *float32, *float64, *bool, *string: 18 //value := reflect.ValueOf(v) 19 //if value.Kind() == reflect.Pointer { 20 // v = value.Elem().Interface() 21 //} 22 // 基础数据类型, 复制N次 23 x := AnyToGeneric[T](v) 24 d = Repeat[T](x, N) 25 case []T: 26 // 类型相同 27 d = Align(v, TypeDefault[T](), N) 28 case []int8: 29 sliceT := make([]T, len(v)) 30 for i := 0; i < len(v); i++ { 31 sliceT[i] = AnyToGeneric[T](v[i]) 32 } 33 d = Align[T](sliceT, TypeDefault[T](), N) 34 case []uint8: 35 sliceT := make([]T, len(v)) 36 for i := 0; i < len(v); i++ { 37 sliceT[i] = AnyToGeneric[T](v[i]) 38 } 39 d = Align[T](sliceT, TypeDefault[T](), N) 40 case []int16: 41 sliceT := make([]T, len(v)) 42 for i := 0; i < len(v); i++ { 43 sliceT[i] = AnyToGeneric[T](v[i]) 44 } 45 d = Align[T](sliceT, TypeDefault[T](), N) 46 case []uint16: 47 sliceT := make([]T, len(v)) 48 for i := 0; i < len(v); i++ { 49 sliceT[i] = AnyToGeneric[T](v[i]) 50 } 51 d = Align[T](sliceT, TypeDefault[T](), N) 52 case []int32: 53 sliceT := make([]T, len(v)) 54 for i := 0; i < len(v); i++ { 55 sliceT[i] = AnyToGeneric[T](v[i]) 56 } 57 d = Align[T](sliceT, TypeDefault[T](), N) 58 case []uint32: 59 sliceT := make([]T, len(v)) 60 for i := 0; i < len(v); i++ { 61 sliceT[i] = AnyToGeneric[T](v[i]) 62 } 63 d = Align[T](sliceT, TypeDefault[T](), N) 64 case []int64: 65 sliceT := make([]T, len(v)) 66 for i := 0; i < len(v); i++ { 67 sliceT[i] = AnyToGeneric[T](v[i]) 68 } 69 d = Align[T](sliceT, TypeDefault[T](), N) 70 case []uint64: 71 sliceT := make([]T, len(v)) 72 for i := 0; i < len(v); i++ { 73 sliceT[i] = AnyToGeneric[T](v[i]) 74 } 75 d = Align[T](sliceT, TypeDefault[T](), N) 76 case []int: 77 sliceT := make([]T, len(v)) 78 for i := 0; i < len(v); i++ { 79 sliceT[i] = AnyToGeneric[T](v[i]) 80 } 81 d = Align[T](sliceT, TypeDefault[T](), N) 82 case []uint: 83 sliceT := make([]T, len(v)) 84 for i := 0; i < len(v); i++ { 85 sliceT[i] = AnyToGeneric[T](v[i]) 86 } 87 d = Align[T](sliceT, TypeDefault[T](), N) 88 case []uintptr: 89 sliceT := make([]T, len(v)) 90 for i := 0; i < len(v); i++ { 91 sliceT[i] = AnyToGeneric[T](v[i]) 92 } 93 d = Align[T](sliceT, TypeDefault[T](), N) 94 case []float32: 95 sliceT := make([]T, len(v)) 96 for i := 0; i < len(v); i++ { 97 sliceT[i] = AnyToGeneric[T](v[i]) 98 } 99 d = Align[T](sliceT, TypeDefault[T](), N) 100 case []float64: 101 sliceT := make([]T, len(v)) 102 for i := 0; i < len(v); i++ { 103 sliceT[i] = AnyToGeneric[T](v[i]) 104 } 105 d = Align[T](sliceT, TypeDefault[T](), N) 106 case []string: 107 sliceT := make([]T, len(v)) 108 for i := 0; i < len(v); i++ { 109 sliceT[i] = AnyToGeneric[T](v[i]) 110 } 111 d = Align[T](sliceT, TypeDefault[T](), N) 112 case []bool: 113 sliceT := make([]T, len(v)) 114 for i := 0; i < len(v); i++ { 115 sliceT[i] = AnyToGeneric[T](v[i]) 116 } 117 d = Align[T](sliceT, TypeDefault[T](), N) 118 //case []int8, []uint8, []int16, []uint16, []int32, []uint32, []int64, []uint64, []int, []uint, []uintptr, []float32, []float64, []bool, []string: 119 // // 效率不高 120 // // T和A类型不同 121 // //typ := reflect.TypeOf(v).Elem() //CheckoutRawType(v) 122 // var t0 T 123 // //ok := typ.ConvertibleTo(reflect.TypeOf(t0)) 124 // typ := reflect.TypeOf(t0) 125 // var sliceT []T 126 // vs := reflect.ValueOf(v) 127 // if vs.Kind() == reflect.Slice || vs.Kind() == reflect.Array { 128 // sliceT = make([]T, vs.Len()) 129 // for i := 0; i < vs.Len(); i++ { 130 // ev := vs.Index(i).Convert(typ).Interface() 131 // sliceT[i] = ev.(T) 132 // } 133 // } 134 // d = Align[T](sliceT, TypeDefault[T](), n) 135 case Array: 136 values := v.Values() 137 d = AnyToSlice[T](values, N) 138 default: 139 panic(TypeError(A)) 140 } 141 return d.([]T) 142 }