Compute all state-to-state distances ahead of time then refer to them
authorNick Downing <downing.nick@gmail.com>
Sun, 22 Jul 2018 06:18:50 +0000 (16:18 +1000)
committerNick Downing <downing.nick@gmail.com>
Sun, 22 Jul 2018 06:18:50 +0000 (16:18 +1000)
flex_dfa.py

index 015f0b3..b742740 100644 (file)
@@ -173,14 +173,16 @@ class FlexDFA:
     n_entries = 0x101
 
     # compress the transition table
+    dist = numpy.sum(
+      transitions[:, numpy.newaxis, :] != transitions[numpy.newaxis, :, :],
+      2
+    )
     while n_states < n_accept:
       # find most similar/earliest existing state to use as default
-      mask = (
-        transitions[n_states:n_states + 1, :] !=
-        transitions[:n_states, :]
-      )
-      default_state = numpy.argmin(numpy.sum(mask, 1), 0)
-      indices = numpy.nonzero(mask[default_state, :])[0]
+      default_state = numpy.argmin(dist[:n_states, n_states], 0)
+      indices = numpy.nonzero(
+        transitions[n_states] != transitions[default_state]
+      )[0]
       if indices.shape[0] == 0:
         # when copying another state, need to have the same base, though
         # the base will not matter since there will be no entries, it is