github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/ascii/ascii.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 ascii
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  )
    21  
    22  var (
    23  	ints  = []int64{1e16, 1e8, 1e4, 1e2, 1e1}
    24  	uints = []uint64{1e16, 1e8, 1e4, 1e2, 1e1}
    25  )
    26  
    27  func IntBatch[T types.Ints](xs []T, start int, rs []uint8, nsp *nulls.Nulls) {
    28  	for i, x := range xs {
    29  		if nsp.Contains(uint64(i)) {
    30  			continue
    31  		}
    32  		rs[i] = IntSingle(int64(x), start)
    33  	}
    34  }
    35  
    36  func IntSingle[T types.Ints](val T, start int) uint8 {
    37  	if val < 0 {
    38  		return '-'
    39  	}
    40  	i64Val := int64(val)
    41  	for _, v := range ints[start:] {
    42  		if i64Val >= v {
    43  			i64Val /= v
    44  		}
    45  	}
    46  	return uint8(i64Val) + '0'
    47  }
    48  
    49  func UintBatch[T types.UInts](xs []T, start int, rs []uint8, nsp *nulls.Nulls) {
    50  	for i, x := range xs {
    51  		if nsp.Contains(uint64(i)) {
    52  			continue
    53  		}
    54  		rs[i] = UintSingle(x, start)
    55  	}
    56  }
    57  
    58  func UintSingle[T types.UInts](val T, start int) uint8 {
    59  	u64Val := uint64(val)
    60  	for _, v := range uints[start:] {
    61  		if u64Val >= v {
    62  			u64Val /= v
    63  		}
    64  	}
    65  	return uint8(u64Val) + '0'
    66  }
    67  
    68  func StringBatch(xs [][]byte, rs []uint8, nsp *nulls.Nulls) {
    69  	for i, x := range xs {
    70  		if nsp.Contains(uint64(i)) {
    71  			continue
    72  		}
    73  		rs[i] = StringSingle(x)
    74  	}
    75  }
    76  
    77  func StringSingle(val []byte) uint8 {
    78  	if len(val) == 0 {
    79  		return 0
    80  	}
    81  	return val[0]
    82  }