Libraryless. Click here for Pure Java version (3459L/21K).
// Note: as nodes are in hash maps, order of pair replacement is rather undefined sclass LineComp_LinkedList { sclass Node { int value; Node prev, next; } Node tail; new MultiSetMap<Int, Node> indexByElement; bool verbose; *() {} *(L<Int> l) { fOr (int i : l) add(i); } void add(int i) { new Node n; n.value = i; n.prev = tail; if (tail != null) tail.next = n; tail = n; indexByElement.put(i, n); } void replacePair(int pa, int pb, int replaceWith, LineComp_PairCounts pairCounts) { Set<Node> setA = indexByElement.get(pa); if (setA == null) ret; Set<Node> setB = indexByElement.get(pb); if (setB == null) ret; bool change; if (setA.size() <= setB.size()) for (Node na : cloneList(setA)) { continue if na.value < 0; // node might have been deleted Node nb = na.next; if (nb != null && nb.value == pb) { if (verbose && !change) print("Before replacement: " + toList()); replacePair(na, replaceWith, pairCounts); set change; } } else for (Node nb : cloneList(setB)) { continue if nb.value < 0; // node might have been deleted Node na = nb.prev; if (na != null && na.value == pa) { if (verbose && !change) print("Before replacement: " + toList()); replacePair(na, replaceWith, pairCounts); set change; } } if (verbose && change) print("After replacement: " + toList()); } L<Int> toList() { new L<Int> l; Node node = tail; while (node != null) { l.add(node.value); node = node.prev; } ret reverseInPlace(l); } void deleteNode(Node node) { indexByElement.remove(node.value, node); if (node.next != null) node.next.prev = node.prev; else tail = node.prev; if (node.prev != null) node.prev.next = node.next; node.value = -1; // mark invalid } void replacePair(Node node, int replaceWith, LineComp_PairCounts pairCounts) { Node nb = node.next; int pa = node.value, pb = nb.value; if (node.prev != null) pairCounts.remove(node.prev.value, pa); pairCounts.remove(pa, pb); if (nb.next != null) pairCounts.remove(pb, nb.next.value); setValue(node, replaceWith); deleteNode(nb); if (node.prev != null) pairCounts.add(node.prev.value, replaceWith); if (node.next != null) pairCounts.add(replaceWith, node.next.value); } void setValue(Node node, int value) { indexByElement.remove(node.value, node); node.value = value; indexByElement.add(node.value, node); } }
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1028244 |
Snippet name: | LineComp_LinkedList - for efficient pair replacement |
Eternal ID of this version: | #1028244/28 |
Text MD5: | a6e487b9c2e5bcfd7ddfa059d162d15b |
Transpilation MD5: | e67b8a663338936788ef3d10a0cb0d3a |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-05-27 23:23:10 |
Source code size: | 2727 bytes / 92 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 302 / 686 |
Version history: | 27 change(s) |
Referenced in: | #1028246 - LineComp_LinkedList_v2 (dev.) #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) |