-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfhash.f90
425 lines (352 loc) · 13 KB
/
fhash.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#ifdef __DO_NOT_PREPROCESS_DOC__
! Hash table implementation imitating to GCC STL (with singly linked list).
! DO NOT COMPILE THIS TEMPLATE FILE DIRECTLY.
! Use a wrapper module and include this file instead, e.g. fhash_modules.f90.
! Remove is not implemented since not needed currently.
!
! #define | meaning
! --------------------------------+-----------------------------------------------------
! SHORTNAME <Name> | (optional) The name of the type this FHASH table is
! | for. If set, it overrides all settings that have
! | have possibly been made for FHASH_MODULE_NAME,
! | FHASH_TYPE_NAME and FHASH_TYPE_ITERATOR_NAME.
! |
! FHASH_MODULE_NAME <Name> | The name of the module that encapsulates the FHASH
! | types and functionality
! FHASH_TYPE_NAME <Name> | The name of the actual FHASH type
! FHASH_TYPE_ITERATOR_NAME <Name> | The name of the FHASH type that can iterate through
! | the whole FHASH
! |
! KEY_USE <use stmt> | (optional) A use statement that is required to use
! | a specific type as a key for the FHASH
! KEY_TYPE <typename> | The type of the keys. May require KEY_USE to be
! | accessible.
! |
! VALUE_USE <use stmt> | (optional) A use statement that is required to use
! | a specific type as a value for the FHASH
! VALUE_TYPE <typename> | The type of the values. May require VALUE_USE to be
! | accessible.
! |
! VALUE_VALUE | Flag indicating that the values in FHASH are value
! | values. This is the default. (see VALUE_POINTER)
! VALUE_POINTER | Flag indicating that the values in FHASH are value
! | pointers.
! VALUE_ASSIGNMENT | (internal) The assignment operator, do not set it
! | anywhere, it is configured based on VALUE_VALUE or
! | VALUE_POINTER
#endif
#ifdef SHORTNAME
#undef FHASH_MODULE_NAME
#undef FHASH_TYPE_NAME
#undef FHASH_TYPE_ITERATOR_NAME
#ifdef __GFORTRAN__
#define PASTE(a) a
#define CONCAT(a,b) PASTE(a)b
#else
#define PASTE(a,b) a ## b
#define CONCAT(a,b) PASTE(a,b)
#endif
#define FHASH_MODULE_NAME CONCAT(fhash_module__,SHORTNAME)
#define FHASH_TYPE_NAME CONCAT(fhash_type__,SHORTNAME)
#define FHASH_TYPE_ITERATOR_NAME CONCAT(fhash_type_iterator__,SHORTNAME)
#endif
#undef VALUE_ASSIGNMENT
#ifndef VALUE_VALUE
#ifndef VALUE_POINTER
#define VALUE_VALUE
#endif
#endif
#ifdef VALUE_POINTER
#define VALUE_ASSIGNMENT =>
#else
#define VALUE_ASSIGNMENT =
#endif
module FHASH_MODULE_NAME
#undef FHASH_MODULE_NAME
#ifdef KEY_USE
KEY_USE
#undef KEY_USE
#endif
#ifdef VALUE_USE
VALUE_USE
#undef VALUE_USE
#endif
implicit none
private
public :: FHASH_TYPE_NAME
public :: FHASH_TYPE_ITERATOR_NAME
type kv_type
KEY_TYPE :: key
VALUE_TYPE :: value
end type
type node_type
type(kv_type), allocatable :: kv
type(node_type), pointer :: next => null()
contains
! If kv is not allocated, allocate and set to the key, value passed in.
! If key is present and the same as the key passed in, overwrite the value.
! Otherwise, defer to the next node (allocate if not allocated)
procedure :: node_set
! If kv is not allocated, fail and return 0.
! If key is present and the same as the key passed in, return the value in kv.
! If next pointer is associated, delegate to it.
! Otherwise, fail and return 0.
procedure :: node_get
! If kv is not allocated, fail and return
! If key is present and node is first in bucket, set first node in bucket to
! the next node of first. Return success
! If key is present and the node is another member of the linked list, link the
! previous node's next node to this node's next node, deallocate this node,
! return success
! Otherwise, fail and return 0
procedure :: node_remove
! Deallocate kv is allocated.
! Call the clear method of the next node if the next pointer associated.
! Deallocate and nullify the next pointer.
procedure :: node_clear
! Return the length of the linked list start from the current node.
procedure :: node_depth
end type
type FHASH_TYPE_NAME
private
integer :: n_buckets = 0
integer :: n_keys = 0
type(node_type), allocatable :: buckets(:)
contains
! Returns the number of buckets.
procedure, public :: bucket_count
! Return the number of collisions.
procedure, public :: n_collisions
! Reserve certain number of buckets.
procedure, public :: reserve
! Returns number of keys.
procedure, public :: key_count
! Set the value at a given a key.
procedure, public :: set
! Get the value at the given key.
procedure, public :: get
! Remove the value with the given key.
procedure, public :: remove
! Clear all the allocated memory (must be called to prevent memory leak).
procedure, public :: clear
end type
type FHASH_TYPE_ITERATOR_NAME
private
integer :: bucket_id
type(node_type), pointer :: node_ptr => null()
type(FHASH_TYPE_NAME), pointer :: fhash_ptr => null()
contains
! Set the iterator to the beginning of a hash table.
procedure, public :: begin
! Get the key value of the next element and advance the iterator.
procedure, public :: next
end type
contains
function bucket_count(this)
class(FHASH_TYPE_NAME), intent(inout) :: this
integer :: bucket_count
bucket_count = this%n_buckets
end function
function n_collisions(this)
class(FHASH_TYPE_NAME), intent(inout) :: this
integer :: n_collisions
integer :: i
n_collisions = 0
do i = 1, this%n_buckets
n_collisions = n_collisions + node_depth(this%buckets(i)) - 1
enddo
end function
recursive function node_depth(this) result(depth)
class(node_type), intent(inout) :: this
integer :: depth
if (.not. associated(this%next)) then
depth = 1
else
depth = 1 + node_depth(this%next)
endif
end function
subroutine reserve(this, n_buckets)
class(FHASH_TYPE_NAME), intent(inout) :: this
integer, intent(in) :: n_buckets
integer, dimension(29) :: sizes
integer :: i
if (this%key_count() > 0) stop 'Cannot reserve when fhash is not empty.'
sizes = (/5, 11, 23, 47, 97, 199, 409, 823, 1741, 3469, 6949, 14033, &
& 28411, 57557, 116731, 236897, 480881, 976369,1982627, 4026031, &
& 8175383, 16601593, 33712729, 68460391, 139022417, 282312799, &
& 573292817, 1164186217, 2147483647/)
do i = 1, size(sizes)
if (sizes(i) >= n_buckets) then
this%n_buckets = sizes(i)
allocate(this%buckets(this%n_buckets))
return
endif
enddo
end subroutine
function key_count(this)
class(FHASH_TYPE_NAME), intent(inout) :: this
integer :: key_count
key_count = this%n_keys
end function
subroutine set(this, key, value)
class(FHASH_TYPE_NAME), intent(inout) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, intent(in) :: value
integer :: bucket_id
logical :: is_new
bucket_id = modulo(hash_value(key), this%n_buckets) + 1
call this%buckets(bucket_id)%node_set(key, value, is_new)
if (is_new) this%n_keys = this%n_keys + 1
end subroutine
recursive subroutine node_set(this, key, value, is_new)
class(node_type), intent(inout) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, intent(in) :: value
logical, optional, intent(out) :: is_new
if (.not. allocated(this%kv)) then
allocate(this%kv)
this%kv%key = key
this%kv%value VALUE_ASSIGNMENT value
if (present(is_new)) is_new = .true.
else if (this%kv%key == key) then
this%kv%value VALUE_ASSIGNMENT value
if (present(is_new)) is_new = .false.
else
if (.not. associated(this%next)) allocate(this%next)
call this%next%node_set(key, value, is_new)
endif
end subroutine
subroutine get(this, key, value, success)
class(FHASH_TYPE_NAME), intent(inout) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, intent(out) :: value
logical, optional, intent(out) :: success
integer :: bucket_id
bucket_id = modulo(hash_value(key), this%n_buckets) + 1
call this%buckets(bucket_id)%node_get(key, value, success)
end subroutine
recursive subroutine node_get(this, key, value, success)
class(node_type), intent(inout) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, intent(out) :: value
logical, optional, intent(out) :: success
if (.not. allocated(this%kv)) then
! Not found. (Initial node in the bucket not set)
if (present(success)) success = .false.
else if (this%kv%key == key) then
value VALUE_ASSIGNMENT this%kv%value
if (present(success)) success = .true.
else if (associated(this%next)) then
call this%next%node_get(key, value, success)
else
if (present(success)) success = .false.
endif
end subroutine
subroutine remove(this, key, success)
class(FHASH_TYPE_NAME), intent(inout) :: this
KEY_TYPE, intent(in) :: key
logical, optional, intent(out) :: success
integer :: bucket_id
type(node_type) :: first
logical :: locSuccess
bucket_id = modulo(hash_value(key), this%n_buckets) + 1
first = this%buckets(bucket_id)
if (allocated(first%kv)) then
if (first%kv%key == key) then
if (associated(first%next)) then
this%buckets(bucket_id)%kv%key = this%buckets(bucket_id)%next%kv%key
this%buckets(bucket_id)%kv%value VALUE_ASSIGNMENT this%buckets(bucket_id)%next%kv%value
deallocate(first%next%kv)
this%buckets(bucket_id)%next => this%buckets(bucket_id)%next%next
else
deallocate(this%buckets(bucket_id)%kv)
endif
locSuccess = .true.
else
call node_remove(first%next, key, locSuccess, first)
end if
else
locSuccess = .false.
endif
if (locSuccess) this%n_keys = this%n_keys - 1
if (present(success)) success = locSuccess
end subroutine
recursive subroutine node_remove(this, key, success, last)
class(node_type), intent(inout) :: this, last
KEY_TYPE, intent(in) :: key
logical, intent(out) :: success
if (.not. allocated(this%kv)) then
! Not found. (Initial node in the bucket not set)
success = .false.
else if (this%kv%key == key) then
last%next => this%next
nullify(this%next)
deallocate(this%kv)
success = .true.
else if (associated(this%next)) then
call this%next%node_remove(key, success, this)
else
success = .false.
endif
end subroutine
subroutine clear(this)
class(FHASH_TYPE_NAME), intent(inout) :: this
integer :: i
if (.not. allocated(this%buckets)) return
do i = 1, size(this%buckets)
if (associated(this%buckets(i)%next)) then
call this%buckets(i)%next%node_clear()
deallocate(this%buckets(i)%next)
endif
enddo
deallocate(this%buckets)
this%n_keys = 0
this%n_buckets = 0
end subroutine
recursive subroutine node_clear(this)
class(node_type), intent(inout) :: this
if (associated(this%next)) then
call this%next%node_clear()
deallocate(this%next)
nullify(this%next)
endif
end subroutine
subroutine begin(this, fhash_target)
class(FHASH_TYPE_ITERATOR_NAME), intent(inout) :: this
type(FHASH_TYPE_NAME), target, intent(in) :: fhash_target
this%bucket_id = 1
this%node_ptr => fhash_target%buckets(1)
this%fhash_ptr => fhash_target
end subroutine
subroutine next(this, key, value, status)
class(FHASH_TYPE_ITERATOR_NAME), intent(inout) :: this
KEY_TYPE, intent(out) :: key
VALUE_TYPE, intent(out) :: value
integer, optional, intent(out) :: status
do while (.not. associated(this%node_ptr) .or. .not. allocated(this%node_ptr%kv))
if (this%bucket_id < this%fhash_ptr%n_buckets) then
this%bucket_id = this%bucket_id + 1
this%node_ptr => this%fhash_ptr%buckets(this%bucket_id)
else
if (present(status)) status = -1
#ifdef VALUE_TYPE_INIT
value VALUE_ASSIGNMENT VALUE_TYPE_INIT
#endif
return
endif
enddo
key = this%node_ptr%kv%key
value VALUE_ASSIGNMENT this%node_ptr%kv%value
if (present(status)) status = 0
this%node_ptr => this%node_ptr%next
end subroutine
end module
#undef KEY_TYPE
#undef VALUE_TYPE
#undef VALUE_TYPE_INIT
#undef VALUE_ASSIGNMENT
#undef FHASH_TYPE_NAME
#undef FHASH_TYPE_ITERATOR_NAME
#undef SHORTNAME
#undef CONCAT
#undef PASTE