-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.go
68 lines (56 loc) · 1.13 KB
/
cluster.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
package main
import (
"errors"
"unicode"
)
type RuneCluster map[rune]int
func NewRuneCluster(input string) *RuneCluster {
rc := make(RuneCluster)
for i := 0; i < len(input); i++ {
r := rune(input[i])
if unicode.IsLetter(r) {
rc[unicode.ToLower(r)] += 1
}
}
return &rc
}
func (rc *RuneCluster) Count(r rune) int {
return (*rc)[unicode.ToLower(r)]
}
func (rc *RuneCluster) Has(r rune) bool {
return rc.Count(r) > 0
}
func (rc *RuneCluster) SubSetOf(other *RuneCluster) bool {
for r, c := range *rc {
if c > other.Count(r) {
return false
}
}
return true
}
func (rc *RuneCluster) Equals(other *RuneCluster) bool {
if len(*rc) != len(*other) {
return false
}
for r, c := range *rc {
if c != other.Count(r) {
return false
}
}
return true
}
func (rc *RuneCluster) Minus(other *RuneCluster) (*RuneCluster, error) {
result := NewRuneCluster("")
for r, c := range *rc {
oc := other.Count(r)
if c > oc {
(*result)[r] = c - oc
} else if c < oc {
return nil, errors.New("Not a superset of other cluster")
}
}
return result, nil
}
func (rc *RuneCluster) IsEmpty() bool {
return len(*rc) == 0
}