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

52
LINES

< > BotCompany Repo | #1000465 // LCertainElement (learner)

JavaX fragment (include)

// finds a fixed element, e.g. a Java token

static class LCertainElement extends LearnerImpl {
  static boolean debug;
  ReversibleFunction f; // e.g. JavaTok
  String element;
  
  *(ReversibleFunction *f) {}
  
  public void processInOut(Object _in, Object _out) {
    String in = (String) _in, out = (String) _out;
    Set<String> set = new HashSet<String>(getMarked(out));
    if (debug)
      System.out.println("LCertainElement: set=" + set);
    if (set.size() == 1)
      element = set.iterator().next();
    if (debug)
      System.out.println("LCertainElement: element=" + element);
  }
  
  public Object processIn(Object _in) {
    List<String> l = (List<String>) f.process(_in);
    new List<String> l2;
    for (String x : l)
      l2.add(x.equals(element) ? "[[" + x + "]]" : x);
    return f.unprocess(l2);
  }
  
  // returns positions in marked string (from/to/from/to...)
  static List<Integer> getFindMarkers2(String s) {
    List<Integer> l = new ArrayList<Integer>();
    int i = 0;
    while (i < s.length()) {
      int j = s.indexOf("[[", i);
      if (j < 0) break;
      int k = s.indexOf("]]", j+2);
      if (k < 0) break;
      l.add(j);
      l.add(k+2);
      i = k+2;
    }
    return l;
  }
  
  static List<String> getMarked(String s) {
    List<Integer> l = getFindMarkers2(s);
    new List<String> result;
    for (int i = 0; i < l.size(); i += 2)
      result.add(s.substring(l.get(i)+2, l.get(i+1)-2));
    return result;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
808 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #1000465
Snippet name: LCertainElement (learner)
Eternal ID of this version: #1000465/1
Text MD5: 1f1f3c3a12867d4a483db0e8aea29414
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-09 20:23:09
Source code size: 1518 bytes / 52 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 768 / 1963
Referenced in: [show references]