Libraryless. Click here for Pure Java version (2532L/16K).
1 | // syntax 1: replace all occurrences of x in l with y |
2 | static <A> L<A> replaceSublist(L<A> l, L<A> x, L<A> y) { |
3 | if (x == null) ret l; |
4 | |
5 | int i = 0; |
6 | while (true) { |
7 | i = indexOfSubList(l, x, i); |
8 | if (i < 0) break; |
9 | |
10 | replaceSublist(l, i, i+l(x), y); |
11 | i += l(y); |
12 | } |
13 | ret l; |
14 | } |
15 | |
16 | // syntax 2: splice l at fromIndex-toIndex and replace middle part with y |
17 | static <A> L<A> replaceSublist(L<A> l, int fromIndex, int toIndex, L<A> y) { |
18 | int n = y.size(), toIndex_new = fromIndex+n; |
19 | if (toIndex_new < toIndex) { |
20 | removeSubList(l, toIndex_new, toIndex); |
21 | copyListPart(y, 0, l, fromIndex, n); |
22 | } else { |
23 | copyListPart(y, 0, l, fromIndex, toIndex-fromIndex); |
24 | if (toIndex_new > toIndex) |
25 | l.addAll(toIndex, subList(y, toIndex-fromIndex)); |
26 | } |
27 | ret l; |
28 | } |
29 | |
30 | ifclass IntRange |
31 | static <A> L<A> replaceSublist(L<A> l, IntRange r, L<A> y) { |
32 | ret replaceSublist(l, r.start, r.end, y); |
33 | } |
34 | endif |
download show line numbers debug dex old transpilations
Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1002136 |
Snippet name: | replaceSublist - modifies original list & also returns it |
Eternal ID of this version: | #1002136/14 |
Text MD5: | a65fcd26472907746a98534ccb854771 |
Transpilation MD5: | 1464596fcf011d4b3ae286e117e86bca |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-05-26 12:05:32 |
Source code size: | 943 bytes / 34 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 788 / 2397 |
Version history: | 13 change(s) |
Referenced in: | [show references] |