// 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 int magicIndexOfSubList(L list, L 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;
}