Skip to content

Commit 116679d

Browse files
committed
simplify union
1 parent 8f613b6 commit 116679d

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

javascript/extractor/src/com/semmle/js/extractor/CFGExtractor.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@
114114
import java.util.Collections;
115115
import java.util.function.IntFunction;
116116
import java.util.HashMap;
117+
import java.util.HashSet;
117118
import java.util.Iterator;
118119
import java.util.LinkedHashMap;
119120
import java.util.LinkedHashSet;
120121
import java.util.List;
121122
import java.util.Map;
122123
import java.util.Set;
123124
import java.util.Stack;
125+
import java.util.stream.Collectors;
124126

125127
/**
126128
* Extractor for intra-procedural expression-level control flow graphs.
@@ -228,33 +230,18 @@ private static List<Node> flattenNestedList(Collection<?> lists, List<Node> outp
228230

229231
/**
230232
* Creates an order preserving concatenation of the nodes in `xs` and `ys` without duplicates.
231-
* Returns `null`, or an `Node` or a `List<Node>`.
233+
* Returns `null`, or an `Node`, or a `List<Node>`.
232234
*/
235+
@SuppressWarnings("unchecked")
233236
private static Object union(Object xs, Object ys) {
234-
if (xs == null) return ys;
235-
if (ys == null) return xs;
236-
if (xs instanceof List<?>) {
237-
@SuppressWarnings("unchecked")
238-
List<Node> xsCopy = new ArrayList<Node>((List<Node>) xs);
239-
for (Node y : createCollection(ys)) if (!xsCopy.contains(y)) xsCopy.add(y);
240-
return xsCopy;
237+
Set<Node> set = new HashSet<>();
238+
List<Node> result = flattenNestedList(Arrays.asList(xs, ys)).stream().filter(set::add).collect(Collectors.toList());
239+
if (result.size() == 0) {
240+
return null;
241+
} else if (result.size() == 1) {
242+
return result.get(0);
241243
} else {
242-
if (ys instanceof List<?>) {
243-
@SuppressWarnings("unchecked")
244-
List<Object> lys = (List<Object>) ys;
245-
if (!lys.contains(xs)) {
246-
lys = new ArrayList<Object>(lys);
247-
lys.add(0, xs);
248-
}
249-
return lys;
250-
} else if (xs == ys) {
251-
return xs;
252-
} else {
253-
List<Node> res = new ArrayList<Node>(2);
254-
res.add((Node) xs);
255-
res.add((Node) ys);
256-
return res;
257-
}
244+
return result;
258245
}
259246
}
260247

0 commit comments

Comments
 (0)