Skip to content

Commit 1facbe6

Browse files
feat: 🎸 implement child removal function for tree
1 parent 05d615f commit 1facbe6

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

‎core/src/main/kotlin/tw/xcc/gumtree/model/BasicTree.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ abstract class BasicTree<T> : Tree, Traversable<T> where T : BasicTree<T> {
6868
}
6969
}
7070

71-
fun addChild(child: T) {
71+
open fun addChild(child: T) {
7272
synchronized(this) {
7373
val newChildrenMap = childrenMap.get()
7474
newChildrenMap[newChildrenMap.size] = child.also { it.setParentTo(self) }

‎core/src/main/kotlin/tw/xcc/gumtree/model/GumTree.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ open class GumTree(
7676
child: GumTree
7777
) = insertChildAtImpl(pos, child)
7878

79+
open fun tryRemoveChild(child: GumTree): Boolean =
80+
synchronized(this) {
81+
val pos = child.positionOfParent
82+
if (pos == -1) {
83+
return false
84+
}
85+
86+
val children = childrenMap.get()
87+
val removedChild = children.remove(pos) ?: return false
88+
89+
removedChild.setParentTo(null)
90+
removedChild._positionOfParent.set(-1)
91+
childrenMap.set(children)
92+
93+
return true
94+
}
95+
7996
open fun toNewFrozen(): GumTreeView =
8097
synchronized(this) {
8198
val children = childrenMap.get()
@@ -88,6 +105,11 @@ open class GumTree(
88105

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

108+
override fun addChild(child: GumTree) {
109+
super.addChild(child)
110+
child._positionOfParent.set(childCount() - 1)
111+
}
112+
91113
final override infix fun isIsomorphicTo(other: GumTree): Boolean = isIsomorphicTo(this, other)
92114

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

‎core/src/main/kotlin/tw/xcc/gumtree/model/GumTreeView.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ class GumTreeView private constructor(target: GumTree) : GumTree(target) {
4141
throw NoSuchMethodException("calling this method in GumTreeView is not allowed")
4242
}
4343

44+
override fun addChild(child: GumTree) {
45+
throw NoSuchMethodException("calling this method in GumTreeView is not allowed")
46+
}
47+
48+
override fun tryRemoveChild(child: GumTree): Boolean {
49+
throw NoSuchMethodException("calling this method in GumTreeView is not allowed")
50+
}
51+
4452
override fun inOrdered(): List<GumTree> = throw NotImplementedError()
4553

4654
companion object {

‎core/src/test/kotlin/GumTreeTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,20 @@ internal class GumTreeTest {
142142
assertEquals(child.id, frozen.childAt(0)?.id)
143143
assertTrue(child !== frozen.childAt(0))
144144
}
145+
146+
@Test
147+
fun `test tryRemoveChild`() {
148+
val child = GumTree(TreeType.empty(), "child")
149+
givenTree.addChild(child)
150+
val actualResult = givenTree.tryRemoveChild(child)
151+
assertTrue(actualResult)
152+
assertEquals(0, givenTree.childCount())
153+
}
154+
155+
@Test
156+
fun `test tryRemoveChild with non-child`() {
157+
val child = GumTree(TreeType.empty(), "child")
158+
val actualResult = givenTree.tryRemoveChild(child)
159+
assertFalse(actualResult)
160+
}
145161
}

0 commit comments

Comments
 (0)