-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/java/org/cqfn/astranaut/core/algorithms/Depth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
src/test/java/org/cqfn/astranaut/core/algorithms/DepthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters