minecraft-src/net/minecraft/client/searchtree/SearchTree.java
2025-07-04 01:41:11 +03:00

39 lines
1.2 KiB
Java

package net.minecraft.client.searchtree;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;
import java.util.stream.Stream;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@FunctionalInterface
@Environment(EnvType.CLIENT)
public interface SearchTree<T> {
static <T> SearchTree<T> empty() {
return string -> List.of();
}
static <T> SearchTree<T> plainText(List<T> list, Function<T, Stream<String>> function) {
if (list.isEmpty()) {
return empty();
} else {
SuffixArray<T> suffixArray = new SuffixArray<>();
for (T object : list) {
((Stream)function.apply(object)).forEach(string -> suffixArray.add(object, string.toLowerCase(Locale.ROOT)));
}
suffixArray.generate();
return suffixArray::search;
}
}
/**
* Searches this search tree for the given text.
* <p>
* If the query does not contain a {@code :}, then only {@link #byName} is searched. If it does contain a colon, both {@link #byName} and {@link #byId} are searched and the results are merged using a {@link MergingIterator}.
* @return A list of all matching items in this search tree.
*/
List<T> search(String query);
}