-
Notifications
You must be signed in to change notification settings - Fork 105
/
ordered_string_set.go
44 lines (37 loc) · 1.22 KB
/
ordered_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
package helpers
import "strings"
// OrderedStringSet is a Set for strings
// that provides the accumulated strings in the order they were received.
// The zero value is a fully functional empty set.
type OrderedStringSet struct {
elements []string
}
// NewOrderedStringSet provides instances of OrderedStringSet
// populated with the given elements.
func NewOrderedStringSet(elements ...string) OrderedStringSet {
return OrderedStringSet{elements: elements}
}
// Add provides a new Set with the given element added.
// If the element already exists, it re-uses the existing element and does not append a new one.
func (set OrderedStringSet) Add(text string) OrderedStringSet {
if !set.Contains(text) {
return OrderedStringSet{elements: append(set.elements, text)}
}
return set
}
// Contains indicates whether this Set contains the given string.
func (set OrderedStringSet) Contains(text string) bool {
for _, element := range set.elements {
if text == element {
return true
}
}
return false
}
// Slice provides the elements of this set in the order they were received.
func (set OrderedStringSet) Slice() []string {
return set.elements
}
func (set OrderedStringSet) String() string {
return strings.Join(set.elements, ", ")
}