-
Notifications
You must be signed in to change notification settings - Fork 0
/
runeset.go
108 lines (94 loc) · 2 KB
/
runeset.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
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
package runeset
import (
"bytes"
"sort"
)
// Set represents a set of runes
type Set map[rune]struct{}
// Add a rune to the set.
func (s Set) Add(r rune) {
s[r] = struct{}{} // zero-byte struct
}
// Contains reports whether set contains given rune
func (s Set) Contains(r rune) bool {
_, found := s[r]
return found
}
// Make creates and returns a new Set
func Make(chars ...rune) Set {
s := Set{}
for _, c := range chars {
s.Add(c)
}
return s
}
// MakeFromString creates and returns a new Set
func MakeFromString(text string) Set {
return Make([]rune(text)...)
}
// Equal reports whether set is equal to other
func (s Set) Equal(other Set) bool {
if len(s) != len(other) {
return false
}
for elem := range s {
if _, found := other[elem]; !found {
return false
}
}
return true
}
// Copy returns a new Set: a copy of s.
func (s Set) Copy() Set {
res := Set{}
for elem := range s {
res[elem] = struct{}{}
}
return res
}
// Intersection returns a new set: the intersection of s AND other
func (s Set) Intersection(other Set) Set {
result := Set{}
if len(other) > 0 {
for r := range s {
if other.Contains(r) {
result.Add(r)
}
}
}
return result
}
// IntersectionUpdate changes receiver in-place, keeping only
// elements that are in it AND in other.
func (s Set) IntersectionUpdate(other Set) {
for r := range s {
if !other.Contains(r) {
delete(s, r)
}
}
}
type runeSlice []rune
func (rs runeSlice) Len() int { return len(rs) }
func (rs runeSlice) Less(i, j int) bool { return rs[i] < rs[j] }
func (rs runeSlice) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
// Sorted returns sorted slice with runes from s
func (s Set) Sorted() []rune {
var rs []rune
for c := range s {
rs = append(rs, c)
}
sort.Sort(runeSlice(rs))
return rs
}
func (s Set) String() string {
var buf bytes.Buffer
buf.WriteString("Set{")
for i, c := range s.Sorted() {
if i > 0 {
buf.WriteByte(' ')
}
buf.WriteString(string(c))
}
buf.WriteByte('}')
return buf.String()
}