prog8/examples/cx16/assembler/perfecthash.py
Irmen de Jong 76101d7f8d assem
2021-01-05 22:56:52 +01:00

181 lines
2.8 KiB
Python

TOTAL_KEYWORDS = 98
MIN_WORD_LENGTH = 3
MAX_WORD_LENGTH = 4
MIN_HASH_VALUE = 2
MAX_HASH_VALUE = 134
def hash(string: str, length: int) -> int:
asso_values = [
135, 135, 135, 135, 135, 135, 135, 135, 135, 135,
135, 135, 135, 135, 135, 135, 135, 135, 135, 135,
135, 135, 135, 135, 135, 135, 135, 135, 135, 135,
135, 135, 135, 135, 135, 135, 135, 135, 135, 135,
135, 135, 135, 135, 135, 135, 135, 135, 65, 62,
61, 58, 57, 54, 47, 46, 135, 135, 135, 135,
135, 135, 135, 135, 135, 26, 4, 1, 2, 33,
2, 135, 135, 15, 69, 4, 30, 10, 52, 17,
3, 34, 13, 0, 5, 29, 7, 69, 18, 6,
53, 135, 135, 135, 135, 135, 135, 135, 135, 135,
135, 135, 135, 135, 135, 135, 135, 135, 135, 135,
135, 135, 135, 135, 135, 135, 135, 135, 135, 135,
135, 135, 135, 135, 135, 135, 135, 135, 135 ]
hval = 0
if length > 3:
hval += asso_values[ord(string[3])]
if length > 2:
hval += asso_values[ord(string[2])]
if length > 1:
hval += asso_values[ord(string[1])+1]
hval += asso_values[ord(string[0])]
return hval
wordlist = [
None,
None,
"SBC",
"SEC",
"SED",
"DEC",
"BCS",
"BCC",
"BRK",
"TRB",
"DEY",
"TXS",
"CLC",
"CLD",
"TSB",
"TAY",
"PLP",
"SEI",
"CLV",
"PLY",
None,
"PHP",
"DEX",
None,
"PHY",
None,
"CLI",
"TAX",
"TSX",
"ROR",
"BRA",
"PLX",
"STP",
"INC",
None,
"STY",
"PHX",
"TXA",
"INY",
"PLA",
"BEQ",
"CPY",
"RTS",
"ORA",
"PHA",
"AND",
"ROL",
"STX",
"LSR",
"EOR",
"INX",
"BBS7",
"BBS6",
"CPX",
"BNE",
"STA",
"CMP",
"RTI",
"NOP",
"BBS5",
"ADC",
"ASL",
"BBS4",
"BBS3",
"BBR7",
"BBR6",
"BBS2",
"BBS1",
"BPL",
"LDY",
"BBS0",
"BMI",
"BBR5",
"BVS",
"BVC",
"BBR4",
"BBR3",
None,
"BIT",
"BBR2",
"BBR1",
"LDX",
"STZ",
"BBR0",
"TYA",
None,
None,
"JSR",
"WAI",
"LDA",
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
"SMB7",
"SMB6",
None,
None,
None,
None,
None,
None,
"SMB5",
None,
None,
"SMB4",
"SMB3",
"RMB7",
"RMB6",
"SMB2",
"SMB1",
None,
None,
"SMB0",
None,
"RMB5",
"JMP",
None,
"RMB4",
"RMB3",
None,
None,
"RMB2",
"RMB1",
None,
None,
"RMB0"
]
def in_word_set(string: str) -> bool:
length = len(string)
if 3 <= length <= 4:
key = hash(string, length)
if key <= MAX_HASH_VALUE:
word = wordlist[key]
return word and word==string
return False