MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
PtrHashTable.f90
Go to the documentation of this file.
3 use kindmodule, only: dp, i4b, lgp
6 use simmodule, only: store_warning
7
8 implicit none
9 private
10
11 public :: ptrhashtabletype
12
13 integer(I4B), parameter, private :: hash_size = 4993
14 integer(I4B), parameter, private :: multiplier = 31
15 integer(I4B), parameter, public :: bucket_size = 1000
16
17 !> @brief HashTable that stores void pointer items.
18 !!
19 !<
21 private
22 type(keyvaluelisttype) :: buckets(bucket_size) !< the HashTable buckets
23 integer(I4B) :: cnt = 0 !< the number of items in the HashTable
24 contains
25 procedure :: iterator
26 procedure :: add
27 procedure :: get
28 procedure :: contains
29 procedure :: count
30 procedure :: clear
31 end type ptrhashtabletype
32
33contains
34
35 !> @brief An iterator used to iterate through the HashTable
36 !!
37 !<
38 function iterator(this) result(itr)
39 class(ptrhashtabletype), target :: this
40 class(iteratortype), allocatable :: itr
41
42 itr = ptrhashtableiteratortype(this%buckets)
43 end function
44
45 !> @brief Add a void pointer to the HashTable
46 !!
47 !<
48 subroutine add(this, key, val)
49 class(ptrhashtabletype), target :: this
50 character(len=*), intent(in) :: key !< key of the item to be added
51 class(*), pointer, intent(in) :: val !< value to be added
52 ! -- local
53 type(keyvaluelisttype), pointer :: bucket !< bucket the key points to
54 integer(I4B) :: hash !< hashed value of the key
55
56 if (this%contains(key)) then
57 call store_warning( &
58 "Already existing variable being added to the HashTable -"//key)
59 end if
60
61 hash = compute_hash(key)
62
63 bucket => this%buckets(hash)
64 call bucket%add(key, val)
65 this%cnt = this%cnt + 1
66 end subroutine add
67
68 !> @brief Get a void pointer from the HashTable using a key
69 !!
70 !<
71 function get(this, key) result(val)
72 class(ptrhashtabletype), target :: this
73 character(len=*), intent(in) :: key !< key of the item to retrieve
74 class(*), pointer :: val !< item associated with the key
75 ! -- local
76 type(keyvaluelisttype), pointer :: bucket !< bucket the key points to
77 integer(I4B) :: hash !< hashed value of the key
78
79 hash = compute_hash(key)
80
81 bucket => this%buckets(hash)
82 val => bucket%get(key)
83
84 end function
85
86 !> @brief Boolean indicating if an item exists in the hashtable
87 !!
88 !<
89 function contains(this, key) result(res)
90 class(ptrhashtabletype), target :: this
91 character(len=*), intent(in) :: key !< key of the item to retrieve
92 logical :: res !< item found
93
94 ! -- local
95 res = associated(this%get(key))
96
97 end function
98
99 !> @brief The nummer of items in the HashTable
100 !!
101 !<
102 function count(this) result(val)
103 class(ptrhashtabletype) :: this
104 integer(I4B) :: val
105
106 val = this%cnt
107 end function
108
109 !> @brief Clears the HashTable
110 !!
111 !! Loops over all the buckets and clears them.
112 !<
113 subroutine clear(this)
114 class(ptrhashtabletype), target :: this
115 ! -- local
116 type(keyvaluelisttype), pointer :: bucket
117 integer(I4B) :: bucket_index
118
119 do bucket_index = 1, bucket_size
120 bucket => this%buckets(bucket_index)
121 call bucket%clear()
122 end do
123
124 this%cnt = 0
125 end subroutine
126
127 !> @brief Compute the hash of a key
128 !!
129 !! The hash produced will be in the interval 1 - BUCKET_SIZE
130 !<
131 function compute_hash(key) result(hash)
132 character(len=*), intent(in) :: key !< the key
133 integer(I4B) :: hash !< the hash
134 ! -- local
135 integer(I4B) :: i !< character index of the key
136
137 hash = 0
138 do i = 1, len_trim(key)
139 hash = modulo(multiplier * hash + ichar(key(i:i)), hash_size)
140 end do
141 hash = 1 + modulo(hash, bucket_size)
142
143 end function compute_hash
144
145end module ptrhashtablemodule
class(*) function, pointer get(this, key)
Get a value using a key.
integer(i4b) function count(this)
The nummer of items in the list.
class(iteratortype) function, allocatable iterator(this)
subroutine clear(this)
Clears the list.
subroutine add(this, key, val)
Add a key-value pair to the list.
This module defines variable data types.
Definition kind.f90:8
integer(i4b), parameter, private multiplier
integer(i4b), parameter, private hash_size
integer(i4b) function compute_hash(key)
Compute the hash of a key.
logical function contains(this, key)
Boolean indicating if an item exists in the hashtable.
integer(i4b), parameter, public bucket_size
This module contains simulation methods.
Definition Sim.f90:10
subroutine, public store_warning(msg, substring)
Store warning message.
Definition Sim.f90:241
A list that stores items as a key-value pair.
An iterator used to iterate through a PtrHashTable.
HashTable that stores void pointer items.