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).

// i must point at the (possibly imaginary) opening bracket ("{")
// index returned is index of closing bracket + 1 (or l(tok))

// version using iterator (much faster than v1!)

static int tok_findEndOfBlock_IContentsIndexedList2_v2(IContentsIndexedList2<S> tok, int i) {
  int n = l(tok);
  
  // find opening & closing brackets after start index
  
  HasIndex start = new(i);
  TreeSet<HasIndex> openers = tok.indicesOf_treeSetOfHasIndex("{");
  Iterator<HasIndex> iOpener = openers?.tailSet(start, false).iterator();
  TreeSet<HasIndex> closers = tok.indicesOf_treeSetOfHasIndex("}");
  Iterator<HasIndex> iCloser = closers?.tailSet(start, false).iterator();

  // now step through both sets
  
  int level = 1;
  HasIndex nextOpener = iOpener != null && iOpener.hasNext() ? iOpener.next() : null;
  HasIndex nextCloser = iCloser != null && iCloser.hasNext() ? iCloser.next() : null;

  while true {
    if (nextCloser == null) ret n; // block isn't closed
    
    if (nextOpener != null && nextOpener.idx < nextCloser.idx) {
      // process opener
      ++level;
      nextOpener = iOpener.hasNext() ? iOpener.next() : null;
    } else {
      // process closer
      if (--level == 0)
        ret nextCloser.idx+1;
      nextCloser = iCloser.hasNext() ? iCloser.next() : null;
    }
  }
}

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: 151 / 217
Version history: 3 change(s)
Referenced in: [show references]