forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsets.go
More file actions
171 lines (126 loc) · 2.4 KB
/
Copy pathsets.go
File metadata and controls
171 lines (126 loc) · 2.4 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
170
171
package errors
import (
"reflect"
"sort"
)
type Empty struct{}
type String map[string]Empty
func NewString(items ...string) String {
new := make(String)
new.Insert(items...)
return new
}
func MapKeysToString(theMap any) String {
new := NewString()
reflectedMap := reflect.ValueOf(theMap)
for _, v := range reflectedMap.MapKeys() {
new.Insert(v.Interface().(string))
}
return new
}
func (s String) Insert(items ...string) {
for _, item := range items {
s[item] = Empty{}
}
}
func (s String) Delete(item string) {
delete(s, item)
}
func (s String) Has(item string) bool {
_, ok := s[item]
return ok
}
func (s String) HasAny(items ...string) bool {
for _, item := range items {
if s.Has(item) {
return true
}
}
return false
}
func (s String) HasAll(items ...string) bool {
for _, item := range items {
if !s.Has(item) {
return false
}
}
return true
}
func (s String) Difference(b String) String {
d := NewString()
for key := range s {
if !b.Has(key) {
d.Insert(key)
}
}
return d
}
func (s String) InterSection(b String) String {
i := NewString()
var walk String
var other String
if len(s) < len(b) {
walk = s
other = b
} else {
walk = b
other = s
}
for key := range walk {
if other.Has(key) {
i.Insert(key)
}
}
return i
}
func (s String) Union(b String) String {
u := NewString()
for key := range s {
u.Insert(key)
}
for key := range b {
u.Insert(key)
}
return u
}
func (s String) IsSuperSet(b String) bool {
for key := range b {
if !s.Has(key) {
return false
}
}
return true
}
func (s String) IsEqual(b String) bool {
return len(s) == len(b) && s.IsSuperSet(b)
}
type SortableStringList []string
func (l SortableStringList) Len() int { return len(l) }
func (l SortableStringList) Less(i, j int) bool { return l[i] < l[j] }
func (l SortableStringList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (s String) ConvertToSortedList() SortableStringList {
l := make(SortableStringList, 0, len(s))
for key := range s {
l = append(l, key)
}
sort.Sort(l)
return l
}
func (s String) ConvertToUnsortedList() SortableStringList {
l := make(SortableStringList, 0, len(s))
for key := range s {
l = append(l, key)
}
return l
}
func (s String) PopAny() (string, bool) {
for key := range s {
s.Delete(key)
return key, true
}
var ZeroValue string
return ZeroValue, false
}
func (s String) Len() int {
return len(s)
}