From 07ab90954b561fc796053524f69426001b15ec79 Mon Sep 17 00:00:00 2001 From: wangfeng Date: Wed, 1 Mar 2023 19:00:57 +0800 Subject: [PATCH] =?UTF-8?q?#I6IUM9=20=E5=89=94=E9=99=A4go-clone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 3 +-- go.sum | 10 ++------ series_generic.go | 15 +++++++---- series_range.go | 60 +++++++++++++++---------------------------- stat/builtin.go | 7 +---- stat/ndarray_range.go | 35 ++++++++++--------------- 6 files changed, 48 insertions(+), 82 deletions(-) diff --git a/go.mod b/go.mod index ea409fa..d2086e8 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,8 @@ module gitee.com/quant1x/pandas go 1.20 require ( - gitee.com/quant1x/data v0.7.16 + gitee.com/quant1x/data v0.7.21 github.com/chewxy/math32 v1.10.1 - github.com/huandu/go-clone v1.4.1 github.com/mymmsc/gox v1.3.9 github.com/olekukonko/tablewriter v0.0.5 github.com/shirou/gopsutil/v3 v3.23.1 diff --git a/go.sum b/go.sum index 748e256..81735f9 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -gitee.com/quant1x/data v0.7.16 h1:QYJcg/sAowoumurMDLDl7MsDFVNGc0w+57UCNwZMhrI= -gitee.com/quant1x/data v0.7.16/go.mod h1:9xgfw26RyhtjKml+C1tcU2sBWT+PxIXYZ1EJajbAuCA= +gitee.com/quant1x/data v0.7.21 h1:oR9USvCjEs5P0x73ZiJ3uDSRFEOF0jC0dDJJMQt37JE= +gitee.com/quant1x/data v0.7.21/go.mod h1:NPxc3I4s4SW6hVlTCxe3DXqgq6NYfDX3Ksa81pCgi3c= gitee.com/quant1x/gotdx v1.2.5 h1:zPbAPyLRg0uRv9bLziEV5EPoO5PBwffbkKQzI1yuDgA= gitee.com/quant1x/gotdx v1.2.5/go.mod h1:lOJYzC4mcKmeHpm1L5jl94S0pPifue1sf9JMTP2kmho= github.com/chewxy/math32 v1.10.1 h1:LFpeY0SLJXeaiej/eIp2L40VYfscTvKh/FSEZ68uMkU= @@ -28,10 +28,6 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/huandu/go-assert v1.1.5 h1:fjemmA7sSfYHJD7CUqs9qTwwfdNAx7/j2/ZlHXzNB3c= -github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0JrPVhn/06U= -github.com/huandu/go-clone v1.4.1 h1:QQYjiLadyxOvdwgZoH8f1xGkvvf4+Cm8be7fo9W2QQA= -github.com/huandu/go-clone v1.4.1/go.mod h1:ReGivhG6op3GYr+UY3lS6mxjKp7MIGTknuU5TbTVaXE= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= @@ -72,7 +68,6 @@ github.com/shirou/gopsutil/v3 v3.23.1/go.mod h1:NN6mnm5/0k8jw4cBfCnJtr5L7ErOTg18 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= @@ -116,7 +111,6 @@ gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/series_generic.go b/series_generic.go index c7f63ef..0249250 100644 --- a/series_generic.go +++ b/series_generic.go @@ -29,19 +29,24 @@ func NewSeriesWithType(_type stat.Type, name string, values ...any) stat.Series frame.type_ = _type if frame.type_ == stat.SERIES_TYPE_BOOL { // bool - frame.values = reflect.MakeSlice(stat.TypeBool, 0, 0).Interface() + //frame.values = reflect.MakeSlice(stat.TypeBool, 0, 0).Interface() + frame.values = make([]bool, 0) } else if frame.type_ == stat.SERIES_TYPE_INT64 { // int64 - frame.values = reflect.MakeSlice(stat.TypeInt64, 0, 0).Interface() + //frame.values = reflect.MakeSlice(stat.TypeInt64, 0, 0).Interface() + frame.values = make([]int64, 0) } else if frame.type_ == stat.SERIES_TYPE_FLOAT32 { // float32 - frame.values = reflect.MakeSlice(stat.TypeFloat32, 0, 0).Interface() + //frame.values = reflect.MakeSlice(stat.TypeFloat32, 0, 0).Interface() + frame.values = make([]float32, 0) } else if frame.type_ == stat.SERIES_TYPE_FLOAT64 { // float64 - frame.values = reflect.MakeSlice(stat.TypeFloat64, 0, 0).Interface() + //frame.values = reflect.MakeSlice(stat.TypeFloat64, 0, 0).Interface() + frame.values = make([]float64, 0) } else { // string, 字符串最后容错使用 - frame.values = reflect.MakeSlice(stat.TypeString, 0, 0).Interface() + //frame.values = reflect.MakeSlice(stat.TypeString, 0, 0).Interface() + frame.values = make([]string, 0) } frame.Append(values...) diff --git a/series_range.go b/series_range.go index 01179aa..a3e0f79 100644 --- a/series_range.go +++ b/series_range.go @@ -2,7 +2,6 @@ package pandas import ( "gitee.com/quant1x/pandas/stat" - gc "github.com/huandu/go-clone" "reflect" ) @@ -14,44 +13,7 @@ func (self *NDFrame) Copy() stat.Series { func (self *NDFrame) Subset(start, end int, opt ...any) stat.Series { // 默认不copy - var __optCopy bool = false - if len(opt) > 0 { - // 第一个参数为是否copy - if _cp, ok := opt[0].(bool); ok { - __optCopy = _cp - } - } - var vs any - var rows int - vv := reflect.ValueOf(self.values) - vk := vv.Kind() - switch vk { - case reflect.Slice, reflect.Array: // 切片和数组同样的处理逻辑 - vvs := vv.Slice(start, end) - vs = vvs.Interface() - rows = vv.Len() - if __optCopy && rows > 0 { - vs = gc.Clone(vs) - } - rows = vvs.Len() - frame := NDFrame{ - formatter: self.formatter, - name: self.name, - type_: self.type_, - nilCount: 0, - rows: rows, - values: vs, - } - return &frame - default: - // 其它类型忽略 - } - return self.Empty(0) -} - -func (self *NDFrame) oldSubset(start, end int, opt ...any) stat.Series { - // 默认不copy - var __optCopy bool = false + var __optCopy = false if len(opt) > 0 { // 第一个参数为是否copy if _cp, ok := opt[0].(bool); ok { @@ -81,6 +43,16 @@ func (self *NDFrame) oldSubset(start, end int, opt ...any) stat.Series { _vs = append(_vs, subset...) vs = _vs } + case []int32: + subset := values[start:end] + rows = len(subset) + if !__optCopy { + vs = subset + } else { + _vs := make([]int32, 0) + _vs = append(_vs, subset...) + vs = _vs + } case []int64: subset := values[start:end] rows = len(subset) @@ -91,6 +63,16 @@ func (self *NDFrame) oldSubset(start, end int, opt ...any) stat.Series { _vs = append(_vs, subset...) vs = _vs } + case []float32: + subset := values[start:end] + rows = len(subset) + if !__optCopy { + vs = subset + } else { + _vs := make([]float32, 0) + _vs = append(_vs, subset...) + vs = _vs + } case []float64: subset := values[start:end] rows = len(subset) diff --git a/stat/builtin.go b/stat/builtin.go index 0b9cccb..3eb415d 100644 --- a/stat/builtin.go +++ b/stat/builtin.go @@ -1,7 +1,7 @@ package stat import ( - gc "github.com/huandu/go-clone" + //gc "github.com/huandu/go-clone" "github.com/viterin/vek" "math" "reflect" @@ -70,8 +70,3 @@ func IsEmpty(s string) bool { return false } } - -// Clone 克隆一个any -func Clone(v any) any { - return gc.Clone(v) -} diff --git a/stat/ndarray_range.go b/stat/ndarray_range.go index 1b24c96..ba5049f 100644 --- a/stat/ndarray_range.go +++ b/stat/ndarray_range.go @@ -1,6 +1,9 @@ package stat -import "reflect" +import ( + "golang.org/x/exp/slices" + "reflect" +) func (self NDArray[T]) IndexOf(index int, opt ...any) any { if index < 0 { @@ -26,34 +29,22 @@ func (self NDArray[T]) IndexOf(index int, opt ...any) any { func (self NDArray[T]) Subset(start, end int, opt ...any) Series { // 默认不copy - var __optCopy bool = false + var __optCopy = false if len(opt) > 0 { // 第一个参数为是否copy if _cp, ok := opt[0].(bool); ok { __optCopy = _cp } } - var vs any - var rows int - vv := reflect.ValueOf(self.Values()) - vk := vv.Kind() - switch vk { - case reflect.Slice, reflect.Array: // 切片和数组同样的处理逻辑 - vvs := vv.Slice(start, end) - vs = vvs.Interface() - rows = vv.Len() - if __optCopy && rows > 0 { - vs = Clone(vs) - //vs = slices.Clone(vs) - } - rows = vvs.Len() - var d Series - d = NDArray[T](vs.([]T)) - return d - default: - // 其它类型忽略 + values := []T(self) + rows := self.Len() + vvs := values[start:end] + if __optCopy && rows > 0 { + vvs = slices.Clone(vvs) } - return self.Empty() + var d Series + d = NDArray[T](vvs) + return d } func (self NDArray[T]) Select(r ScopeLimit) Series { -- Gitee