1 Star 4 Fork 11

王布衣 / pandas

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
ndarray_sort.go 1.72 KB
Copy Edit Raw Blame History
王布衣 authored 2023-02-14 01:00 . !58#I6EVCO实现NDArray
package stat
func (arr NDArray[T]) Len() int {
return len(arr)
}
// Less 实现sort.Interface接口的比较元素方法
func (arr NDArray[T]) Less(i, j int) bool {
type_ := arr.Type()
if type_ == SERIES_TYPE_BOOL {
values := arr.Values().([]bool)
var (
a = int(0)
b = int(0)
)
if values[i] {
a = 1
}
if values[j] {
b = 1
}
return a < b
} else if type_ == SERIES_TYPE_INT64 {
values := arr.Values().([]int64)
return values[i] < values[j]
} else if type_ == SERIES_TYPE_FLOAT32 {
values := arr.Values().([]float32)
return values[i] < values[j]
} else if type_ == SERIES_TYPE_FLOAT64 {
values := arr.Values().([]float64)
return values[i] < values[j]
} else if type_ == SERIES_TYPE_STRING {
values := arr.Values().([]string)
return values[i] < values[j]
} else {
// SERIES_TYPE_INVAILD
// 应该到不了这里, Len()会返回0
panic(ErrUnsupportedType)
}
return false
}
// Swap 实现sort.Interface接口的交换元素方法
func (arr NDArray[T]) Swap(i, j int) {
type_ := arr.Type()
if type_ == SERIES_TYPE_BOOL {
values := arr.Values().([]bool)
values[i], values[j] = values[j], values[i]
} else if type_ == SERIES_TYPE_INT64 {
values := arr.Values().([]int64)
values[i], values[j] = values[j], values[i]
} else if type_ == SERIES_TYPE_FLOAT32 {
values := arr.Values().([]float32)
values[i], values[j] = values[j], values[i]
} else if type_ == SERIES_TYPE_FLOAT64 {
values := arr.Values().([]float64)
values[i], values[j] = values[j], values[i]
} else if type_ == SERIES_TYPE_STRING {
values := arr.Values().([]string)
values[i], values[j] = values[j], values[i]
} else {
// SERIES_TYPE_INVAILD
// 应该到不了这里, Len()会返回0
panic(ErrUnsupportedType)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/pandas.git
git@gitee.com:quant1x/pandas.git
quant1x
pandas
pandas
v0.7.4

Search

344bd9b3 5694891 D2dac590 5694891