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

92
LINES

< > BotCompany Repo | #1028244 // LineComp_LinkedList - for efficient pair replacement

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

Libraryless. Click here for Pure Java version (3459L/21K).

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

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: 301 / 686
Version history: 27 change(s)
Referenced in: [show references]