Skip to content

Instantly share code, notes, and snippets.

@4hg
Last active July 19, 2023 03:47
Show Gist options
  • Save 4hg/f412476edba86599f1b0e6dd2ea8b12d to your computer and use it in GitHub Desktop.
Save 4hg/f412476edba86599f1b0e6dd2ea8b12d to your computer and use it in GitHub Desktop.
Separate chaining hash table
coclass 'ht'
create =: {{
table =: y # a:
ts =: # table NB. table size
o =: 0 NB. occupancy
}}
destroy =: codestroy
inspect =: {{ table }}
hash =: {{
horners =. 0 [F..((2^32x)|[+37x*]) 3 u: ]
horners^:('literal' -: datatype) y
}}
index =: {{ ts | hash y }}
contains =: {{ (<y) e. table {::~ index y }}
clear =: {{ EMPTY [ o =: 0 [ table =: a: #~ ts }}
NB. TODO: test with strings
insert =: {{
if. contains y do. 0 return. end.
l =. < (<y) , table {::~ i =. index y
o =: >: o [ table =: l i } table
1 [ rehash^:(o > ts)''
}}
remove =: {{
if. -. contains y do. 0 return. end.
l =. < -.&(<y) table {::~ i =. index y
1 [ o =: <: o [ table =: l i } table
}}
rehash =: {{
items =. ; table
clear''
o =: 0 [ table =: a: #~ ts =: 4 p: +: ts
EMPTY [ insert every items
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment