-
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.
add familyTree and descdentants counting
- Loading branch information
1 parent
967f979
commit f5503af
Showing
10 changed files
with
147 additions
and
96 deletions.
There are no files selected for viewing
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
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
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,32 @@ | ||
package frontend.simulation | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import java.util.* | ||
|
||
class FamilyTree private constructor( | ||
private val id: UUID? = null, | ||
private val children: MutableStateFlow<Set<FamilyTree>> = MutableStateFlow(setOf()), | ||
) { | ||
constructor(children: List<UUID>) : this(children = MutableStateFlow(children.map(::FamilyTree).toSet())) | ||
|
||
fun add(childId: UUID, vararg parentIds: UUID) = FamilyTree(id = childId).let { child -> | ||
parentIds.forEach { id -> | ||
find(id)?.children?.update { it + child } //shit, it can be null | ||
} | ||
} | ||
|
||
fun find(id: UUID): FamilyTree? = if (this.id == id) this else children.value.firstNotNullOfOrNull { it.find(id) } | ||
|
||
val descendants: Int | ||
get() { | ||
tailrec fun loop(visited: Set<FamilyTree>, acc: Set<FamilyTree>): Int = | ||
if (acc.isEmpty()) visited.size | ||
else { | ||
val current = acc.first() | ||
loop(visited + current, acc - current + current.children.value) | ||
} | ||
return loop(setOf(), children.value) | ||
} | ||
|
||
} |
Oops, something went wrong.