Skip to content

Commit

Permalink
Depth algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Feb 7, 2024
1 parent 944c6c3 commit d3c4188
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3 deletions.
72 changes: 72 additions & 0 deletions src/main/java/org/cqfn/astranaut/core/algorithms/Depth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Ivan Kniazkov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.astranaut.core.algorithms;

import java.util.HashMap;
import java.util.Map;
import org.cqfn.astranaut.core.Node;

/**
* Algorithm for measuring the depth of syntax trees.
*
* @since 1.1.0
*/
public final class Depth {
/**
* A table with the calculated values.
* Since nodes are immutable, depth calculated once for a node will never change.
*/
private final Map<Node, Integer> calculated;

/**
* Constructor.
*/
public Depth() {
this.calculated = new HashMap<>();
}

/**
* Counts the depth of the subtree. For a node that has no children, the depth is 1.
* @param node The root node of the subtree
* @return Calculated depth
*/
public int calculate(final Node node) {
final int depth;
if (this.calculated.containsKey(node)) {
depth = this.calculated.get(node);
} else {
int max = 0;
final int count = node.getChildCount();
for (int index = 0; index < count; index = index + 1) {
final int value = this.calculate(node.getChild(index));
if (max < value) {
max = value;
}
}
depth = 1 + max;
this.calculated.put(node, depth);
}
return depth;
}
}
46 changes: 46 additions & 0 deletions src/test/java/org/cqfn/astranaut/core/algorithms/DepthTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Ivan Kniazkov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.astranaut.core.algorithms;

import org.cqfn.astranaut.core.Node;
import org.cqfn.astranaut.core.example.LittleTrees;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Testing {@link Depth} class.
*
* @since 1.1.0
*/
class DepthTest {
@Test
void test() {
final Node tree = LittleTrees.createTreeWithDeleteAction();
final Depth depth = new Depth();
int value = depth.calculate(tree);
Assertions.assertEquals(5, value);
value = depth.calculate(tree.getChild(0));
Assertions.assertEquals(3, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ class AbsoluteHashTest {
*/
@Test
void testingEqualityFeature() {
final Hash hash = new AbsoluteHash();
final Node first = LittleTrees.createTreeWithDeleteAction();
final Node second = LittleTrees.createTreeWithDeleteAction();
final Hash hash = new AbsoluteHash();
final int expected = hash.calculate(first);
final int actual = hash.calculate(second);
int expected = hash.calculate(first);
int actual = hash.calculate(second);
Assertions.assertEquals(expected, actual);
expected = hash.calculate(first.getChild(0));
actual = hash.calculate(second.getChild(0));
Assertions.assertEquals(expected, actual);
}
}

0 comments on commit d3c4188

Please sign in to comment.