@@ -46,11 +46,17 @@ export class LimitedFilter implements NodeFilter {
46
46
47
47
private readonly reverse_ : boolean ;
48
48
49
+ private readonly startIsInclusive_ : boolean ;
50
+
51
+ private readonly endIsInclusive_ : boolean ;
52
+
49
53
constructor ( params : QueryParams ) {
50
54
this . rangedFilter_ = new RangedFilter ( params ) ;
51
55
this . index_ = params . getIndex ( ) ;
52
56
this . limit_ = params . getLimit ( ) ;
53
57
this . reverse_ = ! params . isViewFromLeft ( ) ;
58
+ this . startIsInclusive_ = ! params . startAfterSet_ ;
59
+ this . endIsInclusive_ = ! params . endBeforeSet_ ;
54
60
}
55
61
updateChild (
56
62
snap : Node ,
@@ -119,19 +125,17 @@ export class LimitedFilter implements NodeFilter {
119
125
let count = 0 ;
120
126
while ( iterator . hasNext ( ) && count < this . limit_ ) {
121
127
const next = iterator . getNext ( ) ;
122
- let inRange ;
123
- if ( this . reverse_ ) {
124
- inRange =
125
- this . index_ . compare ( this . rangedFilter_ . getStartPost ( ) , next ) <= 0 ;
126
- } else {
127
- inRange =
128
- this . index_ . compare ( next , this . rangedFilter_ . getEndPost ( ) ) <= 0 ;
129
- }
128
+ const inRange =
129
+ this . withinDirectionalStart ( next ) &&
130
+ this . withinDirectionalEnd ( next ) ;
130
131
if ( inRange ) {
131
132
filtered = filtered . updateImmediateChild ( next . name , next . node ) ;
132
133
count ++ ;
134
+ } else if ( ! this . withinDirectionalStart ( next ) ) {
135
+ // if we have not reached the start, skip to the next element
136
+ continue ;
133
137
} else {
134
- // if we have reached the end post, we cannot keep adding elemments
138
+ // if we have reached the end, stop adding elements
135
139
break ;
136
140
}
137
141
}
@@ -142,33 +146,21 @@ export class LimitedFilter implements NodeFilter {
142
146
filtered = filtered . updatePriority (
143
147
ChildrenNode . EMPTY_NODE
144
148
) as ChildrenNode ;
145
- let startPost ;
146
- let endPost ;
147
- let cmp ;
149
+
148
150
let iterator ;
149
151
if ( this . reverse_ ) {
150
152
iterator = filtered . getReverseIterator ( this . index_ ) ;
151
- startPost = this . rangedFilter_ . getEndPost ( ) ;
152
- endPost = this . rangedFilter_ . getStartPost ( ) ;
153
- const indexCompare = this . index_ . getCompare ( ) ;
154
- cmp = ( a : NamedNode , b : NamedNode ) => indexCompare ( b , a ) ;
155
153
} else {
156
154
iterator = filtered . getIterator ( this . index_ ) ;
157
- startPost = this . rangedFilter_ . getStartPost ( ) ;
158
- endPost = this . rangedFilter_ . getEndPost ( ) ;
159
- cmp = this . index_ . getCompare ( ) ;
160
155
}
161
156
162
157
let count = 0 ;
163
- let foundStartPost = false ;
164
158
while ( iterator . hasNext ( ) ) {
165
159
const next = iterator . getNext ( ) ;
166
- if ( ! foundStartPost && cmp ( startPost , next ) <= 0 ) {
167
- // start adding
168
- foundStartPost = true ;
169
- }
170
160
const inRange =
171
- foundStartPost && count < this . limit_ && cmp ( next , endPost ) <= 0 ;
161
+ count < this . limit_ &&
162
+ this . withinDirectionalStart ( next ) &&
163
+ this . withinDirectionalEnd ( next ) ;
172
164
if ( inRange ) {
173
165
count ++ ;
174
166
} else {
@@ -300,4 +292,26 @@ export class LimitedFilter implements NodeFilter {
300
292
return snap ;
301
293
}
302
294
}
295
+
296
+ private withinDirectionalStart = ( node : NamedNode ) =>
297
+ this . reverse_ ? this . withinEndPost ( node ) : this . withinStartPost ( node ) ;
298
+
299
+ private withinDirectionalEnd = ( node : NamedNode ) =>
300
+ this . reverse_ ? this . withinStartPost ( node ) : this . withinEndPost ( node ) ;
301
+
302
+ private withinStartPost = ( node : NamedNode ) => {
303
+ const compareRes = this . index_ . getCompare ( ) (
304
+ this . rangedFilter_ . getStartPost ( ) ,
305
+ node
306
+ ) ;
307
+ return this . startIsInclusive_ ? compareRes <= 0 : compareRes < 0 ;
308
+ } ;
309
+
310
+ private withinEndPost = ( node : NamedNode ) => {
311
+ const compareRes = this . index_ . getCompare ( ) (
312
+ node ,
313
+ this . rangedFilter_ . getEndPost ( )
314
+ ) ;
315
+ return this . endIsInclusive_ ? compareRes <= 0 : compareRes < 0 ;
316
+ } ;
303
317
}
0 commit comments