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)

1  
// map: index of opening bracket -> index of closing bracket
2  
static Map<Int, Int> getBracketMap(L tok) {
3  
  ret getBracketMap(tok, getBracketMap_opening, getBracketMap_closing);
4  
}
5  
6  
static Map<Int, Int> getBracketMap(L tok, Collection<S> opening, Collection<S> closing) {
7  
  ret getBracketMap(tok, opening, closing, 0, l(tok));
8  
}
9  
10  
static Map<Int, Int> getBracketMap(L tok, Collection<S> opening, Collection<S> closing, int from, int to) {
11  
  new TreeMap<Int, Int> map;
12  
  new L<int> stack;
13  
  for (int i = from|1; i < to; i+= 2) {
14  
    if (opening.contains(tok.get(i)))
15  
      stack.add(i);
16  
    else if (closing.contains(tok.get(i))) {
17  
      if (!empty(stack))
18  
        map.put(liftLast(stack), i);
19  
    }
20  
  }
21  
  ret map;
22  
}
23  
24  
static Set<S> getBracketMap_opening = lithashset("{", "(");
25  
static Set<S> getBracketMap_closing = lithashset("}", ")");

download  show line numbers  debug dex   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pzhvpgtvlbxg, 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: 500 / 1299
Version history: 10 change(s)
Referenced in: [show references]

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