Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

127
LINES

< > BotCompany Repo | #1028204 // LineCompCompressor v1.0 (working, with slow pair making, before IntPair)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6651L/42K).

1  
sclass LineCompCompressor {
2  
  replace Encodings with Map<S, L<Int>>.
3  
  
4  
  abstract sclass Chunk {
5  
    abstract S text(L<Chunk> chunks);
6  
  }
7  
  
8  
  srecord CPair(int i1, int i2) > Chunk {
9  
    CPair(Pair<Int> p) { i1 = p.a; i2 = p.b; }
10  
    
11  
    S text(L<Chunk> chunks) {
12  
      ret linesLL_rtrim(chunks.get(i1).text(chunks), chunks.get(i2).text(chunks));
13  
    }
14  
  }
15  
  
16  
  srecord CPrim(S s) > Chunk {
17  
    S text(L<Chunk> chunks) { ret s; }
18  
  }
19  
20  
  bool verbose = false;
21  
  bool sortLines = true;
22  
  bool verify = true;
23  
  
24  
  Map<S, LS> textIDToLines;
25  
  LS allUniqueLines;
26  
  new L<Chunk> chunks;
27  
  int primChunks;
28  
  Map<S, Int> lineIndex;
29  
  new Map<PairS, Int> linePairIndex;
30  
  Encodings finalEncodings;
31  
  
32  
  // key = version ID, values = text
33  
  *(SS texts) {
34  
    textIDToLines = mapValuesToLinkedHashMap lines(texts);
35  
  }
36  
  
37  
  run {
38  
    allUniqueLines = uniquify(concatLists(values(textIDToLines)));
39  
    if (sortLines) sortInPlace(allUniqueLines);
40  
    for (S line : allUniqueLines)
41  
      chunks.add(new CPrim(line));
42  
    primChunks = l(chunks);
43  
    lineIndex = listIndex(collect s(chunks));
44  
    
45  
    // simple encoding (only direct line references)
46  
    Encodings simpleEncodings = mapValues(textIDToLines,
47  
      (IF1<LS, L<Int>>) (lines -> map(lines, line -> lineIndex.get(line))));
48  
    //printAndCheckEncodings(simpleEncodings);
49  
    
50  
    finalEncodings = repeatUntilSame compressPairs(simpleEncodings);
51  
    if (verbose || verify) printAndCheckEncodings(finalEncodings);
52  
  }
53  
  
54  
  void saveAsTextFile(File f) {
55  
    S out = exportEncoding(finalEncodings);
56  
    saveTextFile(f, out);
57  
    
58  
    if (verify) checkDecompression(f, textIDToLines);
59  
  }
60  
  
61  
  void checkDecompression(File file, Map<S, LS> textIDToLines) {
62  
    temp BufferedReader reader = bufferedUtf8Reader(file);
63  
    LineCompReader lcr = new(reader);
64  
    assertEquals(keysList(textIDToLines), asList(lcr.versions()));
65  
    for (S version : keys(textIDToLines))
66  
      assertEquals(lcr.textForVersion(version), lines_rtrim(textIDToLines.get(version)));
67  
    if (verbose) print("Decompression OK for " + nVersions(textIDToLines));
68  
  }
69  
  
70  
  S exportEncoding(Encodings encodings) {
71  
    new LS buf;
72  
    buf.add("LINECOMP " + primChunks); // magic signature
73  
    for (Chunk c : chunks) {
74  
      if (c cast CPair)
75  
        buf.add(c.i1 + " " + c.i2);
76  
      else
77  
        buf.add(((CPrim) c).s);
78  
    }
79  
    for (S id, L<Int> l : encodings)
80  
      buf.add(id + "=" + joinWithSpace(l));
81  
    ret lines_rtrim(buf);
82  
  }
83  
  
84  
  Encodings compressPairs(Encodings encodings) {
85  
    new MultiSet<Pair<Int>> pairCounts;
86  
    
87  
    for (L<Int> l : values(encodings)) {
88  
      Pair<Int> lastPair = null;
89  
      for (Pair<Int> pair : overlappingPairs(l)) {
90  
        if (neq(pair, lastPair)) {
91  
          lastPair = pair;
92  
          pairCounts.add(pair);
93  
        }
94  
      }
95  
    }
96  
    
97  
    //print("Pair counts: " + pairCounts);
98  
    Pair<Int> toCompress = msMostPopularDuplicate(pairCounts);
99  
100  
    // Compress only most popular pair
101  
    if (toCompress == null) ret encodings; // Nothing to do
102  
    int idx = makeCPair(toCompress);
103  
    print("Made pair: " + toCompress + " -> " + idx + ", " + (msNumberOfDuplicates(pairCounts)-1) + " remaining");
104  
    ret mapValues(encodings, (IF1<L<Int>>) encoded ->
105  
      replaceSublist(encoded, pairToList(toCompress), ll(idx)));
106  
  }
107  
  
108  
  int makeCPair(Pair<Int> p) {
109  
    int idx = addAndReturnIndex(chunks, new CPair(p));
110  
    ret idx;
111  
  }
112  
  
113  
  void printAndCheckEncodings(Encodings encodings) {
114  
    for (S id, L<Int> encoded : encodings) {
115  
      if (verbose) print(id + ": " + joinWithSpace(encoded));
116  
      assertEquals(lines(textIDToLines.get(id)), decode(encoded));
117  
    }
118  
  }
119  
  
120  
  S decode(L<Int> encoded) {
121  
    ret lines(lambdaMap chunkText(encoded));
122  
  }
123  
  
124  
  S chunkText(int idx) {
125  
    ret chunks.get(idx).text(chunks);
126  
  }
127  
}

Author comment

Began life as a copy of #1028192

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028204
Snippet name: LineCompCompressor v1.0 (working, with slow pair making, before IntPair)
Eternal ID of this version: #1028204/1
Text MD5: 58ff7657b5ae50ec8a53ae5a030dc36d
Transpilation MD5: 0455ff992ae44ec739360d5a43d43d84
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-26 13:15:01
Source code size: 3894 bytes / 127 lines
Pitched / IR pitched: No / No
Views / Downloads: 191 / 278
Referenced in: [show references]