forked from jl2922/fhash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfhash.f90
More file actions
818 lines (668 loc) · 25.3 KB
/
fhash.f90
File metadata and controls
818 lines (668 loc) · 25.3 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
#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
! --------------------------------+-----------------------------------------------------
! FHASH_NAME <Name> | The name of the type of FHASH table.
! |
! 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.
! KEYS_EQUAL_FUNC <function> | (optional) function that returns whether two keys
! | are equal. Defaults to `a == b` or `all(a == b)`,
! | depending on whether the key is a scalar.
! |
! 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.
!
! HASH_FUNC | (optional) hash function name. Defaults to 'hash'.
! |
! VALUE_POINTER | (optional) If defined, the values are pointers.
! VALUE_ASSIGNMENT | (internal) The assignment operator, do not set it
! | anywhere, it is configured based on VALUE_POINTER
#endif
#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_NAME,_mod)
#define FHASH_TYPE_NAME CONCAT(FHASH_NAME,_t)
#define FHASH_TYPE_ITERATOR_NAME CONCAT(FHASH_NAME,_iter_t)
#define FHASH_TYPE_KV_TYPE_NAME CONCAT(FHASH_NAME,_kv_t)
#define FHASH_SORT_KV_NAME CONCAT(sort_,FHASH_NAME)
! For some bizar reason both gfortran-10 and ifort-2021.4 fail to compile, unless
! this function has a unique name for every time that this file is included:
#define __COMPARE_AT_IDX CONCAT(fhash_type_compare__,FHASH_NAME)
#ifdef VALUE_POINTER
# define VALUE_ASSIGNMENT =>
#else
# define VALUE_ASSIGNMENT =
#endif
! Not all compilers implement finalization:
#if defined __GFORTRAN__ && __GNUC__ <= 5
#else
# define _FINAL_IS_IMPLEMENTED
#endif
#ifdef _FINAL_IS_IMPLEMENTED
# define _FINAL_TYPEORCLASS type
#else
# define _FINAL_TYPEORCLASS class
#endif
module 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
public :: FHASH_TYPE_KV_TYPE_NAME
public :: FHASH_SORT_KV_NAME ! for convenience, because it's hard for the users to write a generic sort
! (that circumvents the compiler bugs when passing pointers to internal functions to `qsort`)
type :: FHASH_TYPE_KV_TYPE_NAME
KEY_TYPE :: key
VALUE_TYPE :: value
end type
type :: node_type
type(FHASH_TYPE_KV_TYPE_NAME), allocatable :: kv
type(node_type), pointer :: next => null()
contains
! Return the length of the linked list start from the current node.
procedure, non_overridable :: node_depth
! No FINAL procedure here, because it would have to be recursive (at least
! implicitly, because it finalizes the 'next' pointer), and a recursive
! procedure is not performant.
! Fortunately this type is not public, and it gets deallocated when finalizing the fhash.
end type
type FHASH_TYPE_NAME
private
integer :: n_keys = 0
type(node_type), contiguous, pointer :: buckets(:) => null()
contains
! Returns the number of buckets.
procedure, non_overridable, public :: bucket_count
! Return the number of collisions.
procedure, non_overridable, public :: n_collisions
! Reserve certain number of buckets.
procedure, non_overridable, public :: reserve
! Returns number of keys.
procedure, non_overridable, public :: key_count
! Set the value at a given a key.
procedure, non_overridable, public :: set
! Get the value at the given key.
procedure, non_overridable, public :: get
#ifndef VALUE_POINTER
generic :: get_ptr => get_ptr_or_autoval, get_ptr_or_null
procedure, non_overridable, public :: get_ptr_or_null
procedure, non_overridable, public :: get_ptr_or_autoval
#endif
! Remove the value with the given key.
procedure, non_overridable, public :: remove
! Get the key/value pairs as a list:
procedure, non_overridable, public :: as_list
procedure, non_overridable, public :: as_sorted_list
! Return the accumalated storage size of an fhash, including the underlying pointers.
! Takes the bit size of a key-value pair as an argument.
procedure, non_overridable, public :: deep_storage_size => fhash_deep_storage_size
! Clear all the allocated memory
procedure, non_overridable, public :: clear
#ifdef _FINAL_IS_IMPLEMENTED
final :: clear_final
#endif
generic, public :: assignment(=) => deepcopy_fhash
procedure, non_overridable, private :: deepcopy_fhash
procedure, non_overridable, private :: key2bucket
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, non_overridable, public :: begin
! Get the key value of the next element and advance the iterator.
procedure, non_overridable, public :: next
end type
interface default_hash
module procedure :: default_hash__int
module procedure :: default_hash__int_array
end interface
interface all
module procedure :: scalar_all
end interface
interface
integer function compare_keys_i(a, b)
import
implicit none
KEY_TYPE, intent(in) :: a, b
end function
end interface
procedure(compare_keys_i), pointer :: global_compare_ptr => null()
type(FHASH_TYPE_KV_TYPE_NAME), pointer :: global_sorted_kv_list_ptr(:) => null()
contains
logical function keys_equal(a, b)
KEY_TYPE, intent(in) :: a, b
#ifdef KEYS_EQUAL_FUNC
keys_equal = KEYS_EQUAL_FUNC(a, b)
#else
keys_equal = all(a == b)
#endif
end function
function bucket_count(this)
class(FHASH_TYPE_NAME), intent(in) :: this
integer :: bucket_count
if (.not. associated(this%buckets)) then
bucket_count = 0
else
bucket_count = size(this%buckets)
endif
end function
function n_collisions(this)
class(FHASH_TYPE_NAME), intent(in) :: this
integer :: n_collisions
integer :: i
call assert(associated(this%buckets), "n_collisions: fhash has not been initialized")
n_collisions = 0
do i = 1, size(this%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(in) :: this
integer :: depth
if (.not. associated(this%next)) then
depth = 1
else
depth = 1 + node_depth(this%next)
endif
end function
impure elemental subroutine reserve(this, n_buckets)
class(FHASH_TYPE_NAME), intent(out) :: this
integer, intent(in) :: n_buckets
integer :: i
integer, parameter :: 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]
integer, parameter :: n = size(sizes)
call assert(sizes(2:) - sizes(:n-1) > 0, "PROGRAMMING ERROR: sizes should be strictly increasing")
call assert(sizes(n) >= n_buckets, "Did not expect to need this many buckets.")
do i = 1, n
if (sizes(i) >= n_buckets) then
allocate(this%buckets(sizes(i)))
exit
endif
enddo
end subroutine
impure elemental function key_count(this)
class(FHASH_TYPE_NAME), intent(in) :: 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
call assert(associated(this%buckets), "set: fhash has not been initialized")
bucket_id = this%key2bucket(key)
call node_set(this%buckets(bucket_id), 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)
! 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)
type(node_type), intent(inout) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, intent(in) :: value
logical, intent(out) :: is_new
if (.not. allocated(this%kv)) then
allocate(this%kv)
this%kv%key = key
this%kv%value VALUE_ASSIGNMENT value
is_new = .true.
else if (keys_equal(this%kv%key, key)) then
this%kv%value VALUE_ASSIGNMENT value
is_new = .false.
else
if (.not. associated(this%next)) allocate(this%next)
call node_set(this%next, key, value, is_new)
endif
end subroutine
subroutine get(this, key, value, success)
class(FHASH_TYPE_NAME), intent(in) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, intent(out) :: value
logical, optional, intent(out) :: success
integer :: bucket_id
call assert(associated(this%buckets), "get: fhash has not been initialized")
bucket_id = this%key2bucket(key)
call node_get(this%buckets(bucket_id), key, value, success)
end subroutine
recursive subroutine node_get(this, key, value, success)
! 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.
type(node_type), intent(in) :: 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 (keys_equal(this%kv%key, key)) then
value VALUE_ASSIGNMENT this%kv%value
if (present(success)) success = .true.
elseif (.not. associated(this%next)) then
if (present(success)) success = .false.
else
call node_get(this%next, key, value, success)
endif
end subroutine
#ifndef VALUE_POINTER
function get_ptr_or_null(this, key) result(value)
class(FHASH_TYPE_NAME), intent(in) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, pointer :: value
integer :: bucket_id
type(node_type), pointer :: bucket
call assert(associated(this%buckets), "get: fhash has not been initialized")
bucket_id = this%key2bucket(key)
call assert(1 <= bucket_id .and. bucket_id <= size(this%buckets), "get: fhash has not been initialized")
bucket => this%buckets(bucket_id)
value => node_get_ptr_or_null(bucket, key)
end function
recursive function node_get_ptr_or_null(this, key) result(value)
type(node_type), target, intent(in) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, pointer :: value
if (.not. allocated(this%kv)) then
value => null()
else if (keys_equal(this%kv%key, key)) then
value => this%kv%value
else if (.not. associated(this%next)) then
value => null()
else
value => node_get_ptr_or_null(this%next, key)
endif
end function
function get_ptr_or_autoval(this, key, autoval) result(value)
class(FHASH_TYPE_NAME), intent(inout) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, intent(in) :: autoval
VALUE_TYPE, pointer :: value
integer :: bucket_id
type(node_type), pointer :: bucket
logical :: is_new
call assert(associated(this%buckets), "get: fhash has not been initialized")
bucket_id = this%key2bucket(key)
call assert(1 <= bucket_id .and. bucket_id <= size(this%buckets), "get: fhash has not been initialized")
bucket => this%buckets(bucket_id)
call node_get_ptr_or_autoval(bucket, key, value, is_new, autoval)
if (is_new) this%n_keys = this%n_keys + 1
end function
recursive subroutine node_get_ptr_or_autoval(this, key, value, is_new, autoval)
type(node_type), target, intent(inout) :: this
KEY_TYPE, intent(in) :: key
VALUE_TYPE, pointer, intent(out) :: value
logical, intent(out) :: is_new
VALUE_TYPE, intent(in) :: autoval
if (.not. allocated(this%kv)) then
allocate(this%kv)
this%kv%key = key
this%kv%value = autoval
value => this%kv%value
is_new = .true.
else if (keys_equal(this%kv%key, key)) then
value => this%kv%value
is_new = .false.
else if (.not. associated(this%next)) then
allocate(this%next)
allocate(this%next%kv)
this%next%kv%key = key
this%next%kv%value = autoval
value => this%next%kv%value
is_new = .true.
else
call node_get_ptr_or_autoval(this%next, key, value, is_new, autoval)
endif
end subroutine
#endif
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
logical :: locSuccess
type(node_type), pointer :: first, temp
call assert(associated(this%buckets), "remove: fhash has not been initialized")
bucket_id = this%key2bucket(key)
first => this%buckets(bucket_id)
if (.not. allocated(first%kv)) then
locSuccess = .false.
elseif (.not. keys_equal(first%kv%key, key)) then
call node_remove(first, key, locSuccess)
elseif (associated(first%next)) then
call move_alloc(first%next%kv, first%kv)
temp => first%next
first%next => first%next%next
deallocate(temp)
locSuccess = .true.
else
deallocate(first%kv)
locSuccess = .true.
endif
if (locSuccess) this%n_keys = this%n_keys - 1
if (present(success)) success = locSuccess
end subroutine
recursive subroutine node_remove(last, key, success)
! 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
type(node_type), intent(inout) :: last
KEY_TYPE, intent(in) :: key
logical, intent(out) :: success
type(node_type), pointer :: next
next => last%next
if (.not. allocated(next%kv)) then
success = .false.
else if (keys_equal(next%kv%key, key)) then
last%next => next%next
deallocate(next%kv)
success = .true.
else if (.not. associated(next%next)) then
success = .false.
else
call node_remove(next, key, success)
endif
end subroutine
subroutine as_list(this, kv_list)
class(FHASH_TYPE_NAME), target, intent(in) :: this
type(FHASH_TYPE_KV_TYPE_NAME), intent(out) :: kv_list(:)
integer :: i, n
type(FHASH_TYPE_ITERATOR_NAME) :: iter
integer :: iter_stat
n = this%key_count()
call assert(size(kv_list) == n, "as_list: kv_list has a bad size")
call iter%begin(this)
do i = 1, n
call iter%next(kv_list(i)%key, kv_list(i)%value, iter_stat)
call assert(iter_stat == 0, "as_list: internal error: iterator stopped unexpectedly")
enddo
end subroutine
subroutine as_sorted_list(this, kv_list, compare)
class(FHASH_TYPE_NAME), target, intent(in) :: this
type(FHASH_TYPE_KV_TYPE_NAME), target, intent(out) :: kv_list(:)
procedure(compare_keys_i) :: compare
call this%as_list(kv_list)
call FHASH_SORT_KV_NAME(kv_list, compare)
end subroutine
subroutine FHASH_SORT_KV_NAME(kv_list, compare)
type(FHASH_TYPE_KV_TYPE_NAME), target, intent(inout) :: kv_list(:)
procedure(compare_keys_i) :: compare
call assert(.not. (associated(global_compare_ptr) .or. associated(global_sorted_kv_list_ptr)), &
"It looks like I am already sorting, and this is not thread-safe.")
global_compare_ptr => compare
global_sorted_kv_list_ptr => kv_list
call permute(kv_list, sorting_perm())
global_compare_ptr => null()
global_sorted_kv_list_ptr => null()
end subroutine
subroutine permute(x, perm)
! Performs
! x = x(perm)
! but (i) this is more efficient, and (ii) ifort appears to put `x(perm)` on
! the stack before copying, causing a segfault for large arrays.
use, intrinsic :: iso_c_binding, only: c_int
use, intrinsic :: iso_fortran_env, only: int8, int16
type(FHASH_TYPE_KV_TYPE_NAME), intent(inout) :: x(:)
integer(c_int), intent(in) :: perm(:)
type(FHASH_TYPE_KV_TYPE_NAME) :: temp
integer :: i, n, j, jnew
integer, parameter :: smallest_int = merge(int8, int16, int8 > 0)
logical(smallest_int), allocatable :: done(:)
call assert(size(x) == size(perm), "INTERNAL ERROR: permute: inconsistent sizes")
n = size(x)
allocate(done(n))
done = .false._smallest_int
do i = 1, n
! Follow the permutations, which form a cycle:
j = i
temp = x(i)
do
if (done(j)) exit
jnew = perm(j)
if (jnew == i) then
x(j) = temp
else
x(j) = x(jnew)
endif
done(j) = .true._smallest_int
j = jnew
enddo
enddo
end subroutine
impure elemental subroutine deepcopy_fhash(lhs, rhs)
class(FHASH_TYPE_NAME), intent(out) :: lhs
type(FHASH_TYPE_NAME), intent(in) :: rhs
integer :: i
if (.not. associated(rhs%buckets)) return
lhs%n_keys = rhs%n_keys
allocate(lhs%buckets(size(rhs%buckets)))
do i = 1, size(lhs%buckets)
call deepcopy_node(rhs%buckets(i), lhs%buckets(i))
enddo
end subroutine
recursive subroutine deepcopy_node(this, copy)
class(node_type), intent(in) :: this
type(node_type), intent(out) :: copy
if (.not. allocated(this%kv)) then
call assert(.not. associated(this%next), 'internal error: node has a "next" pointer, but it''s kv pair has not been set')
else
allocate(copy%kv, source=this%kv)
endif
if (associated(this%next)) then
allocate(copy%next)
call deepcopy_node(this%next, copy%next)
endif
end subroutine
impure elemental integer function fhash_deep_storage_size(this, keyval_ss) result(s)
class(FHASH_TYPE_NAME), intent(in) :: this
integer, intent(in) :: keyval_ss
integer :: i
s = storage_size(this)
if (associated(this%buckets)) then
do i = 1, size(this%buckets)
s = s + node_deep_storage_size(this%buckets(i), keyval_ss)
enddo
endif
end function
recursive integer function node_deep_storage_size(node, keyval_ss) result(s)
type(node_type), intent(in) :: node
integer, intent(in) :: keyval_ss
s = storage_size(node) + keyval_ss
if (associated(node%next)) s = s + node_deep_storage_size(node%next, keyval_ss)
end function
impure elemental subroutine clear(this)
class(FHASH_TYPE_NAME), intent(inout) :: this
integer :: i
this%n_keys = 0
if (associated(this%buckets)) then
do i = 1, size(this%buckets)
call clear_children(this%buckets(i))
if (allocated(this%buckets(i)%kv)) deallocate(this%buckets(i)%kv)
enddo
deallocate(this%buckets)
endif
end subroutine
#ifdef _FINAL_IS_IMPLEMENTED
impure elemental subroutine clear_final(this)
type(FHASH_TYPE_NAME), intent(inout) :: this
call this%clear()
end subroutine
#endif
subroutine clear_children(node)
! Not a recursive subroutine, because (i) this is much more performant, and
! (ii) gfortran thinks that it cannot be both elemental and recursive.
_FINAL_TYPEORCLASS(node_type), intent(inout) :: node
type(node_type), pointer :: prev, next
next => node%next
do
if (.not. associated(next)) return
prev => next
next => prev%next
deallocate(prev)
enddo
end subroutine
integer function key2bucket(this, key) result(bucket_id)
class(FHASH_TYPE_NAME), intent(in) :: this
KEY_TYPE, intent(in) :: key
integer :: hash
#ifdef HASH_FUNC
hash = HASH_FUNC(key)
#else
hash = default_hash(key)
#endif
bucket_id = modulo(hash, size(this%buckets)) + 1
end function
subroutine begin(this, fhash_target)
class(FHASH_TYPE_ITERATOR_NAME), intent(inout) :: this
type(FHASH_TYPE_NAME), target, intent(in) :: fhash_target
call assert(associated(fhash_target%buckets), "cannot start iteration when fhash is empty")
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
call assert(associated(this%fhash_ptr), "next: iterator has not been initialized")
do
if (associated(this%node_ptr)) then
if (allocated(this%node_ptr%kv)) exit
endif
if (this%bucket_id < size(this%fhash_ptr%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
integer function default_hash__int(key) result(hash)
integer, intent(in) :: key
hash = key
end function
integer function default_hash__int_array(key) result(hash)
integer, intent(in) :: key(:)
real(kind(1.0d0)), parameter :: phi = (sqrt(5.0d0) + 1) / 2
! Do not use `nint` intrinsic, because ifort claims that "Fortran 2018 specifies that
! "an elemental intrinsic function here be of type integer or character and
! each argument must be an initialization expr of type integer or character":
integer, parameter :: magic_number = 0.5d0 + 2.0d0**bit_size(hash) * (1 - 1 / phi)
integer :: i
hash = 0
do i = 1, size(key)
! This triggers an error in `gfortran` (version 9.3.0) with the `-ftrapv` option.
! Compiler bug?
hash = ieor(hash, key(i) + magic_number + ishft(hash, 6) + ishft(hash, -2))
enddo
end function
logical function scalar_all(scal)
logical, intent(in) :: scal
scalar_all = scal
end function
impure elemental subroutine assert(condition, msg)
use, intrinsic :: iso_fortran_env, only: error_unit
logical, intent(in) :: condition
character(*), intent(in) :: msg
if (.not. condition) then
write(error_unit, '(a)') msg
error stop
endif
end subroutine
integer(c_int) function __COMPARE_AT_IDX(c_a, c_b) bind(C)
use, intrinsic :: iso_c_binding, only: c_int, c_ptr, c_f_pointer
type(c_ptr), value :: c_a, c_b
integer(c_int), pointer :: f_a, f_b
call c_f_pointer(c_a, f_a)
call c_f_pointer(c_b, f_b)
__COMPARE_AT_IDX = int(global_compare_ptr(global_sorted_kv_list_ptr(f_a)%key, &
global_sorted_kv_list_ptr(f_b)%key), kind=c_int)
end function
function sorting_perm() result(perm)
use, intrinsic :: iso_c_binding
integer(c_int), allocatable, target :: perm(:)
integer(c_int) :: i, n
type(c_funptr) :: fun
interface
subroutine c_qsort(array, elem_count, elem_size, compare) bind(C, name="qsort")
! The function pointer has the interface
! int(*compar)(const void *, const void *)
use, intrinsic :: iso_c_binding
implicit none
type(c_ptr), value :: array
integer(c_size_t), value :: elem_count
integer(c_size_t), value :: elem_size
type(c_funptr), value :: compare
end subroutine
end interface
call assert(associated(global_sorted_kv_list_ptr) .and. associated(global_compare_ptr), &
"internal error: global sorting state has not been set yet")
n = size(global_sorted_kv_list_ptr, kind=c_int)
allocate(perm(n))
do i = 1, n
perm(i) = i
enddo
fun = c_funloc(__COMPARE_AT_IDX)
if (n > 0_c_int) call c_qsort(c_loc(perm(1)), int(n, kind=c_size_t), c_sizeof(perm(1)), fun)
end function
end module
#undef FHASH_NAME
#undef FHASH_MODULE_NAME
#undef FHASH_TYPE_NAME
#undef FHASH_TYPE_ITERATOR_NAME
#undef FHASH_TYPE_KV_TYPE_NAME
#undef HASH_FUNC
#undef _FINAL_IS_IMPLEMENTED
#undef _FINAL_TYPEORCLASS
#undef __COMPARE_AT_IDX
#undef KEY_TYPE
#undef KEYS_EQUAL_FUNC
#undef VALUE_TYPE
#undef VALUE_TYPE_INIT
#undef VALUE_ASSIGNMENT
#undef CONCAT
#undef PASTE