Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

细节功能优化 #51

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kgo

type IntUintBig interface {
~int | ~int64 | ~uint | ~uint64
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type IntUintFloat interface {
Expand Down
19 changes: 18 additions & 1 deletion slice.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kgo

import "reflect"
import (
"reflect"
)

type ChsSort interface {
}
Expand Down Expand Up @@ -124,3 +126,18 @@ func Diff[T any](s1, s2 []T) (results []T) {
}
return results
}

// SplitCounter 根据给定的页大小和总数,生成一个二维切片
func SplitCounter[T any](pageSize, count int) (items [][]T) {
if pageSize <= 0 || count <= 0 {
return
}
for page := 1; page <= count/pageSize+1; page++ {
if page <= count/pageSize {
items = append(items, make([]T, 0, pageSize))
} else {
items = append(items, make([]T, 0, count%pageSize))
}
}
return items
}
46 changes: 46 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,49 @@ func TestSlice_Intersection_Union_Diff_String(t *testing.T) {
}
}
}

func TestSplitCounter(t *testing.T) {
type args struct {
count int
pageSize int
}
type testCase[T any] struct {
name string
args args
want [][]T
}
tests := []testCase[string]{
{
name: "SplitCounter1",
args: args{count: 13, pageSize: 5},
want: [][]string{{"", "", "", "", ""}, {"", "", "", "", ""}, {"", ""}},
},
{
name: "SplitCounter2",
args: args{count: 3, pageSize: 5},
want: [][]string{{"", "", ""}},
},
{
name: "SplitCounter3",
args: args{count: 10, pageSize: 5},
want: [][]string{{"", "", "", "", ""}, {"", "", "", "", ""}},
},
}
for _, test := range tests {
pageCount := test.args.count/test.args.pageSize + 1
t.Run(test.name, func(t *testing.T) {
items := SplitCounter[string](test.args.pageSize, test.args.count)
if len(items) != pageCount {
t.Errorf("len(items) = %v, want 3", len(items))
}
for i, item := range items {
if i+1 < pageCount && cap(item) != test.args.pageSize {
t.Errorf("cap(item) = %v, want %v", cap(item), test.args.pageSize)
}
if i+1 == pageCount && cap(item) != test.args.count%test.args.pageSize {
t.Errorf("cap(item) = %v, want %v", cap(item), test.args.count%test.args.pageSize)
}
}
})
}
}
Loading