Skip to content

Commit

Permalink
optimize postprocessing and add commentary
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Jul 1, 2024
1 parent 850040c commit 5dd0c4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,11 @@ private fun manhattanDistance(first: Pair<Int, Int>, second: Pair<Int, Int>): In
(second.second - first.second).absoluteValue + (second.first - first.first).absoluteValue

// Range of the shortest path to the longest path, i.e., Manhattan distance
fun FSA.SPLP(a: STC, b: STC) =
(APSP[a.π1 to b.π1] ?: Int.MAX_VALUE)..//.also { /*if (Random.nextInt(100000) == 3) if(it == Int.MAX_VALUE) println("Miss! ${hash(a.π1, b.π1)} / ${a.first} / ${b.first}") else */
// if (it != Int.MAX_VALUE) println("Hit: ${hash(a.π1, b.π1)} / ${a.first} / ${b.first}") }..
manhattanDistance(a.coords(), b.coords())
fun FSA.SPLP(a: STC, b: STC): IntRange {
val rng = APSP[a.π1 to b.π1]
return if (rng == null) Int.MAX_VALUE..Int.MAX_VALUE
else rng..manhattanDistance(a.coords(), b.coords())
}

fun IntRange.overlaps(other: IntRange) =
(other.first in first..last) || (other.last in first..last)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,14 @@ fun CFG.jvmIntersectLevFSAP(fsa: FSA,
val ct2 = Array(fsa.states.size) { Array(nonterminals.size) { Array(fsa.states.size) { false } } }
ct.parallelStream()
.filter {
lbc[it.π3].overlaps(fsa.SPLP(it.π1, it.π2)) &&
// Checks whether the length bounds for the noterminal (i.e., the range of the number of terminals it can
// parse) is compatible with the range of path lengths across all paths connecting two states in an FSA.
// This is a coarse approximation, but is cheaper to compute, so it filters out most invalid triples.
lbc[it.π3].overlaps(
fsa.SPLP(it.π1, it.π2)
) &&
// Checks the Parikh map for compatibility between the CFG nonterminals and state pairs in the FSA.
// This is a finer grained filter, but more expensive to compute, so we use the coarse filter first
fsa.obeys(it.π1, it.π2, it.π3, parikhMap)
}.toList().also {
val fraction = it.size.toDouble() / (fsa.states.size * nonterminals.size * fsa.states.size)
Expand Down Expand Up @@ -344,7 +351,7 @@ fun CFG.jvmDropVestigialProductions(clock: TimeSource.Monotonic.ValueTimeMark):
.collect(Collectors.toSet())
.also { println("Removed ${size - it.size} invalid productions in ${clock.elapsedNow() - start}") }
.freeze()
.jvmRemoveUselessSymbols()
.jvmRemoveUselessSymbols(nts)
//.jdvpNew()

println("Removed ${size - rw.size} vestigial productions, resulting in ${rw.size} productions.")
Expand All @@ -367,7 +374,8 @@ fun CFG.jvmDropVestigialProductions(clock: TimeSource.Monotonic.ValueTimeMark):
*/

fun CFG.jvmRemoveUselessSymbols(
generating: Set<Σᐩ> = jvmGenSym(),
nonterminals: Set<Σᐩ>,
generating: Set<Σᐩ> = jvmGenSym(nonterminals),
reachable: Set<Σᐩ> = jvmReachSym()
): CFG =
asSequence().asStream().parallel()
Expand Down

0 comments on commit 5dd0c4a

Please sign in to comment.