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

34
LINES

< > BotCompany Repo | #1002136 // replaceSublist - modifies original list & also returns it

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

Libraryless. Click here for Pure Java version (2532L/16K).

// syntax 1: replace all occurrences of x in l with y
static <A> L<A> replaceSublist(L<A> l, L<A> x, L<A> 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 <A> L<A> replaceSublist(L<A> l, int fromIndex, int toIndex, L<A> 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 <A> L<A> replaceSublist(L<A> l, IntRange r, L<A> y) {
  ret replaceSublist(l, r.start, r.end, y);
}
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: 776 / 2380
Version history: 13 change(s)
Referenced in: [show references]