| 1 | /******************************************************************************* | |
| 2 | * Copyright (C) 2026, Leo Galambos | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, | |
| 9 | * this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 12 | * this list of conditions and the following disclaimer in the documentation | |
| 13 | * and/or other materials provided with the distribution. | |
| 14 | * | |
| 15 | * 3. Neither the name of the copyright holder nor the names of its contributors | |
| 16 | * may be used to endorse or promote products derived from this software | |
| 17 | * without specific prior written permission. | |
| 18 | * | |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 29 | * POSSIBILITY OF SUCH DAMAGE. | |
| 30 | ******************************************************************************/ | |
| 31 | package org.egothor.stemmer.trie; | |
| 32 | ||
| 33 | import java.util.LinkedHashMap; | |
| 34 | import java.util.Map; | |
| 35 | ||
| 36 | /** | |
| 37 | * Canonical reduced node used during subtree merging. | |
| 38 | * | |
| 39 | * <p> | |
| 40 | * The maps exposed by the accessors are the internal backing state of the | |
| 41 | * canonical reduced node. They are returned directly for efficiency and are | |
| 42 | * intended only for closely related trie-reduction infrastructure. | |
| 43 | * </p> | |
| 44 | * | |
| 45 | * <p> | |
| 46 | * Instances are mutable only during one builder compilation and are confined to | |
| 47 | * its reduction context. They are converted to immutable {@link CompiledNode} | |
| 48 | * instances before becoming observable to lookup callers. | |
| 49 | * </p> | |
| 50 | * | |
| 51 | * @param <V> value type | |
| 52 | */ | |
| 53 | public final class ReducedNode<V> { | |
| 54 | ||
| 55 | /** | |
| 56 | * Reduction signature. | |
| 57 | */ | |
| 58 | private final ReductionSignature<V> signature; | |
| 59 | ||
| 60 | /** | |
| 61 | * Aggregated local value counts. | |
| 62 | */ | |
| 63 | private final Map<V, Integer> localCounts; | |
| 64 | ||
| 65 | /** | |
| 66 | * Canonical children by edge. | |
| 67 | */ | |
| 68 | private final Map<Character, ReducedNode<V>> children; | |
| 69 | ||
| 70 | /** | |
| 71 | * Whether this reduced node accepts any remaining lookup input. | |
| 72 | */ | |
| 73 | private final boolean acceptsRemainingInput; | |
| 74 | ||
| 75 | /** | |
| 76 | * Creates a new reduced node. | |
| 77 | * | |
| 78 | * @param signature reduction signature | |
| 79 | * @param localCounts local counts | |
| 80 | * @param children children | |
| 81 | * @param acceptsRemainingInput whether this node accepts any remaining lookup | |
| 82 | * input | |
| 83 | */ | |
| 84 | public ReducedNode(final ReductionSignature<V> signature, final Map<V, Integer> localCounts, | |
| 85 | final Map<Character, ReducedNode<V>> children, final boolean acceptsRemainingInput) { | |
| 86 | this.signature = signature; | |
| 87 | this.localCounts = new LinkedHashMap<>(localCounts); | |
| 88 | this.children = new LinkedHashMap<>(children); | |
| 89 | this.acceptsRemainingInput = acceptsRemainingInput; | |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * Creates a new non-accepting reduced node. | |
| 94 | * | |
| 95 | * @param signature reduction signature | |
| 96 | * @param localCounts local counts | |
| 97 | * @param children children | |
| 98 | */ | |
| 99 | public ReducedNode(final ReductionSignature<V> signature, final Map<V, Integer> localCounts, | |
| 100 | final Map<Character, ReducedNode<V>> children) { | |
| 101 | this(signature, localCounts, children, false); | |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * Returns the reduction signature of this canonical node. | |
| 106 | * | |
| 107 | * @return reduction signature | |
| 108 | */ | |
| 109 | public ReductionSignature<V> signature() { | |
| 110 |
1
1. signature : replaced return value with null for org/egothor/stemmer/trie/ReducedNode::signature → KILLED |
return this.signature; |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * Returns the internal aggregated local value-count map. | |
| 115 | * | |
| 116 | * <p> | |
| 117 | * The returned map is the internal backing state of this canonical reduced node | |
| 118 | * and is exposed only for efficient cooperation with trie-reduction | |
| 119 | * infrastructure. | |
| 120 | * | |
| 121 | * @return internal aggregated local value-count map | |
| 122 | */ | |
| 123 | public Map<V, Integer> localCounts() { | |
| 124 |
1
1. localCounts : replaced return value with Collections.emptyMap for org/egothor/stemmer/trie/ReducedNode::localCounts → KILLED |
return this.localCounts; |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * Returns the internal canonical child map indexed by transition character. | |
| 129 | * | |
| 130 | * <p> | |
| 131 | * The returned map is the internal backing state of this canonical reduced node | |
| 132 | * and is exposed only for efficient cooperation with trie-reduction | |
| 133 | * infrastructure. | |
| 134 | * | |
| 135 | * @return internal canonical child map | |
| 136 | */ | |
| 137 | public Map<Character, ReducedNode<V>> children() { | |
| 138 |
1
1. children : replaced return value with Collections.emptyMap for org/egothor/stemmer/trie/ReducedNode::children → KILLED |
return this.children; |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * Returns whether this node accepts any remaining lookup input. | |
| 143 | * | |
| 144 | * @return {@code true} for a contracted accepting leaf | |
| 145 | */ | |
| 146 | public boolean acceptsRemainingInput() { | |
| 147 |
2
1. acceptsRemainingInput : replaced boolean return with false for org/egothor/stemmer/trie/ReducedNode::acceptsRemainingInput → KILLED 2. acceptsRemainingInput : replaced boolean return with true for org/egothor/stemmer/trie/ReducedNode::acceptsRemainingInput → KILLED |
return this.acceptsRemainingInput; |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * Merges additional local counts into this node. | |
| 152 | * | |
| 153 | * @param additionalCounts additional local counts | |
| 154 | * @throws ArithmeticException if an aggregated count exceeds | |
| 155 | * {@link Integer#MAX_VALUE} | |
| 156 | */ | |
| 157 | public void mergeLocalCounts(final Map<V, Integer> additionalCounts) { | |
| 158 | for (Map.Entry<V, Integer> entry : additionalCounts.entrySet()) { | |
| 159 | final Integer previous = this.localCounts.get(entry.getKey()); | |
| 160 |
1
1. mergeLocalCounts : negated conditional → KILLED |
if (previous == null) { |
| 161 | this.localCounts.put(entry.getKey(), entry.getValue()); | |
| 162 | } else { | |
| 163 | this.localCounts.put(entry.getKey(), Math.addExact(previous, entry.getValue())); | |
| 164 | } | |
| 165 | } | |
| 166 | } | |
| 167 | ||
| 168 | /** | |
| 169 | * Merges child references into this node. | |
| 170 | * | |
| 171 | * <p> | |
| 172 | * For nodes with the same reduction signature, child edge sets and child | |
| 173 | * signatures must be compatible. This method therefore only needs to verify | |
| 174 | * consistency and store the canonical child instance. | |
| 175 | * | |
| 176 | * @param additionalChildren additional children | |
| 177 | */ | |
| 178 | public void mergeChildren(final Map<Character, ReducedNode<V>> additionalChildren) { | |
| 179 | for (Map.Entry<Character, ReducedNode<V>> entry : additionalChildren.entrySet()) { | |
| 180 | final ReducedNode<V> existing = this.children.get(entry.getKey()); | |
| 181 |
1
1. mergeChildren : negated conditional → KILLED |
if (existing == null) { |
| 182 | this.children.put(entry.getKey(), entry.getValue()); | |
| 183 |
1
1. mergeChildren : negated conditional → KILLED |
} else if (existing != entry.getValue()) { // NOPMD - we have canonical instances |
| 184 | throw new IllegalStateException("Incompatible canonical child encountered during reduction."); | |
| 185 | } | |
| 186 | } | |
| 187 | } | |
| 188 | } | |
Mutations | ||
| 110 |
1.1 |
|
| 124 |
1.1 |
|
| 138 |
1.1 |
|
| 147 |
1.1 2.2 |
|
| 160 |
1.1 |
|
| 181 |
1.1 |
|
| 183 |
1.1 |