优化 完善代码

This commit is contained in:
xing 2023-02-28 19:19:24 +08:00
parent 9b46a86d5d
commit ccc02399ef
6 changed files with 72 additions and 8 deletions

View File

@ -1,6 +1,14 @@
package slice package slice
import "sort" import (
"golang.org/x/exp/constraints"
"sort"
)
const (
ASC = iota
DESC
)
type anyArr[T any] struct { type anyArr[T any] struct {
data []T data []T
@ -28,6 +36,7 @@ func SortSelf[T any](arr []T, fn func(i, j T) bool) {
return return
} }
// Sort fn 中i>j 为降序,反之为升序
func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) { func Sort[T any](arr []T, fn func(i, j T) bool) (r []T) {
r = make([]T, len(arr)) r = make([]T, len(arr))
copy(r, 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) sort.Sort(slice)
return 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)
}

View File

@ -1,6 +1,8 @@
package slice package slice
import ( import (
"fmt"
"golang.org/x/exp/constraints"
"reflect" "reflect"
"testing" "testing"
) )
@ -110,3 +112,36 @@ 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)
})
}
}

View File

@ -13,6 +13,7 @@ const (
Error404 Error404
ParamError ParamError
InternalErr InternalErr
AllStats
Defaults = "default" Defaults = "default"

View File

@ -28,7 +28,7 @@ type Handle struct {
Class []string Class []string
Components map[string][]Components Components map[string][]Components
ThemeMods wpconfig.ThemeMods ThemeMods wpconfig.ThemeMods
HandleFns []HandleFn[*Handle] HandleFns map[int][]HandleFn[*Handle]
} }
func NewHandle(c *gin.Context, scene int, theme string) *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} return Components{Fn: fn, Order: order}
} }
func (h *Handle) PushHandleFn(fns ...HandleFn[*Handle]) { func (h *Handle) PushHandleFn(stats int, fns ...HandleFn[*Handle]) {
h.HandleFns = append(h.HandleFns, fns...) h.HandleFns[stats] = append(h.HandleFns[stats], fns...)
} }
func (h *Handle) AddComponent(name string, fn func(*Handle) string) { func (h *Handle) AddComponent(name string, fn func(*Handle) string) {
@ -84,7 +84,13 @@ func (h *Handle) GetPassword() {
} }
func (h *Handle) ExecHandleFns() { 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) fn(h)
} }
} }

View File

@ -41,7 +41,7 @@ func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
common.NewComponents(CalCustomBackGround, 10), common.NewComponents(CalCustomBackGround, 10),
common.NewComponents(colorSchemeCss, 10), common.NewComponents(colorSchemeCss, 10),
) )
h.PushHandleFn(customHeader) h.PushHandleFn(constraints.AllStats, customHeader)
switch h.Scene { switch h.Scene {
case constraints.Detail: case constraints.Detail:
detail(next, h.Detail) detail(next, h.Detail)

View File

@ -36,7 +36,7 @@ func Hook(h *common.Handle) {
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) { func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.PushHandleFn(calClass) h.PushHandleFn(constraints.AllStats, calClass)
h.PushHeadScript( h.PushHeadScript(
common.NewComponents(colorScheme, 10), common.NewComponents(colorScheme, 10),
) )