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

Method opt

basex-core/src/main/java/org/basex/query/func/fn/FnSlice.java:39–71  ·  view source on GitHub ↗
(final CompileContext cc)

Source from the content-addressed store, hash-verified

37 }
38
39 @Override
40 protected Expr opt(final CompileContext cc) throws QueryException {
41 final Expr input = arg(0);
42 final SeqType st = input.seqType();
43 if(st.zero()) return input;
44
45 // for the following optimizations, the numeric properties must either be static or absent
46 final IntPredicate value = e -> !defined(e) || arg(e) instanceof Value;
47 if(value.test(1) && value.test(2) && value.test(3)) {
48 final long size = input.size();
49 if(size != -1) {
50 final Slice slice = slice(size, cc.qc);
51 if(slice.step == 1) {
52 // slice(E, 2) → util:range(E, 2)
53 // slice(TEN, 2, 1) → util:range(reverse(TEN), 9, 10)
54 final Expr arg = slice.reverse ? cc.function(REVERSE, info, input) : input;
55 return cc.function(_UTIL_RANGE, info, arg, Itr.get(slice.start), Itr.get(slice.end));
56 }
57 }
58 // input size is unknown: exact range cannot be computed, check original properties
59 final long start = toLong(1, 1, cc.qc), end = toLong(2, Long.MAX_VALUE, cc.qc);
60 if(end == Long.MAX_VALUE && toLong(3, 1, cc.qc) == 1) {
61 // slice(E, -1) → foot(E)
62 if(start == -1) return cc.function(FOOT, info, input);
63 // slice(E, 1) → E
64 if(start == 0 || start == 1) return input;
65 // no rewritings possible for greater start values (slice always returns last item)
66 }
67 }
68
69 exprType.assign(st.union(Occ.ZERO)).data(input);
70 return this;
71 }
72
73 /**
74 * Returns slice properties.

Callers

nothing calls this directly

Calls 13

seqTypeMethod · 0.95
zeroMethod · 0.95
sizeMethod · 0.95
sliceMethod · 0.95
getMethod · 0.95
toLongMethod · 0.95
unionMethod · 0.95
definedMethod · 0.80
testMethod · 0.65
argMethod · 0.45
functionMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected