forked from tobgu/qframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
285 lines (227 loc) · 6.07 KB
/
filter.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package qframe
import (
"fmt"
"strings"
"github.com/tobgu/qframe/filter"
"github.com/tobgu/qframe/internal/index"
"github.com/tobgu/qframe/internal/math/integer"
"github.com/tobgu/qframe/qerrors"
)
// FilterClause is an internal interface representing a filter of some kind that can be applied on a QFrame.
type FilterClause interface {
fmt.Stringer
filter(qf QFrame) QFrame
Err() error
}
// Filter is the lowest level in a filter clause.
// See the docs for filter.Filter for an in depth description of the fields.
type Filter filter.Filter
type comboClause struct {
err error //nolint:structcheck
subClauses []FilterClause //nolint:structcheck
}
// AndClause represents the logical conjunction of multiple clauses.
type AndClause comboClause
// OrClause represents the logical disjunction of multiple clauses.
type OrClause comboClause
// NotClause represents the logical inverse of of a filter clause.
type NotClause struct {
subClause FilterClause
}
// NullClause is a convenience type to simplify clients when no filtering is to be done.
type NullClause struct{}
func anyFilterErr(clauses []FilterClause) error {
for _, c := range clauses {
if c.Err() != nil {
return c.Err()
}
}
return nil
}
// And returns a new AndClause that represents the conjunction of the passed filter clauses.
func And(clauses ...FilterClause) AndClause {
if len(clauses) == 0 {
return AndClause{err: qerrors.New("new AND clause", "zero subclauses not allowed")}
}
return AndClause{subClauses: clauses, err: anyFilterErr(clauses)}
}
func clauseString(clauses []FilterClause) string {
reps := make([]string, 0, len(clauses))
for _, c := range clauses {
reps = append(reps, c.String())
}
return strings.Join(reps, ", ")
}
// String returns a textual description of the filter.
func (c AndClause) String() string {
if c.Err() != nil {
return c.Err().Error()
}
return fmt.Sprintf(`["and", %s]`, clauseString(c.subClauses))
}
func (c AndClause) filter(qf QFrame) QFrame {
if qf.Err != nil {
return qf
}
if c.Err() != nil {
return qf.withErr(c.Err())
}
filteredQf := &qf
for _, c := range c.subClauses {
newQf := c.filter(*filteredQf)
filteredQf = &newQf
}
return *filteredQf
}
// Err returns any error that may have occurred during creation of the filter
func (c AndClause) Err() error {
return c.err
}
// Or returns a new OrClause that represents the disjunction of the passed filter clauses.
func Or(clauses ...FilterClause) OrClause {
if len(clauses) == 0 {
return OrClause{err: qerrors.New("new OR clause", "zero subclauses not allowed")}
}
return OrClause{subClauses: clauses, err: anyFilterErr(clauses)}
}
// String returns a textual description of the filter.
func (c OrClause) String() string {
if c.Err() != nil {
return c.Err().Error()
}
return fmt.Sprintf(`["or", %s]`, clauseString(c.subClauses))
}
func orFrames(original, lhs, rhs *QFrame) *QFrame {
if lhs == nil {
return rhs
}
if lhs.Err != nil {
return lhs
}
if rhs.Err != nil {
return rhs
}
resultIx := make(index.Int, 0, integer.Max(len(lhs.index), len(rhs.index)))
lhsI, rhsI := 0, 0
for _, ix := range original.index {
found := false
if lhsI < len(lhs.index) && lhs.index[lhsI] == ix {
found = true
lhsI++
}
if rhsI < len(rhs.index) && rhs.index[rhsI] == ix {
found = true
rhsI++
}
if found {
resultIx = append(resultIx, ix)
}
// Perhaps optimized special cases here for when one or both of
// the sides are exhausted?
}
newFrame := original.withIndex(resultIx)
return &newFrame
}
func (c OrClause) filter(qf QFrame) QFrame {
if qf.Err != nil {
return qf
}
if c.Err() != nil {
return qf.withErr(c.Err())
}
filters := make([]filter.Filter, 0)
var filteredQf *QFrame
for _, c := range c.subClauses {
if f, ok := c.(Filter); ok {
filters = append(filters, filter.Filter(f))
} else {
if len(filters) > 0 {
newQf := qf.filter(filters...)
filteredQf = orFrames(&qf, filteredQf, &newQf)
filters = filters[:0]
}
newQf := c.filter(qf)
filteredQf = orFrames(&qf, filteredQf, &newQf)
}
}
if len(filters) > 0 {
newQf := qf.filter(filters...)
filteredQf = orFrames(&qf, filteredQf, &newQf)
}
return *filteredQf
}
// Err returns any error that may have occurred during creation of the filter
func (c OrClause) Err() error {
return c.err
}
// String returns a textual description of the filter.
func (c Filter) String() string {
if c.Err() != nil {
return c.Err().Error()
}
return filter.Filter(c).String()
}
func (c Filter) filter(qf QFrame) QFrame {
return qf.filter(filter.Filter(c))
}
// Err returns any error that may have occurred during creation of the filter
func (c Filter) Err() error {
return nil
}
// Not creates a new NotClause that represents the inverse of the passed filter clause.
func Not(c FilterClause) NotClause {
return NotClause{subClause: c}
}
// String returns a textual description of the filter clause.
func (c NotClause) String() string {
if c.Err() != nil {
return c.Err().Error()
}
return fmt.Sprintf(`["!", %s]`, c.subClause.String())
}
func (c NotClause) filter(qf QFrame) QFrame {
if qf.Err != nil {
return qf
}
if c.Err() != nil {
return qf.withErr(c.Err())
}
if fc, ok := c.subClause.(Filter); ok {
f := filter.Filter(fc)
f.Inverse = !f.Inverse
return qf.filter(f)
}
newQf := c.subClause.filter(qf)
if newQf.Err != nil {
return newQf
}
newIx := make(index.Int, 0, qf.index.Len()-newQf.index.Len())
newQfI := 0
for _, ix := range qf.index {
if newQfI < newQf.index.Len() && newQf.index[newQfI] == ix {
newQfI++
} else {
newIx = append(newIx, ix)
}
}
return qf.withIndex(newIx)
}
// Err returns any error that may have occurred during creation of the filter
func (c NotClause) Err() error {
return c.subClause.Err()
}
// Null returns a new NullClause
func Null() NullClause {
return NullClause{}
}
// Err for NullClause always returns an empty string.
func (c NullClause) String() string {
return ""
}
func (c NullClause) filter(qf QFrame) QFrame {
return qf
}
// Err for NullClause always returns nil.
func (c NullClause) Err() error {
return nil
}