All checks were successful
Test Workflow / Lint and test library (push) Successful in 3m56s
Contributes to #2 . Handle cyclic folders explicitly, instead of relying on the filesystem. Reviewed-on: #3
27 lines
873 B
Kotlin
27 lines
873 B
Kotlin
package filesystem
|
|
|
|
// Note sealed allows for simpler logic in FSCreator by guaranteeing FSFile and FSFolder are the only possible FSEntries
|
|
// (as we expect), and it also implicitly makes the class abstract as required.
|
|
sealed class FSEntry(val name: String)
|
|
|
|
class FSFile(name: String, val content: String) : FSEntry(name)
|
|
|
|
class FSFolder(name: String, val entries: List<FSEntry>) : FSEntry(name) {
|
|
/**
|
|
* Check whether a folder is cyclic.
|
|
*/
|
|
fun isCyclic(): Boolean {
|
|
val seen = listOf(this).toHashSet<FSEntry>()
|
|
val queue = ArrayDeque(entries)
|
|
while (queue.isNotEmpty()) {
|
|
val entry = queue.removeFirst()
|
|
if (!seen.add(entry)) {
|
|
return true
|
|
}
|
|
if (entry is FSFolder) {
|
|
queue.addAll(entry.entries)
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
} |