gitee.com/quant1x/num@v0.3.2/builtin.go (about)

     1  package num
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"gitee.com/quant1x/num/internal/functions"
     7  	"reflect"
     8  	"runtime"
     9  )
    10  
    11  // SetAvx2Enabled 设定AVX2加速开关, 非线程安全
    12  func SetAvx2Enabled(enabled bool) {
    13  	functions.SetAcceleration(enabled)
    14  }
    15  
    16  // GetAvx2Enabled 获取avx2加速状态
    17  func GetAvx2Enabled() bool {
    18  	return functions.UseAVX2
    19  }
    20  
    21  var (
    22  	IgnoreParseExceptions = true // 忽略解析异常
    23  )
    24  
    25  // ExtractValueFromPointer 从指针/地址提取值
    26  // Extract value from pointer
    27  func ExtractValueFromPointer(v any) (any, bool) {
    28  	vv := reflect.ValueOf(v)
    29  	if vv.Kind() == reflect.Pointer {
    30  		if vv.IsNil() {
    31  			return nil, true
    32  		}
    33  		ve := vv.Elem()
    34  		return ve.Interface(), true
    35  	}
    36  	return v, false
    37  }
    38  
    39  // Array 获取any类型切片数据
    40  type Array interface {
    41  	Values() any // 保持原类型
    42  }
    43  
    44  // DTypeArray 获取DType切片的接口
    45  type DTypeArray interface {
    46  	DTypes() []DType // to dtype
    47  }
    48  
    49  // PanicTrace panic 堆栈信息
    50  func PanicTrace(err interface{}) string {
    51  	buf := new(bytes.Buffer)
    52  	_, _ = fmt.Fprintf(buf, "%v\n", err)
    53  	for i := 1; ; i++ {
    54  		pc, file, line, ok := runtime.Caller(i)
    55  		if !ok {
    56  			break
    57  		}
    58  		_, _ = fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
    59  	}
    60  	return buf.String()
    61  }
    62  
    63  // StringFormatter is used to convert a value
    64  // into a string. Val can be nil or the concrete
    65  // type stored by the series.
    66  type StringFormatter func(val any) string
    67  
    68  // DefaultFormatter will return a string representation
    69  // of the data in a particular row.
    70  func DefaultFormatter(v any) string {
    71  	if v == nil {
    72  		return StringNaN
    73  	}
    74  	return fmt.Sprintf("%v", v)
    75  }