Tries to rewrite consecutive integers to range sequences. @param cc compilation context
(final CompileContext cc)
| 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) { |