Skip to content

Commit

Permalink
feat: 🎸 implement child removal function for tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jul 28, 2024
1 parent 05d615f commit 1facbe6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/main/kotlin/tw/xcc/gumtree/model/BasicTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ abstract class BasicTree<T> : Tree, Traversable<T> where T : BasicTree<T> {
}
}

fun addChild(child: T) {
open fun addChild(child: T) {
synchronized(this) {
val newChildrenMap = childrenMap.get()
newChildrenMap[newChildrenMap.size] = child.also { it.setParentTo(self) }
Expand Down
22 changes: 22 additions & 0 deletions core/src/main/kotlin/tw/xcc/gumtree/model/GumTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ open class GumTree(
child: GumTree
) = insertChildAtImpl(pos, child)

open fun tryRemoveChild(child: GumTree): Boolean =
synchronized(this) {
val pos = child.positionOfParent
if (pos == -1) {
return false
}

val children = childrenMap.get()
val removedChild = children.remove(pos) ?: return false

removedChild.setParentTo(null)
removedChild._positionOfParent.set(-1)
childrenMap.set(children)

return true
}

open fun toNewFrozen(): GumTreeView =
synchronized(this) {
val children = childrenMap.get()
Expand All @@ -88,6 +105,11 @@ open class GumTree(

infix fun hasSameLabelAs(other: GumTree): Boolean = label == other.label

override fun addChild(child: GumTree) {
super.addChild(child)
child._positionOfParent.set(childCount() - 1)
}

final override infix fun isIsomorphicTo(other: GumTree): Boolean = isIsomorphicTo(this, other)

final override infix fun isIsoStructuralTo(other: GumTree): Boolean = isIsoStructuralTo(this, other)
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/kotlin/tw/xcc/gumtree/model/GumTreeView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class GumTreeView private constructor(target: GumTree) : GumTree(target) {
throw NoSuchMethodException("calling this method in GumTreeView is not allowed")
}

override fun addChild(child: GumTree) {
throw NoSuchMethodException("calling this method in GumTreeView is not allowed")
}

override fun tryRemoveChild(child: GumTree): Boolean {
throw NoSuchMethodException("calling this method in GumTreeView is not allowed")
}

override fun inOrdered(): List<GumTree> = throw NotImplementedError()

companion object {
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/kotlin/GumTreeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,20 @@ internal class GumTreeTest {
assertEquals(child.id, frozen.childAt(0)?.id)
assertTrue(child !== frozen.childAt(0))
}

@Test
fun `test tryRemoveChild`() {
val child = GumTree(TreeType.empty(), "child")
givenTree.addChild(child)
val actualResult = givenTree.tryRemoveChild(child)
assertTrue(actualResult)
assertEquals(0, givenTree.childCount())
}

@Test
fun `test tryRemoveChild with non-child`() {
val child = GumTree(TreeType.empty(), "child")
val actualResult = givenTree.tryRemoveChild(child)
assertFalse(actualResult)
}
}

0 comments on commit 1facbe6

Please sign in to comment.