-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 implement tree similarity comparison methods
- Loading branch information
1 parent
07e63c9
commit b62cec8
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
core/src/main/kotlin/tw/xcc/gumtree/model/CompareHelper.kt
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,41 @@ | ||
package tw.xcc.gumtree.model | ||
|
||
import tw.xcc.gumtree.api.tree.Comparable | ||
|
||
class CompareHelper(private val tree: GumTree) : Comparable<GumTree> { | ||
override fun isIsomorphicTo(other: GumTree): Boolean = | ||
compareTrees(other, true) { child, otherChild -> | ||
child isIsomorphicTo otherChild | ||
} | ||
|
||
override fun isIsoStructuralTo(other: GumTree): Boolean = | ||
compareTrees(other, false) { child, otherChild -> | ||
child isIsoStructuralTo otherChild | ||
} | ||
|
||
private inline fun compareTrees( | ||
other: GumTree, | ||
shouldCheckLabel: Boolean, | ||
childComparison: (GumTree, GumTree) -> Boolean | ||
): Boolean { | ||
synchronized(tree) { | ||
if (!(tree hasSameTypeAs other && tree.childCount() == other.childCount())) { | ||
return false | ||
} | ||
|
||
if (shouldCheckLabel && !(tree hasSameLabelAs other)) { | ||
return false | ||
} | ||
|
||
for (i in 0 until tree.childCount()) { | ||
val child = tree.childAt(i) ?: return false | ||
val otherChild = other.childAt(i) ?: return false | ||
if (!childComparison(child, otherChild)) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
} | ||
} |
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
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,5 @@ | ||
package tw.xcc.gumtree.model | ||
|
||
data class TreeType(val name: String) { | ||
fun isEmpty(): Boolean = name.isEmpty() | ||
} |