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

32
LINES

< > BotCompany Repo | #1017956 // findUsingWordTree

JavaX fragment (include)

static Pair<IntRange, O> findUsingWordTree(L<S> tok, Map<S, O> tree) {
  ret findUsingWordTree(tok, tree, 0);
}

// tok = CNC
// returns (index range C to N, tree leaf)
static Pair<IntRange, O> findUsingWordTree(L<S> tok, Map<S, O> tree, int i) {
  if (tree == null) null;
  O best = null; // best leaf
  int bestIndex = 0; // best index
  int n = l(tok);
  
  i |= 1; // Proceed to C token
  while (i <= n) {
    int j = i;
    while true {
      O def = tree.get("<default>");
      if (def != null) { best = def; bestIndex = j; }
      
      if (j >= n) break;
      O t = tree.get(tok.get(j));
      j += 2;
      if (t == null) break;
      if (!t instanceof Map) ret pair(intRange(i, j-1), t); // leaf!
      tree = (Map) t;
    }
    if (best != null) ret pair(intRange(i, bestIndex), best);
    i += 2;
  }
  
  null;
}

Author comment

Began life as a copy of #1017908

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1017956
Snippet name: findUsingWordTree
Eternal ID of this version: #1017956/4
Text MD5: b5d0ef72ebba8f9f51f3a531147384b1
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-08-23 01:43:08
Source code size: 859 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 263 / 279
Version history: 3 change(s)
Referenced in: [show references]