From 5531451c277af5959d52bbbdffa387b05ea00382 Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Tue, 27 Feb 2024 09:31:11 +0300 Subject: [PATCH] A little optimization. --- .../algorithms/mapping/BottomUpAlgorithm.java | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/cqfn/astranaut/core/algorithms/mapping/BottomUpAlgorithm.java b/src/main/java/org/cqfn/astranaut/core/algorithms/mapping/BottomUpAlgorithm.java index f4bc172..6dafa8f 100644 --- a/src/main/java/org/cqfn/astranaut/core/algorithms/mapping/BottomUpAlgorithm.java +++ b/src/main/java/org/cqfn/astranaut/core/algorithms/mapping/BottomUpAlgorithm.java @@ -62,6 +62,11 @@ class BottomUpAlgorithm { */ private final Map parents; + /** + * A set of nodes that have not yet been processed. + */ + private final Set unprocessed; + /** * Sorted nodes from the 'left' tree. */ @@ -101,6 +106,7 @@ class BottomUpAlgorithm { this.hashes = new AbsoluteHash(); this.depth = new Depth(); this.parents = new HashMap<>(); + this.unprocessed = new HashSet<>(); this.left = this.createNodeList(left); this.right = this.createNodeList(right); this.ltr = new HashMap<>(); @@ -153,6 +159,8 @@ private void createNodeList(final Node node, final Node parent, final List this.parents.put(node, parent); node.forEachChild(child -> this.createNodeList(child, node, list)); list.add(node); + final boolean added = this.unprocessed.add(node); + assert added; } /** @@ -194,9 +202,9 @@ private Map> performInitialMapping() { private void absorbLargestSubtrees(final Map> draft) { final List sorted = new ArrayList<>(draft.keySet()); sorted.sort( - (first, second) -> -Integer.compare( - this.depth.calculate(first), - this.depth.calculate(second) + (first, second) -> Integer.compare( + this.depth.calculate(second), + this.depth.calculate(first) ) ); for (final Node node : sorted) { @@ -223,8 +231,8 @@ private void mapSubtreesWithTheSameHash( final Map> draft) { assert this.hashes.calculate(node) == this.hashes.calculate(related); draft.remove(node); - this.left.remove(node); - this.right.remove(related); + this.unprocessed.remove(node); + this.unprocessed.remove(related); this.ltr.put(node, related); this.rtl.put(related, node); final int count = node.getChildCount(); @@ -245,12 +253,14 @@ private Node findPartiallyMappedLeftNode() { final Iterator iterator = this.left.iterator(); while (result == null && iterator.hasNext()) { final Node node = iterator.next(); - final int count = node.getChildCount(); - for (int index = 0; index < count; index = index + 1) { - final Node child = node.getChild(index); - if (this.ltr.containsKey(child)) { - result = node; - break; + if (this.unprocessed.contains(node)) { + final int count = node.getChildCount(); + for (int index = 0; index < count; index = index + 1) { + final Node child = node.getChild(index); + if (this.ltr.containsKey(child)) { + result = node; + break; + } } } } @@ -282,13 +292,13 @@ private Node mapPartiallyMappedLeftNode(final Node node) { || !node.getData().equals(related.getData())) { break; } - this.right.remove(related); + this.unprocessed.remove(related); this.ltr.put(node, related); this.rtl.put(related, node); this.mapChildren(node, related); next = this.parents.get(node); } while (false); - this.left.remove(node); + this.unprocessed.remove(node); return next; } @@ -321,8 +331,8 @@ private void mapChildrenIfReplaced(final Node before, final Node after) { if (!this.ltr.containsKey(first)) { final Node second = after.getChild(index); this.replaced.put(first, second); - this.left.remove(first); - this.right.remove(second); + this.unprocessed.remove(first); + this.unprocessed.remove(second); } } } @@ -339,7 +349,7 @@ private void mapChildrenIfDeleted(final Node before) { final Node child = before.getChild(index); if (!this.ltr.containsKey(child)) { this.deleted.add(child); - this.left.remove(child); + this.unprocessed.remove(child); } } }