-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgfc_dictionary.F90
1912 lines (1854 loc) · 86.3 KB
/
gfc_dictionary.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
!Generic Fortran Containers (GFC): Dictionary (ordered map), AVL BST
!AUTHOR: Dmitry I. Lyakh (Liakh): [email protected], [email protected]
!REVISION: 2017/12/20 (recycling my old dictionary implementation)
!Copyright (C) 2014-2017 Dmitry I. Lyakh (Liakh)
!Copyright (C) 2014-2017 Oak Ridge National Laboratory (UT-Battelle)
!This file is part of ExaTensor.
!ExaTensor is free software: you can redistribute it and/or modify
!it under the terms of the GNU Lesser General Public License as published
!by the Free Software Foundation, either version 3 of the License, or
!(at your option) any later version.
!ExaTensor is distributed in the hope that it will be useful,
!but WITHOUT ANY WARRANTY; without even the implied warranty of
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!GNU Lesser General Public License for more details.
!You should have received a copy of the GNU Lesser General Public License
!along with ExaTensor. If not, see <http://www.gnu.org/licenses/>.
!FOR DEVELOPERS ONLY:
module gfc_dictionary
use gfc_base
use gfc_list
use gfc_vector
use timers
implicit none
private
!PARAMETERS:
!Basic:
integer(INTD), private:: CONS_OUT=6 !output device
logical, private:: VERBOSE=.TRUE. !verbosity for errors
integer(INTD), private:: DEBUG=0 !debugging level (0:none)
!Directions:
logical, parameter, public:: GFC_DICT_LEFT=.FALSE.
logical, parameter, public:: GFC_DICT_RIGHT=.TRUE.
logical, parameter, public:: GFC_DICT_SUCCESSOR=.TRUE.
logical, parameter, public:: GFC_DICT_PREDECESSOR=.FALSE.
!Search:
integer(INTD), parameter, public:: GFC_DICT_JUST_FIND=0 !action: just find the key in the dictionary, if it is there
integer(INTD), parameter, public:: GFC_DICT_DELETE_IF_FOUND=1 !action: delete the dictionary element, if key found
integer(INTD), parameter, public:: GFC_DICT_REPLACE_IF_FOUND=2 !action: replace the value of the dictionary element, if key found
integer(INTD), parameter, public:: GFC_DICT_ADD_IF_NOT_FOUND=3 !action: create a new dictionary element, if key not found
integer(INTD), parameter, public:: GFC_DICT_ADD_OR_MODIFY=4 !action: combines ADD_IF_NOT_FOUND and REPLACE_IF_FOUND
integer(INTD), parameter, public:: GFC_DICT_FETCH_IF_FOUND=5 !action: simply fetch the dictionary element value, if key found
!TYPES:
!Dictionary element:
type, extends(gfc_cont_elem_t), public:: dict_elem_t
class(*), pointer, private:: key=>NULL() !dictionary element key
class(dict_elem_t), pointer, private:: child_lt=>NULL() !lesser child element
class(dict_elem_t), pointer, private:: child_gt=>NULL() !greater child element
class(dict_elem_t), pointer, private:: parent=>NULL() !parent element
integer(INTD), private:: balance_fac !balance factor (for AVL BST)
contains
procedure, private:: DictElemConstruct
generic, public:: dict_elem_ctor=>DictElemConstruct !constructs a dictionary element from a key-value pair
procedure, public:: destruct_keyval=>DictElemDestruct !destructs a dictionary element (both key and value)
procedure, non_overridable, public:: is_root=>DictElemIsRoot !returns GFC_TRUE if the element is the root of the dictionary binary search tree
procedure, non_overridable, public:: is_leaf=>DictElemIsLeaf !returns GFC_TRUE if the element is a leaf of the dictionary binary search tree
procedure, public:: get_key=>DictElemGetKey !returns an unlimited polymorphic pointer to the element key
procedure, public:: predicate_key=>DictElemPredicateKey !returns the result of predication on the element key
procedure, public:: compare_key=>DictElemCompareKey !compares the element key with another key
procedure, public:: print_key=>DictElemPrintKey !prints the dictionary element key
procedure, public:: print_it=>DictElemPrintIt !prints the dictionary elemet (key,value)
end type dict_elem_t
!Dictionary (all operations on the dictionary are performed via an iterator):
type, extends(gfc_container_t), public:: dictionary_t
class(dict_elem_t), pointer, private:: root=>NULL() !root of the AVL binary search tree (boundary element)
! class(dict_elem_t), pointer, private:: first=>NULL() !first element (boundary element) `Do I need this?
! class(dict_elem_t), pointer, private:: last=>NULL() !last element (boundary element) `Do I need this?
logical, private:: key_storage=GFC_BY_VAL !dictionary key storage policy
contains
procedure, public:: is_empty=>DictionaryIsEmpty !returns GFC_TRUE if the dictionary is empty, GFC_FALSE otherwise (or error code)
procedure, public:: is_subdictionary=>DictionaryIsSubdictionary !returns TRUE if the dictionary is subdictionary, FALSE otherwise
procedure, public:: set_key_storage=>DictionarySetKeyStorage !sets the key storage policy (by value or by reference), the dictionary must be empty
procedure, private:: reroot_=>DictionaryReroot !PRIVATE: changes the root of the dictionary
end type dictionary_t
!Dictionary iterator:
type, extends(gfc_iter_t), public:: dictionary_iter_t
class(dict_elem_t), pointer, private:: current=>NULL() !currently pointed element of the container
class(dictionary_t), pointer, private:: container=>NULL() !container
contains
procedure, private:: jump_=>DictionaryIterJump !PRIVATE: moves the iterator to an arbitrary position
procedure, public:: init=>DictionaryIterInit !associates the iterator with a container and positions it to the root element
procedure, public:: reset=>DictionaryIterReset !resets the iterator to the beginning of the container (root element)
procedure, public:: release=>DictionaryIterRelease !dissociates the iterator from its container
procedure, public:: pointee=>DictionaryIterPointee !returns a pointer to the container element currently in focus
procedure, public:: next=>DictionaryIterNext !moves the iterator to the "next" element, if any (not necessarily in order)
procedure, public:: previous=>DictionaryIterPrevious !moves the iterator to the "previous" element, if any (not necessarily in order)
procedure, public:: next_in_order=>DictionaryIterNextInOrder !moves the iterator to the next-in-order element, if any
procedure, public:: prev_in_order=>DictionaryIterPrevInOrder !moves the iterator to the previous-in-order element, if any
procedure, public:: move_in_order=>DictionaryIterMoveInOrder !moves the iterator to either the next-in-order or previous-in-order element
procedure, public:: move_to_min=>DictionaryIterMoveToMin !moves the iterator to the minimal element
procedure, public:: move_to_max=>DictionaryIterMoveToMax !moves the iterator to the maximal element
procedure, public:: move_up=>DictionaryIterMoveUp !moves the iterator up the binary search tree (to the parent element)
procedure, public:: move_down=>DictionaryIterMoveDown !moves the iterator down the binary search tree, either left or right
procedure, public:: get_key=>DictionaryIterGetKey !returns a pointer to the key in the current iterator position
procedure, public:: delete_all=>DictionaryIterDeleteAll !deletes all elements of the dictionary
procedure, public:: search=>DictionaryIterSearch !performs a key-based search in the dictionary
procedure, public:: sort_to_list=>DictionaryIterSortToList !returns a list of references to dictionary elements in a sorted (by key) order
procedure, public:: sort_to_vector=>DictionaryIterSortToVector !returns a vector of references to dictionary elements in a sorted (by key) order
end type dictionary_iter_t
!INTERFACES:
!VISIBILITY:
!dict_elem_t:
private DictElemConstruct
private DictElemDestruct
private DictElemIsRoot
private DictElemIsLeaf
private DictElemGetKey
private DictElemPredicateKey
private DictElemCompareKey
private DictElemPrintKey
private DictElemPrintIt
!dictionary_t:
private DictionaryIsEmpty
private DictionaryIsSubdictionary
private DictionarySetKeyStorage
private DictionaryReroot
!dictionary_iter_t:
private DictionaryIterJump
private DictionaryIterInit
private DictionaryIterReset
private DictionaryIterRelease
private DictionaryIterPointee
private DictionaryIterNext
private DictionaryIterPrevious
private DictionaryIterNextInOrder
private DictionaryIterPrevInOrder
private DictionaryIterMoveInOrder
private DictionaryIterMoveToMin
private DictionaryIterMoveToMax
private DictionaryIterMoveUp
private DictionaryIterMoveDown
private DictionaryIterGetKey
private DictionaryIterDeleteAll
private DictionaryIterSearch
private DictionaryIterSortToList
private DictionaryIterSortToVector
!DEFINITIONS:
contains
![dict_elem_t]=============================================================================================
#if !(defined(__GNUC__) && __GNUC__ < 9)
subroutine DictElemConstruct(this,key,val,ierr,assoc_key,assoc_val,key_copy_ctor_f,val_copy_ctor_f)
#else
subroutine DictElemConstruct(this,key,val,ierr,assoc_key,assoc_val)
#endif
!Given a key-value pair, constructs an element of a dictionary. Note
!that if construction fails, the element may be left underconstructed,
!requiring a separate call to the destructor after return.
implicit none
class(dict_elem_t), intent(inout):: this !inout: element of the dictionary
class(*), target, intent(in):: key !in: key to be stored
class(*), target, intent(in):: val !in: value to be stored (either by value or by reference)
integer(INTD), intent(out), optional:: ierr !out: error code
logical, intent(in), optional:: assoc_key !in: if TRUE, <key> will be stored by reference, otherwise by value (default)
logical, intent(in), optional:: assoc_val !in: if TRUE, <val> will be stored by reference, otherwise by value (default)
#if !(defined(__GNUC__) && __GNUC__ < 9)
procedure(gfc_copy_i), optional:: key_copy_ctor_f !in: user-defined generic copy constructor for the key (by value)
procedure(gfc_copy_i), optional:: val_copy_ctor_f !in: user-defined generic copy constructor for the value (by value)
#endif
integer(INTD):: errc
integer:: ier
logical:: assk,assv
errc=GFC_SUCCESS
if(present(assoc_key)) then; assk=assoc_key; else; assk=.FALSE.; endif
if(present(assoc_val)) then; assv=assoc_val; else; assv=.FALSE.; endif
if(this%in_use(errc,set_lock=.TRUE.,report_refs=.FALSE.).eq.GFC_FALSE) then
if(this%is_empty()) then
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(val_copy_ctor_f)) then
call this%construct_base(val,errc,assoc_only=assv,copy_ctor_f=val_copy_ctor_f,locked=.TRUE.)
else
#endif
call this%construct_base(val,errc,assoc_only=assv,locked=.TRUE.)
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(errc.eq.GFC_SUCCESS) then !base constructor succeeded
if(assk) then
this%key=>key
else
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(key_copy_ctor_f)) then
this%key=>key_copy_ctor_f(key,errc)
else
#endif
allocate(this%key,SOURCE=key,STAT=ier)
if(ier.ne.0) errc=GFC_MEM_ALLOC_FAILED
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
endif
endif
else
errc=GFC_ELEM_NOT_EMPTY
endif
if(errc.eq.GFC_SUCCESS) then
call this%release_lock(errc)
else
call this%release_lock()
endif
else
errc=GFC_IN_USE
endif
if(present(ierr)) ierr=errc
return
end subroutine DictElemConstruct
!---------------------------------------------------------------------
subroutine DictElemDestruct(this,ierr,key_assoc,dtor_f,locked)
!Destructs the key-value pair inside the dictionary element.
!<dtor_f> provides an optional explicit destructor for the dictionary
!element value, if needed. Alternatively, the value may have the final
!subroutine defined. In contrast, the dictionary element key must
!have the final subroutine defined if it requires a non-trivial destruction.
implicit none
class(dict_elem_t), intent(inout):: this !inout: element of a dictionary
integer(INTD), intent(out), optional:: ierr !out: error code
logical, intent(in), optional:: key_assoc !in: if TRUE, the key will be assumed stored by reference
procedure(gfc_destruct_i), optional:: dtor_f !in: explicit destructor for the value
logical, intent(in), optional:: locked !in: if TRUE, the dictionary element will be assumed already locked (defaults to FALSE)
integer(INTD):: errc
logical:: assk,lckd,lck
integer:: ier
errc=GFC_SUCCESS
if(present(key_assoc)) then; assk=key_assoc; else; assk=.FALSE.; endif
if(present(locked)) then; lckd=locked; else; lckd=.FALSE.; endif; lck=lckd
if(.not.lck) lck=(this%in_use(errc,set_lock=.TRUE.,report_refs=.FALSE.).eq.GFC_FALSE)
if(lck) then
if(errc.eq.GFC_SUCCESS) then
if(associated(this%key)) then
if(.not.assk) then
if(present(dtor_f)) then
call this%gfc_cont_elem_t%destruct(errc,dtor_f=dtor_f,locked=.TRUE.)
else
call this%gfc_cont_elem_t%destruct(errc,locked=.TRUE.)
endif
deallocate(this%key,STAT=ier)
if(ier.ne.0.and.errc.eq.GFC_SUCCESS) errc=GFC_MEM_FREE_FAILED
endif
this%key=>NULL()
else
errc=GFC_CORRUPTED_CONT
endif
endif
if(.not.lckd) then
if(errc.eq.GFC_SUCCESS) then
call this%release_lock(errc)
else
call this%release_lock()
endif
endif
else
if(errc.eq.GFC_SUCCESS) errc=GFC_IN_USE
endif
if(present(ierr)) ierr=errc
return
end subroutine DictElemDestruct
!------------------------------------------------
function DictElemIsRoot(this) result(res)
implicit none
integer(INTD):: res !out: answer {GFC_TRUE,GFC_FALSE,GFC_ERROR}
class(dict_elem_t), intent(in):: this !in: dictionary element
if(associated(this%parent)) then
res=GFC_FALSE
else
res=GFC_TRUE
endif
return
end function DictElemIsRoot
!------------------------------------------------
function DictElemIsLeaf(this) result(res)
implicit none
integer(INTD):: res !out: answer {GFC_TRUE,GFC_FALSE,GFC_ERROR}
class(dict_elem_t), intent(in):: this !in: dictionary element
if(associated(this%child_lt).or.associated(this%child_gt)) then
res=GFC_FALSE
else
res=GFC_TRUE
endif
return
end function DictElemIsLeaf
!-------------------------------------------------------
function DictElemGetKey(this,ierr) result(key_p)
implicit none
class(*), pointer:: key_p !out: pointer to the element key
class(dict_elem_t), intent(in):: this !in: dictionary element
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=GFC_SUCCESS; key_p=>NULL()
if(.not.this%is_empty()) then
if(associated(this%key)) then
key_p=>this%key
else
errc=GFC_CORRUPTED_CONT
endif
else
errc=GFC_ELEM_EMPTY
endif
if(present(ierr)) ierr=errc
return
end function DictElemGetKey
!-----------------------------------------------------------------------
function DictElemPredicateKey(this,predicat_f,ierr) result(pred)
!Evaluates a user-defined predicate on the key of a given dictionary element.
implicit none
integer(INTD):: pred !out: evaluated predicate value {GFC_TRUE,GFC_FALSE,GFC_ERROR}
class(dict_elem_t), intent(in):: this !in: element of a dictionary
procedure(gfc_predicate_i):: predicat_f !in: user-defined predicate
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=GFC_SUCCESS; pred=GFC_ERROR
if(.not.this%is_empty()) then
pred=predicat_f(this%key); if(pred.eq.GFC_ERROR) errc=GFC_ERROR
else
errc=GFC_ELEM_EMPTY
endif
if(present(ierr)) ierr=errc
return
end function DictElemPredicateKey
!-------------------------------------------------------------------------
function DictElemCompareKey(this,other_key,cmp_f,ierr) result(cmp)
!Compares the dictionary element key (on the left) with another key (on the right).
implicit none
integer(INTD):: cmp !out: result of the comparison (see GFC_CMP_XXX in gfc_base.F90)
class(dict_elem_t), intent(in):: this !in: element of a dictionary whose key is being compared
class(*), intent(in):: other_key !in: the other value to be compared with
procedure(gfc_cmp_i):: cmp_f !in: user-defined comparison function
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=GFC_SUCCESS; cmp=GFC_CMP_NA
if(.not.this%is_empty()) then
cmp=cmp_f(this%key,other_key); if(cmp.eq.GFC_ERROR) errc=GFC_ERROR
else
errc=GFC_ELEM_EMPTY
endif
if(present(ierr)) ierr=errc
return
end function DictElemCompareKey
!------------------------------------------------------------
subroutine DictElemPrintKey(this,print_f,ierr,dev_id)
!Prints the key of a dictionary element using a user-defined print function.
implicit none
class(dict_elem_t), intent(in):: this !in: element of a dictionary
procedure(gfc_print_i):: print_f !in: user-defined printing function
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD), intent(in), optional:: dev_id !in: output device (default to screen, 6)
integer(INTD):: dev,errc
errc=GFC_SUCCESS
if(.not.this%is_empty()) then
if(present(dev_id)) then; dev=dev_id; else; dev=6; endif !defaults to screen
write(dev,'("#GFC container element key:")')
errc=print_f(this%key,dev); if(errc.ne.0) errc=GFC_ACTION_FAILED
else
errc=GFC_ELEM_EMPTY
endif
if(present(ierr)) ierr=errc
return
end subroutine DictElemPrintKey
!---------------------------------------------------------------------------
subroutine DictElemPrintIt(this,print_key_f,print_val_f,ierr,dev_id)
!Prints the dictionary element (key,value).
implicit none
class(dict_elem_t), intent(in):: this !in: dictionary element
procedure(gfc_print_i):: print_key_f !in: key printing function
procedure(gfc_print_i):: print_val_f !in: value printing function
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD), intent(in), optional:: dev_id !in: output device (defaults to screen, 6)
integer(INTD):: dev,errc
errc=GFC_SUCCESS
if(.not.this%is_empty()) then
if(present(dev_id)) then; dev=dev_id; else; dev=6; endif !defaults to screen
write(dev,'("#GFC dictionary element (key,value):")')
call this%print_key(print_key_f,errc,dev)
if(errc.eq.GFC_SUCCESS) call this%print_value(print_val_f,errc,dev)
else
errc=GFC_ELEM_EMPTY
endif
if(present(ierr)) ierr=errc
return
end subroutine DictElemPrintIt
![dictionary_t]=====================================
function DictionaryIsEmpty(this) result(res)
!Returns GFC_TRUE if the dictionary is empty, GFC_FALSE otherwise (or error code).
implicit none
integer(INTD):: res !out: result of query
class(dictionary_t), intent(in):: this !in: dictionary
if(associated(this%root)) then
res=GFC_FALSE
else
res=GFC_TRUE
endif
return
end function DictionaryIsEmpty
!----------------------------------------------------------------
function DictionaryIsSubdictionary(this,ierr) result(res)
!Returns TRUE if the dictionary is a subdictionary of a larger dictionary.
implicit none
logical:: res !out: result
class(dictionary_t), intent(in):: this !in: dictionary
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=GFC_SUCCESS; res=.FALSE.
if(associated(this%root)) then
res=associated(this%root%parent)
else
errc=GFC_EMPTY_CONT
endif
if(present(ierr)) ierr=errc
return
end function DictionaryIsSubdictionary
!-----------------------------------------------------------
subroutine DictionarySetKeyStorage(this,policy,ierr)
!Sets the key storage policy.
implicit none
class(dictionary_t), intent(inout):: this !inout: dictionary (must be empty)
logical, intent(in):: policy !in: storage policy: {GFC_BY_VAL,GFC_BY_REF}
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=GFC_SUCCESS
if(.not.associated(this%root)) then
this%key_storage=policy
else
errc=GFC_INVALID_REQUEST
endif
if(present(ierr)) ierr=errc
return
end subroutine DictionarySetKeyStorage
!-------------------------------------------------
subroutine DictionaryReroot(this,new_root)
!Changes the root of the dictionary.
implicit none
class(dictionary_t), intent(inout):: this !inout: dictionary
class(dict_elem_t), pointer, intent(inout):: new_root !in: pointer to the new root or NULL()
if(associated(this%root)) call this%root%decr_ref_()
this%root=>new_root
if(associated(this%root)) call this%root%incr_ref_()
return
end subroutine DictionaryReroot
![dictionary_iter_t]================================
subroutine DictionaryIterJump(this,new_elem)
!Moves the iterator to an arbitrary specified position.
implicit none
class(dictionary_iter_t), intent(inout):: this !inout: dictionary iterator
class(dict_elem_t), pointer, intent(inout):: new_elem !in: pointer to the new element or NULL()
integer(INTD):: errc,sts
if(associated(this%current)) call this%current%decr_ref_()
this%current=>new_elem
if(associated(this%current)) then
call this%current%incr_ref_()
errc=this%set_status_(GFC_IT_ACTIVE)
else
if(associated(this%container%root)) then
errc=this%set_status_(GFC_IT_DONE)
else
errc=this%set_status_(GFC_IT_EMPTY)
endif
endif
return
end subroutine DictionaryIterJump
!----------------------------------------------------------
function DictionaryIterInit(this,cont) result(ierr)
!Initializes an iterator and resets it to the beginning of the container.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
class(gfc_container_t), target, intent(in):: cont !in: container
ierr=GFC_SUCCESS
select type(cont)
class is (dictionary_t)
this%container=>cont
ierr=this%reset()
class default
ierr=GFC_INVALID_ARGS
end select
return
end function DictionaryIterInit
!------------------------------------------------------
function DictionaryIterReset(this) result(ierr)
!Resets the iterator to the beginning (root element).
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
ierr=GFC_SUCCESS
if(associated(this%container)) then
if(associated(this%current)) call this%current%decr_ref_()
this%current=>this%container%root
if(associated(this%current)) then
call this%current%incr_ref_()
ierr=this%set_status_(GFC_IT_ACTIVE) !non-empty iterator/container
else
ierr=this%set_status_(GFC_IT_EMPTY) !empty iterator/container
endif
call this%reset_count() !reset all iteration counters
else
ierr=this%set_status_(GFC_IT_NULL)
ierr=GFC_IT_NULL
endif
return
end function DictionaryIterReset
!--------------------------------------------------------
function DictionaryIterRelease(this) result(ierr)
!Dissociates the iterator from its container.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
if(associated(this%current)) call this%current%decr_ref_()
this%current=>NULL(); this%container=>NULL()
call this%reset_count(); ierr=this%set_status_(GFC_IT_NULL)
return
end function DictionaryIterRelease
!--------------------------------------------------------------
function DictionaryIterPointee(this,ierr) result(pntee)
!Returns the container element the iterator is currently pointing to.
implicit none
class(gfc_cont_elem_t), pointer:: pntee !out: container element currently pointed to by the iterator
class(dictionary_iter_t), intent(in):: this !in: iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
pntee=>this%current; errc=GFC_SUCCESS
else
pntee=>NULL()
endif
if(present(ierr)) ierr=errc
return
end function DictionaryIterPointee
!------------------------------------------------------------
function DictionaryIterNext(this,elem_p) result(ierr)
!Moves the iterator position to the next element (in some sense).
!If <elem_p> is absent, the iterator moves to the next element, if any.
!If <elem_p> is present, the iterator simply returns the next element in <elem_p> without moving.
!Complexity: O(1)...O(logN). No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
class(dict_elem_t), pointer:: dp
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
ierr=GFC_SUCCESS
dp=>this%current%child_lt
if(.not.associated(dp)) then
dp=>this%current%child_gt
if(.not.associated(dp)) then
dp=>this%current
do while(associated(dp))
if(.not.associated(dp,this%container%root)) then !root of a subdictionary may have a parent
if(associated(dp,dp%parent%child_lt)) then !moving up from the left
if(associated(dp%parent%child_gt)) then
dp=>dp%parent%child_gt; exit
endif
endif
dp=>dp%parent
else
dp=>NULL()
endif
enddo
endif
endif
if(present(elem_p)) then
elem_p=>dp
else
call this%current%decr_ref_()
this%current=>dp
if(associated(this%current)) then
call this%current%incr_ref_()
else
ierr=this%set_status_(GFC_IT_DONE)
endif
endif
if(.not.associated(dp)) ierr=GFC_NO_MOVE
dp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function DictionaryIterNext
!----------------------------------------------------------------
function DictionaryIterPrevious(this,elem_p) result(ierr)
!Moves the iterator position to the previous element (in some sense).
!If <elem_p> is absent, the iterator moves to the previous element, if any.
!If <elem_p> is present, the iterator simply returns the previous element in <elem_p> without moving.
!Complexity: O(1)...O(logN). No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
class(dict_elem_t), pointer:: dp
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
ierr=GFC_SUCCESS
if(associated(this%current,this%container%root)) then
dp=>NULL()
else
dp=>this%current%parent
if(associated(this%current,dp%child_gt).and.associated(dp%child_lt)) then
dp=>dp%child_lt
do
if(associated(dp%child_gt)) then
dp=>dp%child_gt
else
if(associated(dp%child_lt)) then
dp=>dp%child_lt
else
exit
endif
endif
enddo
endif
endif
if(present(elem_p)) then
elem_p=>dp
else
call this%current%decr_ref_()
this%current=>dp
if(associated(this%current)) then
call this%current%incr_ref_()
else
ierr=this%set_status_(GFC_IT_DONE)
endif
endif
if(.not.associated(dp)) ierr=GFC_NO_MOVE
dp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function DictionaryIterPrevious
!-------------------------------------------------------------------
function DictionaryIterNextInOrder(this,elem_p) result(ierr)
!Moves the iterator position to the next-in-order element.
!If <elem_p> is absent, the iterator moves to the next-in-order element, if any.
!If <elem_p> is present, the iterator simply returns the next-in-order element in <elem_p> without moving.
!Complexity: O(1)...O(logN). No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
class(dict_elem_t), pointer:: dp
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
ierr=GFC_SUCCESS
if(associated(this%current%child_gt)) then
dp=>this%current%child_gt
do while(associated(dp%child_lt)); dp=>dp%child_lt; enddo
else
ierr=GFC_NO_MOVE; dp=>this%current
do while(associated(dp%parent).and.(.not.associated(dp,this%container%root)))
if(associated(dp,dp%parent%child_lt)) then
dp=>dp%parent; ierr=GFC_SUCCESS; exit
else
dp=>dp%parent
endif
enddo
if(ierr.eq.GFC_NO_MOVE) dp=>NULL()
endif
if(present(elem_p)) then
elem_p=>dp
else
call this%current%decr_ref_()
this%current=>dp
if(associated(this%current)) then
call this%current%incr_ref_()
else
ierr=this%set_status_(GFC_IT_DONE)
endif
endif
if(.not.associated(dp)) ierr=GFC_NO_MOVE
dp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function DictionaryIterNextInOrder
!-------------------------------------------------------------------
function DictionaryIterPrevInOrder(this,elem_p) result(ierr)
!Moves the iterator position to the previous-in-order element.
!If <elem_p> is absent, the iterator moves to the previous-in-order element, if any.
!If <elem_p> is present, the iterator simply returns the previous-in-order element in <elem_p> without moving.
!Complexity: O(1)...O(logN). No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
class(dict_elem_t), pointer:: dp
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
ierr=GFC_SUCCESS
if(associated(this%current%child_lt)) then
dp=>this%current%child_lt
do while(associated(dp%child_gt)); dp=>dp%child_gt; enddo
else
ierr=GFC_NO_MOVE; dp=>this%current
do while(associated(dp%parent).and.(.not.associated(dp,this%container%root)))
if(associated(dp,dp%parent%child_gt)) then
dp=>dp%parent; ierr=GFC_SUCCESS; exit
else
dp=>dp%parent
endif
enddo
if(ierr.eq.GFC_NO_MOVE) dp=>NULL()
endif
if(present(elem_p)) then
elem_p=>dp
else
call this%current%decr_ref_()
this%current=>dp
if(associated(this%current)) then
call this%current%incr_ref_()
else
ierr=this%set_status_(GFC_IT_DONE)
endif
endif
if(.not.associated(dp)) ierr=GFC_NO_MOVE
dp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function DictionaryIterPrevInOrder
!-----------------------------------------------------------------------------
function DictionaryIterMoveInOrder(this,direction,elem_p) result(ierr)
!Moves the iterator position either to the next-in-order or previous-in-order element.
!If <elem_p> is absent, the iterator moves to the corresponding in-order element, if any.
!If <elem_p> is present, the iterator simply returns the corresponding in-order element
!in <elem_p> without moving. Complexity: O(1)...O(logN). No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
logical, intent(in):: direction !direction: {GFC_DICT_SUCCESSOR,GFC_DICT_PREDECESSOR}
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
if(present(elem_p)) then
if(direction.eqv.GFC_DICT_SUCCESSOR) then
ierr=this%next_in_order(elem_p)
else
ierr=this%prev_in_order(elem_p)
endif
else
if(direction.eqv.GFC_DICT_SUCCESSOR) then
ierr=this%next_in_order()
else
ierr=this%prev_in_order()
endif
endif
return
end function DictionaryIterMoveInOrder
!-----------------------------------------------------------------
function DictionaryIterMoveToMin(this,elem_p) result(ierr)
!Moves the iterator position to the minimal element.
!If <elem_p> is absent, the iterator moves to the minimal element.
!If <elem_p> is present, the iterator simply returns the minimal element in <elem_p> without moving.
!Complexity: O(logN). No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
class(dict_elem_t), pointer:: dp
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%container%root)) then
ierr=GFC_SUCCESS; dp=>this%container%root
do while(associated(dp%child_lt)); dp=>dp%child_lt; enddo
if(present(elem_p)) then
elem_p=>dp
else
call this%current%decr_ref_()
this%current=>dp
call this%current%incr_ref_()
endif
dp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function DictionaryIterMoveToMin
!-----------------------------------------------------------------
function DictionaryIterMoveToMax(this,elem_p) result(ierr)
!Moves the iterator position to the maximal element.
!If <elem_p> is absent, the iterator moves to the maximal element.
!If <elem_p> is present, the iterator simply returns the maximal element in <elem_p> without moving.
!Complexity: O(logN). No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
class(dict_elem_t), pointer:: dp
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%container%root)) then
ierr=GFC_SUCCESS; dp=>this%container%root
do while(associated(dp%child_gt)); dp=>dp%child_gt; enddo
if(present(elem_p)) then
elem_p=>dp
else
call this%current%decr_ref_()
this%current=>dp
call this%current%incr_ref_()
endif
dp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function DictionaryIterMoveToMax
!--------------------------------------------------------------
function DictionaryIterMoveUp(this,elem_p) result(ierr)
!Moves the iterator position up the binary search tree (to the parental element).
!If <elem_p> is absent, the iterator moves up.
!If <elem_p> is present, the iterator simply returns the corresponding element in <elem_p> without moving.
!Complexity: O(1). No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
class(dict_elem_t), pointer:: dp
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
ierr=GFC_SUCCESS; dp=>NULL()
if(associated(this%current%parent)) then
dp=>this%current%parent
else
ierr=GFC_NO_MOVE
endif
if(present(elem_p)) then
elem_p=>dp
else
if(ierr.eq.GFC_SUCCESS) then
call this%current%decr_ref_()
this%current=>dp
call this%current%incr_ref_()
endif
endif
dp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function DictionaryIterMoveUp
!--------------------------------------------------------------------------
function DictionaryIterMoveDown(this,elem_p,direction) result(ierr)
!Moves the iterator position down the binary search tree, either left or right.
!If <elem_p> is absent, the iterator moves down.
!If <elem_p> is present, the iterator simply returns the corresponding element in <elem_p> without moving.
!Complexity: O(1). No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
logical, intent(in), optional:: direction !in: direction: {GFC_DICT_LEFT,GFC_DICT_RIGHT}
class(dict_elem_t), pointer:: dp
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
ierr=GFC_SUCCESS; dp=>NULL()
if(present(direction)) then
if(direction.eqv.GFC_DICT_LEFT) then !move down left
if(associated(this%current%child_lt)) then
dp=>this%current%child_lt
else
ierr=GFC_NO_MOVE
endif
else !move down right
if(associated(this%current%child_gt)) then
dp=>this%current%child_gt
else
ierr=GFC_NO_MOVE
endif
endif
else !move left, if not left, move right
if(associated(this%current%child_lt)) then
dp=>this%current%child_lt
else
if(associated(this%current%child_gt)) then
dp=>this%current%child_gt
else
ierr=GFC_NO_MOVE
endif
endif
endif
if(present(elem_p)) then
elem_p=>dp
else
if(ierr.eq.GFC_SUCCESS) then
call this%current%decr_ref_()
this%current=>dp
call this%current%incr_ref_()
endif
endif
dp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function DictionaryIterMoveDown
!-------------------------------------------------------------
function DictionaryIterGetKey(this,ierr) result(key_p)
!Returns a pointer to the key in the current iterator position.
implicit none
class(*), pointer:: key_p !out: pointer to the current position key
class(dictionary_iter_t), intent(in):: this !in: dictionary iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
key_p=>NULL(); errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
key_p=>this%current%get_key(errc)
else
errc=GFC_ERROR
endif
endif
if(present(ierr)) ierr=errc
return
end function DictionaryIterGetKey
!-----------------------------------------------------------------
function DictionaryIterDeleteAll(this,dtor_f) result(ierr)
!Deletes all dictionary elements, leaving dictionary empty at the end.
!Complexity: O(NLogN)
implicit none
integer(INTD):: ierr !out: error code
class(dictionary_iter_t), intent(inout):: this !inout: dictionary iterator
procedure(gfc_destruct_i), optional:: dtor_f !in: explicit destructor for the dictionary element value
integer(INTD):: errc
class(dict_elem_t), pointer:: dp
integer:: ier
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
ierr=this%reset()
if(ierr.eq.GFC_SUCCESS) then
dloop: do
do while(ierr.eq.GFC_SUCCESS); ierr=this%move_down(); enddo
if(ierr.eq.GFC_NO_MOVE.and.associated(this%current)) then
if(present(dtor_f)) then
call this%current%destruct_keyval(ierr,key_assoc=this%container%key_storage,dtor_f=dtor_f)
else
call this%current%destruct_keyval(ierr,key_assoc=this%container%key_storage)
endif
if(ierr.eq.GFC_SUCCESS) then
if(associated(this%current,this%container%root)) then !last element (root)
if(associated(this%current%parent)) then
if(associated(this%current,this%current%parent%child_lt)) then
this%current%parent%child_lt=>NULL()
elseif(associated(this%current,this%current%parent%child_gt)) then
this%current%parent%child_gt=>NULL()
else
ierr=GFC_CORRUPTED_CONT; exit dloop
endif
endif
call this%current%decr_ref_()
deallocate(this%current,STAT=ier)
if(ier.ne.0) ierr=GFC_MEM_FREE_FAILED
this%current=>NULL(); errc=this%set_status_(GFC_IT_EMPTY)
if(ierr.eq.GFC_SUCCESS) ierr=errc