-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_set.go
56 lines (47 loc) · 1.42 KB
/
string_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
package collections
import "reflect"
// StringSet provides a set-like structure for string keys.
// Adapted from https://play.golang.org/p/tDdutH672-
type StringSet struct {
set map[string]bool
}
// NewStringSet will initialize a new, empty StringSet.
func NewStringSet() *StringSet {
return &StringSet{make(map[string]bool)}
}
// NewStringSetFromArray will initialize a new StringSet, and populate it from the given array.
func NewStringSetFromArray(members []string) *StringSet {
set := NewStringSet()
for _, val := range members {
set.Add(val)
}
return set
}
// Add will add the specified string to the set, if it hasn't already been added.
func (set *StringSet) Add(i string) bool {
_, found := set.set[i]
set.set[i] = true
return !found
}
// Contains will determine whether or not the specified string is contained in the set.
func (set *StringSet) Contains(i string) bool {
_, found := set.set[i]
return found
}
// Remove will remove the specified string from the set.
func (set *StringSet) Remove(i string) {
delete(set.set, i)
}
// Size will tell you how many values are in the set.
func (set *StringSet) Size() int {
return len(set.set)
}
// Members will return the list of set members, as an array of strings.
func (set *StringSet) Members() []string {
keys := reflect.ValueOf(set.set).MapKeys()
strkeys := make([]string, len(keys))
for i := 0; i < len(keys); i++ {
strkeys[i] = keys[i].String()
}
return strkeys
}