github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/oct/oct.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package oct 16 17 import ( 18 "fmt" 19 "strconv" 20 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 23 "golang.org/x/exp/constraints" 24 ) 25 26 var ( 27 OctUint8 func([]uint8, []types.Decimal128) ([]types.Decimal128, error) 28 OctUint16 func([]uint16, []types.Decimal128) ([]types.Decimal128, error) 29 OctUint32 func([]uint32, []types.Decimal128) ([]types.Decimal128, error) 30 OctUint64 func([]uint64, []types.Decimal128) ([]types.Decimal128, error) 31 OctInt8 func([]int8, []types.Decimal128) ([]types.Decimal128, error) 32 OctInt16 func([]int16, []types.Decimal128) ([]types.Decimal128, error) 33 OctInt32 func([]int32, []types.Decimal128) ([]types.Decimal128, error) 34 OctInt64 func([]int64, []types.Decimal128) ([]types.Decimal128, error) 35 ) 36 37 func init() { 38 OctUint8 = Oct[uint8] 39 OctUint16 = Oct[uint16] 40 OctUint32 = Oct[uint32] 41 OctUint64 = Oct[uint64] 42 OctInt8 = Oct[int8] 43 OctInt16 = Oct[int16] 44 OctInt32 = Oct[int32] 45 OctInt64 = Oct[int64] 46 } 47 48 func Oct[T constraints.Unsigned | constraints.Signed](xs []T, rs []types.Decimal128) ([]types.Decimal128, error) { 49 for idx := range xs { 50 res, err := oct(uint64(xs[idx])) 51 if err != nil { 52 return nil, err 53 } 54 rs[idx] = res 55 } 56 return rs, nil 57 } 58 59 func OctFloat[T constraints.Float](xs []T, rs []types.Decimal128) ([]types.Decimal128, error) { 60 var res types.Decimal128 61 for idx := range xs { 62 if xs[idx] < 0 { 63 val, err := strconv.ParseInt(fmt.Sprintf("%1.0f", xs[idx]), 10, 64) 64 if err != nil { 65 return nil, err 66 } 67 res, err = oct(uint64(val)) 68 if err != nil { 69 return nil, err 70 } 71 } else { 72 val, err := strconv.ParseUint(fmt.Sprintf("%1.0f", xs[idx]), 10, 64) 73 if err != nil { 74 return nil, err 75 } 76 res, err = oct(val) 77 if err != nil { 78 return nil, err 79 } 80 } 81 rs[idx] = res 82 } 83 return rs, nil 84 } 85 86 func oct(val uint64) (types.Decimal128, error) { 87 return types.Decimal128_FromStringWithScale(fmt.Sprintf("%o", val), 33, 0) 88 }