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

100
LINES

< > BotCompany Repo | #1029327 // LCMerger [OK]

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

Uses 11335K of libraries. Click here for Pure Java version (6486L/41K).

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  
  new IntBuffer lc2map;
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  
    newLiterals = newPairs = 0;
25  
26  
    if (pairIndex != null) print("Have pairIndex");
27  
    else {
28  
      pairIndex = new LongIntHashMap;
29  
      printWithTime("Making pairIndex");
30  
      for (int i = 0; i < lc1.pairs.size(); i++)
31  
        pairIndex.put(lc1.pairs.get(i), i);
32  
    }
33  
    
34  
    printWithTime("Making literalIndex");
35  
    literalIndex = indexList(lc1.literals);
36  
  
37  
    // add lc2.literals to lc1.literals
38  
  
39  
    lc2map = new IntBuffer(l(lc2.literals) + l(lc2.pairs));
40  
    
41  
    printWithTime("Merging literals");
42  
    for (int i = 0; i < l(lc2.literals); i++) {
43  
      S c = lc2.literals.get(i);
44  
      Int iLit = literalIndex.get(c);
45  
      if (iLit == null) {
46  
        iLit = addAndReturnIndex(lc1.literals, c);
47  
        ++newLiterals;
48  
        literalIndex.put(c, iLit);
49  
      }
50  
      lc2map.add(iLit);
51  
    }
52  
    
53  
    // merge pairs
54  
    
55  
    printWithTime("Merging pairs");
56  
    for i to nOriginalPairs: {
57  
      long p = lc1.pairs.get(i);
58  
      lc1.pairs.set(i, twoIntsToLong(adjust1(firstIntFromLong(p)), adjust1(secondIntFromLong(p))));
59  
    }
60  
    
61  
    for (int i = 0; i < l(lc2.pairs); i++) {
62  
      long p = lc2.pairs.get(i);
63  
      long pAdjusted = twoIntsToLong(adjust2(firstIntFromLong(p)), adjust2(secondIntFromLong(p)));
64  
      int iPair = pairIndex.getIfAbsent(pAdjusted, -1);
65  
      if (iPair < 0) { // new pair
66  
        iPair = l(lc1.pairs);
67  
        lc1.pairs.add(pAdjusted);
68  
        ++newPairs;
69  
        pairIndex.put(pAdjusted, iPair);
70  
      }
71  
      lc2map.add(l(lcOut.literals)+iPair);
72  
    }
73  
    
74  
    // copy files
75  
    
76  
    printWithTime("Merging files");
77  
    lc1.versions = (LinkedHashMap) mapValues(lc1.versions, enc -> lmap adjust1(enc));
78  
79  
    for (S name, L<Int> encoding : lc2.versions) {
80  
      if (lc1.versions.containsKey(name))
81  
        continue with print("Warning: Duplicate file name " + name);
82  
      lc1.versions.put(name, lmap adjust2(encoding));
83  
    }
84  
    
85  
    printVars_str(+newLiterals, +newPairs);
86  
    print("Synergy factor: " + doubleRatio(l(lc1.pairs), nOriginalPairs+l(lc2.pairs)));
87  
  }
88  
  
89  
  // convert symbols from lc1
90  
  int adjust1(int i) {
91  
    if (i >= nOriginalLiterals)
92  
      if (i >= nOriginalLiterals+nOriginalPairs)
93  
        ret i+newLiterals+newPairs;
94  
      else
95  
        ret i+newLiterals;
96  
    ret i;
97  
  }
98  
  
99  
  int adjust2(int i) { ret lc2map.get(i); }
100  
}

Author comment

Began life as a copy of #1029297

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: #1029327
Snippet name: LCMerger [OK]
Eternal ID of this version: #1029327/27
Text MD5: e80e1cde58850e20eb7a2e4fcea3b0f9
Transpilation MD5: 47dcb19326523d29dcd89a1244355e86
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-03 00:47:00
Source code size: 2978 bytes / 100 lines
Pitched / IR pitched: No / No
Views / Downloads: 271 / 622
Version history: 26 change(s)
Referenced in: [show references]