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).

!include once #1027304 // Eclipse Collections

srecord noeq LCMerger(LineCompReader lc1) {
  LineCompReader lc2;
  LineCompReader lcOut; // actually the same as lc1 now
  LongIntHashMap pairIndex;
  Map<S, Int> literalIndex;
  new IntBuffer lc2map;
  int newLiterals; // number of literals added compared to lc1
  int newPairs;
  int nOriginalLiterals, nOriginalPairs;
  
  *(LineCompReader *lc1, LineCompReader *lc2) {}
  
  void add(LineCompReader lc2) {
    this.lc2 = lc2;
    run();
  }

  run {
    lcOut = lc1;
    nOriginalLiterals = l(lc1.literals);
    nOriginalPairs = l(lc1.pairs);
    newLiterals = newPairs = 0;

    if (pairIndex != null) print("Have pairIndex");
    else {
      pairIndex = new LongIntHashMap;
      printWithTime("Making pairIndex");
      for (int i = 0; i < lc1.pairs.size(); i++)
        pairIndex.put(lc1.pairs.get(i), i);
    }
    
    printWithTime("Making literalIndex");
    literalIndex = indexList(lc1.literals);
  
    // add lc2.literals to lc1.literals
  
    lc2map = new IntBuffer(l(lc2.literals) + l(lc2.pairs));
    
    printWithTime("Merging literals");
    for (int i = 0; i < l(lc2.literals); i++) {
      S c = lc2.literals.get(i);
      Int iLit = literalIndex.get(c);
      if (iLit == null) {
        iLit = addAndReturnIndex(lc1.literals, c);
        ++newLiterals;
        literalIndex.put(c, iLit);
      }
      lc2map.add(iLit);
    }
    
    // merge pairs
    
    printWithTime("Merging pairs");
    for i to nOriginalPairs: {
      long p = lc1.pairs.get(i);
      lc1.pairs.set(i, twoIntsToLong(adjust1(firstIntFromLong(p)), adjust1(secondIntFromLong(p))));
    }
    
    for (int i = 0; i < l(lc2.pairs); i++) {
      long p = lc2.pairs.get(i);
      long pAdjusted = twoIntsToLong(adjust2(firstIntFromLong(p)), adjust2(secondIntFromLong(p)));
      int iPair = pairIndex.getIfAbsent(pAdjusted, -1);
      if (iPair < 0) { // new pair
        iPair = l(lc1.pairs);
        lc1.pairs.add(pAdjusted);
        ++newPairs;
        pairIndex.put(pAdjusted, iPair);
      }
      lc2map.add(l(lcOut.literals)+iPair);
    }
    
    // copy files
    
    printWithTime("Merging files");
    lc1.versions = (LinkedHashMap) mapValues(lc1.versions, enc -> lmap adjust1(enc));

    for (S name, L<Int> encoding : lc2.versions) {
      if (lc1.versions.containsKey(name))
        continue with print("Warning: Duplicate file name " + name);
      lc1.versions.put(name, lmap adjust2(encoding));
    }
    
    printVars_str(+newLiterals, +newPairs);
    print("Synergy factor: " + doubleRatio(l(lc1.pairs), nOriginalPairs+l(lc2.pairs)));
  }
  
  // convert symbols from lc1
  int adjust1(int i) {
    if (i >= nOriginalLiterals)
      if (i >= nOriginalLiterals+nOriginalPairs)
        ret i+newLiterals+newPairs;
      else
        ret i+newLiterals;
    ret i;
  }
  
  int adjust2(int i) { ret lc2map.get(i); }
}

Author comment

Began life as a copy of #1029297

download  show line numbers  debug dex   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, 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: 106 / 252
Version history: 26 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1029327 & http://1029327.tinybrain.de