Libraryless. Click here for Pure Java version (3656L/23K).
// Note: as nodes are in hash maps, order of pair replacement is rather undefined sclass LineComp_LinkedList_v2 { sclass Node { int value; Node prev, next; int a() { ret value; } int b() { ret next == null ? -1 : next.value; } IntPair pair() { ret next == null ? null : IntPair(a(), b()); } } LineComp_PairIndex pairIndex; Node tail; new MultiSetMap<Int, Node> indexByElement; bool verbose; *() {} *(LineComp_PairIndex *pairIndex, 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); } }
Began life as a copy of #1028244
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: | #1028246 |
Snippet name: | LineComp_LinkedList_v2 (dev.) |
Eternal ID of this version: | #1028246/4 |
Text MD5: | 309f896fe76cf2846447cdb4f8db8462 |
Transpilation MD5: | 868deca5c78b22a46d918d56cd01cc31 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-05-28 01:42:23 |
Source code size: | 2950 bytes / 97 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 195 / 279 |
Version history: | 3 change(s) |
Referenced in: | [show references] |