Search in sources :

Example 1 with CompactingBitSet

use of buildcraft.lib.misc.data.CompactingBitSet in project BuildCraft by BuildCraft.

the class NbtSquishMapWriter method writeListPacked.

private void writeListPacked(WrittenType type, DataOutput to, NBTTagList list) throws IOException {
    profiler.startSection("list_packed");
    to.writeByte(NbtSquishConstants.COMPLEX_LIST_PACKED);
    profiler.startSection("header");
    profiler.startSection("init");
    int[] data = new int[list.tagCount()];
    TIntIntHashMap indexes = new TIntIntHashMap();
    for (int i = 0; i < list.tagCount(); i++) {
        profiler.startSection("entry");
        profiler.startSection("index");
        int index = map.indexOfTag(list.get(i));
        profiler.endSection();
        data[i] = index;
        if (!indexes.increment(index)) {
            indexes.put(index, 1);
        }
        profiler.endSection();
    }
    // First try to make a simple table
    // First sort the indexes into highest count first
    profiler.endStartSection("sort");
    List<IndexEntry> entries = new ArrayList<>();
    for (int index : indexes.keys()) {
        int count = indexes.get(index);
        IndexEntry entry = new IndexEntry(index, count);
        entries.add(entry);
    }
    entries.sort(Comparator.reverseOrder());
    if (debug)
        log("\n " + entries.size() + " List entries");
    writeVarInt(to, entries.size());
    profiler.endStartSection("write");
    TIntArrayList sortedIndexes = new TIntArrayList();
    int i = 0;
    for (IndexEntry entry : entries) {
        final int j = i;
        NBTBase base = map.getTagForWriting(entry.index);
        String n = safeToString(base);
        if (debug)
            log("\n List entry #" + j + " = " + entry.count + "x" + entry.index + " (" + n + ")");
        sortedIndexes.add(entry.index);
        type.writeIndex(to, entry.index);
        i++;
    }
    TIntArrayList nextData = new TIntArrayList();
    nextData.add(data);
    writeVarInt(to, data.length);
    profiler.endSection();
    profiler.endStartSection("contents");
    for (int b = 1; !nextData.isEmpty(); b++) {
        profiler.startSection("entry");
        CompactingBitSet bitset = new CompactingBitSet(b);
        bitset.ensureCapacityValues(nextData.size());
        TIntArrayList nextNextData = new TIntArrayList();
        int maxVal = (1 << b) - 1;
        profiler.startSection("iter");
        for (int d : nextData.toArray()) {
            // profiler.startSection("entry");
            // profiler.startSection("index");
            int index = sortedIndexes.indexOf(d);
            // profiler.endSection();
            if (index < maxVal) {
                // profiler.startSection("bitset_append");
                bitset.append(index);
            // profiler.endSection();
            } else {
                // profiler.startSection("bitset_append");
                bitset.append(maxVal);
                // profiler.endStartSection("next_add");
                nextNextData.add(d);
            // profiler.endSection();
            }
        // profiler.endSection();
        }
        profiler.endSection();
        sortedIndexes.remove(0, Math.min(sortedIndexes.size(), maxVal));
        byte[] bitsetBytes = bitset.getBytes();
        if (debug)
            log("\n List bitset #" + (bitset.bits - 1));
        writeVarInt(to, bitsetBytes.length);
        to.write(bitsetBytes);
        nextData = nextNextData;
        profiler.endSection();
    }
    profiler.endSection();
    profiler.endSection();
}
Also used : NBTBase(net.minecraft.nbt.NBTBase) TFloatArrayList(gnu.trove.list.array.TFloatArrayList) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) TDoubleArrayList(gnu.trove.list.array.TDoubleArrayList) TShortArrayList(gnu.trove.list.array.TShortArrayList) TByteArrayList(gnu.trove.list.array.TByteArrayList) CompactingBitSet(buildcraft.lib.misc.data.CompactingBitSet) TIntIntHashMap(gnu.trove.map.hash.TIntIntHashMap) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Aggregations

CompactingBitSet (buildcraft.lib.misc.data.CompactingBitSet)1 TByteArrayList (gnu.trove.list.array.TByteArrayList)1 TDoubleArrayList (gnu.trove.list.array.TDoubleArrayList)1 TFloatArrayList (gnu.trove.list.array.TFloatArrayList)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 TShortArrayList (gnu.trove.list.array.TShortArrayList)1 TIntIntHashMap (gnu.trove.map.hash.TIntIntHashMap)1 ArrayList (java.util.ArrayList)1 NBTBase (net.minecraft.nbt.NBTBase)1