-
Notifications
You must be signed in to change notification settings - Fork 6
/
set.go
58 lines (49 loc) · 867 Bytes
/
set.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
package types
// Set -
type Set map[string]struct{}
// Add - add item to set
func (s Set) Add(item string) {
s[item] = struct{}{}
}
// Append - append items to set
func (s Set) Append(str ...string) {
for j := range str {
if str[j] == "" {
continue
}
if _, ok := s[str[j]]; !ok {
s[str[j]] = struct{}{}
}
}
}
// Len - returns length of set
func (s Set) Len() int {
return len(s)
}
// Values - return keys
func (s Set) Values() []string {
r := make([]string, 0)
for k := range s {
r = append(r, k)
}
return r
}
// Merge -
func (s Set) Merge(m Set) {
for k := range m {
s[k] = struct{}{}
}
}
// Has -
func (s Set) Has(key string) bool {
_, ok := s[key]
return ok
}
// ArrayUniqueLen -
func ArrayUniqueLen(arr []string) int {
buf := make(map[string]struct{})
for i := range arr {
buf[arr[i]] = struct{}{}
}
return len(buf)
}