This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvHeapCycleDetector.groovy
140 lines (123 loc) · 3.57 KB
/
csvHeapCycleDetector.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import org.apache.commons.csv.CSVParser;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
public class CsvHeapCycleRemover {
public static void main(String[] args) {
BufferedReader br = null;
Multimap<String, String> parentToChildrenMap = HashMultimap.create();
Set<String> nonRootNodesSet = new HashSet<String>();
try {
br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) {
//System.err.println("main(): line = " + line);
try {
CSVParser p = new CSVParser(new StringReader(line));
String[] line2 = p.getLine();
String parent = line2[0];
String child = line2[1];
parentToChildrenMap.put(parent, child);
if (nonRootNodesSet.contains(child)) {
System.err.println("Already has a parent: " + child);
System.err.println("Removed: " + line);
} else {
System.out.println(line);
}
nonRootNodesSet.add(child);
} catch (Exception e) {
System.err.println("[ERROR] Invaline line: " + line);
continue;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void checkForCycles(String root,
Multimap<String, String> parentToChildrenMap, Set<String> visited) {
Collection<String> children = parentToChildrenMap.get(root);
for (String child : children) {
if (visited.contains(child)) {
throw new RuntimeException("Child of itself: " + child);
} else {
visited.add(child);
}
checkForCycles(child, parentToChildrenMap, visited);
}
}
private static Set<TreeNode> getLeafNodes(TreeNode rootNode) {
Set<TreeNode> leafNodes1 = new HashSet<TreeNode>();
getLeafNodes(rootNode, leafNodes1);
return ImmutableSet.copyOf(leafNodes1);
}
// TODO: Bad - mutable input
private static void getLeafNodes(TreeNode rootNode, Set<TreeNode> oLeafNodes) {
if (rootNode.getChild().size() == 0) {
oLeafNodes.add(rootNode);
} else {
for (TreeNode child : rootNode.getChild()) {
getLeafNodes(child, oLeafNodes);
}
}
}
private static TreeNode buildTree(String root, Multimap<String, String> m) {
Set<TreeNode> childNodes = new HashSet<TreeNode>();
for (String childNodeData : m.get(root)) {
System.err.println("buildTree() - " + root);
TreeNode childNode = buildTree(childNodeData, m);
childNodes.add(childNode);
}
TreeNode n = new TreeNode(root, childNodes);
return n;
}
private static class TreeNode {
private final String data;
private final Set<TreeNode> child;
// TODO: can we make this final?
private TreeNode parent;
TreeNode(String data, Set<TreeNode> child) {
this.data = data;
this.child = ImmutableSet.copyOf(child);
for (TreeNode child1 : child) {
child1.setParent(this);
}
}
public void setParent(TreeNode parent1) {
if (parent == null) {
parent = parent1;
} else {
throw new RuntimeException(parent.getData());
}
}
public TreeNode getParent() {
return parent;
}
public String getData() {
return data;
}
public Set<TreeNode> getChild() {
return child;
}
}
}