Skip to content

Commit 8144dc1

Browse files
committed
address profiling hotspot
1 parent 69159ba commit 8144dc1

File tree

2 files changed

+35
-16
lines changed
  • src/commonMain/kotlin/ai/hypergraph/kaliningraph/automata

2 files changed

+35
-16
lines changed

src/commonMain/kotlin/ai/hypergraph/kaliningraph/automata/FSA.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ open class FSA constructor(open val Q: TSA, open val init: Set<Σᐩ>, open val
9999
return triples
100100
}
101101

102+
data class Int3(val a: Int, val b: Int, val c: Int)
103+
104+
open fun allIndexedTxs2(unitProds: Map<Σᐩ, List<Σᐩ>>, bindex: Bindex<Σᐩ>): List<Int3> {
105+
val triples = mutableListOf<Int3>()
106+
for ((A, σs) in unitProds.entries) {
107+
val Aint = bindex[A]
108+
forin σs) for (arc in nominalForm.flattenedTriples)
109+
if (arc.π2(σ))
110+
triples.add(
111+
Int3(
112+
stateMap[arc.π1]!!,
113+
Aint,
114+
stateMap[arc.π3]!!
115+
)
116+
)
117+
}
118+
return triples
119+
}
120+
102121
val numStates: Int by lazy { states.size }
103122

104123
val stateMap: Map<Σᐩ, Int> by lazy { stateLst.withIndex().associate { it.value to it.index } }

src/commonMain/kotlin/ai/hypergraph/kaliningraph/automata/GRE.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fun repairWithGRE(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
307307
fun nonemptyLevInt(levFSA: FSA): Int? {
308308
val ap: List<List<List<Int>?>> = levFSA.allPairs
309309
val dp = Array(levFSA.numStates) { Array(levFSA.numStates) { BooleanArray(width) { false } } }
310-
levFSA.allIndexedTxs0(ups, bindex).forEach { (q0, nt, q1) -> dp[q0][q1][nt] = true }
310+
levFSA.allIndexedTxs2(ups, bindex).forEach { (q0, nt, q1) -> dp[q0][q1][nt] = true }
311311
var minRad = Int.MAX_VALUE
312312
// For pairs (p,q) in topological order
313313
for (dist in 1..<dp.size) {
@@ -322,16 +322,16 @@ fun repairWithGRE(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
322322
for (r in appq)
323323
if (dp[p][r][B] && dp[r][q][C]) {
324324
dp[p][q][A] = true
325+
if (p == 0 && A == startIdx && levFSA.isFinal[q]) {
326+
val (x, y) = levFSA.idsToCoords[q]!!
327+
/** See final state conditions for [makeExactLevCFL] */
328+
// The minimum radius such that this final state is included in the L-FSA
329+
minRad = minOf(minRad, (brokenStr.size - x + y).absoluteValue)
330+
if (minRad == 1) return 1
331+
}
325332
break@outerloop
326333
}
327334
}
328-
if (p == 0 && A == startIdx && levFSA.isFinal[q] && dp[p][q][A]) {
329-
val (x, y) = levFSA.idsToCoords[q]!!
330-
/** See final state conditions for [makeExactLevCFL] */
331-
// The minimum radius such that this final state is included in the L-FSA
332-
minRad = minOf(minRad, (brokenStr.size - x + y).absoluteValue)
333-
if (minRad == 1) return 1
334-
}
335335
}
336336
}
337337
}
@@ -444,7 +444,7 @@ suspend fun initiateSuspendableRepair(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
444444
val ap: List<List<List<Int>?>> = levFSA.allPairs
445445
val dp = Array(levFSA.numStates) { Array(levFSA.numStates) { BooleanArray(width) { false } } }
446446

447-
levFSA.allIndexedTxs0(ups, bindex).forEach { (q0, nt, q1) -> dp[q0][q1][nt] = true }
447+
levFSA.allIndexedTxs2(ups, bindex).forEach { (q0, nt, q1) -> dp[q0][q1][nt] = true }
448448
var minRad: Int = Int.MAX_VALUE
449449

450450
// For pairs (p,q) in topological order
@@ -461,16 +461,16 @@ suspend fun initiateSuspendableRepair(brokenStr: List<Σᐩ>, cfg: CFG): GRE? {
461461
for (r in appq)
462462
if (dp[p][r][B] && dp[r][q][C]) {
463463
dp[p][q][A] = true
464+
if (p == 0 && A == startIdx && levFSA.isFinal[q]) {
465+
val (x, y) = levFSA.idsToCoords[q]!!
466+
/** See final state conditions for [makeExactLevCFL] */
467+
// The minimum radius such that this final state is included in the L-FSA
468+
minRad = minOf(minRad, (brokenStr.size - x + y).absoluteValue)
469+
if (minRad == 1) return 1
470+
}
464471
break@outerloop
465472
}
466473
}
467-
468-
if (p == 0 && A == startIdx && levFSA.isFinal[q] && dp[p][q][A]) {
469-
val (x, y) = levFSA.idsToCoords[q]!!
470-
/** See final state conditions for [makeExactLevCFL] */
471-
// The minimum radius such that this final state is included in the L-FSA
472-
minRad = minOf(minRad, (brokenStr.size - x + y).absoluteValue)
473-
}
474474
}
475475
}
476476
}

0 commit comments

Comments
 (0)