array.go
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package utils
import (
"fmt"
"strings"
)
type ArrayCmpare interface {
}
//ArrayInt64Diff 返回切片的差集:arr1-arr2
func ArrayInt64Diff(arr1 []int64, arr2 []int64) []int64 {
if len(arr2) == 0 {
return arr1
}
setmap := make(map[int64]bool)
for i := range arr2 {
setmap[arr2[i]] = true
}
var result []int64
for i := range arr1 {
if _, ok := setmap[arr1[i]]; !ok {
result = append(result, arr1[i])
}
}
return result
}
//ArrayIntDiff 返回切片的差集:arr1-arr2
func ArrayIntDiff(arr1 []int, arr2 []int) []int {
if len(arr2) == 0 {
return arr1
}
setmap := make(map[int]bool)
for i := range arr2 {
setmap[arr2[i]] = true
}
var result []int
for i := range arr1 {
if _, ok := setmap[arr1[i]]; !ok {
result = append(result, arr1[i])
}
}
return result
}
//ArrayStringIn 检查s字符串是否在切片sl中
func ArrayStringIn(arr1 []string, s string) bool {
for _, v := range arr1 {
if strings.Compare(v, s) == 0 {
return true
}
}
return false
}
func JoinInt64s(ids []int64, spilt string) string {
var idStrings []string = make([]string, len(ids))
for i := 0; i < len(ids); i++ {
idStrings[i] = fmt.Sprintf("%v", ids[i])
}
return strings.Join(idStrings, spilt)
}
func ArrayInt64Unique(s []int64) []int64 {
m := map[int64]bool{}
newS := []int64{}
for _, v := range s {
if _, ok := m[v]; ok {
continue
}
newS = append(newS, v)
}
return newS
}
//ArraySetString 构建元素不重复的数组
//可按需扩展
type ArraySetString struct {
m map[string]int
}
func NewArraySetString() ArraySetString {
return ArraySetString{
m: make(map[string]int),
}
}
func (set *ArraySetString) Has(str string) bool {
if _, ok := set.m[str]; ok {
return true
}
return false
}
//Add 追加元素
func (set *ArraySetString) Add(str string) {
if _, ok := set.m[str]; ok {
return
}
l := len(set.m)
set.m[str] = l
}
//Remove 移除指定元素
func (set *ArraySetString) Remove(str string) {
delete(set.m, str)
}
//Len 元素长度
func (set *ArraySetString) Len() int {
return len(set.m)
}
//ToSlice 转换为切片输出
func (set *ArraySetString) ToSlice() []string {
newSlice := make([]string, len(set.m))
for k, v := range set.m {
newSlice[v] = k
}
return newSlice
}
//ArraySetString 构建元素不重复的数组
//可按需扩展
type ArraySetInt64 struct {
m map[int64]int
}
func NewArraySetInt64() *ArraySetInt64 {
return &ArraySetInt64{
m: make(map[int64]int),
}
}
func (set *ArraySetInt64) Has(str int64) bool {
if _, ok := set.m[str]; ok {
return true
}
return false
}
//Add 追加元素
func (set *ArraySetInt64) Add(str int64) {
if _, ok := set.m[str]; ok {
return
}
l := len(set.m)
set.m[str] = l
}
//Remove 移除指定元素
func (set *ArraySetInt64) Remove(str int64) {
delete(set.m, str)
}
//Len 元素长度
func (set *ArraySetInt64) Len() int {
return len(set.m)
}
//ToSlice 转换为切片输出
func (set *ArraySetInt64) ToSlice() []int64 {
newSlice := make([]int64, len(set.m))
for k, v := range set.m {
newSlice[v] = k
}
return newSlice
}