-
Notifications
You must be signed in to change notification settings - Fork 1
/
gfc_graph.F90
1422 lines (1359 loc) · 63.7 KB
/
gfc_graph.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): Graph
!AUTHOR: Dmitry I. Lyakh (Liakh): [email protected], [email protected]
!REVISION: 2017/11/14
!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:
! # GFC::graph contains graph vertices (<graph_vertex_t>) and graph links (<graph_link_t>).
! Graph vertices are internally stored in a gfc::vector, making the dynamic type of the
! pointer returned by the graph iterator (method .pointee) to be <vector_elem_t>.
module gfc_graph
use gfc_base
use gfc_vector
use gfc_list
use gfc_dictionary
use combinatoric
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)
!Link (edge) direction:
integer(INTD), parameter, public:: GFC_GRAPH_DIR_NONE=0
integer(INTD), parameter, public:: GFC_GRAPH_DIR_OUTWARD=-1
integer(INTD), parameter, public:: GFC_GRAPH_DIR_INWARD=+1
!TYPES:
!Graph vertex:
type, public:: graph_vertex_t
integer(INTL), private:: color=0_INTL !vertex color
contains
procedure, private:: GraphVertexCtor !ctor
generic, public:: graph_vertex_ctor=>GraphVertexCtor !ctors
procedure, public:: get_color=>GraphVertexGetColor !returns the color of the graph vertex
procedure, public:: compare=>GraphVertexCompare !comparator
procedure, public:: print_it=>GraphVertexPrintIt !prints the graph vertex info
final:: graph_vertex_dtor !dtor
end type graph_vertex_t
!Link between vertices (directed/undirected edge or hyperedge):
type, public:: graph_link_t
integer(INTL), private:: color=0_INTL !link color
logical, private:: directed=.FALSE. !directed or not (if directed, the order of vertices will matter)
integer(INTD), private:: rank=0 !number of vertices in the link (2 for ordinary graphs, >2 for hypergraphs)
integer(INTL), allocatable, private:: vertices(:) !vertices participating in the link
contains
procedure, private:: GraphLinkCtor !ctor
generic, public:: graph_link_ctor=>GraphLinkCtor !ctors
procedure, public:: is_set=>GraphLinkIsSet !returns TRUE if the graph link is set
procedure, public:: get_color=>GraphLinkGetColor !returns the link color
procedure, public:: get_rank=>GraphLinkGetRank !returns the number of vertices in the link (2 for ordinary edges, >2 for hyperedges)
procedure, public:: compare=>GraphLinkCompare !comparator
procedure, public:: print_it=>GraphLinkPrintIt !prints the graph link info
final:: graph_link_dtor !dtor
end type graph_link_t
!Graph vertex links:
type, private:: vert_link_refs_t
integer(INTL), private:: num_links=0_INTL !number of links the vertex participates in
type(dictionary_t), private:: vert_links !vertex link references organized as an ordered associative container
contains
procedure, private:: VertLinkRefsCtor !ctor
generic, public:: vert_link_refs_ctor=>VertLinkRefsCtor !ctors
procedure, public:: get_num_links=>VertLinkRefsGetNumLinks !returns the number of links attached to the corresponding vertex
procedure, public:: get_links_iter=>VertLinkRefsGetLinksIter !returns a dictionary iterator to the vertex links
procedure, public:: find_link=>VertLinkRefsFindLink !finds a specific vertex link
procedure, public:: add_link=>VertLinkRefsAddLink !adds a link reference to the vertex
procedure, public:: delete_link=>VertLinkRefsDeleteLink !deletes a link reference from the vertex
procedure, public:: delete_all=>VertLinkRefsDeleteAll !deletes all link references on the vertex
final:: vert_link_refs_dtor !dtor
end type vert_link_refs_t
!Graph container:
type, extends(gfc_container_t), public:: graph_t
type(vector_t), private:: vertices !graph vertices stored by value: [0..N-1], N is graph cardinality
type(vector_t), private:: link_refs !vector of <vert_link_refs_t> objects containing link references for all vertices
type(list_bi_t), private:: links !links (stored by value): Each unique (generally ordered) combination of vertices may only have one link
integer(INTL), private:: num_links=0_INTL !total number of links in the graph
contains
procedure, private:: incr_num_links_=>GraphIncrNumLinks !PRIVATE: increments the number of graph links by one
procedure, private:: decr_num_links_=>GraphDecrNumLinks !PRIVATE: decrements the number of graph links by one
procedure, public:: is_empty=>GraphIsEmpty !returns TRUE if the graph is empty
procedure, public:: is_set=>GraphIsSet !returns GFC_TRUE if the graph is set, plus additional info
procedure, public:: get_num_vertices=>GraphGetNumVertices !returns the total number of vertices in the graph (graph cardinality)
procedure, public:: get_num_links=>GraphGetNumLinks !returns the total number of links in the graph
procedure, public:: print_it=>GraphPrintIt !prints the graph
end type graph_t
!Graph iterator:
type, extends(gfc_iter_t), public:: graph_iter_t
class(graph_t), pointer, private:: container=>NULL() !pointer to the graph container
type(vector_iter_t), private:: vert_it !vertex iterator
type(vector_iter_t), private:: vert_ln_it !vertex links iterator
type(list_iter_t), private:: link_it !graph links iterator
contains
procedure, private:: update_status_=>GraphIterUpdateStatus !PRIVATE: updates graph iterator status ater component updates
procedure, private:: renumber_=>GraphIterRenumber !PRIVATE: renumbers graph vertices in graph links in case of vertex deletion
procedure, public:: init=>GraphIterInit !initializes the iterator by associating it with a graph container
procedure, public:: reset=>GraphIterReset !resets the iterator to the first graph vertex (vertex 0)
procedure, public:: release=>GraphIterRelease !releases the iterator by dissociating it from its container
procedure, public:: pointee=>GraphIterPointee !returns a pointer to the current graph container element (<vector_elem_t>)
procedure, public:: next=>GraphIterNext !moves the iterator to the next graph vertex
procedure, public:: previous=>GraphIterPrevious !moves the iterator to the previous graph vertex
procedure, public:: get_vertex=>GraphIterGetVertex !returns a pointer to a specified graph vertex (<graph_vertex_t>)
procedure, public:: get_num_vertices=>GraphIterGetNumVertices !returns the total number of vertices in the graph
procedure, public:: get_num_links=>GraphIterGetNumLinks !returns the total number of links in the graph
procedure, public:: get_links=>GraphIterGetLinks !returns the list of links (by reference) for specific graph vertices
procedure, public:: move_to=>GraphIterMoveTo !moves the iterator to the specific graph vertex
procedure, public:: find_link=>GraphIterFindLink !finds a specific link in the graph
procedure, public:: append_vertex=>GraphIterAppendVertex !appends a new vertex to the graph
procedure, public:: append_link=>GraphIterAppendLink !appends a new link between two or more graph vertices
procedure, public:: delete_link=>GraphIterDeleteLink !deletes a specific graph link
procedure, public:: delete_vertex=>GraphIterDeleteVertex !deletes a specific graph vertex
procedure, public:: delete_all=>GraphIterDeleteAll !deletes all graph vertices and links
procedure, public:: merge_vertices=>GraphIterMergeVertices !merges two or more graph vertices into a single vertex
end type graph_iter_t
!VISIBILITY:
!non-member:
public cmp_graph_links
public print_graph_link
public print_graph_link_from_list
!graph_vertex_t:
private GraphVertexCtor
private GraphVertexGetColor
private GraphVertexCompare
private GraphVertexPrintIt
public graph_vertex_dtor
!graph_link_t:
private GraphLinkCtor
private GraphLinkIsSet
private GraphLinkGetColor
private GraphLinkGetRank
private GraphLinkCompare
private GraphLinkPrintIt
public graph_link_dtor
!vert_link_refs_t
private VertLinkRefsCtor
private VertLinkRefsGetNumLinks
private VertLinkRefsGetLinksIter
private VertLinkRefsFindLink
private VertLinkRefsAddLink
private VertLinkRefsDeleteLink
private VertLinkRefsDeleteAll
public vert_link_refs_dtor
!graph_t:
private GraphIncrNumLinks
private GraphDecrNumLinks
private GraphIsEmpty
private GraphIsSet
private GraphGetNumVertices
private GraphGetNumLinks
private GraphPrintIt
!graph_iter_t:
private GraphIterUpdateStatus
private GraphIterRenumber
private GraphIterInit
private GraphIterReset
private GraphIterRelease
private GraphIterPointee
private GraphIterNext
private GraphIterPrevious
private GraphIterGetVertex
private GraphIterGetNumVertices
private GraphIterGetNumLinks
private GraphIterGetLinks
private GraphIterMoveTo
private GraphIterFindLink
private GraphIterAppendVertex
private GraphIterAppendLink
private GraphIterDeleteLink
private GraphIterDeleteVertex
private GraphIterDeleteAll
private GraphIterMergeVertices
!IMPLEMENTATION:
contains
![non-member]==========================================
function cmp_graph_links(obj1,obj2) result(cmp)
!Non-member comparator for graph links.
implicit none
integer(INTD):: cmp !out: comparison result: {CMP_EQ,CMP_LT,CMP_GT,CMP_ER}
class(*), intent(in), target:: obj1 !in: graph link 1
class(*), intent(in), target:: obj2 !in: graph link 2
class(graph_link_t), pointer:: glp1,glp2
glp1=>NULL(); select type(obj1); class is(graph_link_t); glp1=>obj1; end select
glp2=>NULL(); select type(obj2); class is(graph_link_t); glp2=>obj2; end select
if(associated(glp1).and.associated(glp2)) then
cmp=glp1%compare(glp2)
else
cmp=GFC_CMP_ERR
endif
return
end function cmp_graph_links
!---------------------------------------------------------
function print_graph_link(obj,dev_id) result(ierr)
!Prints graph_link_t.
implicit none
integer(INTD):: ierr !out: error code
class(*), intent(in), target:: obj !in: graph_link_t object
integer(INTD), intent(in), optional:: dev_id !in: output device (default to screen)
integer(INTD):: devo
ierr=GFC_SUCCESS
if(present(dev_id)) then; devo=dev_id; else; devo=6; endif
select type(obj)
class is(graph_link_t)
call obj%print_it(ierr,devo)
class default
ierr=GFC_ACTION_FAILED
end select
return
end function print_graph_link
!-------------------------------------------------------------------
function print_graph_link_from_list(obj,dev_id) result(ierr)
!Prints graph_link_t from the list of links.
implicit none
integer(INTD):: ierr !out: error code
class(*), intent(in), target:: obj !in: list_elem_t<graph_link_t> object
integer(INTD), intent(in), optional:: dev_id !in: output device (default to screen)
class(*), pointer:: up
integer(INTD):: devo
ierr=GFC_SUCCESS
if(present(dev_id)) then; devo=dev_id; else; devo=6; endif
select type(obj)
class is(list_elem_t)
up=>obj%get_value(ierr)
if(ierr.eq.GFC_SUCCESS) ierr=print_graph_link(up,devo)
class default
ierr=GFC_ACTION_FAILED
end select
return
end function print_graph_link_from_list
![graph_vertex_t]==================================
subroutine GraphVertexCtor(this,color,ierr)
!Ctor.
implicit none
class(graph_vertex_t), intent(out):: this !out: graph vertex
integer(INTL), intent(in):: color !in: vertex color
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=GFC_SUCCESS
this%color=color
if(present(ierr)) ierr=errc
return
end subroutine GraphVertexCtor
!------------------------------------------------------------
function GraphVertexGetColor(this,ierr) result(color)
!Returns the color of the graph vertex.
implicit none
integer(INTL):: color !out: vertex color
class(graph_vertex_t), intent(in):: this !in: graph vertex
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=GFC_SUCCESS
color=this%color
if(present(ierr)) ierr=errc
return
end function GraphVertexGetColor
!------------------------------------------------------------
function GraphVertexCompare(this,another) result(cmp)
!Compares two graph vertices (comparator).
implicit none
integer(INTD):: cmp !out: result: {CMP_EQ,CMP_LT,CMP_GT,CMP_ERR}
class(graph_vertex_t), intent(in):: this !in: graph vertex 1
class(graph_vertex_t), intent(in):: another !in: graph vertex 2
if(this%color.lt.another%color) then
cmp=CMP_LT
elseif(this%color.gt.another%color) then
cmp=CMP_GT
else !equ
cmp=CMP_EQ
endif
return
end function GraphVertexCompare
!-----------------------------------------------
subroutine GraphVertexPrintIt(this,ierr)
!Prints the graph vertex info.
implicit none
class(graph_vertex_t), intent(in):: this !in: graph vertex
integer(INTD), intent(out), optional:: ierr !out: error code
write(*,'("Vertex color = ",i13)') this%color
if(present(ierr)) ierr=GFC_SUCCESS
return
end subroutine GraphVertexPrintIt
!-----------------------------------------
subroutine graph_vertex_dtor(this)
!Dtor.
implicit none
type(graph_vertex_t):: this
this%color=0_INTL
return
end subroutine graph_vertex_dtor
![graph_link_t]====================================================
subroutine GraphLinkCtor(this,vertices,ierr,directed,color)
!Ctor.
implicit none
class(graph_link_t), intent(out):: this !out: link between graph vertices
integer(INTL), intent(in):: vertices(1:) !in: vertices participating in the link (2 or more)
integer(INTD), intent(out), optional:: ierr !out: error code
logical, intent(in), optional:: directed !in: TRUE for directed links (the order of vertices will matter)
integer(INTL), intent(in), optional:: color !in: color of the link (for multigraphs)
integer(INTD):: sgn,errc
integer(INTL):: ax
errc=GFC_SUCCESS
this%rank=size(vertices)
if(this%rank.ge.2) then !2 for graphs, >2 for hypergraphs
allocate(this%vertices(1:this%rank),STAT=errc)
if(errc.eq.0) then
this%vertices(:)=vertices(:)
if(present(color)) then; this%color=color; else; this%color=0_INTL; endif
if(present(directed)) then; this%directed=directed; else; this%directed=.FALSE.; endif
if(.not.this%directed) then !undirected links need to be ordered
if(this%rank.eq.2) then
if(this%vertices(1).gt.this%vertices(2)) then
ax=this%vertices(1); this%vertices(1)=this%vertices(2); this%vertices(2)=ax
endif
else
call merge_sort(int(this%rank,INTL),this%vertices(1:this%rank),sgn)
endif
endif
else
errc=GFC_MEM_ALLOC_FAILED
endif
else
errc=GFC_INVALID_ARGS
endif
if(errc.ne.GFC_SUCCESS) call graph_link_dtor(this)
if(present(ierr)) ierr=errc
return
end subroutine GraphLinkCtor
!-----------------------------------------------------
function GraphLinkIsSet(this,ierr) result(res)
!Returns TRUE if the graph link is set.
implicit none
logical:: res !out: result
class(graph_link_t), intent(in):: this !in: graph link
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=GFC_SUCCESS; res=(this%rank.ge.2)
if(this%rank.eq.1) errc=GFC_CORRUPTED_CONT
if(present(ierr)) ierr=errc
return
end function GraphLinkIsSet
!----------------------------------------------------------
function GraphLinkGetColor(this,ierr) result(color)
!Returns the color of the graph link.
implicit none
integer(INTL):: color !out: link color
class(graph_link_t), intent(in):: this !in: graph link
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
errc=GFC_SUCCESS; color=this%color
if(this%rank.lt.2) errc=GFC_ELEM_EMPTY
if(present(ierr)) ierr=errc
return
end function GraphLinkGetColor
!-----------------------------------------------------------------
function GraphLinkGetRank(this,ierr,directed) result(rank)
!Returns the rank of the graph link (2 for graphs, >2 for hypergraphs).
implicit none
integer(INTD):: rank !out: graph link rank
class(graph_link_t), intent(in):: this !in: graph link
integer(INTD), intent(out), optional:: ierr !out: error code
logical, intent(out), optional:: directed !out: TRUE if the graph link is directed
integer(INTD):: errc
errc=GFC_SUCCESS; rank=this%rank
if(this%rank.ge.2) then
if(present(directed)) directed=this%directed
else
errc=GFC_ELEM_EMPTY
endif
if(present(ierr)) ierr=errc
return
end function GraphLinkGetRank
!----------------------------------------------------------
function GraphLinkCompare(this,another) result(cmp)
!Compares two graph links (comparator).
implicit none
integer(INTD):: cmp !out: result: {CMP_EQ,CMP_LT,CMP_GT,CMP_ERR}
class(graph_link_t), intent(in):: this !in: graph vertex 1
class(graph_link_t), intent(in):: another !in: graph vertex 2
integer(INTD):: i
cmp=CMP_EQ
if(this%rank.ge.2.and.another%rank.ge.2) then
if(this%rank.lt.another%rank) then
cmp=CMP_LT
elseif(this%rank.gt.another%rank) then
cmp=CMP_GT
else
if((.not.this%directed).and.another%directed) then
cmp=CMP_LT
elseif(this%directed.and.(.not.another%directed)) then
cmp=CMP_GT
else
do i=1,this%rank
if(this%vertices(i).lt.another%vertices(i)) then
cmp=CMP_LT; exit
elseif(this%vertices(i).gt.another%vertices(i)) then
cmp=CMP_GT; exit
endif
enddo
if(cmp.eq.CMP_EQ) then
if(this%color.lt.another%color) then
cmp=CMP_LT
elseif(this%color.gt.another%color) then
cmp=CMP_GT
endif
endif
endif
endif
else
cmp=CMP_ER
endif
return
end function GraphLinkCompare
!----------------------------------------------------
subroutine GraphLinkPrintIt(this,ierr,dev_id)
!Prints the graph link.
implicit none
class(graph_link_t), intent(in):: this !in: graph link
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD), intent(in), optional:: dev_id !in: output device (defaults to screen)
integer(INTD):: errc,devo
if(present(dev_id)) then; devo=dev_id; else; devo=6; endif
if(this%is_set(errc)) then
if(this%directed) then
write(devo,'("Link (directed, color = ",i13,"):",16(1x,i13))') this%color,this%vertices(1:this%rank)
else
write(devo,'("Link (color = ",i13,"):",16(1x,i13))') this%color,this%vertices(1:this%rank)
endif
else
errc=GFC_ELEM_EMPTY
endif
if(present(ierr)) ierr=errc
return
end subroutine GraphLinkPrintIt
!---------------------------------------
subroutine graph_link_dtor(this)
!Dtor.
implicit none
type(graph_link_t):: this
if(allocated(this%vertices)) deallocate(this%vertices)
this%rank=0; this%color=0_INTL; this%directed=.FALSE.
return
end subroutine graph_link_dtor
![vert_link_refs_t]===========================
subroutine VertLinkRefsCtor(this,ierr)
!Ctor.
implicit none
class(vert_link_refs_t), intent(out):: this !out: vertex info
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
this%num_links=0_INTL
call this%vert_links%set_key_storage(GFC_BY_REF,errc)
if(present(ierr)) ierr=errc
return
end subroutine VertLinkRefsCtor
!--------------------------------------------------------------------
function VertLinkRefsGetNumLinks(this,ierr) result(num_links)
!Returns the number of graph links attached to the vertex.
implicit none
integer(INTD):: num_links !out: number of links
class(vert_link_refs_t), intent(in):: this !in: vertex info
integer(INTD), intent(out), optional:: ierr !out: error code
num_links=this%num_links
if(present(ierr)) ierr=GFC_SUCCESS
return
end function VertLinkRefsGetNumLinks
!---------------------------------------------------------------------
function VertLinkRefsGetLinksIter(this,dict_iter) result(ierr)
!Returns a dictionary iterator to the vertex links.
implicit none
integer(INTD):: ierr !out: error code
class(vert_link_refs_t), intent(in):: this !in: vertex info
class(dictionary_iter_t), intent(inout):: dict_iter !inout: dictionary iterator
ierr=dict_iter%get_status()
if(ierr.eq.GFC_IT_NULL) ierr=dict_iter%init(this%vert_links)
return
end function VertLinkRefsGetLinksIter
!------------------------------------------------------------------------------
function VertLinkRefsFindLink(this,link,ierr,list_link_p) result(found)
!Searches for a specific graph link in the vertex info object.
implicit none
logical:: found !out: TRUE if found
class(vert_link_refs_t), intent(in):: this !in: vertex info
class(graph_link_t), intent(in):: link !in: graph link
integer(INTD), intent(out), optional:: ierr !out: error code
type(list_pos_t), intent(out), pointer, optional:: list_link_p !out: pointer to the list element containing the graph link
integer(INTD):: i,errc
type(dictionary_iter_t):: dit
class(*), pointer:: up
errc=GFC_SUCCESS; found=.FALSE.
if(link%is_set()) then
if(this%num_links.gt.0) then
errc=dit%init(this%vert_links)
if(errc.eq.GFC_SUCCESS) then
up=>NULL()
errc=dit%search(GFC_DICT_FETCH_IF_FOUND,cmp_graph_links,link,value_out=up)
found=(errc.eq.GFC_FOUND)
if(errc.eq.GFC_NOT_FOUND.or.found) then
errc=GFC_SUCCESS
if(found.and.present(list_link_p)) then
if(associated(up)) then
select type(up)
type is(list_pos_t)
list_link_p=>up
class default
errc=GFC_CORRUPTED_CONT
end select
else
errc=GFC_ERROR
endif
endif
endif
up=>NULL()
i=dit%release(); if(i.ne.GFC_SUCCESS.and.errc.eq.GFC_SUCCESS) errc=i
endif
endif
else
errc=GFC_INVALID_ARGS
endif
if(present(ierr)) ierr=errc
return
end function VertLinkRefsFindLink
!--------------------------------------------------------------------
function VertLinkRefsAddLink(this,link,link_ref) result(ierr)
!Adds a graph link to the vertex info object.
implicit none
integer(INTD):: ierr !out: error code
class(vert_link_refs_t), intent(inout):: this !inout: vertex info
class(graph_link_t), intent(in), target:: link !in: graph link
type(list_pos_t), intent(in):: link_ref !in: pointer to the list element where value <link> is stored by value
integer(INTD):: i
type(dictionary_iter_t):: dit
if(link%is_set(ierr)) then
if(ierr.eq.GFC_SUCCESS) then
ierr=dit%init(this%vert_links)
if(ierr.eq.GFC_SUCCESS) then
ierr=dit%search(GFC_DICT_ADD_IF_NOT_FOUND,cmp_graph_links,link,link_ref)
if(ierr.eq.GFC_NOT_FOUND) then
this%num_links=this%num_links+1_INTL
ierr=GFC_SUCCESS
else
if(ierr.eq.GFC_FOUND) then
ierr=GFC_INVALID_REQUEST !link already exists
endif
endif
i=dit%release(); if(i.ne.GFC_SUCCESS.and.ierr.eq.GFC_SUCCESS) ierr=i
endif
endif
else
ierr=GFC_INVALID_ARGS
endif
return
end function VertLinkRefsAddLink
!--------------------------------------------------------------
function VertLinkRefsDeleteLink(this,link) result(ierr)
!Deletes a graph link from the vertex info object.
implicit none
integer(INTD):: ierr !out: error code
class(vert_link_refs_t), intent(inout):: this !inout: vertex info
class(graph_link_t), intent(in):: link !in: graph link
integer(INTD):: i
type(dictionary_iter_t):: dit
if(link%is_set(ierr)) then
if(ierr.eq.GFC_SUCCESS) then
if(this%num_links.gt.0) then
ierr=dit%init(this%vert_links)
if(ierr.eq.GFC_SUCCESS) then
ierr=dit%search(GFC_DICT_DELETE_IF_FOUND,cmp_graph_links,link)
if(ierr.eq.GFC_FOUND) then
this%num_links=this%num_links-1_INTL
ierr=GFC_SUCCESS
else
if(ierr.eq.GFC_NOT_FOUND) ierr=GFC_INVALID_REQUEST !link not found at the vertex
endif
i=dit%release(); if(i.ne.GFC_SUCCESS.and.ierr.eq.GFC_SUCCESS) ierr=i
endif
else
ierr=GFC_INVALID_REQUEST
endif
endif
else
ierr=GFC_INVALID_ARGS
endif
return
end function VertLinkRefsDeleteLink
!--------------------------------------------------------
function VertLinkRefsDeleteAll(this) result(ierr)
!Deletes all link references on the vertex.
implicit none
integer(INTD):: ierr !out: error code
class(vert_link_refs_t), intent(inout):: this !inout: vertex info
type(dictionary_iter_t):: dit
integer(INTD):: i
ierr=dit%init(this%vert_links)
if(ierr.eq.GFC_SUCCESS) then
ierr=dit%delete_all(); if(ierr.eq.GFC_SUCCESS) this%num_links=0_INTL
i=dit%release(); if(i.ne.GFC_SUCCESS.and.ierr.eq.GFC_SUCCESS) ierr=i
endif
return
end function VertLinkRefsDeleteAll
!-------------------------------------------
subroutine vert_link_refs_dtor(this)
implicit none
type(vert_link_refs_t):: this
integer(INTD):: errc
errc=this%delete_all(); this%num_links=0_INTL
return
end subroutine vert_link_refs_dtor
![graph_t]================================
subroutine GraphIncrNumLinks(this)
implicit none
class(graph_t), intent(inout):: this !inout: graph
this%num_links=this%num_links+1_INTL
return
end subroutine GraphIncrNumLinks
!-----------------------------------------
subroutine GraphDecrNumLinks(this)
implicit none
class(graph_t), intent(inout):: this !inout: graph
this%num_links=this%num_links-1_INTL
return
end subroutine GraphDecrNumLinks
!----------------------------------------------
function GraphIsEmpty(this) result(res)
!Returns TRUE if the graph is empty.
implicit none
integer(INTD):: res !out: result: {GFC_TRUE,GFC_FALSE,error}
class(graph_t), intent(in):: this !in: graph
integer(INTL):: nv
nv=this%get_num_vertices(res)
if(res.eq.GFC_SUCCESS) then
if(nv.gt.0_INTL) then; res=GFC_FALSE; else; res=GFC_TRUE; endif
endif
return
end function GraphIsEmpty
!------------------------------------------------------------------------
function GraphIsSet(this,ierr,num_vertices,num_links) result(res)
!Returns TRUE if the graph is set, plus additional info.
implicit none
logical:: res !out: result
class(graph_t), intent(in):: this !in: graph
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTL), intent(out), optional:: num_vertices !out: number of vertices in the graph
integer(INTL), intent(out), optional:: num_links !out: number of (unique) links in the graph
integer(INTD):: errc
integer(INTL):: nv
res=.FALSE.; nv=this%get_num_vertices(errc)
if(errc.eq.GFC_SUCCESS) then
res=(nv.gt.0_INTL)
if(present(num_vertices)) num_vertices=nv
if(present(num_links)) num_links=this%num_links
endif
if(present(ierr)) ierr=errc
return
end function GraphIsSet
!-------------------------------------------------------------------
function GraphGetNumVertices(this,ierr) result(num_vertices)
!Returns the total number of vertices in the graph.
implicit none
integer(INTL):: num_vertices !out: number of vertices
class(graph_t), intent(in):: this !in: graph
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
num_vertices=this%vertices%length(errc)
if(present(ierr)) ierr=errc
return
end function GraphGetNumVertices
!-------------------------------------------------------------
function GraphGetNumLinks(this,ierr) result(num_links)
!Returns the total number of (unique) links in the graph.
!Each (hyper-)link between two or more vertices is counted only once.
implicit none
integer(INTL):: num_links !out: number of links
class(graph_t), intent(in):: this !in: graph
integer(INTD), intent(out), optional:: ierr !out: error code
num_links=this%num_links
if(present(ierr)) ierr=GFC_SUCCESS
return
end function GraphGetNumLinks
!-----------------------------------------
subroutine GraphPrintIt(this,ierr)
!Prints the graph.
implicit none
class(graph_t), intent(in), target:: this !in: graph
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: i,errc
integer(INTL):: nv,nl,l
type(graph_iter_t):: git
class(graph_vertex_t), pointer:: gvp
class(graph_link_t), pointer:: glp
type(list_bi_t):: links
type(list_iter_t):: lit
class(*), pointer:: up
write(*,'("#Printing graph:")')
errc=git%init(this)
if(errc.eq.GFC_SUCCESS) then
nv=git%get_num_vertices(errc)
if(errc.eq.GFC_SUCCESS.and.nv.gt.0) then
do l=0_INTL,nv-1_INTL !loop over vertices
gvp=>git%get_vertex(l,errc); if(errc.ne.GFC_SUCCESS) exit
write(*,'("Vertex ",i13,1x)',ADVANCE='NO') l; call gvp%print_it()
call git%get_links((/l/),links,nl,errc); if(errc.ne.GFC_SUCCESS) exit
if(nl.gt.0) then
errc=lit%init(links)
do while(errc.eq.GFC_SUCCESS)
up=>lit%get_value(errc); if(errc.ne.GFC_SUCCESS) exit
glp=>NULL(); select type(up); class is(graph_link_t); glp=>up; end select
if(.not.associated(glp)) then; errc=GFC_ERROR; exit; endif
write(*,'(1x)',ADVANCE='NO'); call glp%print_it(errc); if(errc.ne.GFC_SUCCESS) exit
errc=lit%next()
enddo
if(errc.eq.GFC_NO_MOVE) errc=GFC_SUCCESS
i=lit%delete_all(); if(i.ne.GFC_SUCCESS.and.errc.eq.GFC_SUCCESS) errc=i
if(errc.eq.GFC_SUCCESS) then
errc=lit%release(); if(errc.ne.GFC_SUCCESS) exit
else
exit
endif
endif
enddo
endif
i=git%release(); if(i.ne.GFC_SUCCESS.and.errc.eq.GFC_SUCCESS) errc=i
endif
write(*,'("#End of printing")')
if(present(ierr)) ierr=errc
return
end subroutine GraphPrintIt
![graph_iter_t]===============================
subroutine GraphIterUpdateStatus(this)
!Updates the iterator status after component updates.
implicit none
class(graph_iter_t), intent(inout):: this !inout: iterator
integer(INTD):: errc
errc=this%set_status_(this%vert_it%get_status())
return
end subroutine GraphIterUpdateStatus
!--------------------------------------------------------
subroutine GraphIterRenumber(this,vertex_id,ierr)
!Renumbers graph vertices in graph links in case of vertex deletion.
!All vertex id's larger than <vertex_id> will be decremented by one.
implicit none
class(graph_iter_t), intent(inout):: this !inout: graph iterator
integer(INTL), intent(in):: vertex_id !in: deleted vertex id
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: i,errc
integer(INTL):: vlv,vll,vid
class(*), pointer:: up
class(graph_link_t), pointer:: glp
if(vertex_id.ge.0_INTL) then
vlv=this%vert_it%get_length(errc)
if(errc.eq.GFC_SUCCESS) then
vll=this%vert_ln_it%get_length(errc)
if(errc.eq.GFC_SUCCESS) then
if(vlv.eq.vll) then
if(vlv.gt.0_INTL.and.vertex_id.lt.vlv.and.this%get_num_links().gt.0_INTL) then
errc=this%link_it%reset()
lloop: do while(errc.eq.GFC_SUCCESS)
up=>this%link_it%get_value(errc); if(errc.ne.GFC_SUCCESS) exit lloop
if(.not.associated(up)) then; errc=GFC_ERROR; exit lloop; endif
select type(up); class is(graph_link_t); glp=>up; end select
if(.not.associated(glp)) then; errc=GFC_CORRUPTED_CONT; exit lloop; endif
do i=1,glp%rank
vid=glp%vertices(i); if(vid.eq.vertex_id) then; errc=GFC_ERROR; exit lloop; endif
if(vid.gt.vertex_id) glp%vertices(i)=vid-1_INTL
enddo
errc=this%link_it%next()
enddo lloop
if(errc.eq.GFC_NO_MOVE) errc=this%link_it%reset()
glp=>NULL(); up=>NULL()
endif
else
errc=GFC_CORRUPTED_CONT
endif
endif
endif
else
errc=GFC_INVALID_ARGS
endif
if(present(ierr)) ierr=errc
return
end subroutine GraphIterRenumber
!-----------------------------------------------------
function GraphIterInit(this,cont) result(ierr)
!Initializes the iterator with its container and resets it to the beginning.
implicit none
integer(INTD):: ierr !out: error code
class(graph_iter_t), intent(inout):: this !inout: iterator
class(gfc_container_t), target, intent(in):: cont !in: container
select type(cont)
class is(graph_t)
this%container=>cont
ierr=this%vert_it%init(this%container%vertices)
if(ierr.eq.GFC_SUCCESS) then
ierr=this%vert_ln_it%init(this%container%link_refs)
if(ierr.eq.GFC_SUCCESS) then
ierr=this%link_it%init(this%container%links)
if(ierr.eq.GFC_SUCCESS) ierr=this%reset()
endif
endif
class default
ierr=GFC_INVALID_ARGS
end select
return
end function GraphIterInit
!-------------------------------------------------
function GraphIterReset(this) result(ierr)
!Resets the iterator to the beginning of the container.
implicit none
integer(INTD):: ierr !out: error code
class(graph_iter_t), intent(inout):: this !inout: iterator
ierr=this%vert_it%reset()
if(ierr.eq.GFC_SUCCESS) then
ierr=this%vert_ln_it%reset()
if(ierr.eq.GFC_SUCCESS) ierr=this%link_it%reset()
call this%reset_count()
endif
call this%update_status_()
return
end function GraphIterReset
!---------------------------------------------------
function GraphIterRelease(this) result(ierr)
!Dissociates the iterator from its container.
implicit none
integer(INTD):: ierr !out: error code
class(graph_iter_t), intent(inout):: this !inout: iterator
integer(INTD):: errc
call this%reset_count()
ierr=this%link_it%release()
errc=this%vert_ln_it%release(); if(ierr.eq.GFC_SUCCESS.and.errc.ne.GFC_SUCCESS) ierr=errc
errc=this%link_it%release(); if(ierr.eq.GFC_SUCCESS.and.errc.ne.GFC_SUCCESS) ierr=errc
this%container=>NULL()
errc=this%set_status_(GFC_IT_NULL); if(ierr.eq.GFC_SUCCESS.and.errc.ne.GFC_SUCCESS) ierr=errc
return
end function GraphIterRelease
!---------------------------------------------------------
function GraphIterPointee(this,ierr) result(pntee)
!Returns a pointer to the current container element.
!Note that the dynamic type of the returned pointer is <vector_elem_t>
!since the graph vertices (<graph_vertex_t>) are stored in a vector.
implicit none
class(gfc_cont_elem_t), pointer:: pntee !out: container element currently pointed to by the iterator: <vector_elem_t>
class(graph_iter_t), intent(in):: this !in: iterator
integer(INTD), intent(out), optional:: ierr !out: error code
pntee=>NULL()
if(present(ierr)) then
pntee=>this%vert_it%pointee(ierr)
else
pntee=>this%vert_it%pointee()
endif
return
end function GraphIterPointee
!-------------------------------------------------------
function GraphIterNext(this,elem_p) result(ierr)
!Moves the iterator to the next element of the graph. If <elem_p> is present,
!the next element will be returned in <elem_p> without moving the iterator.
implicit none
integer(INTD):: ierr !out: error code
class(graph_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
if(present(elem_p)) then
ierr=this%vert_it%next(elem_p)
else
ierr=this%vert_it%next()
if(ierr.eq.GFC_SUCCESS) ierr=this%vert_ln_it%next()
endif
call this%update_status_()
return
end function GraphIterNext
!-----------------------------------------------------------
function GraphIterPrevious(this,elem_p) result(ierr)
!Moves the iterator to the previous element of the graph. If <elem_p> is present,
!the previous element will be returned in <elem_p> without moving the iterator.
implicit none
integer(INTD):: ierr !out: error code
class(graph_iter_t), intent(inout):: this !inout: iterator
class(gfc_cont_elem_t), pointer, intent(out), optional:: elem_p !out: pointer to the container element
if(present(elem_p)) then
ierr=this%vert_it%previous(elem_p)
else
ierr=this%vert_it%previous()
if(ierr.eq.GFC_SUCCESS) ierr=this%vert_ln_it%previous()
endif
call this%update_status_()
return
end function GraphIterPrevious
!----------------------------------------------------------------------
function GraphIterGetVertex(this,vert_id,ierr) result(vertex_p)
!Returns a pointer to a specific graph vertex.
implicit none
class(graph_vertex_t), pointer:: vertex_p !out: pointer to the specified graph vertex
class(graph_iter_t), intent(in):: this !in: iterator
integer(INTL), intent(in):: vert_id !in: vertex id: [0..max]
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
class(*), pointer:: up
vertex_p=>NULL()
up=>this%vert_it%element_value(vert_id,errc)
if(errc.eq.GFC_SUCCESS) then
if(associated(up)) then
select type(up); class is(graph_vertex_t); vertex_p=>up; end select
if(.not.associated(vertex_p)) errc=GFC_CORRUPTED_CONT
up=>NULL()
else
errc=GFC_ERROR
endif
endif
if(present(ierr)) ierr=errc
return
end function GraphIterGetVertex
!-----------------------------------------------------------------------
function GraphIterGetNumVertices(this,ierr) result(num_vertices)
!Returns the total number of vertices in the graph.
implicit none
integer(INTL):: num_vertices !out: number of vertices
class(graph_iter_t), intent(in):: this !in: iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
num_vertices=0_INTL; errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE.or.errc.eq.GFC_IT_DONE) then
num_vertices=this%container%get_num_vertices(errc)
elseif(errc.eq.GFC_IT_EMPTY) then
errc=GFC_SUCCESS
endif
if(present(ierr)) ierr=errc
return
end function GraphIterGetNumVertices
!-----------------------------------------------------------------
function GraphIterGetNumLinks(this,ierr) result(num_links)
!Returns the total number of (unique) links in the graph.
implicit none
integer(INTL):: num_links !out: number of links
class(graph_iter_t), intent(in):: this !in: iterator
integer(INTD), intent(out), optional:: ierr !out: error code
integer(INTD):: errc
num_links=0_INTL; errc=this%get_status()
if(errc.eq.GFC_IT_ACTIVE.or.errc.eq.GFC_IT_DONE) then
num_links=this%container%get_num_links(errc)
elseif(errc.eq.GFC_IT_EMPTY) then
errc=GFC_SUCCESS
endif
if(present(ierr)) ierr=errc
return
end function GraphIterGetNumLinks
!---------------------------------------------------------------------------