Skip to content

Commit 2418ac8

Browse files
committed
resolve discrepancy
1 parent a85b187 commit 2418ac8

File tree

1 file changed

+42
-1
lines changed
  • src/commonMain/kotlin/ai/hypergraph/kaliningraph/automata

1 file changed

+42
-1
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,45 @@ fun Σᐩ.parseFSA(): FSA {
371371
val init = Q.filter { it.π1 == "INIT" }.map { it.π3 }.toSet()
372372
val final = Q.filter { it.π1 == "DONE" }.map { it.π3 }.toSet()
373373
return FSA(Q.filter { it.π1 !in setOf("INIT", "DONE") }.toSet(), init, final)
374-
}
374+
}
375+
376+
const val NEG_LITERAL = 0x40000000u //=1.shl(30)
377+
// Sparse index nonzero entries of the M_0 parse chart
378+
fun FSA.byteFormat(cfg: CFG): IntArray { // TODO: kernelize
379+
val t0 = TimeSource.Monotonic.markNow()
380+
val bindex = cfg.bindex
381+
val terminalLists = cfg.terminalLists
382+
383+
// 0 and 1 are reserved for (0) no parse exists and (1) parse exists, but an internal nonterminal node
384+
// Other byte values are used to denote the presence (+) or absence (-) of a leaf terminal
385+
fun StrPred.predByte(A: Int): Int = (
386+
if (arg == "[.*]" || (arg.startsWith("[!=]") && arg.drop(4) !in terminalLists[A])) Int.MAX_VALUE - 1 // All possible terminals
387+
else if (arg.startsWith("[!=]")) (NEG_LITERAL.toInt() + (terminalLists[A].indexOf(arg.drop(4)) + 1).shl(1)) // Represent negation using sign bit
388+
else (terminalLists[A].indexOf(arg) + 1).shl(1)
389+
)
390+
391+
fun buildSparseChart(cfg: CFG, nominalForm: NOM, stateMap: Map<String, Int>, bindex: Bindex<String>): IntArray {
392+
val rowCount = cfg.unitProductions.sumOf { (_, σ) -> nominalForm.flattenedTriples.count { arc -> arc.second(σ) } }
393+
394+
val out = IntArray(rowCount * 4)
395+
396+
var p = 0
397+
for ((A, σ) in cfg.unitProductions) {
398+
val Aidx = bindex[A]
399+
for ((q0, sp, q1) in nominalForm.flattenedTriples) {
400+
if (!sp(σ)) continue
401+
402+
out[p++] = stateMap[q0]!! // q0
403+
out[p++] = stateMap[q1]!! // q1
404+
out[p++] = Aidx // non‑terminal
405+
out[p++] = sp.predByte(Aidx) // terminal byte
406+
}
407+
}
408+
return out
409+
}
410+
411+
val sparseChart = buildSparseChart(cfg, nominalForm, stateMap, bindex)
412+
println("Byte format took: ${t0.elapsedNow()}")
413+
return sparseChart
414+
}
415+

0 commit comments

Comments
 (0)