Debian Codegraph @

gcc/ada/libgnat/g-graphs.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . G R A P H S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2018-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Ada.Unchecked_Deallocation;
33
34 package body GNAT.Graphs is
35
36 -----------------------
37 -- Local subprograms --
38 -----------------------
39
40 function Sequence_Next_Component return Component_Id;
41 -- Produce the next handle for a component. The handle is guaranteed to be
42 -- unique across all graphs.
43
44 --------------------
45 -- Directed_Graph --
46 --------------------
47
48 package body Directed_Graphs is
49
50 -----------------------
51 -- Local subprograms --
52 -----------------------
53
54 procedure Add_Component
55 (G : Directed_Graph;
56 Comp : Component_Id;
57 Vertices : Vertex_List.Doubly_Linked_List);
58 pragma Inline (Add_Component);
59 -- Add component Comp which houses vertices Vertices to graph G
60
61 procedure Ensure_Created (G : Directed_Graph);
62 pragma Inline (Ensure_Created);
63 -- Verify that graph G is created. Raise Not_Created if this is not the
64 -- case.
65
66 procedure Ensure_Not_Present
67 (G : Directed_Graph;
68 E : Edge_Id);
69 pragma Inline (Ensure_Not_Present);
70 -- Verify that graph G lacks edge E. Raise Duplicate_Edge if this is not
71 -- the case.
72
73 procedure Ensure_Not_Present
74 (G : Directed_Graph;
75 V : Vertex_Id);
76 pragma Inline (Ensure_Not_Present);
77 -- Verify that graph G lacks vertex V. Raise Duplicate_Vertex if this is
78 -- not the case.
79
80 procedure Ensure_Present
81 (G : Directed_Graph;
82 Comp : Component_Id);
83 pragma Inline (Ensure_Present);
84 -- Verify that component Comp exists in graph G. Raise Missing_Component
85 -- if this is not the case.
86
87 procedure Ensure_Present
88 (G : Directed_Graph;
89 E : Edge_Id);
90 pragma Inline (Ensure_Present);
91 -- Verify that edge E is present in graph G. Raise Missing_Edge if this
92 -- is not the case.
93
94 procedure Ensure_Present
95 (G : Directed_Graph;
96 V : Vertex_Id);
97 pragma Inline (Ensure_Present);
98 -- Verify that vertex V is present in graph G. Raise Missing_Vertex if
99 -- this is not the case.
100
101 procedure Free is
102 new Ada.Unchecked_Deallocation
103 (Directed_Graph_Attributes, Directed_Graph);
104
105 function Get_Component_Attributes
106 (G : Directed_Graph;
107 Comp : Component_Id) return Component_Attributes;
108 pragma Inline (Get_Component_Attributes);
109 -- Obtain the attributes of component Comp of graph G
110
111 function Get_Edge_Attributes
112 (G : Directed_Graph;
113 E : Edge_Id) return Edge_Attributes;
114 pragma Inline (Get_Edge_Attributes);
115 -- Obtain the attributes of edge E of graph G
116
117 function Get_Vertex_Attributes
118 (G : Directed_Graph;
119 V : Vertex_Id) return Vertex_Attributes;
120 pragma Inline (Get_Vertex_Attributes);
121 -- Obtain the attributes of vertex V of graph G
122
123 function Get_Outgoing_Edges
124 (G : Directed_Graph;
125 V : Vertex_Id) return Edge_Set.Membership_Set;
126 pragma Inline (Get_Outgoing_Edges);
127 -- Obtain the Outgoing_Edges attribute of vertex V of graph G
128
129 function Get_Vertices
130 (G : Directed_Graph;
131 Comp : Component_Id) return Vertex_List.Doubly_Linked_List;
132 pragma Inline (Get_Vertices);
133 -- Obtain the Vertices attribute of component Comp of graph G
134
135 procedure Set_Component
136 (G : Directed_Graph;
137 V : Vertex_Id;
138 Val : Component_Id);
139 pragma Inline (Set_Component);
140 -- Set attribute Component of vertex V of graph G to value Val
141
142 procedure Set_Outgoing_Edges
143 (G : Directed_Graph;
144 V : Vertex_Id;
145 Val : Edge_Set.Membership_Set);
146 pragma Inline (Set_Outgoing_Edges);
147 -- Set attribute Outgoing_Edges of vertex V of graph G to value Val
148
149 procedure Set_Vertex_Attributes
150 (G : Directed_Graph;
151 V : Vertex_Id;
152 Val : Vertex_Attributes);
153 pragma Inline (Set_Vertex_Attributes);
154 -- Set the attributes of vertex V of graph G to value Val
155
156 -------------------
157 -- Add_Component --
158 -------------------
159
160 procedure Add_Component
161 (G : Directed_Graph;
162 Comp : Component_Id;
163 Vertices : Vertex_List.Doubly_Linked_List)
164 is
165 begin
166 pragma Assert (Present (G));
167
168 -- Add the component to the set of all components in the graph
169
170 Component_Map.Put
171 (T => G.Components,
172 Key => Comp,
173 Value => (Vertices => Vertices));
174 end Add_Component;
175
176 --------------
177 -- Add_Edge --
178 --------------
179
180 procedure Add_Edge
181 (G : Directed_Graph;
182 E : Edge_Id;
183 Source : Vertex_Id;
184 Destination : Vertex_Id)
185 is
186 begin
187 Ensure_Created (G);
188 Ensure_Not_Present (G, E);
189 Ensure_Present (G, Source);
190 Ensure_Present (G, Destination);
191
192 -- Add the edge to the set of all edges in the graph
193
194 Edge_Map.Put
195 (T => G.All_Edges,
196 Key => E,
197 Value =>
198 (Destination => Destination,
199 Source => Source));
200
201 -- Associate the edge with its source vertex which effectively "owns"
202 -- the edge.
203
204 Edge_Set.Insert
205 (S => Get_Outgoing_Edges (G, Source),
206 Elem => E);
207 end Add_Edge;
208
209 ----------------
210 -- Add_Vertex --
211 ----------------
212
213 procedure Add_Vertex
214 (G : Directed_Graph;
215 V : Vertex_Id)
216 is
217 begin
218 Ensure_Created (G);
219 Ensure_Not_Present (G, V);
220
221 -- Add the vertex to the set of all vertices in the graph
222
223 Vertex_Map.Put
224 (T => G.All_Vertices,
225 Key => V,
226 Value =>
227 (Component => No_Component,
228 Outgoing_Edges => Edge_Set.Nil));
229
230 -- It is assumed that the vertex will have at least one outgoing
231 -- edge. It is important not to create the set of edges above as
232 -- the call to Put may fail in case the vertices are iterated.
233 -- This would lead to a memory leak because the set would not be
234 -- reclaimed.
235
236 Set_Outgoing_Edges (G, V, Edge_Set.Create (1));
237 end Add_Vertex;
238
239 ---------------
240 -- Component --
241 ---------------
242
243 function Component
244 (G : Directed_Graph;
245 V : Vertex_Id) return Component_Id
246 is
247 begin
248 Ensure_Created (G);
249 Ensure_Present (G, V);
250
251 return Get_Vertex_Attributes (G, V).Component;
252 end Component;
253
254 ------------------------
255 -- Contains_Component --
256 ------------------------
257
258 function Contains_Component
259 (G : Directed_Graph;
260 Comp : Component_Id) return Boolean
261 is
262 begin
263 Ensure_Created (G);
264
265 return Component_Map.Contains (G.Components, Comp);
266 end Contains_Component;
267
268 -------------------
269 -- Contains_Edge --
270 -------------------
271
272 function Contains_Edge
273 (G : Directed_Graph;
274 E : Edge_Id) return Boolean
275 is
276 begin
277 Ensure_Created (G);
278
279 return Edge_Map.Contains (G.All_Edges, E);
280 end Contains_Edge;
281
282 ---------------------
283 -- Contains_Vertex --
284 ---------------------
285
286 function Contains_Vertex
287 (G : Directed_Graph;
288 V : Vertex_Id) return Boolean
289 is
290 begin
291 Ensure_Created (G);
292
293 return Vertex_Map.Contains (G.All_Vertices, V);
294 end Contains_Vertex;
295
296 ------------
297 -- Create --
298 ------------
299
300 function Create
301 (Initial_Vertices : Positive;
302 Initial_Edges : Positive) return Directed_Graph
303 is
304 G : constant Directed_Graph := new Directed_Graph_Attributes;
305
306 begin
307 G.All_Edges := Edge_Map.Create (Initial_Edges);
308 G.All_Vertices := Vertex_Map.Create (Initial_Vertices);
309 G.Components := Component_Map.Create (Initial_Vertices);
310
311 return G;
312 end Create;
313
314 -----------------
315 -- Delete_Edge --
316 -----------------
317
318 procedure Delete_Edge
319 (G : Directed_Graph;
320 E : Edge_Id)
321 is
322 Source : Vertex_Id;
323
324 begin
325 Ensure_Created (G);
326 Ensure_Present (G, E);
327
328 Source := Source_Vertex (G, E);
329 Ensure_Present (G, Source);
330
331 -- Delete the edge from its source vertex which effectively "owns"
332 -- the edge.
333
334 Edge_Set.Delete (Get_Outgoing_Edges (G, Source), E);
335
336 -- Delete the edge from the set of all edges
337
338 Edge_Map.Delete (G.All_Edges, E);
339 end Delete_Edge;
340
341 ------------------------
342 -- Destination_Vertex --
343 ------------------------
344
345 function Destination_Vertex
346 (G : Directed_Graph;
347 E : Edge_Id) return Vertex_Id
348 is
349 begin
350 Ensure_Created (G);
351 Ensure_Present (G, E);
352
353 return Get_Edge_Attributes (G, E).Destination;
354 end Destination_Vertex;
355
356 -------------
357 -- Destroy --
358 -------------
359
360 procedure Destroy (G : in out Directed_Graph) is
361 begin
362 Ensure_Created (G);
363
364 Edge_Map.Destroy (G.All_Edges);
365 Vertex_Map.Destroy (G.All_Vertices);
366 Component_Map.Destroy (G.Components);
367
368 Free (G);
369 end Destroy;
370
371 ----------------------------------
372 -- Destroy_Component_Attributes --
373 ----------------------------------
374
375 procedure Destroy_Component_Attributes
376 (Attrs : in out Component_Attributes)
377 is
378 begin
379 Vertex_List.Destroy (Attrs.Vertices);
380 end Destroy_Component_Attributes;
381
382 -----------------------------
383 -- Destroy_Edge_Attributes --
384 -----------------------------
385
386 procedure Destroy_Edge_Attributes (Attrs : in out Edge_Attributes) is
387 pragma Unreferenced (Attrs);
388 begin
389 null;
390 end Destroy_Edge_Attributes;
391
392 --------------------
393 -- Destroy_Vertex --
394 --------------------
395
396 procedure Destroy_Vertex (V : in out Vertex_Id) is
397 pragma Unreferenced (V);
398 begin
399 null;
400 end Destroy_Vertex;
401
402 -------------------------------
403 -- Destroy_Vertex_Attributes --
404 -------------------------------
405
406 procedure Destroy_Vertex_Attributes (Attrs : in out Vertex_Attributes) is
407 begin
408 Edge_Set.Destroy (Attrs.Outgoing_Edges);
409 end Destroy_Vertex_Attributes;
410
411 --------------------
412 -- Ensure_Created --
413 --------------------
414
415 procedure Ensure_Created (G : Directed_Graph) is
416 begin
417 if not Present (G) then
418 raise Not_Created;
419 end if;
420 end Ensure_Created;
421
422 ------------------------
423 -- Ensure_Not_Present --
424 ------------------------
425
426 procedure Ensure_Not_Present
427 (G : Directed_Graph;
428 E : Edge_Id)
429 is
430 begin
431 if Contains_Edge (G, E) then
432 raise Duplicate_Edge;
433 end if;
434 end Ensure_Not_Present;
435
436 ------------------------
437 -- Ensure_Not_Present --
438 ------------------------
439
440 procedure Ensure_Not_Present
441 (G : Directed_Graph;
442 V : Vertex_Id)
443 is
444 begin
445 if Contains_Vertex (G, V) then
446 raise Duplicate_Vertex;
447 end if;
448 end Ensure_Not_Present;
449
450 --------------------
451 -- Ensure_Present --
452 --------------------
453
454 procedure Ensure_Present
455 (G : Directed_Graph;
456 Comp : Component_Id)
457 is
458 begin
459 if not Contains_Component (G, Comp) then
460 raise Missing_Component;
461 end if;
462 end Ensure_Present;
463
464 --------------------
465 -- Ensure_Present --
466 --------------------
467
468 procedure Ensure_Present
469 (G : Directed_Graph;
470 E : Edge_Id)
471 is
472 begin
473 if not Contains_Edge (G, E) then
474 raise Missing_Edge;
475 end if;
476 end Ensure_Present;
477
478 --------------------
479 -- Ensure_Present --
480 --------------------
481
482 procedure Ensure_Present
483 (G : Directed_Graph;
484 V : Vertex_Id)
485 is
486 begin
487 if not Contains_Vertex (G, V) then
488 raise Missing_Vertex;
489 end if;
490 end Ensure_Present;
491
492 ---------------------
493 -- Find_Components --
494 ---------------------
495
496 procedure Find_Components (G : Directed_Graph) is
497
498 -- The components of graph G are discovered using Tarjan's strongly
499 -- connected component algorithm. Do not modify this code unless you
500 -- intimately understand the algorithm.
501
502 ----------------
503 -- Tarjan_Map --
504 ----------------
505
506 type Visitation_Number is new Natural;
507 No_Visitation_Number : constant Visitation_Number :=
508 Visitation_Number'First;
509 First_Visitation_Number : constant Visitation_Number :=
510 No_Visitation_Number + 1;
511
512 type Tarjan_Attributes is record
513 Index : Visitation_Number := No_Visitation_Number;
514 -- Visitation number
515
516 Low_Link : Visitation_Number := No_Visitation_Number;
517 -- Lowest visitation number
518
519 On_Stack : Boolean := False;
520 -- Set when the corresponding vertex appears on the Stack
521 end record;
522
523 No_Tarjan_Attributes : constant Tarjan_Attributes :=
524 (Index => No_Visitation_Number,
525 Low_Link => No_Visitation_Number,
526 On_Stack => False);
527
528 procedure Destroy_Tarjan_Attributes
529 (Attrs : in out Tarjan_Attributes);
530 -- Destroy the contents of attributes Attrs
531
532 package Tarjan_Map is new Dynamic_Hash_Tables
533 (Key_Type => Vertex_Id,
534 Value_Type => Tarjan_Attributes,
535 No_Value => No_Tarjan_Attributes,
536 Expansion_Threshold => 1.5,
537 Expansion_Factor => 2,
538 Compression_Threshold => 0.3,
539 Compression_Factor => 2,
540 "=" => Same_Vertex,
541 Destroy_Value => Destroy_Tarjan_Attributes,
542 Hash => Hash_Vertex);
543
544 ------------------
545 -- Tarjan_Stack --
546 ------------------
547
548 package Tarjan_Stack is new Doubly_Linked_Lists
549 (Element_Type => Vertex_Id,
550 "=" => Same_Vertex,
551 Destroy_Element => Destroy_Vertex);
552
553 -----------------
554 -- Global data --
555 -----------------
556
557 Attrs : Tarjan_Map.Dynamic_Hash_Table := Tarjan_Map.Nil;
558 Stack : Tarjan_Stack.Doubly_Linked_List := Tarjan_Stack.Nil;
559
560 -----------------------
561 -- Local subprograms --
562 -----------------------
563
564 procedure Associate_All_Vertices;
565 pragma Inline (Associate_All_Vertices);
566 -- Associate all vertices in the graph with the corresponding
567 -- components that house them.
568
569 procedure Associate_Vertices (Comp : Component_Id);
570 pragma Inline (Associate_Vertices);
571 -- Associate all vertices of component Comp with the component
572
573 procedure Create_Component (V : Vertex_Id);
574 pragma Inline (Create_Component);
575 -- Create a new component with root vertex V
576
577 function Get_Tarjan_Attributes
578 (V : Vertex_Id) return Tarjan_Attributes;
579 pragma Inline (Get_Tarjan_Attributes);
580 -- Obtain the Tarjan attributes of vertex V
581
582 function Index (V : Vertex_Id) return Visitation_Number;
583 pragma Inline (Index);
584 -- Obtain the Index attribute of vertex V
585
586 procedure Initialize_Components;
587 pragma Inline (Initialize_Components);
588 -- Initialize or reinitialize the components of the graph
589
590 function Is_Visited (V : Vertex_Id) return Boolean;
591 pragma Inline (Is_Visited);
592 -- Determine whether vertex V has been visited
593
594 function Low_Link (V : Vertex_Id) return Visitation_Number;
595 pragma Inline (Low_Link);
596 -- Obtain the Low_Link attribute of vertex V
597
598 function On_Stack (V : Vertex_Id) return Boolean;
599 pragma Inline (On_Stack);
600 -- Obtain the On_Stack attribute of vertex V
601
602 function Pop return Vertex_Id;
603 pragma Inline (Pop);
604 -- Pop a vertex off Stack
605
606 procedure Push (V : Vertex_Id);
607 pragma Inline (Push);
608 -- Push vertex V on Stack
609
610 procedure Record_Visit (V : Vertex_Id);
611 pragma Inline (Record_Visit);
612 -- Save the visitation of vertex V by setting relevant attributes
613
614 function Sequence_Next_Index return Visitation_Number;
615 pragma Inline (Sequence_Next_Index);
616 -- Procedure the next visitation number of the DFS traversal
617
618 procedure Set_Index
619 (V : Vertex_Id;
620 Val : Visitation_Number);
621 pragma Inline (Set_Index);
622 -- Set attribute Index of vertex V to value Val
623
624 procedure Set_Low_Link
625 (V : Vertex_Id;
626 Val : Visitation_Number);
627 pragma Inline (Set_Low_Link);
628 -- Set attribute Low_Link of vertex V to value Val
629
630 procedure Set_On_Stack
631 (V : Vertex_Id;
632 Val : Boolean);
633 pragma Inline (Set_On_Stack);
634 -- Set attribute On_Stack of vertex V to value Val
635
636 procedure Set_Tarjan_Attributes
637 (V : Vertex_Id;
638 Val : Tarjan_Attributes);
639 pragma Inline (Set_Tarjan_Attributes);
640 -- Set the attributes of vertex V to value Val
641
642 procedure Visit_Successors (V : Vertex_Id);
643 pragma Inline (Visit_Successors);
644 -- Visit the successors of vertex V
645
646 procedure Visit_Vertex (V : Vertex_Id);
647 pragma Inline (Visit_Vertex);
648 -- Visit single vertex V
649
650 procedure Visit_Vertices;
651 pragma Inline (Visit_Vertices);
652 -- Visit all vertices in the graph
653
654 ----------------------------
655 -- Associate_All_Vertices --
656 ----------------------------
657
658 procedure Associate_All_Vertices is
659 Comp : Component_Id;
660 Iter : Component_Iterator;
661
662 begin
663 Iter := Iterate_Components (G);
664 while Has_Next (Iter) loop
665 Next (Iter, Comp);
666
667 Associate_Vertices (Comp);
668 end loop;
669 end Associate_All_Vertices;
670
671 ------------------------
672 -- Associate_Vertices --
673 ------------------------
674
675 procedure Associate_Vertices (Comp : Component_Id) is
676 Iter : Component_Vertex_Iterator;
677 V : Vertex_Id;
678
679 begin
680 Iter := Iterate_Component_Vertices (G, Comp);
681 while Has_Next (Iter) loop
682 Next (Iter, V);
683
684 Set_Component (G, V, Comp);
685 end loop;
686 end Associate_Vertices;
687
688 ----------------------
689 -- Create_Component --
690 ----------------------
691
692 procedure Create_Component (V : Vertex_Id) is
693 Curr_V : Vertex_Id;
694 Vertices : Vertex_List.Doubly_Linked_List;
695
696 begin
697 Vertices := Vertex_List.Create;
698
699 -- Collect all vertices that comprise the current component by
700 -- popping the stack until reaching the root vertex V.
701
702 loop
703 Curr_V := Pop;
704 Vertex_List.Append (Vertices, Curr_V);
705
706 exit when Same_Vertex (Curr_V, V);
707 end loop;
708
709 Add_Component
710 (G => G,
711 Comp => Sequence_Next_Component,
712 Vertices => Vertices);
713 end Create_Component;
714
715 -------------------------------
716 -- Destroy_Tarjan_Attributes --
717 -------------------------------
718
719 procedure Destroy_Tarjan_Attributes
720 (Attrs : in out Tarjan_Attributes)
721 is
722 pragma Unreferenced (Attrs);
723 begin
724 null;
725 end Destroy_Tarjan_Attributes;
726
727 ---------------------------
728 -- Get_Tarjan_Attributes --
729 ---------------------------
730
731 function Get_Tarjan_Attributes
732 (V : Vertex_Id) return Tarjan_Attributes
733 is
734 begin
735 pragma Assert (Present (G));
736 pragma Assert (Contains_Vertex (G, V));
737
738 return Tarjan_Map.Get (Attrs, V);
739 end Get_Tarjan_Attributes;
740
741 -----------
742 -- Index --
743 -----------
744
745 function Index (V : Vertex_Id) return Visitation_Number is
746 begin
747 pragma Assert (Present (G));
748 pragma Assert (Contains_Vertex (G, V));
749
750 return Get_Tarjan_Attributes (V).Index;
751 end Index;
752
753 ---------------------------
754 -- Initialize_Components --
755 ---------------------------
756
757 procedure Initialize_Components is
758 begin
759 pragma Assert (Present (G));
760
761 -- The graph already contains a set of components. Reinitialize
762 -- them in order to accommodate the new set of components about to
763 -- be computed.
764
765 if Number_Of_Components (G) > 0 then
766 Component_Map.Destroy (G.Components);
767 G.Components := Component_Map.Create (Number_Of_Vertices (G));
768 end if;
769 end Initialize_Components;
770
771 ----------------
772 -- Is_Visited --
773 ----------------
774
775 function Is_Visited (V : Vertex_Id) return Boolean is
776 begin
777 pragma Assert (Present (G));
778 pragma Assert (Contains_Vertex (G, V));
779
780 return Index (V) /= No_Visitation_Number;
781 end Is_Visited;
782
783 --------------
784 -- Low_Link --
785 --------------
786
787 function Low_Link (V : Vertex_Id) return Visitation_Number is
788 begin
789 pragma Assert (Present (G));
790 pragma Assert (Contains_Vertex (G, V));
791
792 return Get_Tarjan_Attributes (V).Low_Link;
793 end Low_Link;
794
795 --------------
796 -- On_Stack --
797 --------------
798
799 function On_Stack (V : Vertex_Id) return Boolean is
800 begin
801 pragma Assert (Present (G));
802 pragma Assert (Contains_Vertex (G, V));
803
804 return Get_Tarjan_Attributes (V).On_Stack;
805 end On_Stack;
806
807 ---------
808 -- Pop --
809 ---------
810
811 function Pop return Vertex_Id is
812 V : Vertex_Id;
813
814 begin
815 V := Tarjan_Stack.Last (Stack);
816 Tarjan_Stack.Delete_Last (Stack);
817 Set_On_Stack (V, False);
818
819 pragma Assert (Present (G));
820 pragma Assert (Contains_Vertex (G, V));
821
822 return V;
823 end Pop;
824
825 ----------
826 -- Push --
827 ----------
828
829 procedure Push (V : Vertex_Id) is
830 begin
831 pragma Assert (Present (G));
832 pragma Assert (Contains_Vertex (G, V));
833
834 Tarjan_Stack.Append (Stack, V);
835 Set_On_Stack (V, True);
836 end Push;
837
838 ------------------
839 -- Record_Visit --
840 ------------------
841
842 procedure Record_Visit (V : Vertex_Id) is
843 Index : constant Visitation_Number := Sequence_Next_Index;
844
845 begin
846 pragma Assert (Present (G));
847 pragma Assert (Contains_Vertex (G, V));
848
849 Set_Index (V, Index);
850 Set_Low_Link (V, Index);
851 end Record_Visit;
852
853 -------------------------
854 -- Sequence_Next_Index --
855 -------------------------
856
857 Index_Sequencer : Visitation_Number := First_Visitation_Number;
858 -- The counter for visitation numbers. Do not directly manipulate its
859 -- value because this will destroy the Index and Low_Link invariants
860 -- of the algorithm.
861
862 function Sequence_Next_Index return Visitation_Number is
863 Index : constant Visitation_Number := Index_Sequencer;
864
865 begin
866 Index_Sequencer := Index_Sequencer + 1;
867 return Index;
868 end Sequence_Next_Index;
869
870 ---------------
871 -- Set_Index --
872 ---------------
873
874 procedure Set_Index
875 (V : Vertex_Id;
876 Val : Visitation_Number)
877 is
878 TA : Tarjan_Attributes;
879
880 begin
881 pragma Assert (Present (G));
882 pragma Assert (Contains_Vertex (G, V));
883
884 TA := Get_Tarjan_Attributes (V);
885 TA.Index := Val;
886 Set_Tarjan_Attributes (V, TA);
887 end Set_Index;
888
889 ------------------
890 -- Set_Low_Link --
891 ------------------
892
893 procedure Set_Low_Link
894 (V : Vertex_Id;
895 Val : Visitation_Number)
896 is
897 TA : Tarjan_Attributes;
898
899 begin
900 pragma Assert (Present (G));
901 pragma Assert (Contains_Vertex (G, V));
902
903 TA := Get_Tarjan_Attributes (V);
904 TA.Low_Link := Val;
905 Set_Tarjan_Attributes (V, TA);
906 end Set_Low_Link;
907
908 ------------------
909 -- Set_On_Stack --
910 ------------------
911
912 procedure Set_On_Stack
913 (V : Vertex_Id;
914 Val : Boolean)
915 is
916 TA : Tarjan_Attributes;
917
918 begin
919 pragma Assert (Present (G));
920 pragma Assert (Contains_Vertex (G, V));
921
922 TA := Get_Tarjan_Attributes (V);
923 TA.On_Stack := Val;
924 Set_Tarjan_Attributes (V, TA);
925 end Set_On_Stack;
926
927 ---------------------------
928 -- Set_Tarjan_Attributes --
929 ---------------------------
930
931 procedure Set_Tarjan_Attributes
932 (V : Vertex_Id;
933 Val : Tarjan_Attributes)
934 is
935 begin
936 pragma Assert (Present (G));
937 pragma Assert (Contains_Vertex (G, V));
938
939 Tarjan_Map.Put (Attrs, V, Val);
940 end Set_Tarjan_Attributes;
941
942 ----------------------
943 -- Visit_Successors --
944 ----------------------
945
946 procedure Visit_Successors (V : Vertex_Id) is
947 E : Edge_Id;
948 Iter : Outgoing_Edge_Iterator;
949 Succ : Vertex_Id;
950
951 begin
952 pragma Assert (Present (G));
953 pragma Assert (Contains_Vertex (G, V));
954
955 Iter := Iterate_Outgoing_Edges (G, V);
956 while Has_Next (Iter) loop
957 Next (Iter, E);
958
959 Succ := Destination_Vertex (G, E);
960 pragma Assert (Contains_Vertex (G, Succ));
961
962 -- The current successor has not been visited yet. Extend the
963 -- DFS traversal into it.
964
965 if not Is_Visited (Succ) then
966 Visit_Vertex (Succ);
967
968 Set_Low_Link (V,
969 Visitation_Number'Min (Low_Link (V), Low_Link (Succ)));
970
971 -- The current successor has been visited, and still remains on
972 -- the stack which indicates that it does not participate in a
973 -- component yet.
974
975 elsif On_Stack (Succ) then
976 Set_Low_Link (V,
977 Visitation_Number'Min (Low_Link (V), Index (Succ)));
978 end if;
979 end loop;
980 end Visit_Successors;
981
982 ------------------
983 -- Visit_Vertex --
984 ------------------
985
986 procedure Visit_Vertex (V : Vertex_Id) is
987 begin
988 pragma Assert (Present (G));
989 pragma Assert (Contains_Vertex (G, V));
990
991 if not Is_Visited (V) then
992 Record_Visit (V);
993 Push (V);
994 Visit_Successors (V);
995
996 -- The current vertex is the root of a component
997
998 if Low_Link (V) = Index (V) then
999 Create_Component (V);
1000 end if;
1001 end if;
1002 end Visit_Vertex;
1003
1004 --------------------
1005 -- Visit_Vertices --
1006 --------------------
1007
1008 procedure Visit_Vertices is
1009 Iter : All_Vertex_Iterator;
1010 V : Vertex_Id;
1011
1012 begin
1013 Iter := Iterate_All_Vertices (G);
1014 while Has_Next (Iter) loop
1015 Next (Iter, V);
1016
1017 Visit_Vertex (V);
1018 end loop;
1019 end Visit_Vertices;
1020
1021 -- Start of processing for Find_Components
1022
1023 begin
1024 -- Initialize or reinitialize the components of the graph
1025
1026 Initialize_Components;
1027
1028 -- Prepare the extra attributes needed for each vertex, global
1029 -- visitation number, and the stack where examined vertices are
1030 -- placed.
1031
1032 Attrs := Tarjan_Map.Create (Number_Of_Vertices (G));
1033 Stack := Tarjan_Stack.Create;
1034
1035 -- Start the DFS traversal of Tarjan's SCC algorithm
1036
1037 Visit_Vertices;
1038
1039 Tarjan_Map.Destroy (Attrs);
1040 Tarjan_Stack.Destroy (Stack);
1041
1042 -- Associate each vertex with the component it belongs to
1043
1044 Associate_All_Vertices;
1045 end Find_Components;
1046
1047 ------------------------------
1048 -- Get_Component_Attributes --
1049 ------------------------------
1050
1051 function Get_Component_Attributes
1052 (G : Directed_Graph;
1053 Comp : Component_Id) return Component_Attributes
1054 is
1055 begin
1056 pragma Assert (Present (G));
1057 pragma Assert (Contains_Component (G, Comp));
1058
1059 return Component_Map.Get (G.Components, Comp);
1060 end Get_Component_Attributes;
1061
1062 -------------------------
1063 -- Get_Edge_Attributes --
1064 -------------------------
1065
1066 function Get_Edge_Attributes
1067 (G : Directed_Graph;
1068 E : Edge_Id) return Edge_Attributes
1069 is
1070 begin
1071 pragma Assert (Present (G));
1072 pragma Assert (Contains_Edge (G, E));
1073
1074 return Edge_Map.Get (G.All_Edges, E);
1075 end Get_Edge_Attributes;
1076
1077 ---------------------------
1078 -- Get_Vertex_Attributes --
1079 ---------------------------
1080
1081 function Get_Vertex_Attributes
1082 (G : Directed_Graph;
1083 V : Vertex_Id) return Vertex_Attributes
1084 is
1085 begin
1086 pragma Assert (Present (G));
1087 pragma Assert (Contains_Vertex (G, V));
1088
1089 return Vertex_Map.Get (G.All_Vertices, V);
1090 end Get_Vertex_Attributes;
1091
1092 ------------------------
1093 -- Get_Outgoing_Edges --
1094 ------------------------
1095
1096 function Get_Outgoing_Edges
1097 (G : Directed_Graph;
1098 V : Vertex_Id) return Edge_Set.Membership_Set
1099 is
1100 begin
1101 pragma Assert (Present (G));
1102 pragma Assert (Contains_Vertex (G, V));
1103
1104 return Get_Vertex_Attributes (G, V).Outgoing_Edges;
1105 end Get_Outgoing_Edges;
1106
1107 ------------------
1108 -- Get_Vertices --
1109 ------------------
1110
1111 function Get_Vertices
1112 (G : Directed_Graph;
1113 Comp : Component_Id) return Vertex_List.Doubly_Linked_List
1114 is
1115 begin
1116 pragma Assert (Present (G));
1117 pragma Assert (Contains_Component (G, Comp));
1118
1119 return Get_Component_Attributes (G, Comp).Vertices;
1120 end Get_Vertices;
1121
1122 --------------
1123 -- Has_Next --
1124 --------------
1125
1126 function Has_Next (Iter : All_Edge_Iterator) return Boolean is
1127 begin
1128 return Edge_Map.Has_Next (Edge_Map.Iterator (Iter));
1129 end Has_Next;
1130
1131 --------------
1132 -- Has_Next --
1133 --------------
1134
1135 function Has_Next (Iter : All_Vertex_Iterator) return Boolean is
1136 begin
1137 return Vertex_Map.Has_Next (Vertex_Map.Iterator (Iter));
1138 end Has_Next;
1139
1140 --------------
1141 -- Has_Next --
1142 --------------
1143
1144 function Has_Next (Iter : Component_Iterator) return Boolean is
1145 begin
1146 return Component_Map.Has_Next (Component_Map.Iterator (Iter));
1147 end Has_Next;
1148
1149 --------------
1150 -- Has_Next --
1151 --------------
1152
1153 function Has_Next (Iter : Component_Vertex_Iterator) return Boolean is
1154 begin
1155 return Vertex_List.Has_Next (Vertex_List.Iterator (Iter));
1156 end Has_Next;
1157
1158 --------------
1159 -- Has_Next --
1160 --------------
1161
1162 function Has_Next (Iter : Outgoing_Edge_Iterator) return Boolean is
1163 begin
1164 return Edge_Set.Has_Next (Edge_Set.Iterator (Iter));
1165 end Has_Next;
1166
1167 --------------
1168 -- Is_Empty --
1169 --------------
1170
1171 function Is_Empty (G : Directed_Graph) return Boolean is
1172 begin
1173 Ensure_Created (G);
1174
1175 return
1176 Edge_Map.Is_Empty (G.All_Edges)
1177 and then Vertex_Map.Is_Empty (G.All_Vertices);
1178 end Is_Empty;
1179
1180 -----------------------
1181 -- Iterate_All_Edges --
1182 -----------------------
1183
1184 function Iterate_All_Edges
1185 (G : Directed_Graph) return All_Edge_Iterator
1186 is
1187 begin
1188 Ensure_Created (G);
1189
1190 return All_Edge_Iterator (Edge_Map.Iterate (G.All_Edges));
1191 end Iterate_All_Edges;
1192
1193 --------------------------
1194 -- Iterate_All_Vertices --
1195 --------------------------
1196
1197 function Iterate_All_Vertices
1198 (G : Directed_Graph) return All_Vertex_Iterator
1199 is
1200 begin
1201 Ensure_Created (G);
1202
1203 return All_Vertex_Iterator (Vertex_Map.Iterate (G.All_Vertices));
1204 end Iterate_All_Vertices;
1205
1206 ------------------------
1207 -- Iterate_Components --
1208 ------------------------
1209
1210 function Iterate_Components
1211 (G : Directed_Graph) return Component_Iterator
1212 is
1213 begin
1214 Ensure_Created (G);
1215
1216 return Component_Iterator (Component_Map.Iterate (G.Components));
1217 end Iterate_Components;
1218
1219 --------------------------------
1220 -- Iterate_Component_Vertices --
1221 --------------------------------
1222
1223 function Iterate_Component_Vertices
1224 (G : Directed_Graph;
1225 Comp : Component_Id) return Component_Vertex_Iterator
1226 is
1227 begin
1228 Ensure_Created (G);
1229 Ensure_Present (G, Comp);
1230
1231 return
1232 Component_Vertex_Iterator
1233 (Vertex_List.Iterate (Get_Vertices (G, Comp)));
1234 end Iterate_Component_Vertices;
1235
1236 ----------------------------
1237 -- Iterate_Outgoing_Edges --
1238 ----------------------------
1239
1240 function Iterate_Outgoing_Edges
1241 (G : Directed_Graph;
1242 V : Vertex_Id) return Outgoing_Edge_Iterator
1243 is
1244 begin
1245 Ensure_Created (G);
1246 Ensure_Present (G, V);
1247
1248 return
1249 Outgoing_Edge_Iterator
1250 (Edge_Set.Iterate (Get_Outgoing_Edges (G, V)));
1251 end Iterate_Outgoing_Edges;
1252
1253 ----------
1254 -- Next --
1255 ----------
1256
1257 procedure Next
1258 (Iter : in out All_Edge_Iterator;
1259 E : out Edge_Id)
1260 is
1261 begin
1262 Edge_Map.Next (Edge_Map.Iterator (Iter), E);
1263 end Next;
1264
1265 ----------
1266 -- Next --
1267 ----------
1268
1269 procedure Next
1270 (Iter : in out All_Vertex_Iterator;
1271 V : out Vertex_Id)
1272 is
1273 begin
1274 Vertex_Map.Next (Vertex_Map.Iterator (Iter), V);
1275 end Next;
1276
1277 ----------
1278 -- Next --
1279 ----------
1280
1281 procedure Next
1282 (Iter : in out Component_Iterator;
1283 Comp : out Component_Id)
1284 is
1285 begin
1286 Component_Map.Next (Component_Map.Iterator (Iter), Comp);
1287 end Next;
1288
1289 ----------
1290 -- Next --
1291 ----------
1292
1293 procedure Next
1294 (Iter : in out Component_Vertex_Iterator;
1295 V : out Vertex_Id)
1296 is
1297 begin
1298 Vertex_List.Next (Vertex_List.Iterator (Iter), V);
1299 end Next;
1300
1301 ----------
1302 -- Next --
1303 ----------
1304
1305 procedure Next
1306 (Iter : in out Outgoing_Edge_Iterator;
1307 E : out Edge_Id)
1308 is
1309 begin
1310 Edge_Set.Next (Edge_Set.Iterator (Iter), E);
1311 end Next;
1312
1313 ----------------------------------
1314 -- Number_Of_Component_Vertices --
1315 ----------------------------------
1316
1317 function Number_Of_Component_Vertices
1318 (G : Directed_Graph;
1319 Comp : Component_Id) return Natural
1320 is
1321 begin
1322 Ensure_Created (G);
1323 Ensure_Present (G, Comp);
1324
1325 return Vertex_List.Size (Get_Vertices (G, Comp));
1326 end Number_Of_Component_Vertices;
1327
1328 --------------------------
1329 -- Number_Of_Components --
1330 --------------------------
1331
1332 function Number_Of_Components (G : Directed_Graph) return Natural is
1333 begin
1334 Ensure_Created (G);
1335
1336 return Component_Map.Size (G.Components);
1337 end Number_Of_Components;
1338
1339 ---------------------
1340 -- Number_Of_Edges --
1341 ---------------------
1342
1343 function Number_Of_Edges (G : Directed_Graph) return Natural is
1344 begin
1345 Ensure_Created (G);
1346
1347 return Edge_Map.Size (G.All_Edges);
1348 end Number_Of_Edges;
1349
1350 ------------------------------
1351 -- Number_Of_Outgoing_Edges --
1352 ------------------------------
1353
1354 function Number_Of_Outgoing_Edges
1355 (G : Directed_Graph;
1356 V : Vertex_Id) return Natural
1357 is
1358 begin
1359 Ensure_Created (G);
1360 Ensure_Present (G, V);
1361
1362 return Edge_Set.Size (Get_Outgoing_Edges (G, V));
1363 end Number_Of_Outgoing_Edges;
1364
1365 ------------------------
1366 -- Number_Of_Vertices --
1367 ------------------------
1368
1369 function Number_Of_Vertices (G : Directed_Graph) return Natural is
1370 begin
1371 Ensure_Created (G);
1372
1373 return Vertex_Map.Size (G.All_Vertices);
1374 end Number_Of_Vertices;
1375
1376 -------------
1377 -- Present --
1378 -------------
1379
1380 function Present (G : Directed_Graph) return Boolean is
1381 begin
1382 return G /= Nil;
1383 end Present;
1384
1385 -------------------
1386 -- Set_Component --
1387 -------------------
1388
1389 procedure Set_Component
1390 (G : Directed_Graph;
1391 V : Vertex_Id;
1392 Val : Component_Id)
1393 is
1394 VA : Vertex_Attributes;
1395
1396 begin
1397 pragma Assert (Present (G));
1398 pragma Assert (Contains_Vertex (G, V));
1399
1400 VA := Get_Vertex_Attributes (G, V);
1401 VA.Component := Val;
1402 Set_Vertex_Attributes (G, V, VA);
1403 end Set_Component;
1404
1405 ------------------------
1406 -- Set_Outgoing_Edges --
1407 ------------------------
1408
1409 procedure Set_Outgoing_Edges
1410 (G : Directed_Graph;
1411 V : Vertex_Id;
1412 Val : Edge_Set.Membership_Set)
1413 is
1414 VA : Vertex_Attributes;
1415
1416 begin
1417 pragma Assert (Present (G));
1418 pragma Assert (Contains_Vertex (G, V));
1419
1420 VA := Get_Vertex_Attributes (G, V);
1421 VA.Outgoing_Edges := Val;
1422 Set_Vertex_Attributes (G, V, VA);
1423 end Set_Outgoing_Edges;
1424
1425 ---------------------------
1426 -- Set_Vertex_Attributes --
1427 ---------------------------
1428
1429 procedure Set_Vertex_Attributes
1430 (G : Directed_Graph;
1431 V : Vertex_Id;
1432 Val : Vertex_Attributes)
1433 is
1434 begin
1435 pragma Assert (Present (G));
1436 pragma Assert (Contains_Vertex (G, V));
1437
1438 Vertex_Map.Put (G.All_Vertices, V, Val);
1439 end Set_Vertex_Attributes;
1440
1441 -------------------
1442 -- Source_Vertex --
1443 -------------------
1444
1445 function Source_Vertex
1446 (G : Directed_Graph;
1447 E : Edge_Id) return Vertex_Id
1448 is
1449 begin
1450 Ensure_Created (G);
1451 Ensure_Present (G, E);
1452
1453 return Get_Edge_Attributes (G, E).Source;
1454 end Source_Vertex;
1455 end Directed_Graphs;
1456
1457 --------------------
1458 -- Hash_Component --
1459 --------------------
1460
1461 function Hash_Component (Comp : Component_Id) return Bucket_Range_Type is
1462 begin
1463 return Bucket_Range_Type (Comp);
1464 end Hash_Component;
1465
1466 -------------
1467 -- Present --
1468 -------------
1469
1470 function Present (Comp : Component_Id) return Boolean is
1471 begin
1472 return Comp /= No_Component;
1473 end Present;
1474
1475 -----------------------------
1476 -- Sequence_Next_Component --
1477 -----------------------------
1478
1479 Component_Sequencer : Component_Id := First_Component;
1480 -- The counter for component handles. Do not directly manipulate its value
1481 -- because this will destroy the invariant of the handles.
1482
1483 function Sequence_Next_Component return Component_Id is
1484 Component : constant Component_Id := Component_Sequencer;
1485
1486 begin
1487 Component_Sequencer := Component_Sequencer + 1;
1488 return Component;
1489 end Sequence_Next_Component;
1490
1491 end GNAT.Graphs;