forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortedset.go
45 lines (39 loc) · 773 Bytes
/
sortedset.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
package function
import (
"sort"
"sync"
)
// sorted set of strings.
//
// write-optimized and suitable only for fairly small values of N.
// Should this increase dramatically in size, a different implementation,
// such as a linked list, might be more appropriate.
type sortedSet struct {
members map[string]bool
sync.Mutex
}
func newSortedSet() *sortedSet {
return &sortedSet{
members: make(map[string]bool),
}
}
func (s *sortedSet) Add(value string) {
s.Lock()
s.members[value] = true
s.Unlock()
}
func (s *sortedSet) Remove(value string) {
s.Lock()
delete(s.members, value)
s.Unlock()
}
func (s *sortedSet) Items() []string {
s.Lock()
defer s.Unlock()
n := []string{}
for k := range s.members {
n = append(n, k)
}
sort.Strings(n)
return n
}