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

43
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> getBracketMap(L tok, Collection<S> opening, Collection<S> closing) {
  ret getBracketMap(tok, opening, closing, 0, l(tok));
}

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

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

static Map<Int> getBracketMap(LS tok, IPred<S> opening, IPred<S> closing, int from, int to) {
  new TreeMap<Int> map;
  new L<Int> stack;
  for (int i = from|1; i < to; i+= 2) {
    S t = tok.get(i);
    if (opening.get(t))
      stack.add(i);
    else if (closing.get(t))
      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  old transpilations   

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

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/12
Text MD5: e557f5ee3c0661211de1cada5959e009
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-01 18:21:00
Source code size: 1327 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 753 / 1657
Version history: 11 change(s)
Referenced in: #1002427 - Accellerating 629 (SPIKE)
#1003919 - getBracketMap2
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1010376 - getBracketMapC (takes code tokens)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)