| 1 | package net.minecraft.client.searchtree;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import it.unimi.dsi.fastutil.Arrays;
|
| 7 | import it.unimi.dsi.fastutil.Swapper;
|
| 8 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 9 | import it.unimi.dsi.fastutil.ints.IntComparator;
|
| 10 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 11 | import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
| 12 | import it.unimi.dsi.fastutil.ints.IntSet;
|
| 13 | import java.util.Collections;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Set;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class SuffixArray<T> {
|
| 22 | private static final boolean DEBUG_COMPARISONS = Boolean.parseBoolean(System.getProperty("SuffixArray.printComparisons", "false"));
|
| 23 | private static final boolean DEBUG_ARRAY = Boolean.parseBoolean(System.getProperty("SuffixArray.printArray", "false"));
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 | private static final int END_OF_TEXT_MARKER = -1;
|
| 26 | private static final int END_OF_DATA = -2;
|
| 27 | protected final List<T> list = Lists.newArrayList();
|
| 28 | private final IntList chars = new IntArrayList();
|
| 29 | private final IntList wordStarts = new IntArrayList();
|
| 30 | private IntList suffixToT = new IntArrayList();
|
| 31 | private IntList offsets = new IntArrayList();
|
| 32 | private int maxStringLength;
|
| 33 |
|
| 34 | public void add(final T t, final String text) {
|
| 35 | this.maxStringLength = Math.max(this.maxStringLength, text.length());
|
| 36 | int index = this.list.size();
|
| 37 | this.list.add(t);
|
| 38 | this.wordStarts.add(this.chars.size());
|
| 39 |
|
| 40 | for (int i = 0; i < text.length(); i++) {
|
| 41 | this.suffixToT.add(index);
|
| 42 | this.offsets.add(i);
|
| 43 | this.chars.add(text.charAt(i));
|
| 44 | }
|
| 45 |
|
| 46 | this.suffixToT.add(index);
|
| 47 | this.offsets.add(text.length());
|
| 48 | this.chars.add(-1);
|
| 49 | }
|
| 50 |
|
| 51 | public void generate() {
|
| 52 | int charCount = this.chars.size();
|
| 53 | int[] positions = new int[charCount];
|
| 54 | int[] lefts = new int[charCount];
|
| 55 | int[] rights = new int[charCount];
|
| 56 | int[] reverse = new int[charCount];
|
| 57 | IntComparator comparator = (a, b) -> lefts[a] == lefts[b] ? Integer.compare(rights[a], rights[b]) : Integer.compare(lefts[a], lefts[b]);
|
| 58 | Swapper swapper = (a, b) -> {
|
| 59 | if (a != b) {
|
| 60 | int tmp = lefts[a];
|
| 61 | lefts[a] = lefts[b];
|
| 62 | lefts[b] = tmp;
|
| 63 | tmp = rights[a];
|
| 64 | rights[a] = rights[b];
|
| 65 | rights[b] = tmp;
|
| 66 | tmp = reverse[a];
|
| 67 | reverse[a] = reverse[b];
|
| 68 | reverse[b] = tmp;
|
| 69 | }
|
| 70 | };
|
| 71 |
|
| 72 | for (int i = 0; i < charCount; i++) {
|
| 73 | positions[i] = this.chars.getInt(i);
|
| 74 | }
|
| 75 |
|
| 76 | int count = 1;
|
| 77 |
|
| 78 | for (int max = Math.min(charCount, this.maxStringLength); count * 2 < max; count *= 2) {
|
| 79 | for (int i = 0; i < charCount; reverse[i] = i++) {
|
| 80 | lefts[i] = positions[i];
|
| 81 | rights[i] = i + count < charCount ? positions[i + count] : -2;
|
| 82 | }
|
| 83 |
|
| 84 | Arrays.quickSort(0, charCount, comparator, swapper);
|
| 85 |
|
| 86 | for (int i = 0; i < charCount; i++) {
|
| 87 | if (i > 0 && lefts[i] == lefts[i - 1] && rights[i] == rights[i - 1]) {
|
| 88 | positions[reverse[i]] = positions[reverse[i - 1]];
|
| 89 | } else {
|
| 90 | positions[reverse[i]] = i;
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | IntList oldSuffixToT = this.suffixToT;
|
| 96 | IntList oldOffsets = this.offsets;
|
| 97 | this.suffixToT = new IntArrayList(oldSuffixToT.size());
|
| 98 | this.offsets = new IntArrayList(oldOffsets.size());
|
| 99 |
|
| 100 | for (int i = 0; i < charCount; i++) {
|
| 101 | int index = reverse[i];
|
| 102 | this.suffixToT.add(oldSuffixToT.getInt(index));
|
| 103 | this.offsets.add(oldOffsets.getInt(index));
|
| 104 | }
|
| 105 |
|
| 106 | if (DEBUG_ARRAY) {
|
| 107 | this.print();
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | private void print() {
|
| 112 | for (int i = 0; i < this.suffixToT.size(); i++) {
|
| 113 | LOGGER.debug("{} {}", i, this.getString(i));
|
| 114 | }
|
| 115 |
|
| 116 | LOGGER.debug("");
|
| 117 | }
|
| 118 |
|
| 119 | private String getString(final int i) {
|
| 120 | int start = this.offsets.getInt(i);
|
| 121 | int offset = this.wordStarts.getInt(this.suffixToT.getInt(i));
|
| 122 | StringBuilder builder = new StringBuilder();
|
| 123 |
|
| 124 | for (int j = 0; offset + j < this.chars.size(); j++) {
|
| 125 | if (j == start) {
|
| 126 | builder.append('^');
|
| 127 | }
|
| 128 |
|
| 129 | int p = this.chars.getInt(offset + j);
|
| 130 | if (p == -1) {
|
| 131 | break;
|
| 132 | }
|
| 133 |
|
| 134 | builder.append((char)p);
|
| 135 | }
|
| 136 |
|
| 137 | return builder.toString();
|
| 138 | }
|
| 139 |
|
| 140 | private int compare(final String text, final int index) {
|
| 141 | int start = this.wordStarts.getInt(this.suffixToT.getInt(index));
|
| 142 | int offset = this.offsets.getInt(index);
|
| 143 |
|
| 144 | for (int i = 0; i < text.length(); i++) {
|
| 145 | int p = this.chars.getInt(start + offset + i);
|
| 146 | if (p == -1) {
|
| 147 | return 1;
|
| 148 | }
|
| 149 |
|
| 150 | char c = text.charAt(i);
|
| 151 | char c2 = (char)p;
|
| 152 | if (c < c2) {
|
| 153 | return -1;
|
| 154 | }
|
| 155 |
|
| 156 | if (c > c2) {
|
| 157 | return 1;
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | return 0;
|
| 162 | }
|
| 163 |
|
| 164 | public List<T> search(final String text) {
|
| 165 | int suffixCount = this.suffixToT.size();
|
| 166 | int low = 0;
|
| 167 | int high = suffixCount;
|
| 168 |
|
| 169 | while (low < high) {
|
| 170 | int mid = low + (high - low) / 2;
|
| 171 | int c = this.compare(text, mid);
|
| 172 | if (DEBUG_COMPARISONS) {
|
| 173 | LOGGER.debug("comparing lower \"{}\" with {} \"{}\": {}", text, mid, this.getString(mid), c);
|
| 174 | }
|
| 175 |
|
| 176 | if (c > 0) {
|
| 177 | low = mid + 1;
|
| 178 | } else {
|
| 179 | high = mid;
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | if (low >= 0 && low < suffixCount) {
|
| 184 | int lowerBound = low;
|
| 185 | high = suffixCount;
|
| 186 |
|
| 187 | while (low < high) {
|
| 188 | int mid = low + (high - low) / 2;
|
| 189 | int c = this.compare(text, mid);
|
| 190 | if (DEBUG_COMPARISONS) {
|
| 191 | LOGGER.debug("comparing upper \"{}\" with {} \"{}\": {}", text, mid, this.getString(mid), c);
|
| 192 | }
|
| 193 |
|
| 194 | if (c >= 0) {
|
| 195 | low = mid + 1;
|
| 196 | } else {
|
| 197 | high = mid;
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | int upperBound = low;
|
| 202 | IntSet matches = new IntOpenHashSet();
|
| 203 |
|
| 204 | for (int i = lowerBound; i < upperBound; i++) {
|
| 205 | matches.add(this.suffixToT.getInt(i));
|
| 206 | }
|
| 207 |
|
| 208 | int[] ints = matches.toIntArray();
|
| 209 | java.util.Arrays.sort(ints);
|
| 210 | Set<T> result = Sets.newLinkedHashSet();
|
| 211 |
|
| 212 | for (int t : ints) {
|
| 213 | result.add(this.list.get(t));
|
| 214 | }
|
| 215 |
|
| 216 | return Lists.newArrayList(result);
|
| 217 | } else {
|
| 218 | return Collections.emptyList();
|
| 219 | }
|
| 220 | }
|
| 221 | }
|
| 222 |
|