-
Notifications
You must be signed in to change notification settings - Fork 1
/
gfc_tree.F90
1422 lines (1365 loc) · 61.2 KB
/
gfc_tree.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): Tree
!AUTHOR: Dmitry I. Lyakh (Liakh): [email protected], [email protected]
!REVISION: 2018-10-10 (started 2016-02-17)
!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/>.
!NOTES:
! # A tree is a linked derivative of an abstract (unlinked) GFC container.
! A subtree is a tree object that is linked as a part of a larger tree,
! thus having its root element linked to other elements of the larger tree.
! # All accesses, updates, scans, and actions on a tree are performed via
! a tree iterator associated with the tree. When attaching a tree
! to another tree, the attached tree elements can be accessed either
! via its own iterator or via the combined tree iterator. Multiple
! iterators can be associated with a tree at the same time.
!FOR DEVELOPERS ONLY:
module gfc_tree
use gfc_base
use timers
implicit none
private
!PARAMETERS:
!Basic:
integer(INTD), private:: CONS_OUT=6 !output device
logical, private:: VERBOSE=.TRUE. !verbositiy for errors
integer(INTD), private:: DEBUG=0 !debugging level (0:none)
!Sibling selector:
logical, parameter, public:: LEFT_SIBLING=.FALSE.
logical, parameter, public:: RIGHT_SIBLING=.TRUE.
!Tree iterator directions:
integer(INTD), parameter, private:: TREE_IT_DOWN=1 !downward direction
integer(INTD), parameter, private:: TREE_IT_RIGHT=2 !right direction
integer(INTD), parameter, private:: TREE_IT_UP=3 !upward direction
integer(INTD), parameter, private:: TREE_IT_LEFT=4 !left direction
!TYPES:
!Tree vertex with a bi-directional sibling ring (linked) list:
type, extends(gfc_cont_elem_t), public:: tree_vertex_t
class(tree_vertex_t), pointer, private:: parent=>NULL() !link to the parent vertex
class(tree_vertex_t), pointer, private:: next_sibling=>NULL() !link to the next sibling vertex (in a ring)
class(tree_vertex_t), pointer, private:: prev_sibling=>NULL() !link to the previous sibling vertex (in a ring)
class(tree_vertex_t), pointer, private:: first_child=>NULL() !link to the first child vertex
integer(INTL), private:: num_child=0 !total number of children vertices
contains
procedure, private:: TreeVertexConstruct
generic, public:: tree_vertex_ctor=>TreeVertexConstruct !constructs the content of a tree vertex
procedure, non_overridable, public:: num_children=>TreeVertexNumChildren !returns the total number of children
procedure, non_overridable, public:: num_siblings=>TreeVertexNumSiblings !returns the total number of siblings
procedure, non_overridable, public:: is_first_sibling=>TreeVertexIsFirstSibling !returns GFC_TRUE if the vertex is the first in the sibling (ring) list
procedure, non_overridable, public:: is_last_sibling=>TreeVertexIsLastSibling !returns GFC_TRUE if the vertex is the last in the sibling (ring) list
procedure, non_overridable, public:: is_root=>TreeVertexIsRoot !returns GFC_TRUE if the vertex is the root of the tree, GFC_FALSE otherwise
procedure, non_overridable, public:: is_leaf=>TreeVertexIsLeaf !returns GFC_TRUE if the vertex is a leaf, GFC_FALSE otherwise
end type tree_vertex_t
!Tree (all operations on the tree are performend via an iterator):
type, extends(gfc_container_t), public:: tree_t
class(tree_vertex_t), pointer, private:: root=>NULL() !root (boundary) element (beginning)
contains
procedure, public:: is_empty=>TreeIsEmpty !returns GFC_TRUE is the tree is empty, GFC_FALSE otherwise (or error code)
procedure, public:: is_subtree=>TreeIsSubtree !returns TRUE if the tree is a subtree of a larger tree, FALSE otherwise
end type tree_t
!Tree iterator:
type, extends(gfc_iter_t), public:: tree_iter_t
class(tree_vertex_t), pointer, private:: current=>NULL() !currently pointed element of the container
class(tree_t), pointer, private:: container=>NULL() !container
contains
procedure, public:: init=>TreeIterInit !associates the iterator with a container and sets its position to the root element
procedure, public:: reset=>TreeIterReset !resets the iterator to the beginning of the container (root element)
procedure, public:: release=>TreeIterRelease !dissociates the iterator from its container
procedure, public:: pointee=>TreeIterPointee !returns a pointer to the container element currently in focus
procedure, public:: next=>TreeIterNext !moves the iterator to the next element, if any
procedure, public:: previous=>TreeIterPrevious !moves the iterator to the previous element, if any
procedure, public:: move_to_sibling=>TreeIterMoveToSibling!moves the iterator to the next/previous sibling, if any
procedure, public:: move_to_child=>TreeIterMoveToChild !moves the iterator to either the first or the last child, if any
procedure, public:: move_to_parent=>TreeIterMoveToParent !moves the iterator to the parent, if any
procedure, public:: move_up=>TreeIterMoveUp !moves the iterator towards the root a specific number of hops
procedure, public:: move_to_cousin=>TreeIterMoveToCousin !moves the iterator to the next/previous cousin (within the tree level)
procedure, public:: find_first_of_level=>TreeIterFindFirstOfLevel !moves the iterator to the first element at a given tree level
procedure, public:: get_num_children=>TreeIterGetNumChildren !returns the total number of children at the current iterator position
procedure, public:: get_num_siblings=>TreeIterGetNumSiblings !returns the total number of siblings at the current iterator position
procedure, public:: on_first_sibling=>TreeIterOnFirstSibling !returns GFC_TRUE if positioned on the first sibling
procedure, public:: on_last_sibling=>TreeIterOnLastSibling !returns GFC_TRUE if positioned on the last sibling
procedure, public:: get_root=>TreeIterGetRoot !returns a pointer to the tree root
procedure, public:: get_parent=>TreeIterGetParent !returns a pointer to the parent of the current vertex
procedure, public:: get_child=>TreeIterGetChild !returns a pointer to the specific child of the current vertex
procedure, public:: get_level=>TreeIterGetLevel !returns the distance from the root for the current tree vertex
procedure, public:: add_leaf=>TreeIterAddLeaf !adds a new leaf element to the element of the container currently pointed to
procedure, public:: delete_leaf=>TreeIterDeleteLeaf !deletes the leaf pointed to by the iterator (if it is actually a leaf)
procedure, public:: attach_subtree=>TreeIterAttachSubtree !attaches a subtree to the element of the container currently pointed to as the last child
procedure, public:: detach_subtree=>TreeIterDetachSubtree !detaches a subtree beginning from the currently pointed element of the container
procedure, public:: delete_subtree=>TreeIterDeleteSubtree !deletes a subtree beginning from the currently pointed element of the container
procedure, public:: delete_all=>TreeIterDeleteAll !deletes all tree vertices
procedure, public:: jump_=>TreeIterJump !PRIVATE: moves the iterator to a specific tree vertex
end type tree_iter_t
!GLOBAL DATA:
!VISIBILITY:
!tree_vertex_t:
private TreeVertexConstruct
private TreeVertexNumChildren
private TreeVertexNumSiblings
private TreeVertexIsFirstSibling
private TreeVertexIsLastSibling
private TreeVertexIsRoot
private TreeVertexIsLeaf
!tree_t:
private TreeIsEmpty
private TreeIsSubtree
!tree_iter_t:
private TreeIterInit
private TreeIterReset
private TreeIterRelease
private TreeIterPointee
private TreeIterNext
private TreeIterPrevious
private TreeIterMoveToSibling
private TreeIterMoveToChild
private TreeIterMoveToParent
private TreeIterMoveUp
private TreeIterMoveToCousin
private TreeIterFindFirstOfLevel
private TreeIterGetNumChildren
private TreeIterGetNumSiblings
private TreeIterOnFirstSibling
private TreeIterOnLastSibling
private TreeIterGetRoot
private TreeIterGetParent
private TreeIterGetChild
private TreeIterGetLevel
private TreeIterAddLeaf
private TreeIterDeleteLeaf
private TreeIterAttachSubtree
private TreeIterDetachSubtree
private TreeIterDeleteSubtree
private TreeIterDeleteAll
private TreeIterJump
contains
!IMPLEMENTATION:
!---------------------------------------------------------------------------
#if !(defined(__GNUC__) && __GNUC__ < 9)
subroutine TreeVertexConstruct(this,obj,ierr,assoc_only,copy_ctor_f)
#else
subroutine TreeVertexConstruct(this,obj,ierr,assoc_only)
#endif
!Constructs the content of the tree vertex.
implicit none
class(tree_vertex_t), intent(inout):: this !inout: tree vertex
class(*), target, intent(in):: obj !in: value to be stored
integer(INTD), intent(out), optional:: ierr !out: error code
logical, intent(in), optional:: assoc_only !in: if TRUE, the value will be assigned by reference, otherwise by value (allocated): Defaults to FALSE
#if !(defined(__GNUC__) && __GNUC__ < 9)
procedure(gfc_copy_i), optional:: copy_ctor_f !in: generic copy constructor
#endif
integer(INTD):: errc
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
if(present(assoc_only)) then
call this%construct_base(obj,errc,assoc_only,copy_ctor_f)
else
call this%construct_base(obj,errc,copy_ctor_f=copy_ctor_f)
endif
else
#endif
if(present(assoc_only)) then
call this%construct_base(obj,errc,assoc_only=assoc_only)
else
call this%construct_base(obj,errc)
endif
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(present(ierr)) ierr=errc
return
end subroutine TreeVertexConstruct
!---------------------------------------------------------------
function TreeVertexNumChildren(this,ierr) result(nchild)
!Returns the total number of children attached to the tree vertex.
!Complexity: O(1).
implicit none
integer(INTL):: nchild !out: number of children
class(tree_vertex_t), intent(in):: this !in: tree vertex
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
integer(INTD):: errc
errc=GFC_SUCCESS
nchild=this%num_child; if(nchild.lt.0) errc=GFC_CORRUPTED_CONT
if(present(ierr)) ierr=errc
return
end function TreeVertexNumChildren
!--------------------------------------------------------------
function TreeVertexNumSiblings(this,ierr) result(nsibl)
!Returns the total number of siblings for a tree vertex.
!Complexity: O(1).
implicit none
integer(INTL):: nsibl !out: number of siblings
class(tree_vertex_t), intent(in):: this !in: tree vertex
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
integer(INTD):: errc
errc=GFC_SUCCESS
if(associated(this%parent)) then
nsibl=this%parent%num_child-1
else !root vertex cannot have siblings
nsibl=0
endif
if(nsibl.lt.0) errc=GFC_CORRUPTED_CONT
if(present(ierr)) ierr=errc
return
end function TreeVertexNumSiblings
!----------------------------------------------------------
function TreeVertexIsFirstSibling(this) result(res)
!Returns GFC_TRUE if the tree vertex is the first vertex in the sibling list.
implicit none
integer(INTD):: res !out: result {GFC_TRUE,GFC_FALSE}
class(tree_vertex_t), target, intent(in):: this !in: tree vertex
res=GFC_FALSE
if(associated(this%parent)) then
if(associated(this%parent%first_child,this)) res=GFC_TRUE
else !root vertex (only one)
res=GFC_TRUE
endif
return
end function TreeVertexIsFirstSibling
!---------------------------------------------------------
function TreeVertexIsLastSibling(this) result(res)
!Returns GFC_TRUE if the tree vertex is the last vertex in the sibling list.
implicit none
integer(INTD):: res !out: result {GFC_TRUE,GFC_FALSE}
class(tree_vertex_t), intent(in):: this !in: tree vertex
res=GFC_FALSE
if(associated(this%parent)) then
if(associated(this%parent%first_child,this%next_sibling)) res=GFC_TRUE
else !root vertex (only one)
res=GFC_TRUE
endif
return
end function TreeVertexIsLastSibling
!--------------------------------------------------
function TreeVertexIsRoot(this) result(res)
!Returns GFC_TRUE if the vertex is the root of the tree.
implicit none
integer(INTD):: res !out: result {GFC_TRUE,GFC_FALSE}
class(tree_vertex_t), intent(in):: this !in: tree vertex
if(associated(this%parent)) then
res=GFC_FALSE
else
res=GFC_TRUE
endif
return
end function TreeVertexIsRoot
!--------------------------------------------------
function TreeVertexIsLeaf(this) result(res)
!Returns GFC_TRUE if the vertex is a leaf.
implicit none
integer(INTD):: res !out: result
class(tree_vertex_t), intent(in):: this !in: tree vertex
res=GFC_FALSE
if(.not.associated(this%first_child)) res=GFC_TRUE
return
end function TreeVertexIsLeaf
!---------------------------------------------
function TreeIsEmpty(this) result(res)
!Returns GFC_TRUE if the tree is empty, GFC_FALSE otherwise (or error code).
implicit none
integer(INTD):: res !out: result of query (or error code)
class(tree_t), intent(in):: this !in: tree
if(associated(this%root)) then
res=GFC_FALSE
else
res=GFC_TRUE
endif
return
end function TreeIsEmpty
!----------------------------------------------------
function TreeIsSubtree(this,ierr) result(res)
!Returns TRUE if the tree is a subtree of a larger tree.
implicit none
logical:: res !out: result
class(tree_t), intent(in):: this !in: tree
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 TreeIsSubtree
!----------------------------------------------------
function TreeIterInit(this,cont) result(ierr)
!Initializes an iterator and resets it to the beginning of the container.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_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 (tree_t)
this%container=>cont
ierr=this%reset()
class default
ierr=GFC_INVALID_ARGS
end select
return
end function TreeIterInit
!------------------------------------------------
function TreeIterReset(this) result(ierr)
!Resets the iterator to the beginning (root element).
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_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
this%current=>NULL()
ierr=this%set_status_(GFC_IT_NULL)
ierr=GFC_IT_NULL
endif
return
end function TreeIterReset
!--------------------------------------------------
function TreeIterRelease(this) result(ierr)
!Dissociates the iterator from its container.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_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 TreeIterRelease
!--------------------------------------------------------
function TreeIterPointee(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(tree_iter_t), intent(in):: this !in: iterator
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
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 TreeIterPointee
!------------------------------------------------------
function TreeIterNext(this,elem_p) result(ierr)
!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(N), O(N) moving cost may occur in unbalanced trees. No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_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(tree_vertex_t), pointer:: tvp
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
ierr=GFC_SUCCESS
tvp=>this%current%first_child
if(.not.associated(tvp)) then
tvp=>this%current
do while(associated(tvp))
if(.not.associated(tvp,this%container%root)) then !root of a subtree may have a parent
if(tvp%is_last_sibling().eq.GFC_FALSE) then !not the last sibling
tvp=>tvp%next_sibling; exit
else
tvp=>tvp%parent
endif
else
tvp=>NULL()
endif
enddo
endif
if(present(elem_p)) then
elem_p=>tvp
else
call this%current%decr_ref_()
this%current=>tvp
if(associated(this%current)) then
call this%current%incr_ref_()
else
ierr=this%set_status_(GFC_IT_DONE)
endif
endif
if(.not.associated(tvp)) ierr=GFC_NO_MOVE
tvp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function TreeIterNext
!----------------------------------------------------------
function TreeIterPrevious(this,elem_p) result(ierr)
!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(N), O(N) moving cost may occur in unbalanced trees. No additional memory is used.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_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(tree_vertex_t), pointer:: tvp
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 !nothing precedes the root
tvp=>NULL()
else
tvp=>this%current
if(tvp%is_first_sibling().eq.GFC_TRUE) then
tvp=>tvp%parent
else
tvp=>tvp%prev_sibling
do while(associated(tvp%first_child))
tvp=>tvp%first_child%prev_sibling !last sibling among the children (because of ring linking)
enddo
endif
endif
if(present(elem_p)) then
elem_p=>tvp
else
call this%current%decr_ref_()
this%current=>tvp
if(associated(this%current)) then
call this%current%incr_ref_()
else
ierr=this%set_status_(GFC_IT_DONE)
endif
endif
if(.not.associated(tvp)) ierr=GFC_NO_MOVE
tvp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function TreeIterPrevious
!--------------------------------------------------------------------
function TreeIterMoveToSibling(this,to_previous) result(ierr)
!Moves the iterator either to the next or to the previous sibling.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_iter_t), intent(inout):: this !inout: iterator
logical, intent(in), optional:: to_previous !in: if TRUE, the iterator will move to the previous sibling (defaults to FALSE)
class(tree_vertex_t), pointer:: tvp
logical:: to_next
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
if(.not.associated(this%current,this%container%root)) then
if(present(to_previous)) then; to_next=.not.to_previous; else; to_next=.TRUE.; endif
ierr=GFC_SUCCESS
if(to_next) then
tvp=>this%current%next_sibling
if(associated(tvp,this%current%parent%first_child)) then
ierr=GFC_NO_MOVE
else
call this%current%decr_ref_()
this%current=>tvp
if(associated(this%current)) call this%current%incr_ref_()
endif
tvp=>NULL()
else
if(associated(this%current,this%current%parent%first_child)) then
ierr=GFC_NO_MOVE
else
call this%current%decr_ref_()
this%current=>this%current%prev_sibling
if(associated(this%current)) call this%current%incr_ref_()
endif
endif
else
ierr=GFC_NO_MOVE !tree/subtree root does not have siblings within its iterator
endif
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function TreeIterMoveToSibling
!--------------------------------------------------------------
function TreeIterMoveToChild(this,to_last) result(ierr)
!Moves the iterator to either the first or the last child, if any.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_iter_t), intent(inout):: this !inout: iterator
logical, intent(in), optional:: to_last !in: if TRUE, the iterator will move to the last child (defaults to FALSE)
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
if(associated(this%current%first_child)) then
call this%current%decr_ref_()
this%current=>this%current%first_child
call this%current%incr_ref_()
if(present(to_last)) then
if(to_last) then
if(.not.associated(this%current,this%current%prev_sibling)) then !previous sibling of the first child is the last child
call this%current%decr_ref_()
this%current=>this%current%prev_sibling
call this%current%incr_ref_()
endif
endif
endif
ierr=GFC_SUCCESS
else
ierr=GFC_NO_MOVE
endif
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function TreeIterMoveToChild
!-------------------------------------------------------
function TreeIterMoveToParent(this) result(ierr)
!Moves the iterator to the parent, if any.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_iter_t), intent(inout):: this !inout: iterator
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
if(associated(this%current,this%container%root)) then
ierr=GFC_NO_MOVE
else
call this%current%decr_ref_()
this%current=>this%current%parent
if(associated(this%current)) call this%current%incr_ref_()
ierr=GFC_SUCCESS
endif
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function TreeIterMoveToParent
!----------------------------------------------------------
function TreeIterMoveUp(this,num_hops) result(ierr)
!Moves the iterator towards the root a specific number of hops.
implicit none
integer(INTD):: ierr !out: error code
class(tree_iter_t), intent(inout):: this !inout: tree iterator
integer(INTD), intent(in):: num_hops !in: number of hops to move
integer(INTD):: n
ierr=GFC_SUCCESS
if(num_hops.ge.0) then
n=num_hops
do while(n.gt.0)
ierr=this%move_to_parent(); if(ierr.ne.GFC_SUCCESS) exit
n=n-1
enddo
else
ierr=GFC_INVALID_ARGS
endif
return
end function TreeIterMoveUp
!-------------------------------------------------------------------
function TreeIterMoveToCousin(this,to_previous) result(ierr)
!Moves the iterator either to the next or to the previous cousin.
!A cousin is a tree vertex at the same tree level. In case the
!current iterator position has no next/previous cousin, it will
!not change and the error code GFC_NO_MOVE will be returned.
!In case of real errors, the iterator will become undefined.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_iter_t), intent(inout):: this !inout: iterator
logical, intent(in), optional:: to_previous !in: if TRUE, the iterator will move to the previous cousin (defaults to FALSE)
class(tree_vertex_t), pointer:: tvp
logical:: to_prev
integer(INTD):: n
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
tvp=>this%current
if(present(to_previous)) then; to_prev=to_previous; else; to_prev=.FALSE.; endif
ierr=GFC_SUCCESS; n=0
mloop: do while(.not.associated(this%current,this%container%root))
ierr=this%move_to_sibling(to_prev)
if(ierr.eq.GFC_SUCCESS) then
do while(n.gt.0)
ierr=this%move_to_child(to_last=to_prev); if(ierr.ne.GFC_SUCCESS) exit
n=n-1
enddo
if(ierr.ne.GFC_NO_MOVE) exit mloop
else
if(ierr.ne.GFC_NO_MOVE) exit mloop
ierr=this%move_to_parent(); if(ierr.ne.GFC_SUCCESS) exit mloop
n=n+1
endif
enddo mloop
if(ierr.eq.GFC_NO_MOVE) ierr=GFC_ERROR
if(associated(this%current,this%container%root)) then
call this%current%decr_ref_()
this%current=>tvp
call this%current%incr_ref_()
if(ierr.eq.GFC_SUCCESS) ierr=GFC_NO_MOVE !no next/previous sibling (not an error)
else
if(ierr.eq.GFC_SUCCESS.and.n.ne.0) ierr=GFC_ERROR
endif
else
ierr=GFC_CORRUPTED_CONT
endif
endif
return
end function TreeIterMoveToCousin
!-----------------------------------------------------------------
function TreeIterFindFirstOfLevel(this,level) result(ierr)
!Moves the iterator to the first element at a given tree level.
!If no such element exists, returns GFC_NO_MOVE and resets the
!iterator status to GFC_IT_DONE.
implicit none
integer(INTD):: ierr !out: error code
class(tree_iter_t), intent(inout):: this !inout: tree iterator
integer(INTD), intent(in):: level !in: tree level
integer(INTD):: n
ierr=this%reset()
if(ierr.eq.GFC_SUCCESS) then
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
ierr=GFC_SUCCESS
if(level.gt.0) then
n=0
cloop: do
ierr=this%move_to_child()
if(ierr.eq.GFC_SUCCESS) then
n=n+1; if(n.eq.level) exit cloop
elseif(ierr.eq.GFC_NO_MOVE) then
sloop: do
ierr=this%move_to_sibling()
if(ierr.eq.GFC_SUCCESS) then
exit sloop
elseif(ierr.eq.GFC_NO_MOVE) then
if(n.eq.0) then
ierr=GFC_NO_MOVE
n=this%set_status_(GFC_IT_DONE)
exit cloop
endif
ierr=this%move_to_parent()
if(ierr.eq.GFC_SUCCESS) then
n=n-1
else !error
exit cloop
endif
else !error
exit cloop
endif
enddo sloop
else !error
exit cloop
endif
enddo cloop
elseif(level.lt.0) then
ierr=GFC_INVALID_ARGS
endif
endif
endif
return
end function TreeIterFindFirstOfLevel
!----------------------------------------------------------------
function TreeIterGetNumChildren(this,ierr) result(nchild)
!Returns the total number of children at the current iterator position.
implicit none
integer(INTL):: nchild !out: number of children
class(tree_iter_t), intent(in):: this !in: tree iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
nchild=0_INTL; errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) nchild=this%current%num_children(errc)
if(present(ierr)) ierr=errc
return
end function TreeIterGetNumChildren
!---------------------------------------------------------------
function TreeIterGetNumSiblings(this,ierr) result(nsibl)
!Returns the total number of siblings at the current iterator position.
implicit none
integer(INTL):: nsibl !out: number of siblings
class(tree_iter_t), intent(in):: this !in: tree iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
nsibl=0_INTL; errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) nsibl=this%current%num_siblings(errc)
if(present(ierr)) ierr=errc
return
end function TreeIterGetNumSiblings
!-------------------------------------------------------------
function TreeIterOnFirstSibling(this,ierr) result(res)
!Returns GFC_TRUE if the iterator is positioned on the first sibling.
implicit none
integer(INTD):: res !out: {GFC_TRUE,GFC_FALSE,GFC_ERROR}
class(tree_iter_t), intent(in):: this !in: tree iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
res=GFC_ERROR; errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
errc=GFC_SUCCESS; res=this%current%is_first_sibling()
endif
if(present(ierr)) ierr=errc
return
end function TreeIterOnFirstSibling
!------------------------------------------------------------
function TreeIterOnLastSibling(this,ierr) result(res)
!Returns GFC_TRUE if the iterator is positioned on the last sibling.
implicit none
integer(INTD):: res !out: {GFC_TRUE,GFC_FALSE,GFC_ERROR}
class(tree_iter_t), intent(in):: this !in: tree iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
res=GFC_ERROR; errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
errc=GFC_SUCCESS; res=this%current%is_last_sibling()
endif
if(present(ierr)) ierr=errc
return
end function TreeIterOnLastSibling
!-------------------------------------------------------
function TreeIterGetRoot(this,ierr) result(root)
!Returns a pointer to the tree root.
implicit none
class(gfc_cont_elem_t), pointer:: root !out: pointer to the tree root
class(tree_iter_t), intent(in):: this !in: iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
root=>NULL(); errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE.or.errc.eq.GFC_IT_DONE) then
if(associated(this%container)) then
errc=GFC_SUCCESS; root=>this%container%root
if(.not.associated(root)) errc=GFC_CORRUPTED_CONT
else
errc=GFC_CORRUPTED_CONT
endif
endif
if(present(ierr)) ierr=errc
return
end function TreeIterGetRoot
!-----------------------------------------------------------
function TreeIterGetParent(this,ierr) result(parent)
!Returns a pointer to the parent of the current vertex.
implicit none
class(gfc_cont_elem_t), pointer:: parent !out: pointer to the parent
class(tree_iter_t), intent(in):: this !in: iterator
integer(INTD), intent(out), optional:: ierr !out: error code (0:success)
integer(INTD):: errc
errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
if(associated(this%current)) then
parent=>this%current%parent; errc=GFC_SUCCESS
else
parent=>NULL(); errc=GFC_CORRUPTED_CONT
endif
else
parent=>NULL()
endif
if(present(ierr)) ierr=errc
return
end function TreeIterGetParent
!-------------------------------------------------------------------
function TreeIterGetChild(this,child_num,ierr) result(child)
!Returns a pointer to the specific child of the current vertex.
implicit none
class(gfc_cont_elem_t), pointer:: child !out: pointer to the specific child
class(tree_iter_t), intent(in):: this !in: iterator
integer(INTD), intent(in):: child_num !in: child number: [1..last], negative means counting from the last child: -1:last, -2:one before last, etc.
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc,n
class(tree_vertex_t), pointer:: tvp
child=>NULL(); errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
errc=GFC_SUCCESS
if(associated(this%current)) then
if(child_num.gt.0) then
n=child_num-1; tvp=>this%current%first_child
if(associated(tvp)) then
do while(n.gt.0)
if(tvp%is_last_sibling().eq.GFC_TRUE) exit
tvp=>tvp%next_sibling
n=n-1
enddo
if(n.eq.0.and.associated(tvp)) then
child=>tvp; tvp=>NULL()
else
errc=GFC_INVALID_ARGS
endif
else
errc=GFC_INVALID_ARGS
endif
elseif(child_num.lt.0) then
n=-child_num-1; tvp=>this%current%first_child
if(associated(tvp)) then
tvp=>tvp%prev_sibling !last child
do while(n.gt.0)
if(tvp%is_first_sibling().eq.GFC_TRUE) exit
tvp=>tvp%prev_sibling
n=n-1
enddo
if(n.eq.0.and.associated(tvp)) then
child=>tvp; tvp=>NULL()
else
errc=GFC_INVALID_ARGS
endif
else
errc=GFC_INVALID_ARGS
endif
else
errc=GFC_INVALID_ARGS
endif
else
errc=GFC_CORRUPTED_CONT
endif
endif
if(present(ierr)) ierr=errc
return
end function TreeIterGetChild
!---------------------------------------------------------
function TreeIterGetLevel(this,ierr) result(level)
!Returns the distance from the tree root for the current tree vertex.
implicit none
integer(INTD):: level !out: distance from the tree root (in hops)
class(tree_iter_t), intent(inout):: this !in: tree iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
class(tree_vertex_t), pointer:: tvp
level=-1; errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE) then
errc=GFC_SUCCESS; tvp=>this%current
do while(errc.eq.GFC_SUCCESS)
level=level+1
errc=this%move_to_parent()
enddo
if(errc.eq.GFC_NO_MOVE) errc=GFC_SUCCESS
call this%jump_(tvp) !restore the original iterator position
endif
if(present(ierr)) ierr=errc
return
end function TreeIterGetLevel
!------------------------------------------------------------------------------------------
#if !(defined(__GNUC__) && __GNUC__ < 9)
function TreeIterAddLeaf(this,elem_val,assoc_only,no_move,copy_ctor_f) result(ierr)
#else
function TreeIterAddLeaf(this,elem_val,assoc_only,no_move) result(ierr)
#endif
!Creates a new container element (leaf) as the last child of the currently pointed
!element and stores the value <elem_val> in it, either by value or by reference.
implicit none
integer(INTD):: ierr !out: error code (0:success)
class(tree_iter_t), intent(inout):: this !inout: iterator
class(*), target, intent(in):: elem_val !in: value to store in the container
logical, intent(in), optional:: assoc_only !in: TRUE: store by reference, FALSE: store by value (defaults to FALSE)
logical, intent(in), optional:: no_move !in: if TRUE, the iterator will not move to the newly added element (defaults to FALSE)
#if !(defined(__GNUC__) && __GNUC__ < 9)
procedure(gfc_copy_i), optional:: copy_ctor_f !in: user-defined generic copy constructor
#endif
class(tree_vertex_t), pointer:: tvp
integer:: errc
logical:: assoc,nomo
integer(INTL):: nelems
if(present(assoc_only)) then; assoc=assoc_only; else; assoc=.FALSE.; endif
if(present(no_move)) then; nomo=no_move; else; nomo=.FALSE.; endif
ierr=this%get_status()
if(ierr.eq.GFC_IT_ACTIVE) then
ierr=GFC_SUCCESS
if(associated(this%container).and.associated(this%current)) then
if(associated(this%current%first_child)) then
tvp=>this%current%first_child%prev_sibling !last sibling among children
allocate(tvp%next_sibling,STAT=errc)
if(errc.eq.0) then
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
call tvp%next_sibling%tree_vertex_ctor(elem_val,ierr,assoc_only=assoc,copy_ctor_f=copy_ctor_f)
else
#endif
call tvp%next_sibling%tree_vertex_ctor(elem_val,ierr,assoc_only=assoc)
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(ierr.eq.GFC_SUCCESS) then
tvp%next_sibling%prev_sibling=>tvp
tvp=>tvp%next_sibling
else
deallocate(tvp%next_sibling)
endif
else
tvp%next_sibling=>this%current%first_child
ierr=GFC_MEM_ALLOC_FAILED
endif
else
allocate(this%current%first_child,STAT=errc)
if(errc.eq.0) then
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
call this%current%first_child%tree_vertex_ctor(elem_val,ierr,assoc_only=assoc,copy_ctor_f=copy_ctor_f)
else
#endif
call this%current%first_child%tree_vertex_ctor(elem_val,ierr,assoc_only=assoc)
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(ierr.eq.GFC_SUCCESS) then
tvp=>this%current%first_child
else
deallocate(this%current%first_child)
endif
else
this%current%first_child=>NULL()
ierr=GFC_MEM_ALLOC_FAILED
endif
endif
if(ierr.eq.GFC_SUCCESS) then
tvp%parent=>this%current
tvp%next_sibling=>this%current%first_child
this%current%first_child%prev_sibling=>tvp
this%current%num_child=this%current%num_child+1
nelems=this%container%update_num_elems_(1_INTL,ierr); if(ierr.ne.GFC_SUCCESS) ierr=GFC_CORRUPTED_CONT
if(.not.nomo) then
call this%current%decr_ref_()
this%current=>tvp
if(associated(this%current)) call this%current%incr_ref_()
endif
endif
tvp=>NULL()
else
ierr=GFC_CORRUPTED_CONT
endif
elseif(ierr.eq.GFC_IT_EMPTY) then !very first element of the container
ierr=GFC_SUCCESS
if(associated(this%container)) then
if(.not.(associated(this%container%root).or.associated(this%current))) then
allocate(this%container%root,STAT=errc)
if(errc.eq.0) then
#if !(defined(__GNUC__) && __GNUC__ < 9)
if(present(copy_ctor_f)) then
call this%container%root%tree_vertex_ctor(elem_val,ierr,assoc_only=assoc,copy_ctor_f=copy_ctor_f)
else
#endif
call this%container%root%tree_vertex_ctor(elem_val,ierr,assoc_only=assoc)
#if !(defined(__GNUC__) && __GNUC__ < 9)
endif
#endif
if(ierr.eq.GFC_SUCCESS) then
ierr=this%reset() !move to the just added first element regardless of <no_move> and change the EMPTY status to ACTIVE
nelems=this%container%update_num_elems_(1_INTL,ierr); if(ierr.ne.GFC_SUCCESS) ierr=GFC_CORRUPTED_CONT