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

153
LINES

< > BotCompany Repo | #1029354 // LineCompReader [backup before compacting]

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

Libraryless. Click here for Pure Java version (5969L/38K).

1  
sclass LineCompReader {
2  
  new LS literals;
3  
  int[] literalOffsets; // where they start in file
4  
  new L<IntPair> pairs;
5  
  new LinkedHashMap<S, L<Int>> versions;
6  
  bool byteMode;
7  
  
8  
  *() {}
9  
  
10  
  // takes text or gzipped input file
11  
  *(File f) {
12  
    temp BufferedReader reader = rawByteReader_possiblyGZipped(f);
13  
    load(reader);
14  
  }
15  
  
16  
  *(InputStream in) { load(rawBytesReader(in)); }
17  
  *(BufferedReader reader) { load(reader); }
18  
  
19  
  void load(BufferedReader reader) ctex {
20  
    S s = readLineIgnoreCR(reader);
21  
    int ofs = l(s)+1;
22  
    new Matches m;
23  
    if (startsWith(s, "BYTECOMP ", m)) set byteMode;
24  
    else if (!startsWith(s, "LINECOMP ", m))
25  
      fail("Not a LINECOMP file");
26  
    int nLiterals = parseInt(m.rest());
27  
    new IntBuffer offsets;
28  
    for i to nLiterals: {
29  
      S line = readLineIgnoreCR(reader);
30  
      assertNotNull(line);
31  
      literals.add(byteMode ? str(charFromHex(line)) : line);
32  
      offsets.add(ofs);
33  
      ofs += l(line)+1;
34  
    }
35  
    offsets.add(ofs);
36  
    literalOffsets = offsets.toArray();
37  
    while licensed {
38  
      s = readLineIgnoreCR(reader);
39  
      if (s == null || contains(s, "=")) break;
40  
      try {
41  
        pairs.add(assertNotNull(listToIntPair(parseInts(splitAtSpace(s)))));
42  
      } on fail {
43  
        print("On line " + (nLiterals + l(pairs)));
44  
      }
45  
    }
46  
    while (contains(s, "=")) {
47  
      int i = indexOf(s, '=');
48  
      versions.put(takeFirst(s, i), compactIntList(parseInts(splitAtSpace(substring(s, i+1)))));
49  
      s = readLineIgnoreCR(reader);
50  
    }
51  
  }
52  
  
53  
  Set<S> versions() { ret keys(versions); }
54  
  
55  
  S getText(S version) { ret textForVersion(version); }
56  
  S textForVersion(S version) {
57  
    L<Int> encoded = versions.get(version);
58  
    if (encoded == null) null;
59  
    new LS buf;
60  
    for (int idx : encoded)
61  
      decode(idx, buf);
62  
    ret myFromLines(buf);
63  
  }
64  
  
65  
  // name of first (or only) file
66  
  S firstFile() { ret first(versions()); }
67  
  
68  
  // text for first (or only) file
69  
  S text() { ret getText(firstFile()); }
70  
  
71  
  L<Int> encoding() { ret versions.get(firstFile()); }
72  
  
73  
  S myFromLines(LS l) {
74  
    ret byteMode
75  
      ? join(l)
76  
      : fromLines_rtrim(l);
77  
  }
78  
  
79  
  void decode(int idx, LS buf) {
80  
    if (idx < l(literals))
81  
      buf.add(literals.get(idx));
82  
    else {
83  
      IntPair p = pairs.get(idx-l(literals));
84  
      decode(p.a, buf);
85  
      decode(p.b, buf);
86  
    }
87  
  }
88  
  
89  
  // That was it! The rest of this file is just for calculating some stats.
90  
  
91  
  new Map<Int> lineCountsForPairs;
92  
  new Map<Int, Long> byteCountsForPairs;
93  
94  
  int lineCountForPointer(int idx) {
95  
    ret idx < l(literals) ? 1 : lineCountForPair(idx);
96  
  }
97  
  
98  
  long byteCountForPointer(int idx) {
99  
    ret idx < l(literals) ? l(literals.get(idx))+1 : byteCountForPair(idx);
100  
  }
101  
  
102  
  int lineCountForPair(int idx) {
103  
    Int c = lineCountsForPairs.get(idx);
104  
    if (c == null) {
105  
      IntPair p = pairs.get(idx-l(literals));
106  
      c = lineCountForPointer(p.a) + lineCountForPointer(p.b);
107  
      lineCountsForPairs.put(idx, c);
108  
    }
109  
    ret c;
110  
  }
111  
  
112  
  long byteCountForPair(int idx) {
113  
    Long c = byteCountsForPairs.get(idx);
114  
    if (c == null) {
115  
      IntPair p = pairs.get(idx-l(literals));
116  
      c = byteCountForPointer(p.a) + byteCountForPointer(p.b);
117  
      byteCountsForPairs.put(idx, c);
118  
    }
119  
    ret c;
120  
  }
121  
  
122  
  int lineCountForVersion(S version) {
123  
    L<Int> encoded = versions.get(version);
124  
    if (encoded == null) ret 0;
125  
    int n = 0;
126  
    for (int i : encoded) n += lineCountForPointer(i);
127  
    ret n;
128  
  }
129  
  
130  
  long byteCountForVersion(S version) {
131  
    L<Int> encoded = versions.get(version);
132  
    if (encoded == null) ret 0;
133  
    long n = 0;
134  
    for (int i : encoded) n += byteCountForPointer(i);
135  
    ret max(0, n-1);
136  
  }
137  
  
138  
  long totalByteCount() {
139  
    ret longSum(lambdaMap byteCountForVersion(versions()));
140  
  }
141  
  
142  
  // now we can also save again
143  
  
144  
  void save(PrintWriter out) {
145  
    out.println((byteMode ? "BYTECOMP " : "LINECOMP ") + l(literals));
146  
    for (S s : literals)
147  
      out.println(byteMode ? charToHex(first(s)) : s);
148  
    for (IntPair p : pairs)
149  
      out.println(p.a + " " + p.b);
150  
    for (S id, L<Int> l : versions)
151  
      out.println(id + "=" + joinWithSpace(l));
152  
  }  
153  
}

Author comment

Began life as a copy of #1028182

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: #1029354
Snippet name: LineCompReader [backup before compacting]
Eternal ID of this version: #1029354/1
Text MD5: 9bf870dc389236145eb0868f68ef97d2
Transpilation MD5: c455448d7a166c60d49b33e320bdfe21
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-02 00:48:32
Source code size: 4290 bytes / 153 lines
Pitched / IR pitched: No / No
Views / Downloads: 105 / 166
Referenced in: [show references]