| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.ma.arrays; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Atomizer; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.expr.sort.*; |
| 13 |
import net.sf.saxon.functions.SystemFunction; |
| 14 |
import net.sf.saxon.lib.StringCollator; |
| 15 |
import net.sf.saxon.om.*; |
| 16 |
import net.sf.saxon.trans.NoDynamicContextException; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.tree.iter.UnfailingIterator; |
| 19 |
import net.sf.saxon.value.AtomicValue; |
| 20 |
import net.sf.saxon.value.StringValue; |
| 21 |
|
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.List; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class ArraySort extends SystemFunction { |
| 29 |
|
| 30 |
private static class MemberToBeSorted{ |
| 31 |
public GroundedValue<?> value; |
| 32 |
public GroundedValue<?> sortKey; |
| 33 |
int originalPosition; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@Override |
| 43 |
public ArrayItem call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 44 |
ArrayItem array = (ArrayItem) arguments[0].head(); |
| 45 |
final List<MemberToBeSorted> inputList = new ArrayList<>(array.arrayLength()); |
| 46 |
int i = 0; |
| 47 |
StringCollator collation; |
| 48 |
if (arguments.length == 1) { |
| 49 |
collation = context.getConfiguration().getCollation(getRetainedStaticContext().getDefaultCollationName()); |
| 50 |
} else { |
| 51 |
StringValue collName = (StringValue)arguments[1].head(); |
| 52 |
if (collName == null) { |
| 53 |
collation = context.getConfiguration().getCollation(getRetainedStaticContext().getDefaultCollationName()); |
| 54 |
} else { |
| 55 |
collation = context.getConfiguration().getCollation(collName.getStringValue(), getStaticBaseUriString()); |
| 56 |
} |
| 57 |
} |
| 58 |
Function key = null; |
| 59 |
if (arguments.length == 3){ |
| 60 |
key = (Function) arguments[2].head(); |
| 61 |
} |
| 62 |
for (GroundedValue<?> seq: array.members()){ |
| 63 |
MemberToBeSorted member = new MemberToBeSorted(); |
| 64 |
member.value = seq; |
| 65 |
member.originalPosition = i++; |
| 66 |
if (key != null) { |
| 67 |
member.sortKey = dynamicCall(key, context, new Sequence[]{seq}).materialize(); |
| 68 |
} else { |
| 69 |
member.sortKey = atomize(seq); |
| 70 |
} |
| 71 |
inputList.add(member); |
| 72 |
} |
| 73 |
final AtomicComparer atomicComparer = AtomicSortComparer.makeSortComparer( |
| 74 |
collation, StandardNames.XS_ANY_ATOMIC_TYPE, context); |
| 75 |
Sortable sortable = new Sortable() { |
| 76 |
public int compare(int a, int b) { |
| 77 |
int result = compareSortKeys(inputList.get(a).sortKey, inputList.get(b).sortKey, atomicComparer); |
| 78 |
if (result == 0){ |
| 79 |
return inputList.get(a).originalPosition - inputList.get(b).originalPosition; |
| 80 |
} else { |
| 81 |
return result; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public void swap(int a, int b) { |
| 86 |
MemberToBeSorted temp = inputList.get(a); |
| 87 |
inputList.set(a, inputList.get(b)); |
| 88 |
inputList.set(b, temp); |
| 89 |
} |
| 90 |
}; |
| 91 |
try { |
| 92 |
GenericSorter.quickSort(0, array.arrayLength(), sortable); |
| 93 |
} catch (ClassCastException e) { |
| 94 |
XPathException err = new XPathException("Non-comparable types found while sorting: " + e.getMessage()); |
| 95 |
err.setErrorCode("XPTY0004"); |
| 96 |
throw err; |
| 97 |
} |
| 98 |
List<GroundedValue<?>> outputList = new ArrayList<>(array.arrayLength()); |
| 99 |
for (MemberToBeSorted member: inputList){ |
| 100 |
outputList.add(member.value); |
| 101 |
} |
| 102 |
return new SimpleArrayItem(outputList); |
| 103 |
} |
| 104 |
|
| 105 |
public static int compareSortKeys(GroundedValue a, GroundedValue b, AtomicComparer comparer) { |
| 106 |
UnfailingIterator iteratora = a.iterate(); |
| 107 |
UnfailingIterator iteratorb = b.iterate(); |
| 108 |
while (true){ |
| 109 |
AtomicValue firsta = (AtomicValue) iteratora.next(); |
| 110 |
AtomicValue firstb = (AtomicValue) iteratorb.next(); |
| 111 |
if (firsta == null){ |
| 112 |
if (firstb == null){ |
| 113 |
return 0; |
| 114 |
} |
| 115 |
else { |
| 116 |
return -1; |
| 117 |
} |
| 118 |
} |
| 119 |
else if (firstb == null){ |
| 120 |
return +1; |
| 121 |
} |
| 122 |
else { |
| 123 |
try { |
| 124 |
int first = comparer.compareAtomicValues(firsta, firstb); |
| 125 |
if (first == 0){ |
| 126 |
continue; |
| 127 |
} else { |
| 128 |
return first; |
| 129 |
} |
| 130 |
} catch (NoDynamicContextException e) { |
| 131 |
throw new AssertionError(e); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private static GroundedValue<? extends AtomicValue> atomize(Sequence<?> input) throws XPathException { |
| 138 |
SequenceIterator<?> iterator = input.iterate(); |
| 139 |
SequenceIterator<? extends AtomicValue> mapper = Atomizer.getAtomizingIterator(iterator, false); |
| 140 |
return mapper.materialize(); |
| 141 |
} |
| 142 |
} |