!include once #1027304 // Eclipse Collections import org.eclipse.collections.impl.map.mutable.primitive.*; // compressor that takes input byte by byte and compresses // as soon as possible. // byte mode+case-sensitive only // We now may have literals and pairs in any order final sclass LCStreamingCompressor { int safety = 0; abstract sclass Chunk { abstract S text(L chunks); IntPair getPair() { null; } } srecord CPair(int i1, int i2) > Chunk { CPair(IntPair p) { i1 = p.a; i2 = p.b; } S text(L chunks) { ret linesLL_rtrim(chunks.get(i1).text(chunks), chunks.get(i2).text(chunks)); } IntPair getPair() { ret IntPair(i1, i2); } } srecord CPrim(S s) > Chunk { S text(L chunks) { ret s; } } bool verbose = false, verboseCompressionSteps = false; new LineComp_PairIndex pairIndex; new L chunks; Map literalIndex; new LongIntHashMap existingPairsIndex; // formerly Map for (S line : allUniqueLines) chunks.add(new CPrim(line)); primChunks = l(chunks); LS _lines = collect s(chunks); lineIndex = caseInsensitive ? ciListIndex(_lines) : listIndex(_lines); _lines = null; // simple encoding (only direct line references) simpleEncodings = mapValues(textIDToLines, (IF1>) (lines -> wrapIntArrayAsImmutableList(mapToIntArray(lines, line -> lineIndex.get(line))))); //printAndCheckEncodings(simpleEncodings); if (verboseCompressionSteps) print("Have simple encodings"); finalEncodings = compressPairs(); if (verbose || verify) printAndCheckEncodings(finalEncodings); } void saveAsTextFile(File f) { S out = exportEncoding(finalEncodings); saveTextFile(f, out); if (verify) checkDecompression(f, textIDToLines); } void checkDecompression(File file, Map textIDToLines) { temp BufferedReader reader = bufferedUtf8Reader(file); LineCompReader lcr = new(reader); assertEquals(keysList(textIDToLines), asList(lcr.versions())); for (S version : keys(textIDToLines)) assertEquals(lcr.textForVersion(version), lines_rtrim(textIDToLines.get(version))); if (verbose) print("Decompression OK for " + nVersions(textIDToLines)); } S asText() { ret exportEncoding(finalEncodings); } S exportEncoding(Encodings encodings) { new LS buf; buf.add((byteMode ? "BYTECOMP " : "LINECOMP ") + primChunks); // magic signature for (Chunk c : chunks) { if (c cast CPair) buf.add(c.i1 + " " + c.i2); else buf.add(((CPrim) c).s); } for (S id, L l : encodings) buf.add(id + "=" + joinWithSpace(l)); ret lines_rtrim(buf); } Encodings compressPairs() { pairIndex.verbose = verboseCompressionSteps; pairIndex.init(); for (S version, L l : simpleEncodings) pairIndex.newChain(version, l); pairIndex.haveChains(); simpleEncodings = null; // forget ret compressChains(keys(pairIndex.chains)); } Encodings compressChains(Cl files) { NotTooOften nto = new(printStatsEveryNSeconds*1000); IntPair toCompress; // Compress only most popular pair in one step while ping ((toCompress = pairIndex.mostPopularDuplicate()) != null) { if (safety > 0 && --safety <= 0) fail("safety"); ++round; if (verboseStats) if (printStatsEvery > 0 && (round % printStatsEvery) == 0 || round <= printStatsInFirstRounds || nto!) printStats(); int count = pairIndex.getCount(toCompress); Int idx = makeCPair(toCompress); int dups = pairIndex.numberOfDuplicates(); if (verboseCompressionSteps) print("Compressing pair " + toCompress + " (count=" + count + ") -> " + idx + ", " + (dups-1) + " remaining"); pairIndex.replacePair(toCompress.a, toCompress.b, idx); } if (verboseStats) printStats(); // reconvert to normal list ret mapToValues(f -> pairIndex.chains.get(f).toList(), files); } // TODO: check if pair is already there Int makeCPair(IntPair p) { long l = intPairToLong(p); S text = null; if (fullIndex != null) { text = itemToString(p.a) + itemToString(p.b); Int idx = fullIndex.get(text); if (idx != null) ret idx; } int idx = existingPairsIndex.getIfAbsent(l, -1); if (idx < 0) { idx = addAndReturnIndex(chunks, new CPair(p)); existingPairsIndex.put(l, idx); if (fullIndex != null) fullIndex.put(text, idx); } ret idx; } void printAndCheckEncodings(Encodings encodings) { for (S id, L encoded : encodings) { if (verbose) print(id + ": " + joinWithSpace(encoded)); S x = myFromLines(textIDToLines.get(id)), y = decode(encoded); if (caseInsensitive) assertEqualsIC(x, y); else assertEquals_quote(x, y); } } S decode(L encoded) { LS l = lambdaMap chunkText(encoded); if (byteMode) l = lines(lines(l)); ret myFromLines(l); } // for non-byte mode S chunkText(int idx) { ret chunks.get(idx).text(chunks); } void printStats() { print("Round: " + round); pairIndex.printStats(); } // byteMode only S itemToString(int idx) { new StringBuilder buf; itemToString_step(buf, idx); ret str(buf); } void itemToString_step(StringBuilder buf, int idx) { Chunk c = chunks.get(idx); if (c cast CPair) { itemToString_step(buf, c.i1); itemToString_step(buf, c.i2); } else { S s = ((CPrim) c).s; buf.append(byteMode ? charFromHex(s) : s + "\n"); } } int nLiterals() { ret primChunks; } int nItems() { ret l(chunks); } IntPair getPair(int idx) { ret chunks.get(idx).getPair(); } }