MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
hashtablemodule Module Reference

A chaining hash map for integers. More...

Data Types

type  nodetype
 
type  buckettype
 
type  hashtabletype
 

Functions/Subroutines

subroutine, public hash_table_cr (map)
 Create a hash table. More...
 
subroutine, public hash_table_da (map)
 Deallocate the hash table. More...
 
subroutine ht_add (this, k, v)
 Associate the given key and value. More...
 
type(nodetype) function, pointer find_node (this, k)
 Find the node containing the given key. More...
 
integer(i4b) function ht_get (this, k)
 Get the value for the given key if it exists, otherwise return zero. More...
 
subroutine list_cr (list, k, v)
 Create a list with the given key/value pair. More...
 
subroutine list_add (this, k, v)
 Add a key/value pair to the list. More...
 
subroutine list_da (list)
 Deallocate the list. More...
 
integer(i4b) function hash (k)
 Map a character string to an integer. More...
 

Variables

integer, parameter, private hash_size = 4993
 
integer, parameter, private multiplier = 31
 

Detailed Description

Convenient for use as an index into arrays of arbitrary data type. The implementation is based on Arjen Markus' dictionary in the Flibs collection of Fortran utilities.

Function/Subroutine Documentation

◆ find_node()

type(nodetype) function, pointer hashtablemodule::find_node ( class(hashtabletype this,
character(len=*), intent(in)  k 
)
private
Parameters
thisthe hash map
[in]kthe key

Definition at line 111 of file HashTable.f90.

112  ! -- dummy
113  class(HashTableType) :: this !< the hash map
114  character(len=*), intent(in) :: k !< the key
115  ! -- local
116  type(NodeType), pointer :: node
117  integer(I4B) :: h
118 
119  h = hash(trim(k))
120  node => this%buckets(h)%list
121 
122  ! -- search bucket for node with matching key
123  do while (associated(node))
124  if (node%key == k) then
125  exit
126  else
127  node => node%next
128  end if
129  end do
130 
Here is the call graph for this function:

◆ hash()

integer(i4b) function hashtablemodule::hash ( character(len=*), intent(in)  k)
private
Parameters
[in]kthe key

Definition at line 201 of file HashTable.f90.

202  ! -- dummy
203  character(len=*), intent(in) :: k !< the key
204  ! -- local
205  integer(I4B) :: h
206  integer(I4B) :: i
207 
208  h = 0
209  do i = 1, len(k)
210  h = modulo(multiplier * h + ichar(k(i:i)), hash_size)
211  end do
212  h = 1 + modulo(h - 1, hash_size)
213 
Here is the caller graph for this function:

◆ hash_table_cr()

subroutine, public hashtablemodule::hash_table_cr ( type(hashtabletype), pointer  map)

Definition at line 45 of file HashTable.f90.

46  ! -- dummy
47  type(HashTableType), pointer :: map
48  ! -- local
49  integer(I4B) :: i
50 
51  ! -- allocate
52  allocate (map)
53  allocate (map%buckets(hash_size))
54 
55  ! -- initialize nul buckets
56  do i = 1, hash_size
57  map%buckets(i)%list => null()
58  end do
59 
Here is the caller graph for this function:

◆ hash_table_da()

subroutine, public hashtablemodule::hash_table_da ( type(hashtabletype), pointer  map)

Definition at line 63 of file HashTable.f90.

64  ! -- dummy
65  type(HashTableType), pointer :: map
66  ! -- local
67  integer(I4B) :: i
68 
69  ! -- deallocate each bucket
70  do i = 1, size(map%buckets)
71  if (associated(map%buckets(i)%list)) then
72  call list_da(map%buckets(i)%list)
73  end if
74  end do
75 
76  ! -- deallocate bucket array and hash table
77  deallocate (map%buckets)
78  deallocate (map)
79 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ht_add()

subroutine hashtablemodule::ht_add ( class(hashtabletype this,
character(len=*), intent(in)  k,
integer(i4b), intent(in)  v 
)
private

Definition at line 83 of file HashTable.f90.

84  ! -- dummy
85  class(HashTableType) :: this
86  character(len=*), intent(in) :: k
87  integer(I4B), intent(in) :: v
88  ! -- local
89  type(NodeType), pointer :: node
90  integer(I4B) :: h
91 
92  ! -- find the element corresponding to this key and replace index or
93  ! get an unassociated elem that corresponds to this key
94  node => this%find_node(k)
95 
96  ! -- replace index or create new entry
97  if (associated(node)) then
98  node%value = v
99  else
100  h = hash(trim(k))
101  if (associated(this%buckets(h)%list)) then
102  call this%buckets(h)%list%add(k, v)
103  else
104  call list_cr(this%buckets(h)%list, k, v)
105  end if
106  end if
107 
Here is the call graph for this function:

◆ ht_get()

integer(i4b) function hashtablemodule::ht_get ( class(hashtabletype this,
character(len=*), intent(in)  k 
)
private
Parameters
thisthe hash map
[in]kthe key

Definition at line 134 of file HashTable.f90.

135  ! -- dummy
136  class(HashTableType) :: this !< the hash map
137  character(len=*), intent(in) :: k !< the key
138  ! -- return
139  integer(I4B) :: v
140  ! -- local
141  type(NodeType), pointer :: node
142 
143  node => this%find_node(k)
144  if (associated(node)) then
145  v = node%value
146  else
147  v = 0
148  end if
149 

◆ list_add()

subroutine hashtablemodule::list_add ( class(nodetype this,
character(len=*), intent(in)  k,
integer(i4b), intent(in)  v 
)
private
Parameters
thisthe list
[in]kthe key
[in]vthe value

Definition at line 167 of file HashTable.f90.

168  ! -- dummy
169  class(NodeType) :: this !< the list
170  character(len=*), intent(in) :: k !< the key
171  integer(I4B), intent(in) :: v !< the value
172  ! -- local
173  type(NodeType), pointer :: next
174 
175  allocate (next)
176  next%key = k
177  next%value = v
178  next%next => this%next
179  this%next => next
180 

◆ list_cr()

subroutine hashtablemodule::list_cr ( type(nodetype), pointer  list,
character(len=*), intent(in)  k,
integer(i4b), intent(in)  v 
)
private
Parameters
listpointer to the list
[in]kthe first key
[in]vthe first value

Definition at line 153 of file HashTable.f90.

154  ! -- dummy
155  type(NodeType), pointer :: list !< pointer to the list
156  character(len=*), intent(in) :: k !< the first key
157  integer(I4B), intent(in) :: v !< the first value
158 
159  allocate (list)
160  list%next => null()
161  list%key = k
162  list%value = v
163 
Here is the caller graph for this function:

◆ list_da()

subroutine hashtablemodule::list_da ( type(nodetype), intent(in), pointer  list)
private
Parameters
[in]listthe list

Definition at line 184 of file HashTable.f90.

185  ! -- dummy
186  type(NodeType), pointer, intent(in) :: list !< the list
187  ! -- local
188  type(NodeType), pointer :: curr
189  type(NodeType), pointer :: node
190 
191  node => list
192  do while (associated(node))
193  curr => node
194  node => curr%next
195  deallocate (curr)
196  end do
197 
Here is the caller graph for this function:

Variable Documentation

◆ hash_size

integer, parameter, private hashtablemodule::hash_size = 4993
private

Definition at line 18 of file HashTable.f90.

18  integer, parameter, private :: HASH_SIZE = 4993

◆ multiplier

integer, parameter, private hashtablemodule::multiplier = 31
private

Definition at line 19 of file HashTable.f90.

19  integer, parameter, private :: MULTIPLIER = 31