| 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. All advertising materials mentioning features or use of this software must | |
| 16 | * display the following acknowledgement: | |
| 17 | * This product includes software developed by the Egothor project. | |
| 18 | * | |
| 19 | * 4. Neither the name of the copyright holder nor the names of its contributors | |
| 20 | * may be used to endorse or promote products derived from this software | |
| 21 | * without specific prior written permission. | |
| 22 | * | |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 33 | * POSSIBILITY OF SUCH DAMAGE. | |
| 34 | ******************************************************************************/ | |
| 35 | package org.egothor.stemmer; | |
| 36 | ||
| 37 | import java.util.Objects; | |
| 38 | import java.util.function.IntFunction; | |
| 39 | import java.util.logging.Level; | |
| 40 | import java.util.logging.Logger; | |
| 41 | ||
| 42 | import org.egothor.stemmer.trie.CompiledNode; | |
| 43 | ||
| 44 | /** | |
| 45 | * Factory utilities related to {@link FrequencyTrie.Builder}. | |
| 46 | * | |
| 47 | * <p> | |
| 48 | * This helper reconstructs writable builders from compiled read-only tries. The | |
| 49 | * reconstruction preserves the semantics and local counts of the compiled trie | |
| 50 | * as currently stored, which makes it suitable for subsequent modifications | |
| 51 | * followed by recompilation. | |
| 52 | * | |
| 53 | * <p> | |
| 54 | * Reconstruction operates on the compiled form. Therefore, if the compiled trie | |
| 55 | * was produced using a reduction mode that merged semantically equivalent | |
| 56 | * subtrees, the recreated builder reflects that reduced compiled state rather | |
| 57 | * than the exact original unreduced insertion history. | |
| 58 | */ | |
| 59 | public final class FrequencyTrieBuilders { | |
| 60 | ||
| 61 | /** | |
| 62 | * Logger of this class. | |
| 63 | */ | |
| 64 | private static final Logger LOGGER = Logger.getLogger(FrequencyTrieBuilders.class.getName()); | |
| 65 | ||
| 66 | /** | |
| 67 | * Utility class. | |
| 68 | */ | |
| 69 | private FrequencyTrieBuilders() { | |
| 70 | throw new AssertionError("No instances."); | |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * Reconstructs a new writable builder from a compiled read-only trie. | |
| 75 | * | |
| 76 | * <p> | |
| 77 | * The returned builder contains the same key-local value counts as the supplied | |
| 78 | * compiled trie. Callers may continue modifying the returned builder and then | |
| 79 | * compile a new {@link FrequencyTrie} instance. | |
| 80 | * | |
| 81 | * @param source source compiled trie | |
| 82 | * @param arrayFactory array factory for the reconstructed builder | |
| 83 | * @param reductionSettings reduction settings to associate with the new builder | |
| 84 | * @param <V> value type | |
| 85 | * @return reconstructed writable builder | |
| 86 | * @throws NullPointerException if any argument is {@code null} | |
| 87 | */ | |
| 88 | public static <V> FrequencyTrie.Builder<V> copyOf(final FrequencyTrie<V> source, | |
| 89 | final IntFunction<V[]> arrayFactory, final ReductionSettings reductionSettings) { | |
| 90 | Objects.requireNonNull(source, "source"); | |
| 91 | Objects.requireNonNull(arrayFactory, "arrayFactory"); | |
| 92 | Objects.requireNonNull(reductionSettings, "reductionSettings"); | |
| 93 | ||
| 94 | final FrequencyTrie.Builder<V> builder = new FrequencyTrie.Builder<>(arrayFactory, reductionSettings); | |
| 95 | final StringBuilder keyBuilder = new StringBuilder(64); | |
| 96 | ||
| 97 |
1
1. copyOf : removed call to org/egothor/stemmer/FrequencyTrieBuilders::copyNode → KILLED |
copyNode(source.root(), keyBuilder, builder); |
| 98 | ||
| 99 | LOGGER.log(Level.FINE, "Reconstructed writable builder from compiled trie."); | |
| 100 |
1
1. copyOf : replaced return value with null for org/egothor/stemmer/FrequencyTrieBuilders::copyOf → KILLED |
return builder; |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Reconstructs a new writable builder from a compiled read-only trie using | |
| 105 | * default settings for the supplied reduction mode. | |
| 106 | * | |
| 107 | * @param source source compiled trie | |
| 108 | * @param arrayFactory array factory for the reconstructed builder | |
| 109 | * @param reductionMode reduction mode to associate with the new builder | |
| 110 | * @param <V> value type | |
| 111 | * @return reconstructed writable builder | |
| 112 | * @throws NullPointerException if any argument is {@code null} | |
| 113 | */ | |
| 114 | public static <V> FrequencyTrie.Builder<V> copyOf(final FrequencyTrie<V> source, | |
| 115 | final IntFunction<V[]> arrayFactory, final ReductionMode reductionMode) { | |
| 116 | Objects.requireNonNull(reductionMode, "reductionMode"); | |
| 117 |
1
1. copyOf : replaced return value with null for org/egothor/stemmer/FrequencyTrieBuilders::copyOf → KILLED |
return copyOf(source, arrayFactory, ReductionSettings.withDefaults(reductionMode)); |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Copies one compiled node and all reachable descendants into the target | |
| 122 | * builder. | |
| 123 | * | |
| 124 | * @param node current compiled node | |
| 125 | * @param keyBuilder current key builder | |
| 126 | * @param builder target mutable builder | |
| 127 | * @param <V> value type | |
| 128 | */ | |
| 129 | private static <V> void copyNode(final CompiledNode<V> node, final StringBuilder keyBuilder, | |
| 130 | final FrequencyTrie.Builder<V> builder) { | |
| 131 |
2
1. copyNode : changed conditional boundary → KILLED 2. copyNode : negated conditional → KILLED |
for (int valueIndex = 0; valueIndex < node.orderedValues().length; valueIndex++) { |
| 132 | builder.put(keyBuilder.toString(), node.orderedValues()[valueIndex], node.orderedCounts()[valueIndex]); | |
| 133 | } | |
| 134 | ||
| 135 |
2
1. copyNode : changed conditional boundary → KILLED 2. copyNode : negated conditional → KILLED |
for (int childIndex = 0; childIndex < node.edgeLabels().length; childIndex++) { |
| 136 | keyBuilder.append(node.edgeLabels()[childIndex]); | |
| 137 |
1
1. copyNode : removed call to org/egothor/stemmer/FrequencyTrieBuilders::copyNode → KILLED |
copyNode(node.children()[childIndex], keyBuilder, builder); |
| 138 |
2
1. copyNode : Replaced integer subtraction with addition → KILLED 2. copyNode : removed call to java/lang/StringBuilder::setLength → KILLED |
keyBuilder.setLength(keyBuilder.length() - 1); |
| 139 | } | |
| 140 | } | |
| 141 | } | |
Mutations | ||
| 97 |
1.1 |
|
| 100 |
1.1 |
|
| 117 |
1.1 |
|
| 131 |
1.1 2.2 |
|
| 135 |
1.1 2.2 |
|
| 137 |
1.1 |
|
| 138 |
1.1 2.2 |