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

25
LINES

< > BotCompany Repo | #1002118 - getBracketMap - uses curly and round brackets (also takes x-tok [token lists containing sublists])

JavaX fragment (include)

// map: index of opening bracket -> index of closing bracket
static Map<Int, Int> getBracketMap(L tok) {
  ret getBracketMap(tok, getBracketMap_opening, getBracketMap_closing);
}

static Map<Int, Int> getBracketMap(L tok, Collection<S> opening, Collection<S> closing) {
  ret getBracketMap(tok, opening, closing, 0, l(tok));
}

static Map<Int, Int> getBracketMap(L tok, Collection<S> opening, Collection<S> closing, int from, int to) {
  new TreeMap<Int, Int> map;
  new L<int> stack;
  for (int i = from|1; i < to; i+= 2) {
    if (opening.contains(tok.get(i)))
      stack.add(i);
    else if (closing.contains(tok.get(i))) {
      if (!empty(stack))
        map.put(liftLast(stack), i);
    }
  }
  ret map;
}

static Set<S> getBracketMap_opening = lithashset("{", "(");
static Set<S> getBracketMap_closing = lithashset("}", ")");

download  show line numbers  debug dex   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1002118
Snippet name: getBracketMap - uses curly and round brackets (also takes x-tok [token lists containing sublists])
Eternal ID of this version: #1002118/11
Text MD5: 134b095cd7fba325aeb063e25ad3098d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-01-12 21:40:14
Source code size: 857 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 457 / 1224
Version history: 10 change(s)
Referenced in: [show references]

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