Skip to content

Commit 2320dcc

Browse files
committed
init
0 parents  commit 2320dcc

File tree

8 files changed

+342
-0
lines changed

8 files changed

+342
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.go linguist-language=Go
2+
go.sum linguist-generated

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.DS_Store
2+
*.DS_Store
3+
*/*.DS_Store
4+
5+
# Binaries for programs and plugins
6+
*.exe
7+
*.exe~
8+
*.dll
9+
*.so
10+
*.dylib
11+
12+
# Test binary, built with `go test -c`
13+
*.test
14+
15+
# Output of the go coverage tool, specifically when used with LiteIDE
16+
*.out
17+
18+
# Dependency directories (remove the comment below to include it)
19+
# vendor/
20+
go.sum
21+
go.work

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/starfork/go-slice
2+
3+
go 1.21.4
4+
5+
require github.com/starfork/stargo v0.0.6

item.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package slice
2+
3+
// 数字类型特有的
4+
type Item interface {
5+
int | float32 | float64 | uint32 | uint64 | string
6+
}
7+
8+
type SliceItem[T Item] []T
9+
10+
func NewNumber[T Item](a []T) SliceItem[T] {
11+
return a
12+
}
13+
14+
// 求和
15+
func (s SliceItem[T]) Sum() T {
16+
var sum T
17+
for _, v := range s {
18+
sum += v
19+
}
20+
return sum
21+
}
22+
23+
// 取最大值。
24+
func (s SliceItem[T]) Max() T {
25+
var max T = s[0]
26+
for _, item := range s {
27+
if item > max {
28+
max = item
29+
}
30+
}
31+
return max
32+
}

job.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package slice
2+
3+
import "time"
4+
5+
// 分批执行一个slice的任务
6+
func RangeJob[T Item](ids []T, step int, job func([]T), duration ...time.Duration) {
7+
l := len(ids)
8+
for i := 0; i < l; {
9+
h := i + step
10+
if h > l {
11+
h = l
12+
}
13+
if len(duration) > 0 {
14+
time.Sleep(duration[0])
15+
}
16+
go job(ids[i:h])
17+
i = i + step
18+
}
19+
}

job_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package slice
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestRangeFunc(t *testing.T) {
9+
ids := []uint64{123, 4343, 343, 343, 555}
10+
index := "abcd"
11+
testJob := func(ids []uint64) {
12+
fmt.Println(index, ids)
13+
}
14+
15+
RangeJob(ids, 2, testJob)
16+
}

slice.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package slice
2+
3+
import "github.com/starfork/stargo/util/number"
4+
5+
type Slice[T comparable] []T
6+
7+
func New[T comparable](a []T) Slice[T] {
8+
return a
9+
}
10+
11+
// 包含,f过滤函数
12+
func (s Slice[T]) Contains(key T, f ...func(k T) bool) bool {
13+
14+
fn := func(v T) bool {
15+
return key == v
16+
}
17+
if len(f) > 0 {
18+
fn = f[0]
19+
}
20+
for _, v := range s {
21+
if fn(v) {
22+
return true
23+
}
24+
}
25+
return false
26+
}
27+
28+
// 默认返回.不包含k,则返回v
29+
func (s Slice[T]) Default(k, v T) T {
30+
if s.Contains(k) {
31+
return k
32+
}
33+
return v
34+
}
35+
36+
// 取一个.非随机取一个
37+
func (s Slice[T]) One(index ...int) T {
38+
max := len(s)
39+
if max == 1 {
40+
return s[0]
41+
}
42+
var idx int = 0
43+
if len(index) > 0 {
44+
idx = index[0]
45+
}
46+
if idx >= max {
47+
idx = max - 1
48+
}
49+
return s[idx : idx+1][0]
50+
}
51+
52+
func (s Slice[T]) Rand() T {
53+
max := len(s)
54+
idx, _ := number.RangeRand(0, int64(max-1))
55+
return s[idx : idx+1][0]
56+
}
57+
58+
func (s Slice[T]) Map(fn func(k T) T) Slice[T] {
59+
var res []T
60+
for _, item := range s {
61+
res = append(res, fn(item))
62+
}
63+
return res
64+
}
65+
66+
// 过滤
67+
func (s Slice[T]) Filter(fn func(T) bool) Slice[T] {
68+
var res []T
69+
for _, item := range s {
70+
if fn(item) {
71+
res = append(res, item)
72+
}
73+
}
74+
return res
75+
}
76+
77+
// 去重
78+
func (s Slice[T]) Unique() Slice[T] {
79+
80+
tmp := map[T]T{}
81+
82+
for _, item := range s {
83+
tmp[item] = item
84+
}
85+
var res []T
86+
for _, k := range tmp {
87+
res = append(res, k)
88+
}
89+
s = res
90+
return s
91+
}
92+
93+
// Tail 获取切片尾部元素
94+
// dv: 空切片默认值
95+
func (s Slice[T]) Tail(dv ...T) T {
96+
if s == nil && len(dv) > 0 {
97+
return dv[0]
98+
}
99+
return s[len(s)-1]
100+
}
101+
102+
// 交集
103+
func (s Slice[T]) Intersect(b Slice[T]) Slice[T] {
104+
inter := make([]T, 0)
105+
mp := make(map[T]bool)
106+
for _, sa := range s {
107+
if _, ok := mp[sa]; !ok {
108+
mp[sa] = true
109+
}
110+
}
111+
for _, sb := range b {
112+
if _, ok := mp[sb]; ok {
113+
inter = append(inter, sb)
114+
}
115+
}
116+
return inter
117+
}
118+
119+
// 差集
120+
func (s Slice[T]) Diff(b Slice[T]) Slice[T] {
121+
diff := make([]T, 0)
122+
mp := make(map[T]bool)
123+
for _, sa := range s {
124+
if _, ok := mp[sa]; !ok {
125+
mp[sa] = true
126+
}
127+
}
128+
for _, sb := range b {
129+
if ok := mp[sb]; ok {
130+
delete(mp, sb)
131+
}
132+
}
133+
for k := range mp {
134+
diff = append(diff, k)
135+
}
136+
137+
return diff
138+
}
139+
140+
// 并集
141+
func (s Slice[T]) Union(b Slice[T]) Slice[T] {
142+
union := make([]T, 0)
143+
mp := make(map[T]bool)
144+
s = append(s, b...)
145+
for _, v := range s {
146+
if ok := mp[v]; ok {
147+
continue
148+
}
149+
mp[v] = true
150+
union = append(union, v)
151+
}
152+
153+
return union
154+
}
155+
156+
func (s Slice[T]) IsEmpty() bool {
157+
return s == nil
158+
}

slice_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package slice_test
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/starfork/go-slice"
9+
)
10+
11+
func TestContains(t *testing.T) {
12+
noAuth := slice.New([]string{"/v1/passport/", "/v1/public/"})
13+
path := "/v1/passport/login"
14+
rs := noAuth.Contains(path, func(key string) bool {
15+
fmt.Println(path, key)
16+
return strings.Contains(path, key)
17+
})
18+
fmt.Println(rs)
19+
//rs1 := noAuth.Contains("/v1/passport/")
20+
//fmt.Println(rs1)
21+
}
22+
23+
func TestAsSlice(t *testing.T) {
24+
str := ""
25+
s := slice.New(strings.Split(str, ","))
26+
fmt.Println(s)
27+
if !s.Contains("abc") {
28+
s = append(s, "abc")
29+
}
30+
if !s.Contains("abc") {
31+
s = append(s, "abc")
32+
}
33+
ns := slice.New(strings.Split(strings.Join(s, ","), ","))
34+
fmt.Println(ns)
35+
if !ns.Contains("def") {
36+
ns = append(ns, "def")
37+
}
38+
if !ns.Contains("def") {
39+
ns = append(ns, "def")
40+
}
41+
fmt.Println(len(ns))
42+
43+
fmt.Println(strings.Join(ns, ","))
44+
}
45+
46+
func TestIntersect(t *testing.T) {
47+
a := slice.New([]string{"/v1/passport/", "/v1/public/"})
48+
b := slice.New([]string{"/v1/passport/", "/v1/public/1"})
49+
fmt.Println(a.Intersect(b))
50+
}
51+
52+
func TestDiff(t *testing.T) {
53+
a := slice.New([]string{"/v1/passport/", "/v1/public/", "/v1/public/2"})
54+
b := slice.New([]string{"/v1/passport/", "/v1/public/1"})
55+
fmt.Println(a.Diff(b))
56+
}
57+
func TestUnion(t *testing.T) {
58+
a := slice.New([]string{"/v1/passport/", "/v1/public/", "/v1/public/2"})
59+
b := slice.New([]string{"/v1/passport/", "/v1/public/1"})
60+
fmt.Println(a.Union(b))
61+
}
62+
63+
func TestOne(t *testing.T) {
64+
a := slice.New([]string{"/v1/passport/", "/v1/public/", "/v1/public/2", "/v1/public/4"})
65+
66+
fmt.Println(a.One(5))
67+
}
68+
69+
func TestCalc(t *testing.T) {
70+
a := slice.New([]string{}) //老权限
71+
//删除2,新增7,8之后
72+
b := slice.New([]string{"/v1/estate/estates$$GET", "/v1/service/display/services$$GET", "/v1/service/evaluate$$GET", "/v1/service/service$$PUT", "/v1/user/auth/rule$$PUT", "/v1/user/log/login$$GET", "/v1/user/friends$$GET", "/v1/user/auth/roles$$GET", "/v1/user/certifications$$GET"})
73+
inter := a.Intersect(b)
74+
fmt.Printf("交集:%+v", inter)
75+
76+
//需要删除的
77+
fmt.Printf("需要删除的节点:%+v", a.Diff(inter))
78+
add := b.Diff(inter)
79+
fmt.Printf("需要增加的节点:%+v", add)
80+
for k, v := range add {
81+
fmt.Printf("add-v,%d,%+v\n", k, v)
82+
}
83+
}
84+
85+
func TestUnique(t *testing.T) {
86+
a := slice.New([]string{"/v1/passport/", "/v1/public/", "/v1/public/2", "/v1/public/", "/v1/public/", "/v1/public/2"})
87+
88+
fmt.Println(a.Unique())
89+
}

0 commit comments

Comments
 (0)