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