优化 完善代码
This commit is contained in:
parent
9b46a86d5d
commit
ccc02399ef
|
@ -1,6 +1,14 @@
|
|||
package slice
|
||||
|
||||
import "sort"
|
||||
import (
|
||||
"golang.org/x/exp/constraints"
|
||||
"sort"
|
||||
)
|
||||
|
||||
const (
|
||||
ASC = iota
|
||||
DESC
|
||||
)
|
||||
|
||||
type anyArr[T any] struct {
|
||||
data []T
|
||||
|
@ -28,6 +36,7 @@ func SortSelf[T any](arr []T, fn func(i, j T) bool) {
|
|||
return
|
||||
}
|
||||
|
||||
// Sort fn 中i>j 为降序,反之为升序
|
||||
func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) {
|
||||
r = make([]T, len(arr))
|
||||
copy(r, arr)
|
||||
|
@ -38,3 +47,16 @@ func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) {
|
|||
sort.Sort(slice)
|
||||
return
|
||||
}
|
||||
|
||||
func Sorts[T constraints.Ordered](a []T, order int) {
|
||||
slice := anyArr[T]{
|
||||
data: a,
|
||||
fn: func(i, j T) bool {
|
||||
if order == DESC {
|
||||
return i > j
|
||||
}
|
||||
return i < j
|
||||
},
|
||||
}
|
||||
sort.Sort(slice)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package slice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/exp/constraints"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
@ -109,4 +111,37 @@ func TestSort(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSorts(t *testing.T) {
|
||||
type args[T constraints.Ordered] struct {
|
||||
a []T
|
||||
order int
|
||||
}
|
||||
type testCase[T constraints.Ordered] struct {
|
||||
name string
|
||||
args args[T]
|
||||
}
|
||||
tests := []testCase[int]{
|
||||
{
|
||||
name: "asc",
|
||||
args: args[int]{
|
||||
a: []int{1, -3, 6, 10, 3, 2, 8},
|
||||
order: ASC,
|
||||
}, //[-3 1 2 3 6 8 10]
|
||||
},
|
||||
{
|
||||
name: "desc",
|
||||
args: args[int]{
|
||||
a: []int{1, -3, 6, 10, 3, 2, 8},
|
||||
order: DESC,
|
||||
}, //[10 8 6 3 2 1 -3]
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
Sorts(tt.args.a, tt.args.order)
|
||||
fmt.Println(tt.args.a)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ const (
|
|||
Error404
|
||||
ParamError
|
||||
InternalErr
|
||||
AllStats
|
||||
|
||||
Defaults = "default"
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ type Handle struct {
|
|||
Class []string
|
||||
Components map[string][]Components
|
||||
ThemeMods wpconfig.ThemeMods
|
||||
HandleFns []HandleFn[*Handle]
|
||||
HandleFns map[int][]HandleFn[*Handle]
|
||||
}
|
||||
|
||||
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
||||
|
@ -56,8 +56,8 @@ func NewComponents(fn func(*Handle) string, order int) Components {
|
|||
return Components{Fn: fn, Order: order}
|
||||
}
|
||||
|
||||
func (h *Handle) PushHandleFn(fns ...HandleFn[*Handle]) {
|
||||
h.HandleFns = append(h.HandleFns, fns...)
|
||||
func (h *Handle) PushHandleFn(stats int, fns ...HandleFn[*Handle]) {
|
||||
h.HandleFns[stats] = append(h.HandleFns[stats], fns...)
|
||||
}
|
||||
|
||||
func (h *Handle) AddComponent(name string, fn func(*Handle) string) {
|
||||
|
@ -84,7 +84,13 @@ func (h *Handle) GetPassword() {
|
|||
}
|
||||
|
||||
func (h *Handle) ExecHandleFns() {
|
||||
for _, fn := range h.HandleFns {
|
||||
calls, ok := h.HandleFns[h.Stats]
|
||||
if ok {
|
||||
for _, call := range calls {
|
||||
call(h)
|
||||
}
|
||||
}
|
||||
for _, fn := range h.HandleFns[constraints.AllStats] {
|
||||
fn(h)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
|
|||
common.NewComponents(CalCustomBackGround, 10),
|
||||
common.NewComponents(colorSchemeCss, 10),
|
||||
)
|
||||
h.PushHandleFn(customHeader)
|
||||
h.PushHandleFn(constraints.AllStats, customHeader)
|
||||
switch h.Scene {
|
||||
case constraints.Detail:
|
||||
detail(next, h.Detail)
|
||||
|
|
|
@ -36,7 +36,7 @@ func Hook(h *common.Handle) {
|
|||
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
|
||||
h.WidgetAreaData()
|
||||
h.GetPassword()
|
||||
h.PushHandleFn(calClass)
|
||||
h.PushHandleFn(constraints.AllStats, calClass)
|
||||
h.PushHeadScript(
|
||||
common.NewComponents(colorScheme, 10),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user