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

17
LINES

< > TinyBrain | #1002118 - getBracketMap (now 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) {
  new TreeMap<Int, Int> map;
  new L<int> stack;
  for (int i = 1; i < l(tok); i+= 2) {
    if (getBracketMap_opening.contains(tok.get(i)))
      stack.add(i);
    else if (getBracketMap_closing.contains(tok.get(i))) {
      if (!empty(stack))
        map.put(liftLast(stack), i);
    }
  }
  ret map;
}

static L<S> getBracketMap_opening = ll("{", "(");
static L<S> getBracketMap_closing = ll("}", ")");

download  show line numbers  debug dex   

Travelled to 2 computer(s): cfunsshuasjs, tvejysmllsmz

No comments. add comment

Snippet ID: #1002118
Snippet name: getBracketMap (now also takes x-tok [token lists containing sublists])
Eternal ID of this version: #1002118/8
Text MD5: d5a7069a9d06d29754ef1969d6c69e52
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-09-13 16:48:53
Source code size: 525 bytes / 17 lines
Pitched / IR pitched: No / No
Views / Downloads: 174 / 777
Version history: 7 change(s)
Referenced in: [show]