26#include "../base/base_uses.f90"
40 INTEGER,
PARAMETER :: Nbit = 16
41 INTEGER,
PARAMETER :: hash_table_size = 2**nbit
44 INTEGER,
SAVE :: actual_strings
45 INTEGER,
SAVE :: inserted_strings
49 TYPE hash_element_type
50 CHARACTER(LEN=default_string_length),
POINTER :: str => null()
51 TYPE(hash_element_type),
POINTER :: next => null()
55 TYPE(hash_element_type),
SAVE,
ALLOCATABLE,
TARGET,
DIMENSION(:) ::
hash_table
72 CHARACTER(LEN=*) :: str
75 INTEGER :: index, ipos
76 TYPE(hash_element_type),
POINTER :: this
78 inserted_strings = inserted_strings + 1
80 index = joaat_hash(str)
84 IF (.NOT.
ASSOCIATED(this%str))
THEN
88 actual_strings = actual_strings + 1
91 IF (this%str == str)
THEN
95 IF (.NOT.
ASSOCIATED(this%next))
ALLOCATE (this%next)
101 id = ior(index, ishft(ipos, nbit))
116 CHARACTER(LEN=default_string_length) :: str
118 INTEGER :: i, index, ipos
119 TYPE(hash_element_type),
POINTER :: this
121 index = iand(id, 2**nbit - 1)
122 ipos = ishft(id, -nbit)
140 FUNCTION s2s(str)
RESULT(res)
141 CHARACTER(LEN=*) :: str
142 CHARACTER(LEN=default_string_length) :: res
170 INTEGER,
INTENT(IN) :: iw
172 INTEGER :: i, ilist, ipos, ipos_max
173 TYPE(hash_element_type),
POINTER :: next, this
179 DO i = 0, hash_table_size - 1
186 DO WHILE (
ASSOCIATED(this))
189 IF (
ASSOCIATED(this%str))
DEALLOCATE (this%str)
193 ipos_max = max(ipos_max, ipos)
197 WRITE (iw, *)
"string table: # inserted str = ", inserted_strings
198 WRITE (iw, *)
" # actual = ", actual_strings
199 WRITE (iw, *)
" # lists = ", ilist,
" / ", hash_table_size
200 WRITE (iw, *)
" longest list = ", ipos_max
219 FUNCTION joaat_hash(key)
RESULT(hash_index)
220 CHARACTER(LEN=*),
INTENT(IN) :: key
221 INTEGER :: hash_index
223 INTEGER(KIND=int_8),
PARAMETER :: b32 = 2_int_8**32 - 1_int_8
226 INTEGER(KIND=int_8) ::
hash
230 hash = iand(
hash + ichar(key(i:i)), b32)
232 hash = iand(ieor(
hash, iand(ishft(
hash, -6), b32)), b32)
235 hash = iand(ieor(
hash, iand(ishft(
hash, -11), b32)), b32)
239 hash_index = int(mod(
hash, int(hash_table_size, kind=
int_8)))
240 END FUNCTION joaat_hash
static unsigned int hash(const unsigned int row, const unsigned int col)
Private hash function based on Cantor pairing function. https://en.wikipedia.org/wiki/Pairing_functio...
Defines the basic variable types.
integer, parameter, public int_8
integer, parameter, public default_string_length
generates a unique id number for a string (str2id) that can be used two compare two strings....
character(len=default_string_length) function, public s2s(str)
converts a string in a string of default_string_length
integer function, public str2id(str)
returns a unique id for a given string, and stores the string for later retrieval using the id.
character(len=default_string_length) function, public id2str(id)
returns the string associated with a given id
subroutine, public string_table_deallocate(iw)
deallocates the string table
subroutine, public string_table_allocate()
allocates the string table
type(hash_element_type), dimension(:), allocatable, target, save hash_table