Octopus
sihash.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2021 M. Marques, A. Castro, A. Rubio, G. Bertsch,
2!! M. Lueders
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
24
25module sihash_oct_m
26 use global_oct_m
27 use string_oct_m
28
29 use iso_c_binding
30
31 implicit none
32
33 private
34 public :: &
35 sihash_t, &
37 sihash_end, &
41
42 type sihash_t
43 private
44
45 type(c_ptr) :: map
46 end type sihash_t
47
49 private
50
51 type(c_ptr) :: iterator
52 type(c_ptr) :: end
53
54 contains
55 procedure :: start => sihash_iterator_start
56 procedure :: has_next => sihash_iterator_has_next
57 procedure :: get_next => sihash_iterator_get_next
58 end type sihash_iterator_t
59
60contains
61
62 ! ---------------------------------------------------------
68 subroutine sihash_init(h)
69 type(sihash_t), intent(out) :: h
70
71 interface
72 subroutine sihash_map_init(map) bind(c)
73 use iso_c_binding
74 import
75 implicit none
76
77 type(c_ptr) :: map
78 end subroutine sihash_map_init
79 end interface
80
81
82 call sihash_map_init(h%map)
83
84 end subroutine sihash_init
85
86
87 ! ---------------------------------------------------------
89 subroutine sihash_end(h)
90 type(sihash_t), intent(inout) :: h
91
92 interface
93 subroutine sihash_map_end(map) bind(c)
94 use iso_c_binding
95 import
96 implicit none
97
98 type(c_ptr) :: map
99 end subroutine sihash_map_end
100 end interface
101
102 call sihash_map_end(h%map)
103
104 end subroutine sihash_end
105
106
107 ! ---------------------------------------------------------
109 subroutine sihash_insert(h, key, val)
110 type(sihash_t), intent(inout) :: h
111 character(len=*), intent(in) :: key
112 integer, intent(in) :: val
113
114 interface
115 subroutine sihash_map_insert(map, key, val) bind(c)
116 use iso_c_binding
117 import
118 implicit none
119
120 type(c_ptr), value :: map
121 character(kind=c_char), intent(in) :: key(*)
122 integer(kind=c_int), value :: val
123 end subroutine sihash_map_insert
124 end interface
125
126 call sihash_map_insert(h%map, string_f_to_c(key), val)
127 end subroutine sihash_insert
128
129
130 ! ---------------------------------------------------------
135 integer function sihash_lookup(h, key, found)
136 type(sihash_t), intent(in) :: h
137 character(len=*), intent(in) :: key
138 logical, optional, intent(out) :: found
139
140 interface
141 subroutine sihash_map_lookup(map, key, ifound, val) bind(c)
142 use iso_c_binding
143 implicit none
145 type(c_ptr), value :: map
146 character(kind=c_char), intent(in) :: key(*)
147 integer(kind=c_int), intent(out) :: ifound
148 integer(kind=c_int), intent(out) :: val
149 end subroutine sihash_map_lookup
150 end interface
151
152 integer :: ifound, val
153 character(kind=c_char) :: key_tmp(c_str_len(key))
154
155 key_tmp = string_f_to_c(key)
156 call sihash_map_lookup(h%map, key_tmp, ifound, val)
157
158 found = (ifound == 1)
159
160 sihash_lookup = -1
161 if(found) sihash_lookup = val
162
163 end function sihash_lookup
164
165 ! ---------------------------------------------------------
166 subroutine sihash_iterator_start(this, h)
167 class(sihash_iterator_t), intent(inout) :: this
168 class(sihash_t), intent(in) :: h
169
170 interface
171 subroutine sihash_iterator_low_start(iterator, end, map) bind(c)
172 use iso_c_binding
173 import
174 implicit none
175
176 type(c_ptr) :: iterator
177 type(c_ptr) :: end
178 type(c_ptr), value :: map
179 end subroutine sihash_iterator_low_start
180 end interface
181
182 call sihash_iterator_low_start(this%iterator, this%end, h%map)
183
184 end subroutine sihash_iterator_start
185
186 ! ---------------------------------------------------------
187 logical function sihash_iterator_has_next(this)
188 class(sihash_iterator_t), intent(in) :: this
189
190 integer :: value
191
192 interface
193 subroutine sihash_iterator_low_has_next(iterator, end, value) bind(c)
194 use iso_c_binding
195 import
196 implicit none
197
198 type(c_ptr), value, intent(in) :: iterator
199 type(c_ptr), value, intent(in) :: end
200 integer(kind=c_int), intent(out) :: value
201
202 end subroutine sihash_iterator_low_has_next
203 end interface
204
205 call sihash_iterator_low_has_next(this%iterator, this%end, value)
206
207 sihash_iterator_has_next = (value /= 0)
208
209 end function sihash_iterator_has_next
210
211 ! ---------------------------------------------------------
212 integer function sihash_iterator_get_next(this) result(value)
213 class(sihash_iterator_t), intent(inout) :: this
214
215 interface
216 subroutine sihash_iterator_low_get(iterator, value) bind(c)
217 use iso_c_binding
218 import
219 implicit none
220
221 type(c_ptr), intent(in) :: iterator
222 integer(kind=c_int), intent(out) :: value
223
224 end subroutine sihash_iterator_low_get
225 end interface
226
227 call sihash_iterator_low_get(this%iterator, value)
229 end function sihash_iterator_get_next
230
231
232end module sihash_oct_m
233
234!! Local Variables:
235!! mode: f90
236!! coding: utf-8
237!! End:
This module implements a simple hash table for string valued keys and integer values using the C++ ST...
Definition: sihash.F90:118
integer function sihash_iterator_get_next(this)
Definition: sihash.F90:306
logical function sihash_iterator_has_next(this)
Definition: sihash.F90:281
subroutine, public sihash_insert(h, key, val)
Insert a (key, val) pair into the hash table h.
Definition: sihash.F90:203
subroutine sihash_iterator_start(this, h)
Definition: sihash.F90:260
subroutine, public sihash_init(h)
Initialize a hash table h with size entries. Since we use separate chaining, the number of entries in...
Definition: sihash.F90:162
integer function, public sihash_lookup(h, key, found)
Look up a value in the hash table h. If found is present, it indicates if key could be found in the t...
Definition: sihash.F90:229
subroutine, public sihash_end(h)
Free a hash table.
Definition: sihash.F90:183
integer pure function, public c_str_len(fortran_char)
Convert fortran character length to C character length.
Definition: string.F90:322
character(kind=c_char, len=1) function, dimension(c_str_len(f_string)), public string_f_to_c(f_string)
Definition: string.F90:265