| 1 | package net.minecraft.client.gui.font;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntSet;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.function.IntFunction;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import org.jspecify.annotations.Nullable;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class CodepointMap<T> {
|
| 13 | private static final int BLOCK_BITS = 8;
|
| 14 | private static final int BLOCK_SIZE = 256;
|
| 15 | private static final int IN_BLOCK_MASK = 255;
|
| 16 | private static final int MAX_BLOCK = 4351;
|
| 17 | private static final int BLOCK_COUNT = 4352;
|
| 18 | private final T[] empty;
|
| 19 | private final @Nullable T[][] blockMap;
|
| 20 | private final IntFunction<T[]> blockConstructor;
|
| 21 |
|
| 22 | public CodepointMap(final IntFunction<T[]> blockConstructor, final IntFunction<T[][]> blockMapConstructor) {
|
| 23 | this.empty = (T[])((Object[])blockConstructor.apply(256));
|
| 24 | this.blockMap = (T[][])((Object[][])blockMapConstructor.apply(4352));
|
| 25 | Arrays.fill(this.blockMap, this.empty);
|
| 26 | this.blockConstructor = blockConstructor;
|
| 27 | }
|
| 28 |
|
| 29 | public void clear() {
|
| 30 | Arrays.fill(this.blockMap, this.empty);
|
| 31 | }
|
| 32 |
|
| 33 | public @Nullable T get(final int codepoint) {
|
| 34 | int block = codepoint >> 8;
|
| 35 | int offset = codepoint & 0xFF;
|
| 36 | return this.blockMap[block][offset];
|
| 37 | }
|
| 38 |
|
| 39 | public @Nullable T put(final int codepoint, final T value) {
|
| 40 | int block = codepoint >> 8;
|
| 41 | int offset = codepoint & 0xFF;
|
| 42 | T[] blockData = this.blockMap[block];
|
| 43 | if (blockData == this.empty) {
|
| 44 | blockData = (T[])((Object[])this.blockConstructor.apply(256));
|
| 45 | this.blockMap[block] = blockData;
|
| 46 | blockData[offset] = value;
|
| 47 | return null;
|
| 48 | } else {
|
| 49 | T previous = blockData[offset];
|
| 50 | blockData[offset] = value;
|
| 51 | return previous;
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | public T computeIfAbsent(final int codepoint, final IntFunction<T> mapper) {
|
| 56 | int block = codepoint >> 8;
|
| 57 | int offset = codepoint & 0xFF;
|
| 58 | T[] blockData = this.blockMap[block];
|
| 59 | T current = blockData[offset];
|
| 60 | if (current != null) {
|
| 61 | return current;
|
| 62 | }
|
| 63 |
|
| 64 | if (blockData == this.empty) {
|
| 65 | blockData = (T[])((Object[])this.blockConstructor.apply(256));
|
| 66 | this.blockMap[block] = blockData;
|
| 67 | }
|
| 68 |
|
| 69 | T result = mapper.apply(codepoint);
|
| 70 | blockData[offset] = result;
|
| 71 | return result;
|
| 72 | }
|
| 73 |
|
| 74 | public @Nullable T remove(final int codepoint) {
|
| 75 | int block = codepoint >> 8;
|
| 76 | int offset = codepoint & 0xFF;
|
| 77 | T[] blockData = this.blockMap[block];
|
| 78 | if (blockData == this.empty) {
|
| 79 | return null;
|
| 80 | }
|
| 81 |
|
| 82 | T previous = blockData[offset];
|
| 83 | blockData[offset] = null;
|
| 84 | return previous;
|
| 85 | }
|
| 86 |
|
| 87 | public void forEach(final CodepointMap.Output<T> output) {
|
| 88 | for (int block = 0; block < this.blockMap.length; block++) {
|
| 89 | T[] blockData = this.blockMap[block];
|
| 90 | if (blockData != this.empty) {
|
| 91 | for (int offset = 0; offset < blockData.length; offset++) {
|
| 92 | T value = blockData[offset];
|
| 93 | if (value != null) {
|
| 94 | int codepoint = block << 8 | offset;
|
| 95 | output.accept(codepoint, value);
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | public IntSet keySet() {
|
| 103 | IntOpenHashSet result = new IntOpenHashSet();
|
| 104 | this.forEach((codepoint, value) -> result.add(codepoint));
|
| 105 | return result;
|
| 106 | }
|
| 107 |
|
| 108 | @FunctionalInterface
|
| 109 | @Environment(EnvType.CLIENT)
|
| 110 | public interface Output<T> {
|
| 111 | void accept(int codepoint, T value);
|
| 112 | }
|
| 113 | }
|
| 114 |
|