Skip to content

Commit

Permalink
speed up traversing of suffix tree
Browse files Browse the repository at this point in the history
  • Loading branch information
suhininalex committed Jul 7, 2017
1 parent f27ce87 commit 8d6bddd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
22 changes: 17 additions & 5 deletions src/main/com/suhininalex/clones/core/CloneIndexer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.suhininalex.suffixtree.Node
import com.suhininalex.suffixtree.SuffixTree
import java.util.*
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.concurrent.read
import kotlin.concurrent.write
Expand Down Expand Up @@ -57,16 +58,27 @@ object CloneIndexer {
.filterSubClassClones()
}

fun getAllCloneClasses(): Sequence<TreeCloneClass> = rwLock.read {
fun getAllCloneClasses(): List<TreeCloneClass> = rwLock.read {
tree.getAllCloneClasses(PluginSettings.minCloneLength)
}

}

fun SuffixTree<SourceToken>.getAllCloneClasses(minTokenLength: Int): Sequence<TreeCloneClass> =
root.depthFirstTraverse { it.edges.asSequence().mapNotNull { it.terminal }}
.map(::TreeCloneClass)
.filter { it.length > minTokenLength }
fun Node.visitChildren(visit: (Node) -> Unit) {
visit(this)
this.edges.mapNotNull { it.terminal }.forEach { it.visitChildren(visit) }
}

fun SuffixTree<SourceToken>.getAllCloneClasses(minTokenLength: Int): List<TreeCloneClass> {
val clones = ArrayList<TreeCloneClass>()
root.visitChildren {
val cloneClass = TreeCloneClass(it)
if (cloneClass.length > minTokenLength) {
clones.add(cloneClass)
}
}
return clones
}

fun SuffixTree<SourceToken>.getAllSequenceClasses(id: Long, minTokenLength: Int): Sequence<TreeCloneClass> {
val classes = LinkedList<TreeCloneClass>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ val beforeFiltering = PluginLabels.getLabel("progressbar-filtering-before-filter

fun CloneIndexer.getAllFilteredClones(): Promise<List<CloneClass>, Exception> =
task {
ProgressManager.getInstance().backgroundTask(beforeFiltering){ getAllCloneClasses().toList().notLongestSequenceFilter() }.get()
ProgressManager.getInstance().backgroundTask(beforeFiltering){ getAllCloneClasses().notLongestSequenceFilter() }.get()
}.thenApply {
withProgressBar(subClassFiltering).filterSubClassClones().get().validClonesFilter()
}.thenApply {
Expand Down
1 change: 1 addition & 0 deletions src/main/com/suhininalex/clones/ide/CloneFinderIndex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.suhininalex.clones.ide;

import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.GeneratedSourcesFilter
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.psi.search.EverythingGlobalScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.suhininalex.clones.core.utils.tokenSequence
class ExtractSiblingClonesTest : FolderProjectTest("testdata/sphinx4-java/") {

val clones
get() = CloneIndexer.getAllCloneClasses().toList().filterSubClassClones()
get() = CloneIndexer.getAllCloneClasses().filterSubClassClones()

fun testNotAloneDuplicate() {
val problems = clones.splitSiblingClones().mergeCloneClasses().filter { ! checkCountInvariant(it) }
Expand Down
2 changes: 1 addition & 1 deletion src/test/com/suhininalex/clones/TreeClonesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.suhininalex.clones.core.utils.tokenSequence
class TreeClonesTest : FolderProjectTest("testdata/sphinx4-java/") {

val clones
get() = CloneIndexer.getAllCloneClasses().toList().filterSubClassClones().toList()
get() = CloneIndexer.getAllCloneClasses().filterSubClassClones().toList()

fun testNotAloneDuplicate() {
assertTrue(clones.all(::checkCountInvariant))
Expand Down

0 comments on commit 8d6bddd

Please sign in to comment.