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