import { CharSet , DFA , NFA , ReadonlyNFA } from "refa" ; /**
 * Returns whether the languages of the given NFA are equal.
 *
 * This assumes that both NFA do not have unreachable or trap states.
 */ export function nfaEquals ( a , b ) { if ( a . isEmpty || b . isEmpty ) { return a . isEmpty === b . isEmpty ; } if ( a . options . maxCharacter !== b . options . maxCharacter ) { return false ; } const { maxCharacter } = a . options ; const x = a . nodes ; const y = b . nodes ; if ( x . finals . has ( x . initial ) !== y . finals . has ( y . initial ) ) { return false ; } function unionAll ( iter ) { let total = undefined ; for ( const item of iter ) { if ( total === undefined ) { total = item ; } else { total = total . union ( item ) ; } } return total || CharSet . empty ( maxCharacter ) ; } if ( ! unionAll ( x . initial . out . values ( ) ) . equals ( unionAll ( y . initial . out . values ( ) ) ) ) { return false ; } const aDfa = DFA . fromFA ( a ) ; aDfa . minimize ( ) ; const bDfa = DFA . fromFA ( b ) ; bDfa . minimize ( ) ; return aDfa . structurallyEqual ( bDfa ) ; } export function nfaIsSupersetOf ( superset , subset ) { const union = superset . copy ( ) ; union . union ( subset ) ; return nfaEquals ( union , superset ) ; } export function nfaIsSubsetOf ( subset , superset ) { return nfaIsSupersetOf ( superset , subset ) ; } export function nfaUnionAll ( nfas , options ) { const total = NFA . empty ( options ) ; for ( const nfa of nfas ) { total . union ( nfa ) ; } return total ; }