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

35
LINES

< > BotCompany Repo | #1001832 // magicIndexOfSubList

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

Libraryless. Click here for Pure Java version (2995L/17K).

// Finds out the index of a sublist in the original list
// Works with nested (grand-parent) sublists.
// Does not work with lists not made with subList() (returns -1)

// If you use NoIllegalAccesses: Make sure not to use subList(...)
// instead of .subList(...) in case you ever call magicIndexOfSubList
// on the result.

static <A> int magicIndexOfSubList(L<A> list, L<A> sublist) {
  if (sublist == list) ret 0;
  
  ifclass ISubList
    if (sublist cast ISubList) {
      L root = sublist.rootList();
      int o2 = sublist.subListOffset();
      if (list cast ISubList) {
        if (list.rootList() != root) ret -1;
        int o1 = list.subListOffset();
        ret o2-o1;
      } else {
        if (list != root) ret -1;
        ret o2;
      }
    }
  endif
  
  ifndef NoIllegalAccesses
    Int o2 = cast get(sublist, "offset");
    if (o2 == null) ret -1;
    Int o1 = cast getOpt(list, "offset");
    if (true) ret o2-(o1 != null ? o1 : 0);
  endifndef
  
  ret -1;
}

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: #1001832
Snippet name: magicIndexOfSubList
Eternal ID of this version: #1001832/13
Text MD5: 8bedfb93982be057eef9d280d6dff4e0
Transpilation MD5: 3bf34ea240ea1e4a38d7eda543ee9cc7
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-24 03:07:48
Source code size: 1015 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 587 / 1586
Version history: 12 change(s)
Referenced in: [show references]