Skip to content

Commit 84830d5

Browse files
Merge pull request BaseXdb#1117 from LeoWoerteler/fingertree-arrays
XQuery: Implementation of arrays as Hinze and Patterson's Fingertrees.
2 parents 6670e81 + 74bf5e3 commit 84830d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+6130
-188
lines changed

basex-core/src/main/java/org/basex/io/serial/json/JsonSerializer.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.basex.util.Token.*;
55

66
import java.io.*;
7+
import java.util.*;
78

89
import org.basex.build.json.*;
910
import org.basex.io.out.*;
@@ -12,7 +13,7 @@
1213
import org.basex.query.value.*;
1314
import org.basex.query.value.array.*;
1415
import org.basex.query.value.item.*;
15-
import org.basex.query.value.map.*;
16+
import org.basex.query.value.map.Map;
1617
import org.basex.query.value.type.*;
1718
import org.basex.util.hash.*;
1819
import org.basex.util.options.Options.YesNo;
@@ -113,10 +114,11 @@ public void function(final FItem item) throws IOException {
113114
out.print('[');
114115

115116
boolean s = false;
116-
for(final Value v : ((Array) item).members()) {
117+
final Iterator<Value> members = ((Array) item).members();
118+
while(members.hasNext()) {
117119
if(s) out.print(',');
118120
indent();
119-
serialize(v);
121+
serialize(members.next());
120122
s = true;
121123
}
122124

basex-core/src/main/java/org/basex/query/QueryError.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public enum QueryError {
414414
RANGE_X(FOAR, 2, "Value out of range: %."),
415415

416416
/** FOAY0001. */
417-
ARRAYBOUNDS_X_X(FOAY, 1, "Array index % out of bounds (1-%)."),
417+
ARRAYBOUNDS_X_X(FOAY, 1, "Array index % out of bounds (1..%)."),
418418
/** FOAY0001. */
419419
ARRAYEMPTY(FOAY, 1, "Array has no entries."),
420420
/** FOAY0002. */

basex-core/src/main/java/org/basex/query/expr/Lookup.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import static org.basex.query.QueryError.*;
44

5+
import java.util.*;
6+
57
import org.basex.query.*;
68
import org.basex.query.iter.*;
79
import org.basex.query.value.*;
810
import org.basex.query.value.array.Array;
911
import org.basex.query.value.item.*;
10-
import org.basex.query.value.map.*;
12+
import org.basex.query.value.map.Map;
1113
import org.basex.query.var.*;
1214
import org.basex.util.*;
1315
import org.basex.util.hash.*;
@@ -49,7 +51,12 @@ public ValueIter iter(final QueryContext qc) throws QueryException {
4951

5052
final FItem f = (FItem) ctx;
5153
if(exprs[0] == Str.WC) {
52-
for(final Value v : map ? ((Map) f).values() : ((Array) f).members()) vb.add(v);
54+
if(map) {
55+
for(final Value v : ((Map) f).values()) vb.add(v);
56+
} else {
57+
final Iterator<Value> iter = ((Array) f).members();
58+
while(iter.hasNext()) vb.add(iter.next());
59+
}
5360
} else {
5461
final Iter ir = qc.iter(exprs[0]);
5562
for(Item it; (it = ir.next()) != null;) vb.add(f.invokeValue(qc, info, it));

basex-core/src/main/java/org/basex/query/expr/constr/CArray.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import org.basex.query.*;
44
import org.basex.query.expr.*;
5-
import org.basex.query.iter.*;
6-
import org.basex.query.util.list.*;
5+
import org.basex.query.value.*;
6+
import org.basex.query.value.array.*;
77
import org.basex.query.value.array.Array;
88
import org.basex.query.value.item.*;
99
import org.basex.query.value.type.*;
@@ -46,17 +46,16 @@ public Expr optimize(final QueryContext qc, final VarScope scp) throws QueryExce
4646

4747
@Override
4848
public Array item(final QueryContext qc, final InputInfo ii) throws QueryException {
49-
final ValueList vl;
49+
final ArrayBuilder builder = new ArrayBuilder();
5050
if(create) {
51-
final ValueBuilder vb = new ValueBuilder(exprs.length);
52-
for(final Expr expr : exprs) vb.add(qc.value(expr));
53-
vl = new ValueList((int) vb.size());
54-
for(final Item it : vb) vl.add(it);
51+
for(final Expr expr : exprs) {
52+
final Value value = qc.value(expr);
53+
for(final Item it : value) builder.append(it);
54+
}
5555
} else {
56-
vl = new ValueList(exprs.length);
57-
for(final Expr expr : exprs) vl.add(qc.value(expr));
56+
for(final Expr expr : exprs) builder.append(qc.value(expr));
5857
}
59-
return vl.array();
58+
return builder.freeze();
6059
}
6160

6261
@Override

basex-core/src/main/java/org/basex/query/expr/constr/Constr.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static org.basex.query.QueryError.*;
44

5+
import java.util.*;
6+
57
import org.basex.query.*;
68
import org.basex.query.expr.*;
79
import org.basex.query.iter.*;
@@ -86,8 +88,11 @@ public Constr add(final QueryContext qc, final Expr... expr) throws QueryExcepti
8688
*/
8789
private boolean add(final QueryContext qc, final Item it) throws QueryException {
8890
if(it instanceof Array) {
89-
for(final Value v : ((Array) it).members()) {
90-
for(final Item i : v) if(!add(qc, i)) return false;
91+
final Iterator<Value> members = ((Array) it).members();
92+
while(members.hasNext()) {
93+
for(final Item i : members.next()) {
94+
if(!add(qc, i)) return false;
95+
}
9196
}
9297
return true;
9398
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.basex.query.func.array;
22

33
import org.basex.query.*;
4-
import org.basex.query.util.list.*;
5-
import org.basex.query.value.*;
6-
import org.basex.query.value.array.Array;
74
import org.basex.query.value.item.*;
85
import org.basex.util.*;
96

@@ -16,10 +13,6 @@
1613
public final class ArrayAppend extends ArrayFn {
1714
@Override
1815
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
19-
final Array array = toArray(exprs[0], qc);
20-
final int as = array.arraySize();
21-
final ValueList vl = new ValueList(as + 1);
22-
for(final Value v : array.members()) vl.add(v);
23-
return vl.add(qc.value(exprs[1])).array();
16+
return toArray(exprs[0], qc).snoc(qc.value(exprs[1]));
2417
}
2518
}

basex-core/src/main/java/org/basex/query/func/array/ArrayFilter.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.basex.query.func.array;
22

3+
import java.util.*;
4+
35
import org.basex.query.*;
4-
import org.basex.query.util.list.*;
56
import org.basex.query.value.*;
7+
import org.basex.query.value.array.*;
68
import org.basex.query.value.array.Array;
79
import org.basex.query.value.item.*;
810
import org.basex.util.*;
@@ -18,10 +20,12 @@ public final class ArrayFilter extends ArrayFn {
1820
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
1921
final Array array = toArray(exprs[0], qc);
2022
final FItem fun = checkArity(exprs[1], 1, qc);
21-
final ValueList vl = new ValueList();
22-
for(final Value v : array.members()) {
23-
if(toBoolean(fun.invokeItem(qc, info, v))) vl.add(v);
23+
final ArrayBuilder builder = new ArrayBuilder();
24+
final Iterator<Value> iter = array.members();
25+
while(iter.hasNext()) {
26+
final Value v = iter.next();
27+
if(toBoolean(fun.invokeItem(qc, info, v))) builder.append(v);
2428
}
25-
return vl.array();
29+
return builder.freeze();
2630
}
2731
}

basex-core/src/main/java/org/basex/query/func/array/ArrayFn.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ abstract class ArrayFn extends StandardFunc {
2121
* @return specified position -1
2222
* @throws QueryException query exception
2323
*/
24-
final int checkPos(final Array array, final long pos, final boolean incl) throws QueryException {
25-
final int as = array.arraySize() + (incl ? 1 : 0);
24+
final long checkPos(final Array array, final long pos, final boolean incl)
25+
throws QueryException {
26+
final long as = array.arraySize() + (incl ? 1 : 0);
2627
if(pos < 1 || pos > as) throw (as == 0 ? ARRAYEMPTY : ARRAYBOUNDS_X_X).get(info, pos, as);
27-
return (int) pos - 1;
28+
return pos - 1;
2829
}
2930

3031
/**
@@ -34,7 +35,7 @@ final int checkPos(final Array array, final long pos, final boolean incl) throws
3435
* @return specified position -1
3536
* @throws QueryException query exception
3637
*/
37-
final int checkPos(final Array array, final long pos) throws QueryException {
38+
final long checkPos(final Array array, final long pos) throws QueryException {
3839
return checkPos(array, pos, false);
3940
}
4041
}

basex-core/src/main/java/org/basex/query/func/array/ArrayFoldLeft.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.basex.query.func.array;
22

3+
import java.util.*;
4+
35
import org.basex.query.*;
46
import org.basex.query.iter.*;
57
import org.basex.query.value.*;
@@ -23,7 +25,8 @@ public Value value(final QueryContext qc) throws QueryException {
2325
final Array array = toArray(exprs[0], qc);
2426
Value res = qc.value(exprs[1]);
2527
final FItem fun = checkArity(exprs[2], 2, qc);
26-
for(final Value v : array.members()) res = fun.invokeValue(qc, info, res, v);
28+
final Iterator<Value> iter = array.members();
29+
while(iter.hasNext()) res = fun.invokeValue(qc, info, res, iter.next());
2730
return res;
2831
}
2932
}

basex-core/src/main/java/org/basex/query/func/array/ArrayFoldRight.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.basex.query.func.array;
22

3+
import java.util.*;
4+
35
import org.basex.query.*;
46
import org.basex.query.iter.*;
57
import org.basex.query.value.*;
@@ -23,7 +25,8 @@ public Value value(final QueryContext qc) throws QueryException {
2325
final Array array = toArray(exprs[0], qc);
2426
Value res = qc.value(exprs[1]);
2527
final FItem fun = checkArity(exprs[2], 2, qc);
26-
for(int a = array.arraySize(); --a >= 0;) res = fun.invokeValue(qc, info, array.get(a), res);
28+
final ListIterator<Value> iter = array.members(true);
29+
while(iter.hasPrevious()) res = fun.invokeValue(qc, info, iter.previous(), res);
2730
return res;
2831
}
2932
}

basex-core/src/main/java/org/basex/query/func/array/ArrayForEach.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.basex.query.func.array;
22

3+
import java.util.*;
4+
35
import org.basex.query.*;
4-
import org.basex.query.util.list.*;
56
import org.basex.query.value.*;
7+
import org.basex.query.value.array.*;
68
import org.basex.query.value.array.Array;
79
import org.basex.query.value.item.*;
810
import org.basex.util.*;
@@ -18,8 +20,9 @@ public final class ArrayForEach extends ArrayFn {
1820
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
1921
final Array array = toArray(exprs[0], qc);
2022
final FItem fun = checkArity(exprs[1], 1, qc);
21-
final ValueList vl = new ValueList(array.arraySize());
22-
for(final Value v : array.members()) vl.add(fun.invokeValue(qc, info, v));
23-
return vl.array();
23+
final ArrayBuilder builder = new ArrayBuilder();
24+
final Iterator<Value> iter = array.members();
25+
while(iter.hasNext()) builder.append(fun.invokeValue(qc, info, iter.next()));
26+
return builder.freeze();
2427
}
2528
}

basex-core/src/main/java/org/basex/query/func/array/ArrayForEachPair.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.basex.query.func.array;
22

3+
import java.util.*;
4+
35
import org.basex.query.*;
4-
import org.basex.query.util.list.*;
6+
import org.basex.query.value.*;
7+
import org.basex.query.value.array.*;
58
import org.basex.query.value.array.Array;
69
import org.basex.query.value.item.*;
710
import org.basex.util.*;
@@ -17,9 +20,10 @@ public final class ArrayForEachPair extends ArrayFn {
1720
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
1821
final Array array1 = toArray(exprs[0], qc), array2 = toArray(exprs[1], qc);
1922
final FItem fun = checkArity(exprs[2], 2, qc);
20-
final int as = Math.min(array1.arraySize(), array2.arraySize());
21-
final ValueList vl = new ValueList(as);
22-
for(int a = 0; a < as; a++) vl.add(fun.invokeValue(qc, info, array1.get(a), array2.get(a)));
23-
return vl.array();
23+
final ArrayBuilder builder = new ArrayBuilder();
24+
final Iterator<Value> as = array1.members(), bs = array2.members();
25+
while(as.hasNext() && bs.hasNext())
26+
builder.append(fun.invokeValue(qc, info, as.next(), bs.next()));
27+
return builder.freeze();
2428
}
2529
}

basex-core/src/main/java/org/basex/query/func/array/ArrayHead.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public Iter iter(final QueryContext qc) throws QueryException {
2020
@Override
2121
public Value value(final QueryContext qc) throws QueryException {
2222
final Array array = toArray(exprs[0], qc);
23-
return array.get(checkPos(array, 1));
23+
if(array.isEmptyArray()) throw QueryError.ARRAYEMPTY.get(info);
24+
return array.head();
2425
}
2526
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.basex.query.func.array;
22

33
import org.basex.query.*;
4-
import org.basex.query.util.list.*;
5-
import org.basex.query.value.*;
64
import org.basex.query.value.array.Array;
75
import org.basex.query.value.item.*;
86
import org.basex.util.*;
@@ -17,15 +15,7 @@ public final class ArrayInsertBefore extends ArrayFn {
1715
@Override
1816
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
1917
final Array array = toArray(exprs[0], qc);
20-
final int p = checkPos(array, toLong(exprs[1], qc), true);
21-
final Value ins = qc.value(exprs[2]);
22-
final int as = array.arraySize();
23-
final ValueList vl = new ValueList(as + 1);
24-
for(int a = 0; a < as; a++) {
25-
if(a == p) vl.add(ins);
26-
vl.add(array.get(a));
27-
}
28-
if(p == as) vl.add(ins);
29-
return vl.array();
18+
final long p = checkPos(array, toLong(exprs[1], qc), true);
19+
return array.insertBefore(p, qc.value(exprs[2]));
3020
}
3121
}

basex-core/src/main/java/org/basex/query/func/array/ArrayJoin.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import org.basex.query.*;
44
import org.basex.query.iter.*;
5-
import org.basex.query.util.list.*;
6-
import org.basex.query.value.*;
5+
import org.basex.query.value.array.*;
6+
import org.basex.query.value.array.Array;
77
import org.basex.query.value.item.*;
88
import org.basex.util.*;
99

@@ -16,11 +16,20 @@
1616
public final class ArrayJoin extends ArrayFn {
1717
@Override
1818
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
19-
final ValueList vl = new ValueList();
2019
final Iter ir = qc.iter(exprs[0]);
21-
for(Item it; (it = ir.next()) != null;) {
22-
for(final Value v : toArray(it).members()) vl.add(v);
23-
}
24-
return vl.array();
20+
Item it = ir.next();
21+
if(it == null) return Array.empty();
22+
final Array fst = toArray(it);
23+
it = ir.next();
24+
if(it == null) return fst;
25+
final Array snd = toArray(it);
26+
it = ir.next();
27+
if(it == null) return fst.concat(snd);
28+
29+
final ArrayBuilder builder = new ArrayBuilder().append(fst).append(snd);
30+
do {
31+
builder.append(toArray(it));
32+
} while((it = ir.next()) != null);
33+
return builder.freeze();
2534
}
2635
}

basex-core/src/main/java/org/basex/query/func/array/ArrayRemove.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.basex.query.func.array;
22

33
import org.basex.query.*;
4-
import org.basex.query.util.list.*;
54
import org.basex.query.value.array.Array;
65
import org.basex.query.value.item.*;
76
import org.basex.util.*;
@@ -16,10 +15,6 @@ public final class ArrayRemove extends ArrayFn {
1615
@Override
1716
public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
1817
final Array array = toArray(exprs[0], qc);
19-
final int p = checkPos(array, toLong(exprs[1], qc)), as = array.arraySize() - 1;
20-
if(p == 0 || p == as) return Array.get(array, p == 0 ? 1 : 0, as);
21-
final ValueList vl = new ValueList(as);
22-
for(int a = 0; a <= as; a++) if(a != p) vl.add(array.get(a));
23-
return vl.array();
18+
return array.remove(checkPos(array, toLong(exprs[1], qc)));
2419
}
2520
}

0 commit comments

Comments
 (0)