-
Notifications
You must be signed in to change notification settings - Fork 27
/
module_list.go
165 lines (132 loc) · 3.14 KB
/
module_list.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
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
package utils
import (
"sort"
)
// SortReverseByReference returns a new array with items, reverse sorted
// by the order of 'ref' array.
func SortReverseByReference(in []string, ref []string) []string {
res := make([]string, 0)
inValues := make(map[string]bool)
for _, v := range in {
inValues[v] = true
}
for _, v := range ref {
if _, hasKey := inValues[v]; hasKey {
// prepend
res = append([]string{v}, res...)
}
}
return res
}
// SortReverse creates a copy of 'in' array and sort it in a reverse order.
func SortReverse(in []string) []string {
res := make([]string, 0)
res = append(res, in...)
sort.Sort(sort.Reverse(sort.StringSlice(res)))
return res
}
// SortByReference returns a new array with items sorted by the order of 'ref' array.
func SortByReference(in []string, ref []string) []string {
res := make([]string, 0)
inValues := make(map[string]bool)
for _, v := range in {
inValues[v] = true
}
for _, v := range ref {
if _, hasKey := inValues[v]; hasKey {
res = append(res, v)
}
}
return res
}
// KeysSortedByReference returns keys from map sorted by the order of 'ref' array.
// Note: keys not in ref are ignored.
func KeysSortedByReference(m map[string]struct{}, ref []string) []string {
res := make([]string, 0)
for _, v := range ref {
if _, hasKey := m[v]; hasKey {
res = append(res, v)
}
}
return res
}
// ListSubtract creates a new array from 'src' array with items that are
// not present in 'ignored' arrays.
func ListSubtract(src []string, ignored ...[]string) (result []string) {
ignoredMap := make(map[string]bool)
for _, arr := range ignored {
for _, v := range arr {
ignoredMap[v] = true
}
}
for _, v := range src {
if _, ok := ignoredMap[v]; !ok {
result = append(result, v)
}
}
return
}
// ListIntersection returns an array with items that are present in all 'arrs' arrays.
func ListIntersection(arrs ...[]string) (result []string) {
if len(arrs) == 0 {
return
}
// Counts each item in arrays.
m := make(map[string]int)
for _, a := range arrs {
for _, v := range a {
m[v]++
}
}
for k, v := range m {
if v == len(arrs) {
result = append(result, k)
}
}
return
}
// ListUnion creates a new array with unique items from all src arrays.
func ListUnion(src []string, more ...[]string) []string {
m := make(map[string]struct{})
for _, v := range src {
m[v] = struct{}{}
}
for _, arr := range more {
for _, v := range arr {
m[v] = struct{}{}
}
}
res := make([]string, 0, len(m))
for k := range m {
res = append(res, k)
}
return res
}
// ListFullyIn returns whether all 'arr' items contains in `ref` array.
func ListFullyIn(arr []string, ref []string) bool {
res := true
m := make(map[string]bool)
for _, v := range ref {
m[v] = true
}
for _, v := range arr {
if _, ok := m[v]; !ok {
return false
}
}
return res
}
func MapStringStructKeys(m map[string]struct{}) []string {
res := make([]string, 0, len(m))
for k := range m {
res = append(res, k)
}
return res
}
func ListToMapStringStruct(items []string) map[string]struct{} {
res := make(map[string]struct{})
for _, item := range items {
res[item] = struct{}{}
}
return res
}