1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4 ! Purpose: This module provides a dictionary/hashtable with insert, search, and
6 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10 integer, parameter :: TABLESIZE=53 ! Number of spaces in the table (the
11 ! number of linked lists)
14 character (len=256) :: entry ! The actual string to be stored
15 type (hashnode), pointer :: next
19 type (hashnode), pointer :: p ! Pointer to a list of entries
23 type (hashnode_ptr), dimension(TABLESIZE) :: table ! The hashtable array
29 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
32 ! Purpose: To initialize a hashtable
33 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
34 subroutine hash_init(h)
39 type (hashtable), intent(inout) :: h
48 end subroutine hash_init
51 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
54 ! Purpose: Given a hashtable h and a string to be inserted into the hashtable,
55 ! this routine adds string to the table.
57 ! NOTE: If the string already exists in the table, a second copy of the
58 ! string is added to the table
59 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
60 subroutine hash_insert(h, string)
65 character (len=256), intent(in) :: string
66 type (hashtable), intent(inout) :: h
70 type (hashnode), pointer :: hn
74 hashval = hashval + iachar(string(i:i))
76 hashval = mod(hashval, TABLESIZE) + 1
80 hn%next => h%table(hashval)%p
81 h%table(hashval)%p => hn
83 end subroutine hash_insert
86 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
89 ! Purpose: This function returns TRUE if the specified string was found in the
90 ! hashtable h, and FALSE otherwise.
91 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
92 function hash_search(h, string)
97 character (len=256), intent(in) :: string
98 type (hashtable), intent(inout) :: h
101 logical :: hash_search
104 integer :: hashval, i
105 type (hashnode), pointer :: cursor
107 hash_search = .false.
111 hashval = hashval + iachar(string(i:i))
113 hashval = mod(hashval, TABLESIZE) + 1
115 cursor => h%table(hashval)%p
116 do while(associated(cursor))
117 if (cursor%entry == string) then
121 cursor => cursor%next
127 end function hash_search
130 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
133 ! Purpose: Frees all memory associated with hashtable h. This routine may be
134 ! used to remove all entries from a hashtable.
135 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
136 subroutine hash_destroy(h)
141 type (hashtable), intent(inout) :: h
145 type (hashnode), pointer :: cursor, cursor_prev
148 cursor => h%table(i)%p
149 do while(associated(cursor))
150 cursor_prev => cursor
151 cursor => cursor%next
152 deallocate(cursor_prev)
154 nullify(h%table(i)%p)
157 end subroutine hash_destroy
159 end module hash_module