Skip to content

Commit

Permalink
refactor: 💡 re-define certain interfaces of models
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jul 24, 2024
1 parent f656319 commit 6787677
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
10 changes: 4 additions & 6 deletions core/src/main/kotlin/tw/xcc/gumtree/api/tree/PriorityTreeList.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package tw.xcc.gumtree.api.tree

import tw.xcc.gumtree.model.BasicTree

internal interface PriorityTreeList<T : BasicTree<T>> {
internal interface PriorityTreeList<in T : Tree> {
/**
* Inserts the node n in the list.
* */
Expand All @@ -14,15 +12,15 @@ internal interface PriorityTreeList<T : BasicTree<T>> {
fun open(tree: T)

/**
* Returns and removes the set of all nodes which has a priority equals to [peekMax]
* Returns and removes the set of all nodes that has a priority equals to [peekMax]
* */
fun pop(): List<T>
fun pop(): List<Tree>

/**
* Returns and removes the set of all nodes which has a priority equals to [peekMax],
* and inserts all the children of these nodes in the list.
* */
fun popOpen(): List<T>
fun popOpen(): List<Tree>

/**
* Returns the highest priority value of the list.
Expand Down
27 changes: 27 additions & 0 deletions core/src/main/kotlin/tw/xcc/gumtree/api/tree/TreeMappingStorage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tw.xcc.gumtree.api.tree

internal interface TreeMappingStorage<in T : Tree> {
fun addMappingOf(mapping: Pair<T, T>)

fun addMappingRecursivelyOf(mapping: Pair<T, T>)

fun removeMappingOf(mapping: Pair<T, T>)

fun getMappingOfLeft(left: T): Tree?

fun getMappingOfRight(right: T): Tree?

fun isLeftMapped(left: T): Boolean

fun isAnyOfLeftsUnMapped(lefts: Iterable<T>): Boolean

fun isRightMapped(right: T): Boolean

fun isAnyOfRightsUnMapped(rights: Iterable<T>): Boolean

fun areBothUnMapped(mapping: Pair<T, T>): Boolean

fun hasUnMappedDescendentOfLeft(left: T): Boolean

fun hasUnMappedDescendentOfRight(right: T): Boolean
}
27 changes: 14 additions & 13 deletions core/src/main/kotlin/tw/xcc/gumtree/model/MappingStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import tw.xcc.gumtree.api.tree.TreeMappingStorage

/**
* The storage for saving the references of the mapping between two GumTrees. (Left and Right)
* */
class MappingStorage {
class MappingStorage : TreeMappingStorage<GumTree> {
private val mappingLR = mutableMapOf<GumTree, GumTree>()
private val mappingRL = mutableMapOf<GumTree, GumTree>()

Expand Down Expand Up @@ -97,62 +98,62 @@ class MappingStorage {
!mappingLR.containsKey(mapping.first) && !mappingRL.containsKey(mapping.second)
}

fun addMappingOf(mapping: Pair<GumTree, GumTree>) =
override fun addMappingOf(mapping: Pair<GumTree, GumTree>) =
runBlocking(Dispatchers.Default) {
addMappingImpl(mapping)
}

fun addMappingRecursivelyOf(mapping: Pair<GumTree, GumTree>) =
override fun addMappingRecursivelyOf(mapping: Pair<GumTree, GumTree>) =
runBlocking(Dispatchers.Default) {
addMappingRecursivelyImpl(mapping)
}

fun removeMappingOf(mapping: Pair<GumTree, GumTree>) =
override fun removeMappingOf(mapping: Pair<GumTree, GumTree>) =
runBlocking(Dispatchers.Default) {
removeMappingImpl(mapping)
}

fun getMappingOfLeft(left: GumTree): GumTree? =
override fun getMappingOfLeft(left: GumTree): GumTree? =
runBlocking(Dispatchers.Default) {
extractMappedTreeOf(left, mappingLR)
}

fun getMappingOfRight(right: GumTree): GumTree? =
override fun getMappingOfRight(right: GumTree): GumTree? =
runBlocking(Dispatchers.Default) {
extractMappedTreeOf(right, mappingRL)
}

fun isLeftMapped(left: GumTree): Boolean =
override fun isLeftMapped(left: GumTree): Boolean =
runBlocking(Dispatchers.Default) {
isMappingExistsIn(mappingLR, left)
}

fun isAnyOfLeftsUnMapped(lefts: Iterable<GumTree>): Boolean =
override fun isAnyOfLeftsUnMapped(lefts: Iterable<GumTree>): Boolean =
runBlocking(Dispatchers.Default) {
isAnyTreeNotExistsIn(mappingLR, lefts)
}

fun isRightMapped(right: GumTree): Boolean =
override fun isRightMapped(right: GumTree): Boolean =
runBlocking(Dispatchers.Default) {
isMappingExistsIn(mappingRL, right)
}

fun isAnyOfRightsUnMapped(rights: Iterable<GumTree>): Boolean =
override fun isAnyOfRightsUnMapped(rights: Iterable<GumTree>): Boolean =
runBlocking(Dispatchers.Default) {
isAnyTreeNotExistsIn(mappingRL, rights)
}

fun areBothUnMapped(mapping: Pair<GumTree, GumTree>): Boolean =
override fun areBothUnMapped(mapping: Pair<GumTree, GumTree>): Boolean =
runBlocking(Dispatchers.Default) {
areBothUnMappedImpl(mapping)
}

fun hasUnMappedDescendentOfLeft(left: GumTree): Boolean =
override fun hasUnMappedDescendentOfLeft(left: GumTree): Boolean =
runBlocking(Dispatchers.Default) {
hasUnMappedDescendent(left, mappingLR)
}

fun hasUnMappedDescendentOfRight(right: GumTree): Boolean =
override fun hasUnMappedDescendentOfRight(right: GumTree): Boolean =
runBlocking(Dispatchers.Default) {
hasUnMappedDescendent(right, mappingRL)
}
Expand Down

0 comments on commit 6787677

Please sign in to comment.