Debian Codegraph @

net/sf/saxon/tree/tiny/TinyBuilderCondensed.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.tree.tiny;
9
10 import net.sf.saxon.event.PipelineConfiguration;
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.z.IntHashMap;
13 import net.sf.saxon.om.NodeName;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.SimpleType;
16 import net.sf.saxon.type.Type;
17
18 import java.util.Arrays;
19
20 /**
21 * Variant of the TinyBuilder to create a tiny tree in which multiple text nodes or attribute
22 * nodes sharing the same string value economize on space by only holding the value once.
23 */
24 public class TinyBuilderCondensed extends TinyBuilder {
25
26 public TinyBuilderCondensed(PipelineConfiguration pipe) {
27 super(pipe);
28 }
29
30 // Keep a map from the hashcode of the string (calculated within this class) to the list
31 // of node numbers of text nodes having that hashcode
32
33 // Note from the specification of CharSequence:
34 // "It is therefore inappropriate to use arbitrary <tt>CharSequence</tt> instances as elements in a set or as keys in
35 // a map." We therefore take special care over the design of the map.
36 // We rely on the fact that all Saxon implementations of CharSequence have a hashCode() that is compatible with String.
37 // And we don't use equals() unless both CharSequences are of the same kind.
38
39 public IntHashMap<int[]> textValues = new IntHashMap<>(100);
40
41 public void endElement() throws XPathException {
42 // When ending an element, consider whether the just-completed text node can be commoned-up with
43 // any other text nodes. (Don't bother if its more than 256 chars, as it's then likely to be unique)
44 // We do this at endElement() time because we need to make sure that adjacent text nodes are concatenated first.
45
46 // First do the endElement() in the normal way
47 TinyTree tree = getTree();
48 super.endElement();
49
50 // This might have created a TEXTUAL_ELEMENT node
51 int last = tree.numberOfNodes - 1;
52 boolean sameDepth = tree.depth[last] == getCurrentDepth();
53 if (sameDepth) {
54 boolean isTextualElement = tree.nodeKind[last] == Type.TEXTUAL_ELEMENT;
55
56 // Alternatively, see if there was a final text node
57 boolean hasFinalTextNode = tree.nodeKind[last] == Type.TEXT;
58 if ((isTextualElement || hasFinalTextNode) && (tree.beta[last] <= 256)) {
59 // Get the string value of this final node: works both for text nodes and textual element nodes
60 CharSequence chars = TinyTextImpl.getStringValue(tree, last);
61 // We rely on all relevant implementations of CharSequence having compatible hashcodes (but if they
62 // don't, the only consequence is that we get less compression)
63 int hash = chars.hashCode();
64 int[] nodes = textValues.get(hash);
65 if (nodes != null) {
66 // We've seen a previous node with this hash value
67 int used = nodes[0];
68 for (int i = 1; i < used; i++) {
69 int nodeNr = nodes[i];
70 if (nodeNr == 0) {
71 break;
72 } else if (isEqual(chars, TinyTextImpl.getStringValue(tree, nodeNr))) {
73 // the latest text node is equal to some previous text node
74 int length = tree.alpha[last];
75 tree.alpha[last] = tree.alpha[nodeNr];
76 tree.beta[last] = tree.beta[nodeNr];
77 tree.getCharacterBuffer().setLength(length);
78 return;
79 }
80 }
81 } else {
82 // Haven't seen this value before; add an entry to the hash table
83 nodes = new int[4];
84 nodes[0] = 1;
85 textValues.put(hash, nodes);
86 }
87 if (nodes[0] + 1 > nodes.length) {
88 int[] n2 = Arrays.copyOf(nodes, nodes.length * 2);
89 textValues.put(hash, n2);
90 nodes = n2;
91 }
92 // Add this node to the list of distinct nodes with this hash code
93 nodes[nodes[0]++] = last;
94 }
95 }
96 }
97
98 /**
99 * For attribute nodes, the commoning-up of stored values is achieved simply by calling intern() on the
100 * string value of the attribute.
101 */
102
103 public void attribute(/*@NotNull*/ NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
104 super.attribute(nameCode, typeCode, value.toString().intern(), locationId, properties);
105 }
106
107 /**
108 * Test whether two CharSequences contain the same characters
109 *
110 * @param a the first CharSequence
111 * @param b the second CharSequence
112 * @return true if a and b contain the same characters
113 */
114
115 private static boolean isEqual(CharSequence a, /*@NotNull*/ CharSequence b) {
116 if (a.getClass() == b.getClass()) {
117 return a.equals(b);
118 } else {
119 return a.toString().equals(b.toString());
120 }
121 }
122
123 }
124