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

37
LINES

< > BotCompany Repo | #1034670 // tok_findEndOfBlock_IContentsIndexedList2_v2

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5054L/28K).

1  
// i must point at the (possibly imaginary) opening bracket ("{")
2  
// index returned is index of closing bracket + 1 (or l(tok))
3  
4  
// version using iterator (much faster than v1!)
5  
6  
static int tok_findEndOfBlock_IContentsIndexedList2_v2(IContentsIndexedList2<S> tok, int i) {
7  
  int n = l(tok);
8  
  
9  
  // find opening & closing brackets after start index
10  
  
11  
  HasIndex start = new(i);
12  
  TreeSet<HasIndex> openers = tok.indicesOf_treeSetOfHasIndex("{");
13  
  Iterator<HasIndex> iOpener = openers?.tailSet(start, false).iterator();
14  
  TreeSet<HasIndex> closers = tok.indicesOf_treeSetOfHasIndex("}");
15  
  Iterator<HasIndex> iCloser = closers?.tailSet(start, false).iterator();
16  
17  
  // now step through both sets
18  
  
19  
  int level = 1;
20  
  HasIndex nextOpener = iOpener != null && iOpener.hasNext() ? iOpener.next() : null;
21  
  HasIndex nextCloser = iCloser != null && iCloser.hasNext() ? iCloser.next() : null;
22  
23  
  while true {
24  
    if (nextCloser == null) ret n; // block isn't closed
25  
    
26  
    if (nextOpener != null && nextOpener.idx < nextCloser.idx) {
27  
      // process opener
28  
      ++level;
29  
      nextOpener = iOpener.hasNext() ? iOpener.next() : null;
30  
    } else {
31  
      // process closer
32  
      if (--level == 0)
33  
        ret nextCloser.idx+1;
34  
      nextCloser = iCloser.hasNext() ? iCloser.next() : null;
35  
    }
36  
  }
37  
}

Author comment

Began life as a copy of #1034666

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034670
Snippet name: tok_findEndOfBlock_IContentsIndexedList2_v2
Eternal ID of this version: #1034670/4
Text MD5: 8e1925f6495e9f96556c4e2799a1c038
Transpilation MD5: 8adfc2d0d0f42ce9c9acae52152162b6
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-01 16:08:06
Source code size: 1332 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 152 / 220
Version history: 3 change(s)
Referenced in: [show references]