-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
132 lines (106 loc) · 1.82 KB
/
array.go
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
// Copyright 2016 The Goutils Author. All Rights Reserved.
//
// -------------------------------------------------------------------
package goutils
type Array []interface{}
func (a *Array) Contain(val interface{}) bool {
for _, v := range *a {
if v == val {
return true
}
}
return false
}
func (a *Array) Insert(val interface{}) {
for _, v := range *a {
if v == val {
return
}
}
*a = append(*a, val)
}
func (a *Array) Remove(val interface{}) {
for i, v := range *a {
if v == val {
*a = append((*a)[:i], (*a)[i+1:]...)
break
}
}
}
func (a *Array) ToStringSlice() []string {
var arr []string
for _, v := range *a {
if val, ok := v.(string); ok {
arr = append(arr, val)
}
}
return arr
}
func ArrayStringContain(a []string, s string) bool {
for _, v := range a {
if v == s {
return true
}
}
return false
}
func ArrayStringRemove(a []string, s string) []string {
for i, v := range a {
if v == s {
a = append(a[:i], a[i+1:]...)
}
}
return a
}
func ArrayIntContain(a []int, n int) bool {
for _, v := range a {
if v == n {
return true
}
}
return false
}
func ArrayIntRemove(a []int, n int) []int {
for i, v := range a {
if v == n {
a = append(a[:i], a[i+1:]...)
}
}
return a
}
func ArrayInt64Contain(a []int64, n int64) bool {
for _, v := range a {
if v == n {
return true
}
}
return false
}
func ArrayInt64Remove(a []int64, n int64) []int64 {
for i, v := range a {
if v == n {
a = append(a[:i], a[i+1:]...)
}
}
return a
}
func ArrayRemoveDup(a []interface{}) []interface{} {
if len(a) <= 1 {
return a
}
var set []interface{} //nolint:prealloc // ignore
for _, n := range a {
found := false
for _, v := range set {
if n == v {
found = true
break
}
}
if found {
continue
}
set = append(set, n)
}
return set
}