MCPcopy Create free account
hub / github.com/BaseXdb/basex / toRange

Method toRange

basex-core/src/main/java/org/basex/query/expr/List.java:105–148  ·  view source on GitHub ↗

Tries to rewrite consecutive integers to range sequences. @param cc compilation context

(final CompileContext cc)

Source from the content-addressed store, hash-verified

103 * @param cc compilation context
104 */
105 private void toRange(final CompileContext cc) {
106 if(!((Checks<Expr>) expr -> expr instanceof Itr || expr instanceof RangeSeq).any(exprs)) return;
107
108 long min = Long.MIN_VALUE, max = 0;
109 final int el = exprs.length;
110 final ExprList list = new ExprList(el);
111 for(int e = 0; e <= el; e++) {
112 final Expr expr = e < el ? exprs[e] : null;
113 long mn = Long.MIN_VALUE, mx = 0;
114 if(expr instanceof final Itr itr && itr.type == BasicType.INTEGER) {
115 final long l = itr.itr();
116 mn = l;
117 mx = l;
118 } else if(expr instanceof final RangeSeq rs && rs.ascending()) {
119 mn = rs.min();
120 mx = rs.max();
121 }
122 boolean add = mn == Long.MIN_VALUE;
123 if(!add) {
124 if(min == Long.MIN_VALUE) {
125 // start new range: 1 - 2
126 min = mn;
127 max = mx;
128 } else if(mn == max + 1) {
129 // extend range: 1 - 2, 3 - 4 → 1 - 4
130 max = mx;
131 } else {
132 // finalize existing range
133 add = true;
134 }
135 }
136 if(add) {
137 if(min != Long.MIN_VALUE) {
138 final long s = max - min + 1;
139 list.add(RangeSeq.get(min, s, true));
140 if(s > 1) cc.info(OPTMERGE_X, list.peek());
141 min = mn;
142 max = mx;
143 }
144 if(min == Long.MIN_VALUE && expr != null) list.add(expr);
145 }
146 }
147 exprs = list.finish();
148 }
149
150 @Override
151 public Iter iter(final QueryContext qc) {

Callers 2

optimizeMethod · 0.95
simplifyForMethod · 0.95

Calls 10

getMethod · 0.95
anyMethod · 0.80
ascendingMethod · 0.80
addMethod · 0.65
infoMethod · 0.65
itrMethod · 0.45
minMethod · 0.45
maxMethod · 0.45
peekMethod · 0.45
finishMethod · 0.45

Tested by

no test coverage detected