This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
Gleb Koval 8aa21dff54
All checks were successful
Test Workflow / Lint and test library (push) Successful in 3m56s
Deny cyclic FSFolders (#3)
Contributes to #2 .

Handle cyclic folders explicitly, instead of relying on the filesystem.

Reviewed-on: #3
2024-01-07 14:56:16 +00:00

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
}
}