forked from xiaonanln/goworld
/
collections.go
65 lines (55 loc) · 1.28 KB
/
collections.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
package common
// StringSet is a set of strings
type StringSet map[string]struct{}
// Contains checks if Stringset contains the string
func (ss StringSet) Contains(elem string) bool {
_, ok := ss[elem]
return ok
}
// Add adds the string to StringSet
func (ss StringSet) Add(elem string) {
ss[elem] = struct{}{}
}
// Remove removes the string from StringList
func (ss StringSet) Remove(elem string) {
delete(ss, elem)
}
// ToList convert StringSet to string slice
func (ss StringSet) ToList() []string {
keys := make([]string, 0, len(ss))
for s := range ss {
keys = append(keys, s)
}
return keys
}
// StringList is a list of string (slice)
type StringList []string
// Remove removes the string from StringList
func (sl *StringList) Remove(elem string) {
widx := 0
cpsl := *sl
for idx, _elem := range cpsl {
if _elem == elem {
// ignore this elem by doing nothing
} else {
if idx != widx {
cpsl[widx] = _elem
}
widx += 1
}
}
*sl = cpsl[:widx]
}
// Append add the string to the end of StringList
func (sl *StringList) Append(elem string) {
*sl = append(*sl, elem)
}
// Find get the index of string in StringList, returns -1 if not found
func (sl *StringList) Find(s string) int {
for idx, elem := range *sl {
if elem == s {
return idx
}
}
return -1
}