1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

refactor: use constraints from golang.org/x/exp/constraints

This commit is contained in:
dudaodong
2022-12-11 11:25:34 +08:00
parent e435fa271b
commit a70ec6ad1e
10 changed files with 25 additions and 33 deletions

View File

@@ -11,7 +11,7 @@ import (
"reflect"
"sort"
"github.com/duke-git/lancet/v2/lancetconstraints"
"golang.org/x/exp/constraints"
)
// Create a static variable to store the hash table.
@@ -120,7 +120,7 @@ func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(inde
return result
}
//DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal).
// DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal).
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(item1, item2 T) bool) []T {
result := make([]T, 0)
@@ -736,7 +736,7 @@ func Shuffle[T any](slice []T) []T {
// Sort sorts a slice of any ordered type(number or string), use quick sort algrithm.
// default sort order is ascending (asc), if want descending order, set param `sortOrder` to `desc`
func Sort[T lancetconstraints.Ordered](slice []T, sortOrder ...string) {
func Sort[T constraints.Ordered](slice []T, sortOrder ...string) {
if len(sortOrder) > 0 && sortOrder[0] == "desc" {
quickSort(slice, 0, len(slice)-1, "desc")
} else {

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"reflect"
"github.com/duke-git/lancet/v2/lancetconstraints"
"golang.org/x/exp/constraints"
)
// sliceValue return the reflect value of a slice
@@ -27,7 +27,7 @@ func sliceElemType(reflectType reflect.Type) reflect.Type {
}
}
func quickSort[T lancetconstraints.Ordered](slice []T, lowIndex, highIndex int, order string) {
func quickSort[T constraints.Ordered](slice []T, lowIndex, highIndex int, order string) {
if lowIndex < highIndex {
p := partitionOrderedSlice(slice, lowIndex, highIndex, order)
quickSort(slice, lowIndex, p-1, order)
@@ -36,7 +36,7 @@ func quickSort[T lancetconstraints.Ordered](slice []T, lowIndex, highIndex int,
}
// partitionOrderedSlice split ordered slice into two parts for quick sort
func partitionOrderedSlice[T lancetconstraints.Ordered](slice []T, lowIndex, highIndex int, order string) int {
func partitionOrderedSlice[T constraints.Ordered](slice []T, lowIndex, highIndex int, order string) int {
p := slice[highIndex]
i := lowIndex