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();
}
Aggregations