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

1  
// Finds out the index of a sublist in the original list
2  
// Works with nested (grand-parent) sublists.
3  
// Does not work with lists not made with subList() (returns -1)
4  
5  
// If you use NoIllegalAccesses: Make sure not to use subList(...)
6  
// instead of .subList(...) in case you ever call magicIndexOfSubList
7  
// on the result.
8  
9  
static <A> int magicIndexOfSubList(L<A> list, L<A> sublist) {
10  
  if (sublist == list) ret 0;
11  
  
12  
  ifclass ISubList
13  
    if (sublist cast ISubList) {
14  
      L root = sublist.rootList();
15  
      int o2 = sublist.subListOffset();
16  
      if (list cast ISubList) {
17  
        if (list.rootList() != root) ret -1;
18  
        int o1 = list.subListOffset();
19  
        ret o2-o1;
20  
      } else {
21  
        if (list != root) ret -1;
22  
        ret o2;
23  
      }
24  
    }
25  
  endif
26  
  
27  
  ifndef NoIllegalAccesses
28  
    Int o2 = cast get(sublist, "offset");
29  
    if (o2 == null) ret -1;
30  
    Int o1 = cast getOpt(list, "offset");
31  
    if (true) ret o2-(o1 != null ? o1 : 0);
32  
  endifndef
33  
  
34  
  ret -1;
35  
}

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: 522 / 1493
Version history: 12 change(s)
Referenced in: [show references]