2010-04-06 15:19:27 -04:00
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2011-12-26 16:18:36 -05:00
|
|
|
# Note, this file is known to work with rev 120 of the moman
|
2010-04-06 15:19:27 -04:00
|
|
|
# repository (http://bitbucket.org/jpbarrette/moman/overview)
|
|
|
|
#
|
|
|
|
# See also: http://sites.google.com/site/rrettesite/moman
|
|
|
|
|
|
|
|
import math
|
|
|
|
import os
|
|
|
|
import sys
|
2020-02-11 18:56:11 -05:00
|
|
|
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
MODE = 'array'
|
|
|
|
PACKED = True
|
|
|
|
WORD = 64
|
2016-02-04 04:28:46 -05:00
|
|
|
LOG2_WORD = int(math.log(WORD) / math.log(2))
|
2022-08-07 10:01:30 -04:00
|
|
|
HEADER_COMMENT = '''/*
|
|
|
|
Parametric transitions for LEV1.
|
|
|
|
┏━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
|
|
|
|
┃ char vector ┃ State 0 ┃ State 1 ┃ State 2 ┃ State 3 ┃ State 4 ┃
|
|
|
|
┡━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
|
|
|
|
│ (0,0) │ (2, 0) │ (-1, 0) │ (-1, 0) │ (-1, 0) │ (-1, 0) │
|
|
|
|
│ (0,1) │ (3, 0) │ (-1, 0) │ (1, 2) │ (1, 2) │ (-1, 0) │
|
|
|
|
│ (1,0) │ (0, 1) │ (1, 1) │ (1, 1) │ (1, 1) │ (1, 1) │
|
|
|
|
│ (1,1) │ (0, 1) │ (1, 1) │ (2, 1) │ (2, 1) │ (1, 1) │
|
|
|
|
└─────────────┴─────────┴─────────┴─────────┴─────────┴─────────┘
|
|
|
|
char vector is the characteristic vectors in the paper.
|
|
|
|
entry (i,j) in the table means next transitions state is i, next offset is j + currentOffset if we meet the according char vector.
|
|
|
|
When i = -1,it means an empty state.
|
|
|
|
We store this table in toState and offsetIncr.
|
|
|
|
toState = [ i+1 | for entry in entries].
|
|
|
|
offsetIncrs = [j | for entry in entries].
|
|
|
|
*/'''
|
|
|
|
|
|
|
|
STATE0_COMMENT = '''/*
|
|
|
|
* 1 vectors; 2 states per vector; array length = 2
|
|
|
|
* Parametric transitions for LEV1 (position = w)
|
|
|
|
* ┏━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
|
|
|
|
* ┃ char vector ┃ State 0 ┃ State 1 ┃
|
|
|
|
* ┡━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
|
|
|
|
* │ () │ (1, 0) │ (-1, 0) │
|
|
|
|
* └─────────────┴─────────┴─────────┘
|
|
|
|
*/'''
|
|
|
|
|
|
|
|
STATE1_COMMENT = '''/*
|
|
|
|
* 2 vectors; 3 states per vector; array length = 6
|
|
|
|
* Parametric transitions for LEV1 (position = w-1)
|
|
|
|
* ┏━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
|
|
|
|
* ┃ char vector ┃ State 0 ┃ State 1 ┃ State 2 ┃
|
|
|
|
* ┡━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
|
|
|
|
* │ (0) │ (2, 0) │ (-1, 0) │ (-1, 0) │
|
|
|
|
* │ (1) │ (0, 1) │ (1, 1) │ (1, 1) │
|
|
|
|
* └─────────────┴─────────┴─────────┴─────────┘
|
|
|
|
*/'''
|
|
|
|
STATE2_COMMENT = '''/*
|
|
|
|
* 4 vectors; 5 states per vector; array length = 20
|
|
|
|
* Parametric transitions for LEV1 ( position == w-2 )
|
|
|
|
* ┏━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
|
|
|
|
* ┃ char vector ┃ State 0 ┃ State 1 ┃ State 2 ┃ State 3 ┃ State 4 ┃
|
|
|
|
* ┡━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
|
|
|
|
* │ (0,0) │ (2, 0) │ (-1, 0) │ (-1, 0) │ (-1, 0) │ (-1, 0) │
|
|
|
|
* │ (0,1) │ (3, 0) │ (-1, 0) │ (1, 2) │ (1, 2) │ (-1, 0) │
|
|
|
|
* │ (1,0) │ (0, 1) │ (1, 1) │ (1, 1) │ (1, 1) │ (1, 1) │
|
|
|
|
* │ (1,1) │ (0, 1) │ (1, 1) │ (2, 1) │ (2, 1) │ (1, 1) │
|
|
|
|
* └─────────────┴─────────┴─────────┴─────────┴─────────┴─────────┘
|
|
|
|
*/'''
|
|
|
|
STATE3_COMMENT = '''/*
|
|
|
|
* 8 vectors; 5 states per vector; array length = 40
|
|
|
|
* Parametric transitions for LEV1 (0 <= position <= w-3 )
|
|
|
|
* ┏━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
|
|
|
|
* ┃ char vector ┃ State 0 ┃ State 1 ┃ State 2 ┃ State 3 ┃ State 4 ┃
|
|
|
|
* ┡━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
|
|
|
|
* │ (0,0,0) │ (2, 0) │ (-1, 0) │ (-1, 0) │ (-1, 0) │ (-1, 0) │
|
|
|
|
* │ (0,0,1) │ (2, 0) │ (-1, 0) │ (-1, 0) │ (1, 3) │ (1, 3) │
|
|
|
|
* │ (0,1,0) │ (3, 0) │ (-1, 0) │ (1, 2) │ (1, 2) │ (-1, 0) │
|
|
|
|
* │ (0,1,1) │ (3, 0) │ (-1, 0) │ (1, 2) │ (2, 2) │ (1, 3) │
|
|
|
|
* │ (1,0,0) │ (0, 1) │ (1, 1) │ (1, 1) │ (1, 1) │ (1, 1) │
|
|
|
|
* │ (1,0,1) │ (0, 1) │ (1, 1) │ (1, 1) │ (4, 1) │ (4, 1) │
|
|
|
|
* │ (1,1,0) │ (0, 1) │ (1, 1) │ (2, 1) │ (2, 1) │ (1, 1) │
|
|
|
|
* │ (1,1,1) │ (0, 1) │ (1, 1) │ (2, 1) │ (3, 1) │ (4, 1) │
|
|
|
|
* └─────────────┴─────────┴─────────┴─────────┴─────────┴─────────┘
|
|
|
|
*/'''
|
|
|
|
STATE_COMMENT = [STATE0_COMMENT, STATE1_COMMENT, STATE2_COMMENT, STATE3_COMMENT]
|
2016-02-04 04:28:46 -05:00
|
|
|
# MODE = 'switch'
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
class LineOutput:
|
|
|
|
|
|
|
|
def __init__(self, indent=''):
|
|
|
|
self.l = []
|
|
|
|
self._indent = self.startIndent = indent
|
|
|
|
self.inComment = False
|
|
|
|
|
|
|
|
def __call__(self, s, indent=0):
|
|
|
|
if s.find('}') != -1:
|
|
|
|
assert self._indent != self.startIndent
|
|
|
|
self._indent = self._indent[:-2]
|
|
|
|
|
|
|
|
if indent != 0:
|
2020-02-20 21:27:38 -05:00
|
|
|
indent0 = ' ' * (len(self._indent) // 2 + indent)
|
2010-04-06 15:19:27 -04:00
|
|
|
else:
|
|
|
|
indent0 = self._indent
|
|
|
|
|
|
|
|
if s.find('/*') != -1:
|
|
|
|
if s.find('*/') == -1:
|
|
|
|
self.inComment = True
|
|
|
|
elif s.find('*/') != -1:
|
|
|
|
self.inComment = True
|
|
|
|
|
|
|
|
if self.inComment:
|
|
|
|
self.l.append(indent0 + s)
|
|
|
|
else:
|
|
|
|
self.l.append(indent0 + s.lstrip())
|
|
|
|
|
|
|
|
self.inComment = self.inComment and s.find('*/') == -1
|
|
|
|
|
|
|
|
if s.find('{') != -1:
|
|
|
|
self._indent += ' '
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if True:
|
|
|
|
assert self._indent == self.startIndent, 'indent %d vs start indent %d' % \
|
|
|
|
(len(self._indent), len(self.startIndent))
|
|
|
|
return '\n'.join(self.l)
|
|
|
|
|
|
|
|
def indent(self):
|
|
|
|
self._indent += ' '
|
|
|
|
|
|
|
|
def outdent(self):
|
|
|
|
assert self._indent != self.startIndent
|
|
|
|
self._indent = self._indent[:-2]
|
2016-02-04 04:28:46 -05:00
|
|
|
|
2010-04-06 15:19:27 -04:00
|
|
|
def charVarNumber(charVar):
|
|
|
|
"""
|
|
|
|
Maps binary number (eg [1, 0, 1]) to its decimal value (5).
|
|
|
|
"""
|
|
|
|
|
|
|
|
p = 1
|
|
|
|
sum = 0
|
2016-02-04 04:28:46 -05:00
|
|
|
downTo = len(charVar) - 1
|
2010-04-06 15:19:27 -04:00
|
|
|
while downTo >= 0:
|
|
|
|
sum += p * int(charVar[downTo])
|
|
|
|
p *= 2
|
|
|
|
downTo -= 1
|
|
|
|
return sum
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
2020-02-11 18:56:11 -05:00
|
|
|
if len(sys.argv) != 4:
|
2020-02-20 21:27:38 -05:00
|
|
|
print()
|
|
|
|
print('Usage: python -u %s N <True/False> path_to_moman_dir' % sys.argv[0])
|
|
|
|
print()
|
|
|
|
print('NOTE: the resulting .java file is created in the current working dir!')
|
|
|
|
print()
|
2010-04-06 15:19:27 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
n = int(sys.argv[1])
|
|
|
|
|
2011-12-26 16:18:36 -05:00
|
|
|
transpose = (sys.argv[2] == "True")
|
|
|
|
|
2020-02-11 18:56:11 -05:00
|
|
|
sys.path.insert(0, sys.argv[3])
|
|
|
|
|
|
|
|
try:
|
|
|
|
from possibleStates import genTransitions
|
|
|
|
except ImportError:
|
|
|
|
from finenight.possibleStates import genTransitions
|
|
|
|
|
|
|
|
|
2011-12-26 16:18:36 -05:00
|
|
|
tables = genTransitions(n, transpose)
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
stateMap = {}
|
|
|
|
|
|
|
|
# init null state
|
|
|
|
stateMap['[]'] = -1
|
|
|
|
|
|
|
|
# init start state
|
|
|
|
stateMap['[(0, 0)]'] = 0
|
|
|
|
|
|
|
|
w = LineOutput()
|
|
|
|
|
2012-07-09 12:50:25 -04:00
|
|
|
w('/*')
|
2010-04-06 15:19:27 -04:00
|
|
|
w(' * Licensed to the Apache Software Foundation (ASF) under one or more')
|
|
|
|
w(' * contributor license agreements. See the NOTICE file distributed with')
|
|
|
|
w(' * this work for additional information regarding copyright ownership.')
|
|
|
|
w(' * The ASF licenses this file to You under the Apache License, Version 2.0')
|
|
|
|
w(' * (the "License"); you may not use this file except in compliance with')
|
|
|
|
w(' * the License. You may obtain a copy of the License at')
|
|
|
|
w(' *')
|
|
|
|
w(' * http://www.apache.org/licenses/LICENSE-2.0')
|
|
|
|
w(' *')
|
|
|
|
w(' * Unless required by applicable law or agreed to in writing, software')
|
|
|
|
w(' * distributed under the License is distributed on an "AS IS" BASIS,')
|
|
|
|
w(' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.')
|
|
|
|
w(' * See the License for the specific language governing permissions and')
|
|
|
|
w(' * limitations under the License.')
|
|
|
|
w(' */')
|
2016-02-04 04:28:46 -05:00
|
|
|
w('package org.apache.lucene.util.automaton;')
|
2010-04-06 15:19:27 -04:00
|
|
|
w('')
|
|
|
|
w('// The following code was generated with the moman/finenight pkg')
|
|
|
|
w('// This package is available under the MIT License, see NOTICE.txt')
|
|
|
|
w('// for more details.')
|
2022-08-07 10:01:30 -04:00
|
|
|
w('// This source file is auto-generated, Please do not modify it directly.')
|
|
|
|
w('// You should modify the gradle/generation/moman/createAutomata.py instead.')
|
2010-04-06 15:19:27 -04:00
|
|
|
w('')
|
|
|
|
w('import org.apache.lucene.util.automaton.LevenshteinAutomata.ParametricDescription;')
|
|
|
|
w('')
|
2022-08-07 10:01:30 -04:00
|
|
|
if not transpose and n == 1:
|
|
|
|
w(HEADER_COMMENT)
|
|
|
|
w('')
|
2011-12-26 16:18:36 -05:00
|
|
|
if transpose:
|
|
|
|
w('/** Parametric description for generating a Levenshtein automaton of degree %s, ' % n)
|
2022-08-07 10:01:30 -04:00
|
|
|
w(' with transpositions as primitive edits.')
|
2011-12-26 16:18:36 -05:00
|
|
|
className = 'Lev%dTParametricDescription' % n
|
|
|
|
else:
|
2022-08-07 10:01:30 -04:00
|
|
|
w('/** Parametric description for generating a Levenshtein automaton of degree %s.' % n)
|
2011-12-26 16:18:36 -05:00
|
|
|
className = 'Lev%dParametricDescription' % n
|
2022-08-07 10:01:30 -04:00
|
|
|
if not transpose and n == 1:
|
|
|
|
w('*/')
|
|
|
|
else:
|
|
|
|
w(' The comment in Lev1ParametricDescription may be helpful for you to understand this class.')
|
|
|
|
w(' @see Lev1ParametricDescription')
|
|
|
|
w('*/')
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
w('class %s extends ParametricDescription {' % className)
|
|
|
|
|
|
|
|
w('')
|
|
|
|
w('@Override')
|
|
|
|
w('int transition(int absState, int position, int vector) {')
|
|
|
|
|
|
|
|
w(' // null absState should never be passed in')
|
|
|
|
w(' assert absState != -1;')
|
|
|
|
|
|
|
|
w('')
|
|
|
|
w(' // decode absState -> state, offset')
|
|
|
|
w(' int state = absState/(w+1);')
|
|
|
|
w(' int offset = absState%(w+1);')
|
|
|
|
w(' assert offset >= 0;')
|
2016-02-04 04:28:46 -05:00
|
|
|
w('')
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
machines = []
|
2016-02-04 04:28:46 -05:00
|
|
|
|
2010-04-06 15:19:27 -04:00
|
|
|
for i, map in enumerate(tables):
|
|
|
|
if i == 0:
|
|
|
|
w('if (position == w) {')
|
2016-02-04 04:28:46 -05:00
|
|
|
elif i == len(tables) - 1:
|
2010-04-06 15:19:27 -04:00
|
|
|
w('} else {')
|
|
|
|
else:
|
|
|
|
w('} else if (position == w-%d) {' % i)
|
|
|
|
|
|
|
|
if i != 0 and MODE == 'switch':
|
|
|
|
w('switch(vector) {')
|
|
|
|
|
2020-02-20 21:27:38 -05:00
|
|
|
l = list(map.items())
|
2010-04-06 15:19:27 -04:00
|
|
|
l.sort()
|
|
|
|
|
|
|
|
numCasesPerVector = None
|
|
|
|
numVectors = len(l)
|
|
|
|
|
|
|
|
if MODE == 'array':
|
|
|
|
toStateArray = []
|
|
|
|
toOffsetIncrArray = []
|
|
|
|
|
|
|
|
for charVar, states in l:
|
|
|
|
|
|
|
|
# somehow it's a string:
|
|
|
|
charVar = eval(charVar)
|
|
|
|
|
|
|
|
if i != 0 and MODE == 'switch':
|
|
|
|
w('case %s: // <%s>' % (charVarNumber(charVar), ','.join([str(x) for x in charVar])))
|
|
|
|
w.indent()
|
2016-02-04 04:28:46 -05:00
|
|
|
|
2020-02-20 21:27:38 -05:00
|
|
|
l = list(states.items())
|
|
|
|
l.sort()
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
byFromState = {}
|
|
|
|
|
|
|
|
# first pass to assign states
|
|
|
|
byAction = {}
|
|
|
|
for s, (toS, offset) in l:
|
|
|
|
state = str(s)
|
2016-02-04 04:28:46 -05:00
|
|
|
|
2010-04-06 15:19:27 -04:00
|
|
|
toState = str(toS)
|
|
|
|
if state not in stateMap:
|
2016-02-04 04:28:46 -05:00
|
|
|
stateMap[state] = len(stateMap) - 1
|
2010-04-06 15:19:27 -04:00
|
|
|
if toState not in stateMap:
|
2016-02-04 04:28:46 -05:00
|
|
|
stateMap[toState] = len(stateMap) - 1
|
2010-04-06 15:19:27 -04:00
|
|
|
|
2016-02-04 04:28:46 -05:00
|
|
|
byFromState[stateMap[state]] = (1 + stateMap[toState], offset)
|
2010-04-06 15:19:27 -04:00
|
|
|
|
2016-02-04 04:28:46 -05:00
|
|
|
fromStateDesc = s[1:len(s) - 1]
|
|
|
|
toStateDesc = ', '.join([str(x) for x in toS])
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
tup = (stateMap[toState], toStateDesc, offset)
|
|
|
|
if tup not in byAction:
|
|
|
|
byAction[tup] = []
|
|
|
|
byAction[tup].append((fromStateDesc, stateMap[state]))
|
|
|
|
|
|
|
|
if numCasesPerVector is None:
|
2011-12-26 16:18:36 -05:00
|
|
|
numCasesPerVector = len(l)
|
2010-04-06 15:19:27 -04:00
|
|
|
else:
|
|
|
|
# we require this to be uniform... empirically it seems to be!
|
2011-12-26 16:18:36 -05:00
|
|
|
assert numCasesPerVector == len(l)
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
if MODE == 'array':
|
|
|
|
|
|
|
|
for s in range(numCasesPerVector):
|
|
|
|
toState, offsetIncr = byFromState[s]
|
|
|
|
toStateArray.append(toState)
|
|
|
|
toOffsetIncrArray.append(offsetIncr)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# render switches
|
|
|
|
w('switch(state) { // %s cases' % len(l))
|
|
|
|
|
|
|
|
for (toState, toStateDesc, offset), lx in byAction.items():
|
|
|
|
for fromStateDesc, fromState in lx:
|
|
|
|
w('case %s: // %s' % (fromState, fromStateDesc))
|
|
|
|
w.indent()
|
|
|
|
w(' state = %s; // %s' % (toState, toStateDesc))
|
|
|
|
if offset > 0:
|
|
|
|
w(' offset += %s;' % offset)
|
|
|
|
w('break;')
|
|
|
|
w.outdent()
|
|
|
|
|
|
|
|
w('}')
|
|
|
|
if i != 0:
|
|
|
|
w('break;')
|
|
|
|
w.outdent()
|
|
|
|
|
|
|
|
if MODE == 'array':
|
|
|
|
# strangely state can come in wildly out of bounds....
|
|
|
|
w(' if (state < %d) {' % numCasesPerVector)
|
|
|
|
w(' final int loc = vector * %d + state;' % numCasesPerVector)
|
|
|
|
if PACKED:
|
|
|
|
w(' offset += unpack(offsetIncrs%d, loc, NBITSOFFSET%d);' % (i, i))
|
|
|
|
w(' state = unpack(toStates%d, loc, NBITSSTATES%d)-1;' % (i, i))
|
|
|
|
else:
|
|
|
|
w(' offset += offsetIncrs%d[loc];' % i)
|
|
|
|
w(' state = toStates%d[loc]-1;' % i)
|
|
|
|
w(' }')
|
|
|
|
elif i != 0:
|
|
|
|
w('}')
|
|
|
|
|
|
|
|
machines.append((toStateArray, toOffsetIncrArray, numCasesPerVector, numVectors))
|
|
|
|
|
|
|
|
# ends switch statement for machine
|
|
|
|
w('}')
|
|
|
|
|
|
|
|
w('')
|
|
|
|
|
|
|
|
w(' if (state == -1) {')
|
|
|
|
w(' // null state')
|
|
|
|
w(' return -1;')
|
|
|
|
w(' } else {')
|
|
|
|
w(' // translate back to abs')
|
|
|
|
w(' return state*(w+1)+offset;')
|
|
|
|
w(' }')
|
|
|
|
|
|
|
|
# ends transition method
|
|
|
|
w('}')
|
|
|
|
|
|
|
|
subs = []
|
|
|
|
if MODE == 'array':
|
|
|
|
w.indent()
|
|
|
|
for i, (toStateArray, toOffsetIncrsArray, numCasesPerVector, numVectors) in enumerate(machines):
|
|
|
|
w('')
|
|
|
|
w.outdent()
|
2022-08-07 10:01:30 -04:00
|
|
|
if transpose or n == 2:
|
|
|
|
w('// %d vectors; %d states per vector; array length = %d' % \
|
2016-02-04 04:28:46 -05:00
|
|
|
(numVectors, numCasesPerVector, numVectors * numCasesPerVector))
|
2022-08-07 10:01:30 -04:00
|
|
|
else:
|
|
|
|
w(STATE_COMMENT[i])
|
2010-04-06 15:19:27 -04:00
|
|
|
w.indent()
|
|
|
|
if PACKED:
|
|
|
|
# pack in python
|
|
|
|
l, nbits = pack(toStateArray)
|
|
|
|
subs.append(('NBITSSTATES%d' % i, str(nbits)))
|
|
|
|
w(' private final static long[] toStates%d = new long[] /*%d bits per value */ %s;' % \
|
2020-02-20 21:27:38 -05:00
|
|
|
(i, nbits, renderList([(hex(int(x)) + "L") for x in l])))
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
l, nbits = pack(toOffsetIncrsArray)
|
|
|
|
subs.append(('NBITSOFFSET%d' % i, str(nbits)))
|
|
|
|
w(' private final static long[] offsetIncrs%d = new long[] /*%d bits per value */ %s;' % \
|
2020-02-20 21:27:38 -05:00
|
|
|
(i, nbits, renderList([(hex(int(x)) + "L") for x in l])))
|
2010-04-06 15:19:27 -04:00
|
|
|
else:
|
|
|
|
w(' private final static int[] toStates%d = new int[] %s;' % \
|
|
|
|
(i, renderList([str(x) for x in toStateArray])))
|
|
|
|
w(' private final static int[] offsetIncrs%d = new int[] %s;' % \
|
|
|
|
(i, renderList([str(x) for x in toStateArray])))
|
|
|
|
w.outdent()
|
2016-02-04 04:28:46 -05:00
|
|
|
|
|
|
|
stateMap2 = dict([[v, k] for k, v in stateMap.items()])
|
2010-04-06 15:19:27 -04:00
|
|
|
w('')
|
|
|
|
w('// state map')
|
|
|
|
sum = 0
|
|
|
|
minErrors = []
|
2020-02-20 21:27:38 -05:00
|
|
|
for i in range(len(stateMap2) - 1):
|
2010-04-06 15:19:27 -04:00
|
|
|
w('// %s -> %s' % (i, stateMap2[i]))
|
2014-12-24 00:48:58 -05:00
|
|
|
# we replace t-notation as it's not relevant here
|
2011-12-26 16:18:36 -05:00
|
|
|
st = stateMap2[i].replace('t', '')
|
2016-02-04 04:28:46 -05:00
|
|
|
|
2011-12-26 16:18:36 -05:00
|
|
|
v = eval(st)
|
2016-02-04 04:28:46 -05:00
|
|
|
minError = min([-i + e for i, e in v])
|
2010-04-06 15:19:27 -04:00
|
|
|
c = len(v)
|
|
|
|
sum += c
|
|
|
|
minErrors.append(minError)
|
|
|
|
w('')
|
|
|
|
|
|
|
|
w.indent()
|
2016-02-04 04:28:46 -05:00
|
|
|
# w('private final static int[] minErrors = new int[] {%s};' % ','.join([str(x) for x in minErrors]))
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
w.outdent()
|
|
|
|
|
|
|
|
w('')
|
|
|
|
w(' public %s(int w) {' % className)
|
|
|
|
w(' super(w, %d, new int[] {%s});' % (n, ','.join([str(x) for x in minErrors])), indent=1)
|
|
|
|
w(' }')
|
|
|
|
|
|
|
|
if 0:
|
|
|
|
w('')
|
|
|
|
w('@Override')
|
|
|
|
w('public int size() { // this can now move up?')
|
2016-02-04 04:28:46 -05:00
|
|
|
w(' return %d*(w+1);' % (len(stateMap2) - 1))
|
2010-04-06 15:19:27 -04:00
|
|
|
w('}')
|
|
|
|
|
|
|
|
w('')
|
|
|
|
w('@Override')
|
|
|
|
w('public int getPosition(int absState) { // this can now move up?')
|
|
|
|
w(' return absState % (w+1);')
|
|
|
|
w('}')
|
|
|
|
|
|
|
|
w('')
|
|
|
|
w('@Override')
|
|
|
|
w('public boolean isAccept(int absState) { // this can now move up?')
|
|
|
|
w(' // decode absState -> state, offset')
|
|
|
|
w(' int state = absState/(w+1);')
|
|
|
|
w(' if (true || state < minErrors.length) {')
|
|
|
|
w(' int offset = absState%(w+1);')
|
|
|
|
w(' assert offset >= 0;')
|
|
|
|
w(' return w - offset + minErrors[state] <= %d;' % n)
|
|
|
|
w(' } else {')
|
|
|
|
w(' return false;')
|
|
|
|
w(' }')
|
|
|
|
w('}')
|
|
|
|
|
|
|
|
if MODE == 'array' and PACKED:
|
|
|
|
|
|
|
|
# we moved into super class
|
|
|
|
if False:
|
|
|
|
w('')
|
|
|
|
|
|
|
|
v = 2
|
|
|
|
l = []
|
|
|
|
for i in range(63):
|
2016-02-04 04:28:46 -05:00
|
|
|
l.append(hex(v - 1))
|
2010-04-06 15:19:27 -04:00
|
|
|
v *= 2
|
|
|
|
|
|
|
|
w('private final static long[] MASKS = new long[] {%s};' % ','.join(l), indent=1)
|
|
|
|
w('')
|
|
|
|
|
|
|
|
# unpack in java
|
|
|
|
w('private int unpack(long[] data, int index, int bitsPerValue) {')
|
|
|
|
w(' final long bitLoc = bitsPerValue * index;')
|
|
|
|
w(' final int dataLoc = (int) (bitLoc >> %d);' % LOG2_WORD)
|
2016-02-04 04:28:46 -05:00
|
|
|
w(' final int bitStart = (int) (bitLoc & %d);' % (WORD - 1))
|
2010-04-06 15:19:27 -04:00
|
|
|
w(' //System.out.println("index=" + index + " dataLoc=" + dataLoc + " bitStart=" + bitStart + " bitsPerV=" + bitsPerValue);')
|
|
|
|
w(' if (bitStart + bitsPerValue <= %d) {' % WORD)
|
|
|
|
w(' // not split')
|
|
|
|
w(' return (int) ((data[dataLoc] >> bitStart) & MASKS[bitsPerValue-1]);')
|
|
|
|
w(' } else {')
|
|
|
|
w(' // split')
|
|
|
|
w(' final int part = %d-bitStart;' % WORD)
|
|
|
|
w(' return (int) (((data[dataLoc] >> bitStart) & MASKS[part-1]) +')
|
|
|
|
w(' ((data[1+dataLoc] & MASKS[bitsPerValue-part-1]) << part));', indent=1)
|
|
|
|
w(' }')
|
|
|
|
w('}')
|
2016-02-04 04:28:46 -05:00
|
|
|
|
2010-04-06 15:19:27 -04:00
|
|
|
# class
|
|
|
|
w('}')
|
|
|
|
w('')
|
|
|
|
|
|
|
|
fileOut = '%s.java' % className
|
|
|
|
|
|
|
|
s = str(w)
|
|
|
|
for sub, repl in subs:
|
|
|
|
s = s.replace(sub, repl)
|
|
|
|
|
2022-08-07 10:01:30 -04:00
|
|
|
open(fileOut, 'w', encoding='utf-8').write(s)
|
2010-04-06 15:19:27 -04:00
|
|
|
|
2020-02-20 21:27:38 -05:00
|
|
|
print('Wrote %s [%d lines; %.1f KB]' % \
|
|
|
|
(fileOut, len(w.l), os.path.getsize(fileOut) / 1024.))
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
def renderList(l):
|
|
|
|
lx = [' ']
|
2020-02-20 21:27:38 -05:00
|
|
|
for i in range(len(l)):
|
2010-04-06 15:19:27 -04:00
|
|
|
if i > 0:
|
|
|
|
lx.append(',')
|
|
|
|
if i % 4 == 0:
|
|
|
|
lx.append('\n ')
|
|
|
|
lx.append(l[i])
|
|
|
|
return '{\n%s\n }' % ''.join(lx)
|
|
|
|
|
|
|
|
MASKS = []
|
|
|
|
v = 2
|
2020-02-20 21:27:38 -05:00
|
|
|
for i in range(63):
|
2016-02-04 04:28:46 -05:00
|
|
|
MASKS.append(v - 1)
|
2010-04-06 15:19:27 -04:00
|
|
|
v *= 2
|
|
|
|
|
|
|
|
# packs into longs; returns long[], numBits
|
|
|
|
def pack(l):
|
|
|
|
maxV = max(l)
|
2016-02-04 04:28:46 -05:00
|
|
|
bitsPerValue = max(1, int(math.ceil(math.log(maxV + 1) / math.log(2.0))))
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
bitsLeft = WORD
|
|
|
|
pendingValue = 0
|
|
|
|
|
|
|
|
packed = []
|
2020-02-20 21:27:38 -05:00
|
|
|
for i in range(len(l)):
|
2010-04-06 15:19:27 -04:00
|
|
|
v = l[i]
|
|
|
|
if pendingValue > 0:
|
2016-02-04 04:28:46 -05:00
|
|
|
bitsUsed = math.ceil(math.log(pendingValue) / math.log(2.0))
|
|
|
|
assert bitsUsed <= (WORD - bitsLeft), 'bitsLeft=%s (%s-%s=%s) bitsUsed=%s' % (bitsLeft, WORD, bitsLeft, WORD - bitsLeft, bitsUsed)
|
|
|
|
|
2010-04-06 15:19:27 -04:00
|
|
|
if bitsLeft >= bitsPerValue:
|
2016-02-04 04:28:46 -05:00
|
|
|
pendingValue += v << (WORD - bitsLeft)
|
2010-04-06 15:19:27 -04:00
|
|
|
bitsLeft -= bitsPerValue
|
|
|
|
if bitsLeft == 0:
|
|
|
|
packed.append(pendingValue)
|
|
|
|
bitsLeft = WORD
|
|
|
|
pendingValue = 0
|
|
|
|
else:
|
|
|
|
# split
|
|
|
|
|
|
|
|
# bottom bitsLeft go in current word:
|
2016-02-04 04:28:46 -05:00
|
|
|
pendingValue += (v & MASKS[bitsLeft - 1]) << (WORD - bitsLeft)
|
2010-04-06 15:19:27 -04:00
|
|
|
packed.append(pendingValue)
|
|
|
|
|
|
|
|
pendingValue = v >> bitsLeft
|
2016-02-04 04:28:46 -05:00
|
|
|
bitsLeft = WORD - (bitsPerValue - bitsLeft)
|
2010-04-06 15:19:27 -04:00
|
|
|
|
|
|
|
if bitsLeft < WORD:
|
|
|
|
packed.append(pendingValue)
|
|
|
|
|
|
|
|
# verify(l, packed, bitsPerValue)
|
2016-02-04 04:28:46 -05:00
|
|
|
|
2010-04-06 15:19:27 -04:00
|
|
|
return packed, bitsPerValue
|
|
|
|
|
|
|
|
def verify(data, packedData, bitsPerValue):
|
|
|
|
for i in range(len(data)):
|
|
|
|
assert data[i] == unpack(packedData, i, bitsPerValue)
|
|
|
|
|
|
|
|
def unpack(data, index, bitsPerValue):
|
|
|
|
bitLoc = bitsPerValue * index
|
|
|
|
dataLoc = int(bitLoc >> LOG2_WORD)
|
2016-02-04 04:28:46 -05:00
|
|
|
bitStart = int(bitLoc & (WORD - 1))
|
2010-04-06 15:19:27 -04:00
|
|
|
if bitStart + bitsPerValue <= WORD:
|
|
|
|
# not split
|
2016-02-04 04:28:46 -05:00
|
|
|
return int(((data[dataLoc] >> bitStart) & MASKS[bitsPerValue - 1]))
|
2010-04-06 15:19:27 -04:00
|
|
|
else:
|
|
|
|
# split
|
2016-02-04 04:28:46 -05:00
|
|
|
part = WORD - bitStart;
|
|
|
|
return int((((data[dataLoc] >> bitStart) & MASKS[part - 1]) +
|
|
|
|
((data[1 + dataLoc] & MASKS[bitsPerValue - part - 1]) << part)))
|
|
|
|
|
2010-04-06 15:19:27 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
if not __debug__:
|
2020-02-20 21:27:38 -05:00
|
|
|
print()
|
|
|
|
print('ERROR: please run without -O')
|
|
|
|
print()
|
2010-04-06 15:19:27 -04:00
|
|
|
sys.exit(1)
|
|
|
|
main()
|