gitee.com/quant1x/num@v0.3.2/window.go (about) 1 package num 2 3 // Window 参数N的新方式 4 // 5 // Deprecated: 推荐 Periods [wangfeng on 2024/2/26 09:50] 6 type Window[E Number] struct { 7 V []E // 切片 8 C E // 常量 9 } 10 11 func Any2Window[E Number](n any) Window[E] { 12 var constant E 13 var array []E 14 switch m := n.(type) { 15 case Periods: 16 return Window[E]{ 17 V: AnyToSlice[E](m.Array, len(m.Array)), 18 C: E(m.N), 19 } 20 case float32: 21 constant = E(m) 22 case float64: 23 constant = E(m) 24 case int8: 25 constant = E(m) 26 case uint8: 27 constant = E(m) 28 case int16: 29 constant = E(m) 30 case uint16: 31 constant = E(m) 32 case int32: 33 constant = E(m) 34 case uint32: 35 constant = E(m) 36 case int64: 37 constant = E(m) 38 case uint64: 39 constant = E(m) 40 case int: 41 constant = E(m) 42 case uint: 43 constant = E(m) 44 case uintptr: 45 constant = E(m) 46 case []float32: 47 array = AnyToSlice[E](m, len(m)) 48 case []float64: 49 array = AnyToSlice[E](m, len(m)) 50 case []int8: 51 array = AnyToSlice[E](m, len(m)) 52 case []uint8: 53 array = AnyToSlice[E](m, len(m)) 54 case []int16: 55 array = AnyToSlice[E](m, len(m)) 56 case []uint16: 57 array = AnyToSlice[E](m, len(m)) 58 case []int32: 59 array = AnyToSlice[E](m, len(m)) 60 case []uint32: 61 array = AnyToSlice[E](m, len(m)) 62 case []int64: 63 array = AnyToSlice[E](m, len(m)) 64 case []uint64: 65 array = AnyToSlice[E](m, len(m)) 66 case []int: 67 array = AnyToSlice[E](m, len(m)) 68 case []uint: 69 array = AnyToSlice[E](m, len(m)) 70 case []uintptr: 71 array = AnyToSlice[E](m, len(m)) 72 case DTypeArray: 73 tmp := m.DTypes() 74 array = AnyToSlice[E](tmp, len(tmp)) 75 } 76 return Window[E]{V: array, C: constant} 77 } 78 79 // At 获取下标为i的元素 80 // 如果i超过切片V的长度, 则直接返回常量C 81 func (this Window[E]) At(i int) E { 82 if i < len(this.V) { 83 return this.V[i] 84 } 85 return this.C 86 } 87 88 // IsConst 是否全部常量 89 func (this Window[E]) IsConst() bool { 90 return len(this.V) == 0 && this.C != 0 91 } 92 93 func RollingWindow(v []DType, c DType) Window[DType] { 94 w := Window[DType]{ 95 V: v, 96 C: c, 97 } 98 return w 99 }