// syntax 1: replace all occurrences of x in l with y
static L replaceSublist(L l, L x, L y) {
if (x == null) ret l;
int i = 0;
while (true) {
i = indexOfSubList(l, x, i);
if (i < 0) break;
replaceSublist(l, i, i+l(x), y);
i += l(y);
}
ret l;
}
// syntax 2: splice l at fromIndex-toIndex and replace middle part with y
static L replaceSublist(L l, int fromIndex, int toIndex, L y) {
int n = y.size(), toIndex_new = fromIndex+n;
if (toIndex_new < toIndex) {
removeSubList(l, toIndex_new, toIndex);
copyListPart(y, 0, l, fromIndex, n);
} else {
copyListPart(y, 0, l, fromIndex, toIndex-fromIndex);
if (toIndex_new > toIndex)
l.addAll(toIndex, subList(y, toIndex-fromIndex));
}
ret l;
}
ifclass IntRange
static L replaceSublist(L l, IntRange r, L y) {
ret replaceSublist(l, r.start, r.end, y);
}
endif