Uses 11335K of libraries. Click here for Pure Java version (6398L/40K).
1 | !include once #1027304 // Eclipse Collections |
2 | |
3 | srecord noeq LCMerger(LineCompReader lc1) { |
4 | LineCompReader lc2; |
5 | LineCompReader lcOut; // actually the same as lc1 now |
6 | LongIntHashMap pairIndex; |
7 | Map<S, Int> literalIndex; |
8 | IntIntHashMap lc2map; // TODO: Drop. This is actually contained in the pairIndex |
9 | int newLiterals; // number of literals added compared to lc1 |
10 | int newPairs; |
11 | int nOriginalLiterals, nOriginalPairs; |
12 | |
13 | *(LineCompReader *lc1, LineCompReader *lc2) {} |
14 | |
15 | void add(LineCompReader lc2) { |
16 | this.lc2 = lc2; |
17 | run(); |
18 | } |
19 | |
20 | run { |
21 | lcOut = lc1; |
22 | nOriginalLiterals = l(lc1.literals); |
23 | nOriginalPairs = l(lc1.pairs); |
24 | lc2map = new IntIntHashMap; |
25 | newLiterals = newPairs = 0; |
26 | |
27 | if (pairIndex != null) print("Have pairIndex"); |
28 | else { |
29 | pairIndex = new LongIntHashMap; |
30 | printWithTime("Making pairIndex"); |
31 | for (int i = 0; i < lc1.pairs.size(); i++) |
32 | pairIndex.put(lc1.pairs.get(i), i); |
33 | } |
34 | |
35 | printWithTime("Making literalIndex"); |
36 | literalIndex = indexList(lc1.literals); |
37 | |
38 | // add lc2.literals to lc1.literals |
39 | |
40 | printWithTime("Merging literals"); |
41 | for (int i = 0; i < l(lc2.literals); i++) { |
42 | S c = lc2.literals.get(i); |
43 | Int iLit = literalIndex.get(c); |
44 | if (iLit == null) { |
45 | iLit = addAndReturnIndex(lc1.literals, c); |
46 | ++newLiterals; |
47 | literalIndex.put(c, iLit); |
48 | } |
49 | lc2map.put(i, iLit); |
50 | } |
51 | |
52 | // merge pairs |
53 | |
54 | printWithTime("Merging pairs"); |
55 | for i to nOriginalPairs: { |
56 | long p = lc1.pairs.get(i); |
57 | lc1.pairs.set(i, twoIntsToLong(adjust1(firstIntFromLong(p)), adjust1(secondIntFromLong(p)))); |
58 | } |
59 | |
60 | for (int i = 0; i < l(lc2.pairs); i++) { |
61 | long p = lc2.pairs.get(i); |
62 | long pAdjusted = twoIntsToLong(adjust2(firstIntFromLong(p)), adjust2(secondIntFromLong(p))); |
63 | int iPair = pairIndex.getIfAbsent(pAdjusted, -1); |
64 | if (iPair < 0) { // new pair |
65 | iPair = l(lc1.pairs); |
66 | lc1.pairs.add(pAdjusted); |
67 | ++newPairs; |
68 | pairIndex.put(pAdjusted, iPair); |
69 | } |
70 | lc2map.put(l(lc2.literals)+i, l(lcOut.literals)+iPair); |
71 | } |
72 | |
73 | // copy files |
74 | |
75 | printWithTime("Merging files"); |
76 | lc1.versions = (LinkedHashMap) mapValues(lc1.versions, enc -> lmap adjust1(enc)); |
77 | |
78 | for (S name, L<Int> encoding : lc2.versions) { |
79 | if (lc1.versions.containsKey(name)) |
80 | continue with print("Warning: Duplicate file name " + name); |
81 | lc1.versions.put(name, lmap adjust2(encoding)); |
82 | } |
83 | |
84 | printVars_str(+newLiterals, +newPairs); |
85 | print("Synergy factor: " + doubleRatio(l(lc1.pairs), nOriginalPairs+l(lc2.pairs))); |
86 | } |
87 | |
88 | // convert symbols from lc1 |
89 | int adjust1(int i) { |
90 | if (i >= nOriginalLiterals) |
91 | if (i >= nOriginalLiterals+nOriginalPairs) |
92 | ret i+newLiterals+newPairs; |
93 | else |
94 | ret i+newLiterals; |
95 | ret i; |
96 | } |
97 | |
98 | int adjust2(int i) { ret lc2map.getIfAbsent(i, i); } |
99 | } |
Began life as a copy of #1029327
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: | #1029357 |
Snippet name: | LCMerger [working backup] |
Eternal ID of this version: | #1029357/1 |
Text MD5: | 24307a9d1e60eb76224cd3b4defec411 |
Transpilation MD5: | ad3f8bc6b0afd5576cebff1ba7492711 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-08-02 13:43:54 |
Source code size: | 3036 bytes / 99 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 213 / 262 |
Referenced in: | [show references] |