Documentation

CslibFrontier.Algorithms.WordRAM.AhoCorasick.Code

Structured Word-RAM Code for Aho–Corasick #

Internal Code .standard 26 program blocks for all four execution phases of searchCode: setupAndClearCode, buildTrieCode, bfsBuildCode, and scanCode. All blocks live in one module isolated from the pure correctness models and proofs, exposed to sibling phase modules so wram_exec and writeStart checks can unfold them cleanly. Downstream clients importing CslibFrontier.Algorithms.WordRAM.AhoCorasick receive only searchCode and searchProgram.

Main definitions #

Word-RAM structured code #

Load the serialized header metadata (σ, |P|, ‖P‖, |t|) into layout registers.

Compute all scratch-array base address registers from the loaded header metadata.

Header decode and workspace base-address initialization (setupHeaderCode; setupBaseCode).

Phase 1 code: decode header registers and zero-fill all scratch arrays [trieBase, out).

Branch allocating a fresh trie state if trie[u, a] = 0, or moving to existing child r7.

Inner trie insertion step processing a single keyword character a = *r2.

Mark non-root terminal state term[r4] := 1 and decrement remaining keyword counter r1.

Outer trie insertion step parsing one length-prefixed keyword from r2.

Phase 2 code: construct the flat maxS × σ array trie (Algorithm 2).

Depth-1 BFS step initializing delta[0, r5] and enqueueing children of root 0.

Write delta[r, a], fail[s], and enqueue newly discovered trie child s.

Link outNext[s] to outHead[fail[s]] and load term[s] into rState.

Inner BFS step over symbol r5 of dequeued state r3 (failure link in r4).

Outer BFS step dequeuing state r3 = queue[r1] and sweeping a ∈ [0, σ).

Phase 3 code: breadth-first failure, delta, and output-link construction (Algorithm 3).

Set r7 := 1 iff rState > 0 and trie[rState, r5] = 0 (failure loop guard).

Single amortized text-scanning transition following fail links to a matching edge (r5).

Single output-chain emission step appending [r3, depth[r4]] to *r2 and advancing r4.

Walk the linked output chain starting at outHead[rState], emitting all matches at r3.

Process text character text[r1], updating rState via stepCode and emitting matches.

Phase 4 code: scan text[0..textLen) and write the length-prefixed [pos, len] list at out.