Compare commits

...

2 Commits

Author SHA1 Message Date
65a0ca94f6 stable sort 2023-05-10 17:02:58 +08:00
40451970a0 小优化 2023-05-10 14:28:21 +08:00
3 changed files with 73 additions and 9 deletions

View File

@ -70,15 +70,11 @@ func PostPlugin(calls ...PostsPlugin) PostsPlugin {
} }
func UsePostsPlugins() PostsPlugin { func UsePostsPlugins() PostsPlugin {
p := config.GetConfig().ListPagePlugins
var pluginss []func(PostsPlugin, *Handle, *models.Posts)
m := pluginFns.Load() m := pluginFns.Load()
for _, s := range p { pluginss := slice.FilterAndMap(config.GetConfig().ListPagePlugins, func(t string) (func(PostsPlugin, *Handle, *models.Posts), bool) {
f, ok := m[s] f, ok := m[t]
if ok { return f, ok
pluginss = append(pluginss, f) })
}
}
slice.Unshift(&pluginss, PasswordProject) slice.Unshift(&pluginss, PasswordProject)
return PostsPlugins(PostPlugin(ordinaryPlugin.Load()...), pluginss...) return PostsPlugins(PostPlugin(ordinaryPlugin.Load()...), pluginss...)
} }

View File

@ -34,7 +34,14 @@ func Sort[T any](arr []T, fn func(i, j T) bool) {
fn: fn, fn: fn,
} }
sort.Sort(slice) sort.Sort(slice)
return }
func StableSort[T any](arr []T, fn func(i, j T) bool) {
slice := anyArr[T]{
data: arr,
fn: fn,
}
sort.Stable(slice)
} }
func Sorts[T constraints.Ordered](a []T, order int) { func Sorts[T constraints.Ordered](a []T, order int) {

View File

@ -102,3 +102,64 @@ func TestSorts(t *testing.T) {
}) })
} }
} }
func TestStableSort(t *testing.T) {
type xy struct {
x int
y int
}
type args[T any] struct {
arr []T
fn func(i, j T) bool
}
type testCase[T any] struct {
name string
args args[T]
wantR []T
}
tests := []testCase[xy]{
{
name: "t1",
args: args[xy]{
arr: []xy{
{1, 2},
{3, 4},
{1, 3},
{2, 1},
{1, 6},
},
fn: func(i, j xy) bool {
if i.x < j.x {
return true
}
if i.x == j.x && i.y > i.y {
return true
}
return false
},
},
wantR: []xy{
{1, 2},
{1, 3},
{1, 6},
{2, 1},
{3, 4},
},
},
{
name: "t1",
args: args[xy]{
arr: []xy{{1, 2}, {1, 3}, {1, 6}},
fn: func(i, j xy) bool {
return i.x > j.x
},
},
wantR: []xy{{1, 2}, {1, 3}, {1, 6}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
StableSort(tt.args.arr, tt.args.fn)
})
}
}