-
Notifications
You must be signed in to change notification settings - Fork 2
/
keyset_overlay.go
236 lines (203 loc) · 4.85 KB
/
keyset_overlay.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package keyset
func newListOverlay(list KeyList) *listOverlay {
if list == nil {
panic("illegal value")
}
return &listOverlay{
immutable: list,
includes: inclusiveMutable{inclusiveKeySet{emptyBasicKeySet}},
excludes: exclusiveMutable{exclusiveKeySet{emptyBasicKeySet}},
}
}
func newMutableOverlay(list KeyList) mutableKeySet {
return &inclusiveMutableOverlay{inclusiveKeySet{newListOverlay(list)}}
}
func emptyInclusiveMutable() *inclusiveMutable {
return &inclusiveMutable{inclusiveKeySet{emptyBasicKeySet}}
}
var _ internalKeySet = &listOverlay{}
type listOverlay struct {
immutable KeyList
includes inclusiveMutable
excludes exclusiveMutable
}
func (v *listOverlay) Count() int {
return v.immutable.Count() - v.excludes.RawKeyCount() + v.includes.RawKeyCount()
}
func (v *listOverlay) Contains(k Key) bool {
return v.excludes.Contains(k) && (v.immutable.Contains(k) || v.includes.Contains(k))
}
func (v *listOverlay) EnumKeys(fn func(k Key) bool) bool {
if v.includes.keys.EnumKeys(fn) {
return true
}
return v.immutable.EnumKeys(func(k Key) bool {
if !v.excludes.Contains(k) {
return false
}
return fn(k)
})
}
func (v *listOverlay) enumRawKeys(exclusive bool, fn func(k Key, exclusive bool) bool) bool {
if v.includes.EnumRawKeys(fn) {
return true
}
return v.immutable.EnumKeys(func(k Key) bool {
if !v.excludes.Contains(k) {
return false
}
return fn(k, false)
})
}
func (v *listOverlay) copy(n int) basicKeySet {
return listSet{v}.copy(n)
}
func (v *listOverlay) isEmpty() bool {
switch n := v.immutable.Count(); {
case v.includes.RawKeyCount() > 0:
return false
case n == 0:
return true
default:
return n == v.excludes.RawKeyCount()
}
}
func (v *listOverlay) _remove(k Key) {
v.includes.remove(k)
if v.immutable.Contains(k) {
v.excludes.remove(k)
}
}
func (v *listOverlay) _add(k Key) {
v.excludes.add(k)
if !v.immutable.Contains(k) {
v.includes.add(k)
}
}
var _ mutableKeySet = &inclusiveMutableOverlay{}
type inclusiveMutableOverlay struct {
inclusiveKeySet
}
func (v *inclusiveMutableOverlay) copy(n int) basicKeySet {
return v.keys.copy(n)
}
func (v *inclusiveMutableOverlay) retainAll(ks KeySet) mutableKeySet {
keys := v.keys.(*listOverlay)
if keys.isEmpty() {
return nil
}
switch kn := ks.RawKeyCount(); {
case kn == 0:
if !ks.IsOpenSet() {
return emptyInclusiveMutable()
}
return nil
case ks.IsOpenSet() && kn < keys.Count():
ks.EnumRawKeys(func(k Key, exclusive bool) bool {
keys._remove(k)
return keys.isEmpty()
})
default:
// TODO PLAT-20 efficiency can be improved
keys.EnumKeys(func(k Key) bool {
if !ks.Contains(k) {
keys._remove(k)
}
return false
})
}
if keys.isEmpty() {
return emptyInclusiveMutable()
}
return nil
}
func (v *inclusiveMutableOverlay) removeAll(ks KeySet) mutableKeySet {
keys := v.keys.(*listOverlay)
prevIncludes := keys.includes.RawKeyCount()
switch {
case keys.isEmpty():
return nil
case !ks.IsOpenSet():
keys.includes.removeAll(ks)
case ks.RawKeyCount() < keys.Count():
var newKeys basicKeySet
ks.EnumRawKeys(func(k Key, _ bool) bool {
if keys.Contains(k) {
if newKeys == nil {
// there will be no more than ks.RawKeyCount() ...
newKeys = newBasicKeySet(0)
}
newKeys.add(k)
}
return false
})
return &inclusiveMutable{inclusiveKeySet{newKeys}}
default:
keys.includes.retainAll(ks.Inverse())
}
if prevIncludes == keys.includes.RawKeyCount()+ks.RawKeyCount() {
return nil
}
keys.immutable.EnumKeys(func(k Key) bool {
if keys.excludes.Contains(k) && ks.Contains(k) {
keys.excludes.remove(k)
}
return keys.immutable.Count() == keys.excludes.RawKeyCount()
})
if keys.isEmpty() {
return emptyInclusiveMutable()
}
return nil
}
func (v *inclusiveMutableOverlay) addAll(ks KeySet) mutableKeySet {
keys := v.keys.(*listOverlay)
if ks.IsOpenSet() {
// NB! it changes a type of the set to exclusive
var newKeys basicKeySet
ks.EnumRawKeys(func(k Key, _ bool) bool {
if !keys.Contains(k) {
if newKeys == nil {
newKeys = newBasicKeySet(ks.RawKeyCount())
}
newKeys.add(k)
}
return false
})
return &exclusiveMutable{exclusiveKeySet{newKeys}}
}
kn := ks.RawKeyCount()
if kn == 0 {
return nil
}
ks.EnumRawKeys(func(k Key, _ bool) bool {
keys._add(k)
return false
})
return nil
}
func (v *inclusiveMutableOverlay) removeKeys(ks []Key) {
p := v.keys.(*listOverlay)
p.includes.removeKeys(ks)
for _, k := range ks {
if p.immutable.Contains(k) {
p.excludes.remove(k)
}
}
}
func (v *inclusiveMutableOverlay) addKeys(ks []Key) {
p := v.keys.(*listOverlay)
p.excludes.addKeys(ks)
for _, k := range ks {
if !p.immutable.Contains(k) {
p.includes.add(k)
}
}
}
func (v *inclusiveMutableOverlay) remove(k Key) {
p := v.keys.(*listOverlay)
p._remove(k)
}
func (v *inclusiveMutableOverlay) add(k Key) {
p := v.keys.(*listOverlay)
p._add(k)
}