-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
172 lines (143 loc) · 3.34 KB
/
util.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package qualify
import "github.com/bsm/qualify/internal/intsets"
// StrDict can be used to convert string values to integers
// usingdictionary encoding.
type StrDict map[string]int
// NewStrDict inits a new dictionary
func NewStrDict() StrDict { return make(StrDict) }
// Add adds a string to the dictionary
// returning the numeric value/index. Returns the
// previous index value if the string has been added
// before.
func (m StrDict) Add(s string) int {
v, ok := m[s]
if !ok {
v = len(m)
m[s] = v
}
return v
}
// Lookup performs a lookup of multiple strings and appends
// the results to dst returning it
func (m StrDict) Lookup(dst []int, strs ...string) []int {
for _, s := range strs {
if v, ok := m[s]; ok {
dst = append(dst, v)
}
}
return dst
}
// --------------------------------------------------------------------
type none struct{}
// --------------------------------------------------------------------
type fieldSet map[Field]none
func (s fieldSet) Add(field Field) {
if _, ok := s[field]; !ok {
s[field] = none{}
}
}
// --------------------------------------------------------------------
type fieldMap map[Field]*intsets.Dense
func (m fieldMap) Human() map[Field][]int {
x := make(map[Field][]int, len(m))
for f, s := range m {
x[f] = s.AppendTo(nil)
}
return x
}
// InvertTo creates a copy of the field-map with the values inverted
func (m fieldMap) InvertTo(sz int) fieldMap {
full := new(intsets.Dense)
for i := 0; i < sz; i++ {
full.Insert(i)
}
o := make(fieldMap, len(m))
for k, v := range m {
s := new(intsets.Dense)
s.Copy(full)
s.DifferenceWith(v)
o[k] = s
}
return o
}
func (m fieldMap) Fetch(field Field) *intsets.Dense {
v, ok := m[field]
if !ok {
v = new(intsets.Dense)
m[field] = v
}
return v
}
// --------------------------------------------------------------------
type fieldValue struct {
Field Field
Value int
}
type fieldValueMap map[fieldValue]*intsets.Dense
func (m fieldValueMap) Fetch(field Field, value int) *intsets.Dense {
key := fieldValue{Field: field, Value: value}
v, ok := m[key]
if !ok {
v = new(intsets.Dense)
m[key] = v
}
return v
}
func (m fieldValueMap) Copy() fieldValueMap {
o := make(fieldValueMap, len(m))
for k, v := range m {
s := new(intsets.Dense)
s.Copy(v)
o[k] = s
}
return o
}
func (m fieldValueMap) Human() map[fieldValue][]int {
x := make(map[fieldValue][]int, len(m))
for f, s := range m {
x[f] = s.AppendTo(nil)
}
return x
}
// --------------------------------------------------------------------
// outcome -> field -> multiOneOf
type fieldMultiMap map[int]map[Field]*multiOneOf
func (m fieldMultiMap) Human() map[int]map[Field]int {
x := make(map[int]map[Field]int, len(m))
for o, s := range m {
if _, ok := x[o]; !ok {
x[o] = make(map[Field]int)
}
for f, m := range s {
x[o][f] = m.size
}
}
return x
}
// Prune removes irrelevant checks
func (m fieldMultiMap) Prune() {
for outcome, sub := range m {
for field, check := range sub {
if check.size < 2 {
delete(sub, field)
m[outcome] = sub
}
}
if len(m[outcome]) == 0 {
delete(m, outcome)
}
}
}
func (m fieldMultiMap) Fetch(outcome int, field Field) *multiOneOf {
sub, ok := m[outcome]
if !ok {
sub = make(map[Field]*multiOneOf, 1)
m[outcome] = sub
}
set, ok := sub[field]
if !ok {
set = newMultiOneOf()
sub[field] = set
}
return set
}