Libraryless. Click here for Pure Java version (3801L/24K).
1 | sclass LineComp_PairIndex { |
2 | bool verbose; |
3 | bool inOrder = true; |
4 | MultiSetMap<IntPair, Node> nodes; |
5 | MultiSetMap<Int, IntPair> byCount |
6 | //= multiSetMap_innerHashSet_outerRevTreeMap(); // makes a difference in selecting a pair if there are multiple with the same count |
7 | = multiSetMap_innerCompactHashSet_outerRevTreeMap(); // same but more compact |
8 | //= multiSetMap_innerTreeSet_outerRevTreeMap(); // initial version that kind of pointlessly sorts the same-count nodes lexicographically |
9 | new LinkedHashMap<S, Ch> chains; |
10 | |
11 | void init { |
12 | nodes = inOrder |
13 | ? multiSetMap_innerLinkedHashSet_outerHashMap() |
14 | : multiSetMap_innerHashSet_outerHashMap(); |
15 | } |
16 | |
17 | // a "chain" of nodes (one input file) |
18 | sclass Ch { |
19 | Node tail; |
20 | |
21 | *(LineComp_PairIndex pi, L<Int> l) { |
22 | fOr (int i : l) add(pi, i); |
23 | } |
24 | |
25 | L<Int> toList() { |
26 | new L<Int> l; |
27 | Node node = tail; |
28 | while (node != null) { |
29 | l.add(node.value); |
30 | node = node.prev; |
31 | } |
32 | ret reverseInPlace(l); |
33 | } |
34 | |
35 | void add(LineComp_PairIndex pi, int i) { |
36 | new Node n; |
37 | n.ch = this; |
38 | n.value = i; |
39 | n.prev = tail; |
40 | if (tail != null) tail.next = n; |
41 | tail = n; |
42 | pi.addToIndex(n.prev); |
43 | } |
44 | } |
45 | |
46 | sclass Node { |
47 | Ch ch; |
48 | int value; |
49 | Node prev, next; |
50 | |
51 | int a() { ret value; } |
52 | int b() { ret next == null ? -1 : next.value; } |
53 | IntPair pair() { ret next == null ? null : IntPair(a(), b()); } |
54 | } |
55 | |
56 | IntPair nodeToPair(Node n) { |
57 | ret n?.pair(); |
58 | } |
59 | |
60 | // add node to pair index |
61 | void addToIndex(Node n) { |
62 | IntPair p = nodeToPair(n); |
63 | if (p == null) ret; |
64 | int count = nodes.getSize(p); |
65 | nodes.add(p, n); |
66 | if (count != 0) byCount.remove(count, p); |
67 | byCount.put(count+1, p); |
68 | } |
69 | |
70 | // remove node from pair index |
71 | void removeFromIndex(Node n) { |
72 | IntPair p = nodeToPair(n); |
73 | if (p == null) ret; |
74 | int count = nodes.getSize(p); |
75 | if (count == 0) fail("Can't remove pair " + p); |
76 | nodes.remove(p, n); |
77 | byCount.remove(count, p); |
78 | if (--count > 0) byCount.put(count, p); |
79 | } |
80 | |
81 | IntPair mostPopularDuplicate() { |
82 | ret toInt(firstKey(byCount)) < 2 ? null : firstValue(byCount); |
83 | } |
84 | |
85 | int numberOfDuplicates() { |
86 | ret byCount.size()-l(byCount.get(1)); |
87 | } |
88 | |
89 | int getCount(IntPair p) { |
90 | ret nodes.getSize(p); |
91 | } |
92 | |
93 | void replacePair(int pa, int pb, int replaceWith) { |
94 | IntPair p = IntPair(pa, pb); |
95 | Set<Node> set = nodes.get(p); |
96 | for (Node n : cloneList(set)) { |
97 | continue if n.a() != pa || n.b() != pb; // nodes might have been deleted or changed |
98 | replacePair(n, replaceWith); |
99 | } |
100 | } |
101 | |
102 | void replacePair(Node node, int replaceWith) { |
103 | removeFromIndex(node.prev); |
104 | removeFromIndex(node); |
105 | removeFromIndex(node.next); |
106 | node.value = replaceWith; |
107 | deleteNode(node.next); |
108 | addToIndex(node.prev); |
109 | addToIndex(node); |
110 | } |
111 | |
112 | void deleteNode(Node node) { |
113 | if (node.next != null) node.next.prev = node.prev; else node.ch.tail = node.prev; |
114 | if (node.prev != null) node.prev.next = node.next; |
115 | node.value = -1; // mark invalid |
116 | } |
117 | |
118 | void newChain(S version, L<Int> encoding) { |
119 | chains.put(version, new Ch(this, encoding)); |
120 | } |
121 | } |
Began life as a copy of #1028247
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: | #1028508 |
Snippet name: | LineComp_PairIndex [backup before eclipse collections] |
Eternal ID of this version: | #1028508/1 |
Text MD5: | 41ef07c162e033300fc4d3edcefe9b8b |
Transpilation MD5: | da46147b056d3c12e97e7dbe0ef34623 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-06-23 11:54:10 |
Source code size: | 3327 bytes / 121 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 211 / 290 |
Referenced in: | [show references] |