Libraryless. Click here for Pure Java version (3656L/23K).
1 | // Note: as nodes are in hash maps, order of pair replacement is rather undefined |
2 | sclass LineComp_LinkedList_v2 { |
3 | sclass Node { |
4 | int value; |
5 | Node prev, next; |
6 | |
7 | int a() { ret value; } |
8 | int b() { ret next == null ? -1 : next.value; } |
9 | IntPair pair() { ret next == null ? null : IntPair(a(), b()); } |
10 | } |
11 | |
12 | LineComp_PairIndex pairIndex; |
13 | Node tail; |
14 | new MultiSetMap<Int, Node> indexByElement; |
15 | bool verbose; |
16 | |
17 | *() {} |
18 | *(LineComp_PairIndex *pairIndex, L<Int> l) { |
19 | fOr (int i : l) add(i); |
20 | } |
21 | |
22 | void add(int i) { |
23 | new Node n; |
24 | n.value = i; |
25 | n.prev = tail; |
26 | if (tail != null) tail.next = n; |
27 | tail = n; |
28 | indexByElement.put(i, n); |
29 | } |
30 | |
31 | void replacePair(int pa, int pb, int replaceWith, LineComp_PairCounts pairCounts) { |
32 | Set<Node> setA = indexByElement.get(pa); |
33 | if (setA == null) ret; |
34 | Set<Node> setB = indexByElement.get(pb); |
35 | if (setB == null) ret; |
36 | bool change; |
37 | if (setA.size() <= setB.size()) |
38 | for (Node na : cloneList(setA)) { |
39 | continue if na.value < 0; // node might have been deleted |
40 | Node nb = na.next; |
41 | if (nb != null && nb.value == pb) { |
42 | if (verbose && !change) print("Before replacement: " + toList()); |
43 | replacePair(na, replaceWith, pairCounts); |
44 | set change; |
45 | } |
46 | } |
47 | else |
48 | for (Node nb : cloneList(setB)) { |
49 | continue if nb.value < 0; // node might have been deleted |
50 | Node na = nb.prev; |
51 | if (na != null && na.value == pa) { |
52 | if (verbose && !change) print("Before replacement: " + toList()); |
53 | replacePair(na, replaceWith, pairCounts); |
54 | set change; |
55 | } |
56 | } |
57 | if (verbose && change) print("After replacement: " + toList()); |
58 | } |
59 | |
60 | L<Int> toList() { |
61 | new L<Int> l; |
62 | Node node = tail; |
63 | while (node != null) { |
64 | l.add(node.value); |
65 | node = node.prev; |
66 | } |
67 | ret reverseInPlace(l); |
68 | } |
69 | |
70 | void deleteNode(Node node) { |
71 | indexByElement.remove(node.value, node); |
72 | if (node.next != null) node.next.prev = node.prev; else tail = node.prev; |
73 | if (node.prev != null) node.prev.next = node.next; |
74 | node.value = -1; // mark invalid |
75 | } |
76 | |
77 | void replacePair(Node node, int replaceWith, LineComp_PairCounts pairCounts) { |
78 | Node nb = node.next; |
79 | int pa = node.value, pb = nb.value; |
80 | |
81 | if (node.prev != null) pairCounts.remove(node.prev.value, pa); |
82 | pairCounts.remove(pa, pb); |
83 | if (nb.next != null) pairCounts.remove(pb, nb.next.value); |
84 | setValue(node, replaceWith); |
85 | |
86 | deleteNode(nb); |
87 | |
88 | if (node.prev != null) pairCounts.add(node.prev.value, replaceWith); |
89 | if (node.next != null) pairCounts.add(replaceWith, node.next.value); |
90 | } |
91 | |
92 | void setValue(Node node, int value) { |
93 | indexByElement.remove(node.value, node); |
94 | node.value = value; |
95 | indexByElement.add(node.value, node); |
96 | } |
97 | } |
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: | 197 / 282 |
Version history: | 3 change(s) |
Referenced in: | [show references] |