-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.f90
1446 lines (1251 loc) · 73.7 KB
/
hash.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
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!****m* hash_tables/hash_tables ===============================================!
! NAME !
! hash_tables !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! An implementation of hash tables (associative arrays). !
! !
! Internally the hash table is stored as an allocatable array of doubly !
! linked lists of elements - linked lists are used for collision management !
! (where multiple keywords hash to the same bucket). !
! Each element of a list is used to store either a keyword-value pair, or a !
! keyword-block pair (where the values associated with the keyword are !
! stored in a 1D array of strings). !
! !
! Each table is able to shrink/expand depending on the current load factor !
! of the table, to balance perfomance and memory usage. !
! This can be set on a table by table basis. !
! !
! Caching is also supported in order to increase the performance of repeated !
! access to the same element of a table. !
! This is enabled at the module level. !
!------------------------------------------------------------------------------!
! AUTHOR !
! Aaron Hopkinson !
!****==========================================================================!
module hash_tables
implicit none
private
integer, parameter, public :: str_len = 128 ! max length of strings stored in table
integer, parameter :: i32 = selected_int_kind(9)
integer, parameter :: i64 = selected_int_kind(18)
! Performance related parameters:
integer, parameter :: default_num_buckets = 16
logical, parameter :: default_can_expand = .true.
logical, parameter :: default_can_shrink = .false.
! These values are module level and unable to be changed on a per table basis:
real, parameter :: max_load_factor = 0.75
real, parameter :: min_load_factor = 0.25
real, parameter :: expand_scale = 2.00
real, parameter :: shrink_scale = 1.0/expand_scale
logical, parameter :: enable_cache = .true.
! Will need lists of these for collision management
type :: hash_element
integer :: hash_id
character(len=str_len) :: keyword
character(len=str_len) :: value
character(len=str_len), dimension(:), allocatable :: block
type(hash_element), pointer :: next_element
type(hash_element), pointer :: prev_element
end type hash_element
type :: hash_pointer
type(hash_element), pointer :: head
type(hash_element), pointer :: tail
end type hash_pointer
type, public :: hash_table
logical :: initialized = .false.
real :: load_factor
logical :: can_expand
logical :: can_shrink
integer :: nvalues
integer :: nbuckets
type(hash_pointer), dimension(:), allocatable :: buckets
type(hash_element), pointer :: last_access
character(len=str_len) :: last_keyword
end type hash_table
public :: hash_table_init
public :: hash_table_size
public :: hash_table_query
public :: hash_table_add
public :: hash_table_get
public :: hash_table_remove
public :: hash_table_get_remove
public :: hash_table_get_keywords
public :: hash_table_resize
public :: hash_table_list
public :: hash_table_destroy
public :: hash_function
interface hash_table_add
module procedure hash_table_add_value
module procedure hash_table_add_block
end interface hash_table_add
interface hash_table_get
module procedure hash_table_get_value
module procedure hash_table_get_block
end interface hash_table_get
interface hash_table_get_remove
module procedure hash_table_get_remove_value
module procedure hash_table_get_remove_block
end interface hash_table_get_remove
contains
!****s* hash_tables/hash_table_init ===========================================!
! NAME !
! hash_table_init (PUBLIC) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Sets up the hash table ready for use. !
! Buckets are allocated and internal state variables are reset. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! !
! (to override module defaults): !
! integer, optional, intent(in) :: nbuckets !
! logical, optional, intent(in) :: can_expand !
! logical, optional, intent(in) :: can_shrink !
!------------------------------------------------------------------------------!
! NOTES !
! Table should not already be initialized. !
! !
! If nbuckets is present and <1, then we do not give an error but instead !
! allocate the default number. !
! !
! Optional arguments are necessary only to optimize memory usage/speed. !
!==============================================================================!
subroutine hash_table_init(table, nbuckets, can_expand, can_shrink)
implicit none
type(hash_table), intent(inout) :: table
integer, optional, intent(in) :: nbuckets
logical, optional, intent(in) :: can_expand
logical, optional, intent(in) :: can_shrink
integer :: ibucket, istat
if (table%initialized) stop 'Error in hash_table_init: table already allocated/initialized'
table%nvalues = 0
table%load_factor = 0.0
table%can_shrink = default_can_shrink
table%can_expand = default_can_expand
if (present(can_shrink)) table%can_shrink = can_shrink
if (present(can_expand)) table%can_expand = can_expand
! don't give error if nbuckets < 1, just assign default..
table%nbuckets = default_num_buckets
if (present(nbuckets) .and. (nbuckets .ge. 1)) table%nbuckets = nbuckets
allocate(table%buckets(table%nbuckets), stat=istat)
if (istat .ne. 0) stop 'Error in hash_table_init: could not allocate hash table'
do ibucket = 1, table%nbuckets
nullify(table%buckets(ibucket)%head)
nullify(table%buckets(ibucket)%tail)
end do
if (enable_cache) call hash_table_reset_cache(table)
table%initialized = .true.
end subroutine hash_table_init
!****f* hash_tables/hash_table_size ===========================================!
! NAME !
! hash_table_size (PUBLIC) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Returns number of keyword-value pairs stored in the hash table (a block !
! is treated as a single pair). Note: This is not the number of buckets. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(in) :: table !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
!==============================================================================!
function hash_table_size(table)
type(hash_table), intent(in) :: table
integer :: hash_table_size
if (.not. table%initialized) stop 'Error in hash_table_size: table not initialized'
! externally, we only care about the number of values - not the number of buckets
hash_table_size = table%nvalues
end function hash_table_size
!****f* hash_tables/hash_table_query ==========================================!
! NAME !
! hash_table_query (PUBLIC) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Queries a hash table for a given keyword - returns number of lines that !
! match. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! character(len=*), intent(in) :: keyword !
!------------------------------------------------------------------------------!
! RESULT !
! Returns number of matches for a given keyword. !
! - 0 if keyword doesn't exist !
! - 1 if keyword matches a single value (scalar) !
! - N>1 if the keyword matches a block (number of lines) !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
!==============================================================================!
function hash_table_query(table, keyword)
implicit none
type(hash_table), intent(inout) :: table
character(len=*), intent(in) :: keyword
integer :: hash_table_query
! local vars:
type(hash_element), pointer :: ptr
integer :: hash_id, bucket_id
if (.not. table%initialized) stop 'Error in hash_table_query: table not initialized'
call hash_table_find_element(table, keyword, hash_id, bucket_id, ptr)
if (.not. associated(ptr)) then
! keyword does not exist
hash_table_query = 0
else
if (allocated(ptr%block)) then
hash_table_query = size(ptr%block,1)
else
hash_table_query = 1 ! scalar
end if
end if
end function hash_table_query
!****s* hash_tables/hash_table_add_value ======================================!
! NAME !
! hash_table_add_value (PUBLIC, as hash_table_add) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Adds a keyword value pair to the table. In this case the value is a single !
! line, rather than a block. !
! !
! If the keyword is already in the table, then its value will be overwritten !
! by the new value. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! character(len=*), intent(in) :: keyword !
! character(len=*), intent(in) :: value !
! !
! Note that keyword and value must len_trim to no longer than the module !
! level str_len (as the hash table cannot store this). !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
! !
! It is encouraged that the user calls the generic hash_table_add routine !
! instead. This eliminates having to worry about whether the 'value' is a !
! single line or a block. !
!==============================================================================!
recursive subroutine hash_table_add_value(table, keyword, value)
! must be recursive so that hash table can grow if necessary
implicit none
type(hash_table), intent(inout) :: table
character(len=*), intent(in) :: keyword
character(len=*), intent(in) :: value
! local vars:
type(hash_element), pointer :: element
type(hash_element), pointer :: ptr
integer :: hash_id, bucket_id, istat
if (.not. table%initialized) stop 'Error in hash_table_add_value: table not initialized'
! check we won't lose any information - keyword checked in hash_table_find_element
if (len_trim(value) .gt. str_len) stop 'Error in hash_table_add_value: value too long'
call hash_table_find_element(table, keyword, hash_id, bucket_id, ptr)
! check to see if keyword already exists (already have element with this)
if (associated(ptr)) then
! deallocate if we have a block
if (allocated(ptr%block)) then
deallocate(ptr%block, stat=istat)
if (istat .ne. 0) stop 'Error in hash_table_add_value: could not deallocate block'
end if
! set value
ptr%value = value
nullify(ptr) ! no longer necessary
else
! no, keyword doesn't exist.. create new element and append to list (could be head)
! we know hash_id from output of hash_table_find_element
call hash_table_create_element_value(keyword, value, hash_id, element)
if (enable_cache) call hash_table_update_cache(table, element)
if (associated(table%buckets(bucket_id)%head)) then
! append to list
table%buckets(bucket_id)%tail%next_element => element
element%prev_element => table%buckets(bucket_id)%tail
else
! create new list
table%buckets(bucket_id)%head => element
nullify(element%prev_element)
end if
table%buckets(bucket_id)%tail => element
nullify(element%next_element)
table%nvalues = table%nvalues + 1
table%load_factor = real(table%nvalues)/real(table%nbuckets)
nullify(element) ! no harm in doing this..
if (table%can_expand .and. (table%load_factor .gt. max_load_factor)) call hash_table_resize(table, expand_scale)
end if
end subroutine hash_table_add_value
!****s* hash_tables/hash_table_add_block ======================================!
! NAME !
! hash_table_add_block (PUBLIC, as hash_table_add) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Adds a keyword-block pair to the table. In this case the value is a block !
! rather than a single line. !
! !
! If the keyword is already in the table, then its value will be overwritten !
! by the new block. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! character(len=*), intent(in) :: keyword !
! character(len=*), dimension(:), intent(in) :: block !
! !
! Note that keyword and each line of the block must len_trim to no longer !
! than the module level str_len (as the hash table cannot store this). !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
! !
! It is encouraged that the user calls the generic hash_table_add routine !
! instead. This eliminates having to worry about whether the 'value' is a !
! single line or a block. !
! !
! Note that a block of length 1 (a single line) will be added to the table !
! in the same manner as a single line. (As if done by hash_table_add_value.) !
!==============================================================================!
recursive subroutine hash_table_add_block(table, keyword, block)
! must be recursive so that hash table can grow if necessary
implicit none
type(hash_table), intent(inout) :: table
character(len=*), intent(in) :: keyword
character(len=*), dimension(:), intent(in) :: block
! local vars:
type(hash_element), pointer :: element
type(hash_element), pointer :: ptr
integer :: hash_id, bucket_id, iblock, blocklines, istat
if (.not. table%initialized) stop 'Error in hash_table_add_block: table not initialized'
blocklines = size(block,1)
if (blocklines .lt. 1) stop 'Error in hash_table_add_block: block contains no values'
! check we won't lose any information - keyword checked in hash_table_find_element
do iblock = 1, blocklines
if (len_trim(block(iblock)) .gt. str_len) stop 'Error in hash_table_add_block: value too long in block'
end do
call hash_table_find_element(table, keyword, hash_id, bucket_id, ptr)
! check to see if keyword already exists (already have element with this)
if (associated(ptr)) then
! if block is a single line long - just use the scalar 'value'
if (blocklines .eq. 1) then
ptr%value = block(1)
! deallocate if necessary..
if (allocated(ptr%block)) then
deallocate(ptr%block, stat=istat)
if (istat .ne. 0) stop 'Error in hash_table_add_block: cannot deallocate block'
end if
else
! block is muliple lines - reset 'value' and store in block
! can reuse block if it is exactly the right size
if (allocated(ptr%block) .and. (size(ptr%block,1) .ne. blocklines)) then
deallocate(ptr%block, stat=istat)
if (istat .ne. 0) stop 'Error in hash_table_add_block: cannot deallocate block'
end if
if (.not. allocated(ptr%block)) then
allocate(ptr%block(blocklines), stat=istat)
if (istat .ne. 0) stop 'Error in hash_table_add_block: cannot allocate block'
end if
ptr%value = ' '
ptr%block(:) = block(:)
end if
nullify(ptr) ! no longer necessary
else
! no, keyword doesn't exist.. create new element and append to list (could be head)
! we know hash_id from output of hash_table_find_element
if (blocklines .eq. 1) then
! again, use scalar 'value' if block is one line long
call hash_table_create_element_value(keyword, block(1), hash_id, element)
else
call hash_table_create_element_block(keyword, block, hash_id, element)
end if
if (enable_cache) call hash_table_update_cache(table, element)
if (associated(table%buckets(bucket_id)%head)) then
! append to list
table%buckets(bucket_id)%tail%next_element => element
element%prev_element => table%buckets(bucket_id)%tail
else
! create new list
table%buckets(bucket_id)%head => element
nullify(element%prev_element)
end if
table%buckets(bucket_id)%tail => element
nullify(element%next_element)
table%nvalues = table%nvalues + 1
table%load_factor = real(table%nvalues)/real(table%nbuckets)
nullify(element) ! no harm in doing this..
if (table%can_expand .and. (table%load_factor .gt. max_load_factor)) call hash_table_resize(table, expand_scale)
end if
end subroutine hash_table_add_block
!****s* hash_tables/hash_table_get_value ======================================!
! NAME !
! hash_table_get_value (PUBLIC, as hash_table_get) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Returns the value (single line) associated with the keyword in the hash !
! table. !
! !
! If element_index is provided and is > 1, it is assumed that the keyword !
! is associated with a block and returns that line (line numbers starting !
! at 1). !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! character(len=*), intent(in) :: keyword !
! character(len=str_len), intent(out) :: value !
! !
! integer, optional, intent(in) :: element_index !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
! !
! Keyword assumed to exist in table. Gives error if keyword not found. !
! ie: It is assumed that hash_table_query has already been called. !
!==============================================================================!
subroutine hash_table_get_value(table, keyword, value, element_index)
implicit none
type(hash_table), intent(inout) :: table
character(len=*), intent(in) :: keyword
character(len=str_len), intent(out) :: value
integer, optional, intent(in) :: element_index
! local vars
type(hash_element), pointer :: ptr
integer :: hash_id, bucket_id
if (.not. table%initialized) stop 'Error in hash_table_get_value: table not initialized'
call hash_table_find_element(table, keyword, hash_id, bucket_id, ptr)
! hash_table_get_from_ptr_value will flag error if ptr not associated.. no need to deal with this here
call hash_table_get_from_ptr_value(ptr, value, element_index)
nullify(ptr) ! no harm in doing this..
end subroutine hash_table_get_value
!****s* hash_tables/hash_table_get_block ======================================!
! NAME !
! hash_table_get_block (PUBLIC, as hash_table_get) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Returns the block associated with the keyword in the hash table. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! character(len=*), intent(in) :: keyword !
! character(len=str_len), dimension(:), intent(out) :: block !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
! !
! Keyword assumed to exist in table. Gives error if keyword not found. !
! ie: It is assumed that hash_table_query has already been called. !
! !
! The 'block' array *must* be allocated and large enough to store the entire !
! block. (Size given by hash_table_query) !
!==============================================================================!
subroutine hash_table_get_block(table, keyword, block)
implicit none
type(hash_table), intent(inout) :: table
character(len=*), intent(in) :: keyword
character(len=str_len), dimension(:), intent(out) :: block
! local vars
type(hash_element), pointer :: ptr
integer :: hash_id, bucket_id
if (.not. table%initialized) stop 'Error in hash_table_get_block: table not initialized'
call hash_table_find_element(table, keyword, hash_id, bucket_id, ptr)
! hash_table_get_from_ptr_block will flag error if ptr not associated.. no need to deal with this here
call hash_table_get_from_ptr_block(ptr, block)
nullify(ptr) ! no harm in doing this..
end subroutine hash_table_get_block
!****s* hash_tables/hash_table_remove =========================================!
! NAME !
! hash_table_remove (PUBLIC) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Removes a keyword-value pair from the hash table (the value associated !
! with a keyword can be a block). !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! character(len=*), intent(in) :: keyword !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
! !
! Keyword must be in table (else we give an error) - ie: hash_table_query !
! assumed to be > 0. !
!==============================================================================!
subroutine hash_table_remove(table, keyword)
implicit none
type(hash_table), intent(inout) :: table
character(len=*), intent(in) :: keyword
! local vars:
type(hash_element), pointer :: ptr
integer :: hash_id, bucket_id
if (.not. table%initialized) stop 'Error in hash_table_remove: table not initialized'
call hash_table_find_element(table, keyword, hash_id, bucket_id, ptr)
call hash_table_remove_from_ptr(table, ptr)
if (enable_cache) call hash_table_reset_cache(table)
end subroutine hash_table_remove
!****s* hash_tables/hash_table_get_remove_value ===============================!
! NAME !
! hash_table_get_remove_value (PUBLIC, as hash_table_get_remove) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Gets a single line value associated with a keyword from a table and then !
! removes the keyword-value pair from the hash table. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! character(len=*), intent(in) :: keyword !
! character(len=str_len), intent(out) :: value !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
! !
! The return/requested value must be stored as a single line in the hash !
! table. !
!==============================================================================!
subroutine hash_table_get_remove_value(table, keyword, value)
implicit none
type(hash_table), intent(inout) :: table
character(len=*), intent(in) :: keyword
character(len=str_len), intent(out) :: value
! local vars
type(hash_element), pointer :: ptr
integer :: hash_id, bucket_id
if (.not. table%initialized) stop 'Error in hash_table_get_remove_value: table not initialized'
call hash_table_find_element(table, keyword, hash_id, bucket_id, ptr)
call hash_table_get_from_ptr_value(ptr, value)
call hash_table_remove_from_ptr(table, ptr)
if (enable_cache) call hash_table_reset_cache(table)
end subroutine hash_table_get_remove_value
!****s* hash_tables/hash_table_get_remove_block ===============================!
! NAME !
! hash_table_get_remove_block (PUBLIC, as hash_table_get_remove) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Gets a block associated with a keyword from a table and then removes the !
! keyword-block pair from the hash table. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! character(len=*), intent(in) :: keyword !
! character(len=str_len), dimension(:), intent(out) :: block !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
! !
! Output (block) array must be large enough to store the entire associated !
! block. !
!==============================================================================!
subroutine hash_table_get_remove_block(table, keyword, block)
implicit none
type(hash_table), intent(inout) :: table
character(len=*), intent(in) :: keyword
character(len=str_len), dimension(:), intent(out) :: block
! local vars
type(hash_element), pointer :: ptr
integer :: hash_id, bucket_id
if (.not. table%initialized) stop 'Error in hash_table_get_remove_block: table not initialized'
call hash_table_find_element(table, keyword, hash_id, bucket_id, ptr)
call hash_table_get_from_ptr_block(ptr, block)
call hash_table_remove_from_ptr(table, ptr)
if (enable_cache) call hash_table_reset_cache(table)
end subroutine hash_table_get_remove_block
!****s* hash_tables/hash_table_get_keywords ===================================!
! NAME !
! hash_table_get_keywords (PUBLIC) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Returns to an array all of the keywords that are associated with values !
! (single line or blocks) in the table. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(in) :: table !
! character(len=*), dimension(:), intent(out) :: keywords !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
! !
! Output array must be allocated to a size large enough to store all of the !
! keywords in the table. (Assume hash_table_size has been called). !
!==============================================================================!
subroutine hash_table_get_keywords(table, keywords)
implicit none
type(hash_table), intent(in) :: table
character(len=*), dimension(:), intent(out) :: keywords
! local vars
type(hash_element), pointer :: ptr
integer :: ibucket, ikeyword
if (.not. table%initialized) stop 'Error in hash_table_get_keywords: table not initialized'
if (size(keywords,1) .lt. table%nvalues) &
& stop 'Error in hash_table_get_keywords: keyword array too small'
if (len(keywords) .lt. str_len) &
& stop 'Error in hash_table_get_keywords: keyword array character length too small'
ikeyword = 1
if (table%nvalues .gt. 0) then
do ibucket = 1, table%nbuckets
! if bucket in use..
if (associated(table%buckets(ibucket)%head)) then
ptr => table%buckets(ibucket)%head
! loop over elements in bucket (until end of list)
do
keywords(ikeyword) = ptr%keyword
ikeyword = ikeyword + 1
if (.not. associated(ptr%next_element)) exit
ptr => ptr%next_element
end do
end if
end do
end if
end subroutine hash_table_get_keywords
!****s* hash_tables/hash_table_resize =========================================!
! NAME !
! hash_table_resize (PUBLIC) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Shrinks or expands a hash table (used if the enable_expand or !
! enable_shrink flags are set) by temporarily creating a new hash table of !
! same size as the existing one, copying each element into this temporary !
! table, resizing the initial table, copying all of the elements back into !
! the original table and then destroying the temporary table. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! real, intent(in) :: scale_factor !
!------------------------------------------------------------------------------!
! NOTES !
! Routine sufficiently low level that we assume the table is initialized. !
! It is expected that the end user won't call this routine themselves and !
! will instead make use of automatic table resizing. Hence we don't check !
! for table initialization. !
!==============================================================================!
subroutine hash_table_resize(table, scale_factor)
implicit none
type(hash_table), intent(inout) :: table
real, intent(in) :: scale_factor
! local vars:
type(hash_table) :: tmp_table
type(hash_element), pointer :: ptr
integer :: ibucket, istat
! create new table with same number of buckets
call hash_table_init(tmp_table, table%nbuckets)
! copy pointers to head and tail for each bucket
do ibucket = 1, table%nbuckets
if (associated(table%buckets(ibucket)%head)) then
tmp_table%buckets(ibucket)%head => table%buckets(ibucket)%head
nullify(table%buckets(ibucket)%head)
end if
if (associated(table%buckets(ibucket)%tail)) then
tmp_table%buckets(ibucket)%tail => table%buckets(ibucket)%tail
nullify(table%buckets(ibucket)%tail)
end if
end do
! deallocate and reinitialize with new number of buckets
deallocate(table%buckets, stat=istat)
if (istat .ne. 0) stop 'Error in hash_table_resize: cannot deallocate buckets array'
table%initialized = .false. ! to allow for reinitialization without calling hash_table_destroy
call hash_table_init(table, int(tmp_table%nbuckets*scale_factor))
nullify(ptr)
! loop through old (tmp table), for each item in list, create new elements for new table
do ibucket = 1, tmp_table%nbuckets
! if list in use
if (associated(tmp_table%buckets(ibucket)%head)) then
ptr => tmp_table%buckets(ibucket)%head
! loop through each element and create copy for new table
do
if (allocated(ptr%block)) then
call hash_table_add_block(table, ptr%keyword, ptr%block)
else
call hash_table_add_value(table, ptr%keyword, ptr%value)
end if
! exit at end of list
if (.not. associated(ptr%next_element)) exit
ptr => ptr%next_element
end do
end if
end do
! just for safety, reset the cache of the new table (although it should be fine)
if (enable_cache) call hash_table_reset_cache(table)
! free up memory and recalculate load factor for new table
call hash_table_destroy(tmp_table)
table%load_factor = real(table%nvalues)/real(table%nbuckets)
end subroutine hash_table_resize
!****s* hash_tables/hash_table_list ===========================================!
! NAME !
! hash_table_list (PUBLIC) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Outputs (most of) the content of the hash table. Used for debugging. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(in) :: table !
! !
! integer, optional, intent(in) :: unit_num !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
! !
! If unit_num is specified, can write to this unit - else, write to stdout. !
! Note that unit_num must be open/writable etc.. not done in here. !
! !
! Formatting set up for an str_len of 100.. larger than this will look bad. !
!==============================================================================!
subroutine hash_table_list(table, unit_num)
use iso_fortran_env, only: output_unit
implicit none
type(hash_table), intent(in) :: table
integer, optional, intent(in) :: unit_num
! local vars
type(hash_element), pointer :: ptr
integer :: ibucket, iblock
integer :: out_unit
if (.not. table%initialized) stop 'Error in hash_table_list: table not initialized'
!+----------------------------------------------------------------------------------------------------------------------+
!| hash table |
!+----------------------------------------------------------------------------------------------------------------------+
!| general info : |
!| nbuckets : xxxxxxxxxx | nvalues : xxxxxxxxxx | load_factor : x.xxxx | can_expand : x | can_shrink : x |
!| |
!| warning: str_len is larger than this routine is able to print (str_len > 100) |
!+----------------------------------------------------------------------------------------------------------------------+
!| hash_id : xxxxxxxxxxx | bucket_id : xxxxxxxxxx |
!| keyword : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
!| value : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
!+----------------------------------------------------------------------------------------------------------------------+
if (.not. present(unit_num)) then
out_unit = output_unit
else
out_unit = unit_num
end if
write(out_unit, fmt=100)
write(out_unit, "('| hash table', T120, '|')")
write(out_unit, fmt=100)
write(out_unit, "('| general info :', T120, '|')")
write(out_unit, fmt=300) table%nbuckets, table%nvalues, table%load_factor, table%can_expand, table%can_shrink
! explain that we may truncate output.. (output to fit in window of 120 characters)
if (str_len > 100) then
write(out_unit, fmt=200)
write(out_unit, "('|', T7, A, T120, '|')") "warning: str_len is larger than this routine is able to print (str_len > 100)"
end if
write(out_unit, fmt=100)
do ibucket = 1, table%nbuckets
! if bucket in use..
if (associated(table%buckets(ibucket)%head)) then
ptr => table%buckets(ibucket)%head
! loop over elements in bucket (until end of list)
do
write(out_unit, "('|', T7, 'hash_id : ', I11, T55, '| bucket_id : ', I10, T120, '|')") ptr%hash_id, ibucket
write(out_unit, "('|', T7, 'keyword : ', A100, T120, '|')") ptr%keyword
if (allocated(ptr%block)) then
do iblock = 1, size(ptr%block,1)
if (iblock .eq. 1) then
write(out_unit, "('|', T7, 'block : ', A100, T120, '|')") ptr%block(iblock)
else
write(out_unit, "('|', T18, A100, T120, '|')") ptr%block(iblock)
end if
end do
else
write(out_unit, "('|', T7, 'value : ', A100, T120, '|')") ptr%value
end if
write(out_unit, fmt=100)
if (.not. associated(ptr%next_element)) exit
ptr => ptr%next_element
end do
end if
end do
100 format ('+', 118('-'), '+') ! top border
200 format ('|', T120, '|') ! empty line
300 format &
& ('| nbuckets : ',I10,' | nvalues : ',I10,' | load_factor : ',F6.4,' | can_expand :',L2,' | can_shrink :',L2,T120,'|')
end subroutine hash_table_list
!****s* hash_tables/hash_table_destroy ========================================!
! NAME !
! hash_table_destroy (PUBLIC) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Destroys a hash table, freeing up memory and allowing it to be reused. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
!------------------------------------------------------------------------------!
! NOTES !
! Table must be initialized. !
!==============================================================================!
subroutine hash_table_destroy(table)
implicit none
type(hash_table), intent(inout) :: table
! local variables:
type(hash_element), pointer :: ptr, tmp_ptr
integer :: ibucket, istat
! for safety this routine nullifies every pointer even if the element is deallocated
! should hopefully be left with no dangling pointers anywhere..
if (.not. table%initialized) stop 'Error in hash_table_destroy: table not initialized'
nullify(ptr)
do ibucket = 1, table%nbuckets
! if bucket in use..
if (associated(table%buckets(ibucket)%tail)) then
ptr => table%buckets(ibucket)%tail
! loop over all elements in bucket (from tail -> head)
do
! point to previous element (if we can)
nullify(tmp_ptr)
if (associated(ptr%prev_element)) tmp_ptr => ptr%prev_element
! remove element
nullify(ptr%next_element, ptr%prev_element)
if (allocated(ptr%block)) then
deallocate(ptr%block, stat=istat)
if (istat .ne. 0) stop 'Error in hash_table_destroy: could not deallocate block'
end if
deallocate(ptr, stat=istat)
if (istat .ne. 0) stop 'Error in hash_table_destroy: could not deallocate element'
! exit if no previous element, else move backwards
if (.not. associated(tmp_ptr)) exit
ptr => tmp_ptr
end do
end if
nullify(table%buckets(ibucket)%head, table%buckets(ibucket)%tail)
end do
deallocate(table%buckets, stat=istat)
if (istat .ne. 0) stop 'Error in hash_table_destroy: could not deallocate buckets'
table%nvalues = 0
table%nbuckets = 0
table%load_factor = 0.0
table%initialized = .false.
if (enable_cache) call hash_table_reset_cache(table)
end subroutine hash_table_destroy
!==============================================================================!
! PRIVATE ROUTINES BELOW HERE: !
!==============================================================================!
!****s* hash_tables/hash_table_find_element ===================================!
! NAME !
! hash_table_find_element (PRIVATE) !
!------------------------------------------------------------------------------!
! DESCRIPTION !
! Returns a pointer to an element of a hash table associated with the !
! keyword. If keyword does not exist in the table, then pointer is null. !
! !
! The hash_id and bucket_id corresponding to this element are also returned. !
!------------------------------------------------------------------------------!
! ARGUMENTS !
! type(hash_table), intent(inout) :: table !
! character(len=*), intent(in) :: keyword !
! integer, intent(out) :: hash_id !
! integer, intent(out) :: bucket_id !
! type(hash_element), pointer :: ptr !
!------------------------------------------------------------------------------!
! NOTES !
! Since this is a low level routine (returns a pointer) we assume that the !
! table is initialized and everything is allocated as expected. !
!==============================================================================!
subroutine hash_table_find_element(table, keyword, hash_id, bucket_id, ptr)
implicit none
type(hash_table), intent(inout) :: table
character(len=*), intent(in) :: keyword
integer, intent(out) :: hash_id
integer, intent(out) :: bucket_id
type(hash_element), pointer :: ptr
! Returns pointer to element containing keyword if it exists - null otherwise (or bucket not in use)
! hash_id and bucket_id are also returned
! check the table can fit in the keyword..
if (len_trim(keyword) .gt. str_len) stop 'Error in hash_table_find_element: keyword too long'
hash_id = hash_function(trim(keyword))
bucket_id = modulo(hash_id, table%nbuckets) + 1
nullify(ptr)
! check to see if cache already has correct element..
if (enable_cache) then
if (associated(table%last_access) .and. (table%last_keyword .eq. keyword)) then
! sanity check:
if (hash_id .ne. table%last_access%hash_id) stop 'Error in hash_table_find_element: contradictory hash_id'
! last_access is the correct element
ptr => table%last_access
end if
end if
! if we used cache value, then won't go into here..
if (.not. associated(ptr)) then
! check to see if bucket is in use