This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhanced the Crawler.kt using Kotlin Coroutine Channels
- Loading branch information
Showing
7 changed files
with
124 additions
and
57 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
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/io/github/yamin8000/twitterscrapper/util/KTree.kt
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,44 @@ | ||
package io.github.yamin8000.twitterscrapper.util | ||
|
||
class KTree<T>(root : T) { | ||
var parent: KTree<T>? = null | ||
get() = field | ||
var data: T = root | ||
get() = field | ||
|
||
val isRoot: Boolean | ||
get() = parent == null | ||
|
||
val isLeaf: Boolean | ||
get() = directChildren.isEmpty() | ||
|
||
val level: Int | ||
get() = if (isRoot) 0 else (parent?.level ?: 0) + 1 | ||
|
||
private var directChildren = mutableListOf<KTree<T>>() | ||
|
||
private var descendants = mutableListOf<KTree<T>>() | ||
|
||
fun children() = directChildren.toList() | ||
|
||
fun root(): KTree<T> = if (this.parent == null) this else this.root() | ||
|
||
fun addChild(child: T): KTree<T> { | ||
val childNode = KTree(child) | ||
childNode.parent = this | ||
directChildren.add(childNode) | ||
addDescendant(childNode) | ||
parent?.addDescendant(childNode) | ||
return childNode | ||
} | ||
|
||
private fun addDescendant(descendant: KTree<T>) { | ||
descendants.add(descendant) | ||
} | ||
|
||
fun findChild(node: T) = directChildren.find { it.data == node } | ||
|
||
fun findDescendant(node: T) = findChild(node) ?: descendants.find { it.data == node } | ||
|
||
override fun toString() = data.toString() | ||
} |