Skip to content

Commit 15d6a8d

Browse files
committed
speed up suffix completion
1 parent ad78389 commit 15d6a8d

File tree

1 file changed

+12
-3
lines changed
  • src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing

1 file changed

+12
-3
lines changed

src/commonMain/kotlin/ai/hypergraph/kaliningraph/parsing/SeqValiant.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,18 @@ fun CFG.enumNTSmall(nt: String): Sequence<Σᐩ> =
409409
?: emptySequence()
410410
})
411411

412-
fun CFG.admitsPrefix(tokens: List<Σᐩ>): Boolean = language.let { l -> suffixCompletions(tokens).any { it in l } }
413-
fun CFG.enumSuffixes(tokens: List<Σᐩ>, toTake: Int): Sequence<Σᐩ> = suffixCompletions(tokens).flatMap { enumSeq(it) }.take(toTake).distinct()
414-
fun suffixCompletions(tokens: List<Σᐩ>, len: Int = 10): Sequence<List<Σᐩ>> = (1..len).asSequence().map { tokens + List(it) { "_" } }
412+
const val MAX_SUFF_LEN = 10
413+
// Returns whether the prefix concatenated with i wildcards up to MAX_SUFF_LEN fits in the langauge, return all i's
414+
fun CFG.admitsPrefix(prefix: List<Σᐩ>): IntRange = language.let { l ->
415+
var minSuffLen = -1
416+
suffixCompletions(prefix, (0..MAX_SUFF_LEN).toList())
417+
.any { (it.second in l).also { q -> if (q) minSuffLen = it.first } }
418+
minSuffLen..(minSuffLen + MAX_SUFF_LEN)
419+
}
420+
fun CFG.enumSuffixes(tokens: List<Σᐩ>, toTake: Int, suffixLens: List<Int>): Sequence<Σᐩ> =
421+
suffixCompletions(tokens, suffixLens).flatMap { enumSeq(it.second) }.take(toTake)
422+
fun suffixCompletions(tokens: List<Σᐩ>, suffixLens: List<Int>): Sequence<Pair<Int, List<String>>> =
423+
suffixLens.map { it to tokens + List(it) { "_" } }.asSequence()
415424

416425
var maxTrees = 50_000
417426
// This should never return duplicates and is the second fastest.

0 commit comments

Comments
 (0)