| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.ma.map; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.expr.sort.AtomicComparer; |
| 12 |
import net.sf.saxon.expr.sort.AtomicMatchKey; |
| 13 |
import net.sf.saxon.expr.sort.CodepointMatchKey; |
| 14 |
import net.sf.saxon.functions.Count; |
| 15 |
import net.sf.saxon.om.Function; |
| 16 |
import net.sf.saxon.om.GroundedValue; |
| 17 |
import net.sf.saxon.om.NodeInfo; |
| 18 |
import net.sf.saxon.om.Sequence; |
| 19 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.tree.iter.AtomicIterator; |
| 22 |
import net.sf.saxon.type.AtomicType; |
| 23 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 24 |
import net.sf.saxon.type.TypeHierarchy; |
| 25 |
import net.sf.saxon.type.UType; |
| 26 |
import net.sf.saxon.value.*; |
| 27 |
|
| 28 |
import java.util.Iterator; |
| 29 |
import java.util.List; |
| 30 |
import java.util.TreeMap; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class RangeKey implements MapItem { |
| 39 |
|
| 40 |
private CodepointMatchKey min; |
| 41 |
private CodepointMatchKey max; |
| 42 |
private TreeMap<AtomicMatchKey, List<NodeInfo>> index; |
| 43 |
|
| 44 |
public RangeKey(String min, String max, TreeMap<AtomicMatchKey, List<NodeInfo>> index) { |
| 45 |
this.min = min == null ? null : new CodepointMatchKey(min); |
| 46 |
this.max = max == null ? null : new CodepointMatchKey(max); |
| 47 |
this.index = index; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public GroundedValue<?> get(AtomicValue key) { |
| 56 |
CodepointMatchKey k = new CodepointMatchKey(key.getStringValue()); |
| 57 |
if ((min == null || min.compareTo(k) <= 0) && |
| 58 |
(max == null || max.compareTo(k) >= 0)) { |
| 59 |
List<NodeInfo> nodes = index.get(k); |
| 60 |
if (nodes == null) { |
| 61 |
return EmptySequence.getInstance(); |
| 62 |
} else { |
| 63 |
return SequenceExtent.makeSequenceExtent(nodes); |
| 64 |
} |
| 65 |
} |
| 66 |
return EmptySequence.getInstance(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public int size() { |
| 75 |
try { |
| 76 |
return Count.count(keys()); |
| 77 |
} catch (XPathException err) { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public boolean isEmpty() { |
| 88 |
return keys().next() == null; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public AtomicIterator keys() { |
| 97 |
return new RangeKeyIterator(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public Iterable<KeyValuePair> keyValuePairs() { |
| 106 |
return () -> new Iterator<KeyValuePair>() { |
| 107 |
AtomicIterator keys = keys(); |
| 108 |
AtomicValue next = keys.next(); |
| 109 |
public boolean hasNext() { |
| 110 |
return next != null; |
| 111 |
} |
| 112 |
|
| 113 |
public KeyValuePair next() { |
| 114 |
if (next == null) { |
| 115 |
return null; |
| 116 |
} else { |
| 117 |
KeyValuePair kvp = new KeyValuePair(next, get(next)); |
| 118 |
next = keys.next(); |
| 119 |
return kvp; |
| 120 |
} |
| 121 |
} |
| 122 |
}; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public MapItem remove(AtomicValue key) { |
| 133 |
return HashTrieMap.copy(this).remove(key); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public UType getKeyUType() { |
| 143 |
return UType.STRING; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
@Override |
| 156 |
public MapItem addEntry(AtomicValue key, GroundedValue<?> value) { |
| 157 |
return HashTrieMap.copy(this).addEntry(key, value); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
@Override |
| 169 |
public boolean conforms(AtomicType keyType, SequenceType valueType, TypeHierarchy th) { |
| 170 |
AtomicIterator keyIter = keys(); |
| 171 |
AtomicValue key; |
| 172 |
while ((key = keyIter.next()) != null) { |
| 173 |
Sequence<?> value = get(key); |
| 174 |
try { |
| 175 |
if (!valueType.matches(value, th)) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
} catch (XPathException e) { |
| 179 |
throw new AssertionError(e); |
| 180 |
} |
| 181 |
} |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@Override |
| 193 |
public MapType getItemType(TypeHierarchy th) { |
| 194 |
return new MapType(BuiltInAtomicType.STRING, SequenceType.NODE_SEQUENCE); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public MapType getFunctionItemType() { |
| 203 |
return new MapType(BuiltInAtomicType.STRING, SequenceType.NODE_SEQUENCE); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public String getDescription() { |
| 214 |
return "range key"; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public boolean deepEquals(Function other, XPathContext context, AtomicComparer comparer, int flags) { |
| 228 |
if (other instanceof RangeKey) { |
| 229 |
RangeKey rk = (RangeKey) other; |
| 230 |
return min.equals(rk.min) && max.equals(rk.max) && index.equals(rk.index); |
| 231 |
} else { |
| 232 |
return false; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public void export(ExpressionPresenter out) throws XPathException { |
| 240 |
out.startElement("range-key-map"); |
| 241 |
out.emitAttribute("size", size() + ""); |
| 242 |
out.endElement(); |
| 243 |
} |
| 244 |
|
| 245 |
public boolean isTrustedResultType() { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
public String toString() { |
| 250 |
return MapItem.mapToString(this); |
| 251 |
} |
| 252 |
|
| 253 |
private class RangeKeyIterator implements AtomicIterator { |
| 254 |
|
| 255 |
int pos = 0; |
| 256 |
CodepointMatchKey curr = null; |
| 257 |
CodepointMatchKey top; |
| 258 |
|
| 259 |
public RangeKeyIterator() { |
| 260 |
top = (CodepointMatchKey) (max == null ? index.lastKey() : index.floorKey(max)); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public StringValue next() { |
| 269 |
if (pos <= 0) { |
| 270 |
if (pos < 0) { |
| 271 |
return null; |
| 272 |
} |
| 273 |
if (min == null) { |
| 274 |
curr = (CodepointMatchKey) index.firstKey(); |
| 275 |
} else { |
| 276 |
curr = (CodepointMatchKey) index.ceilingKey(min); |
| 277 |
if(curr != null && max != null && curr.compareTo(max) > 0) { |
| 278 |
curr = null; |
| 279 |
} |
| 280 |
} |
| 281 |
} else if (curr.equals(top)) { |
| 282 |
curr = null; |
| 283 |
} else { |
| 284 |
curr = (CodepointMatchKey) index.higherKey(curr); |
| 285 |
} |
| 286 |
if (curr == null) { |
| 287 |
pos = -1; |
| 288 |
return null; |
| 289 |
} else { |
| 290 |
pos++; |
| 291 |
return new StringValue(curr.getValue()); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|