This class represents a node in the directed acyclic word graph (DAWG,
a.k.a. Minimal Acyclic Finite State Automaton, or MA-FSA). It has a list
of edges to other nodes. It has functions for testing whether it is
equivalent to another node. Nodes are equivalent if they have identical
edges, and each identical edge leads to identical states.
class DawgNode
@next_id = 0
constructor: ->
@id = DawgNode.next_id; DawgNode.next_id += 1
@['is_final'] = false
@['edges'] = {}
bisect_left: (edges, edge, lower, upper) ->
while lower < upper
i = (lower + upper) >> 1
if edges[i] < edge
lower = i + 1
else
upper = i
return lower
'toString': ->
edges = []
for label, node of @['edges']
edge = label + node.id.toString()
edges.splice(@bisect_left(edges, edge, 0, edges.length), 0, edge)
(+ @['is_final']) + edges.join('')
class Dawg
constructor: (dictionary) ->
unless dictionary and typeof dictionary.length is 'number'
throw new Error("Expected dictionary to be array-like")
@previous_word = ''
@['root'] = new DawgNode()