Debian Codegraph @

gcc/ada/libgnat/g-graphs.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . G R A P H S --
6 -- --
7 -- S p e c --
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 -- Note: this unit is used during bootstrap, see ADA_GENERATED_FILES in
33 -- gcc-interface/Make-lang.in for details on the constraints.
34
35 with GNAT.Dynamic_HTables; use GNAT.Dynamic_HTables;
36 with GNAT.Lists; use GNAT.Lists;
37 with GNAT.Sets; use GNAT.Sets;
38
39 package GNAT.Graphs is
40
41 ---------------
42 -- Component --
43 ---------------
44
45 -- The following type denotes a strongly connected component handle
46 -- (referred to as simply "component") in a graph.
47
48 type Component_Id is new Natural;
49 No_Component : constant Component_Id := Component_Id'First;
50
51 function Hash_Component (Comp : Component_Id) return Bucket_Range_Type;
52 -- Map component Comp into the range of buckets
53
54 function Present (Comp : Component_Id) return Boolean;
55 -- Determine whether component Comp exists
56
57 ---------------------
58 -- Directed_Graphs --
59 ---------------------
60
61 -- The following package offers a directed graph abstraction with the
62 -- following characteristics:
63 --
64 -- * Dynamic resizing based on number of vertices and edges
65 -- * Creation of multiple instances, of different sizes
66 -- * Discovery of strongly connected components
67 -- * Iterable attributes
68 --
69 -- The following use pattern must be employed when operating this graph:
70 --
71 -- Graph : Directed_Graph := Create (<some size>, <some size>);
72 --
73 -- <various operations>
74 --
75 -- Destroy (Graph);
76 --
77 -- The destruction of the graph reclaims all storage occupied by it.
78
79 generic
80
81 --------------
82 -- Vertices --
83 --------------
84
85 type Vertex_Id is private;
86 -- The handle of a vertex
87
88 No_Vertex : Vertex_Id;
89 -- An indicator for a nonexistent vertex
90
91 with function Hash_Vertex (V : Vertex_Id) return Bucket_Range_Type;
92 -- Map vertex V into the range of buckets
93
94 with function Same_Vertex
95 (Left : Vertex_Id;
96 Right : Vertex_Id) return Boolean;
97 -- Compare vertex Left to vertex Right for identity
98
99 -----------
100 -- Edges --
101 -----------
102
103 type Edge_Id is private;
104 -- The handle of an edge
105
106 No_Edge : Edge_Id;
107 -- An indicator for a nonexistent edge
108
109 with function Hash_Edge (E : Edge_Id) return Bucket_Range_Type;
110 -- Map edge E into the range of buckets
111
112 with function Same_Edge
113 (Left : Edge_Id;
114 Right : Edge_Id) return Boolean;
115 -- Compare edge Left to edge Right for identity
116
117 package Directed_Graphs is
118
119 -- The following exceptions are raised when an attempt is made to add
120 -- the same edge or vertex in a graph.
121
122 Duplicate_Edge : exception;
123 Duplicate_Vertex : exception;
124
125 -- The following exceptions are raised when an attempt is made to delete
126 -- or reference a nonexistent component, edge, or vertex in a graph.
127
128 Missing_Component : exception;
129 Missing_Edge : exception;
130 Missing_Vertex : exception;
131
132 ----------------------
133 -- Graph operations --
134 ----------------------
135
136 -- The following type denotes a graph handle. Each instance must be
137 -- created using routine Create.
138
139 type Directed_Graph is private;
140 Nil : constant Directed_Graph;
141
142 procedure Add_Edge
143 (G : Directed_Graph;
144 E : Edge_Id;
145 Source : Vertex_Id;
146 Destination : Vertex_Id);
147 -- Add edge E to graph G which links vertex source Source and desination
148 -- vertex Destination. The edge is "owned" by vertex Source. This action
149 -- raises the following exceptions:
150 --
151 -- * Duplicate_Edge, when the edge is already present in the graph
152 --
153 -- * Iterated, when the graph has an outstanding edge iterator
154 --
155 -- * Missing_Vertex, when either the source or desination are not
156 -- present in the graph.
157
158 procedure Add_Vertex
159 (G : Directed_Graph;
160 V : Vertex_Id);
161 -- Add vertex V to graph G. This action raises the following exceptions:
162 --
163 -- * Duplicate_Vertex, when the vertex is already present in the graph
164 --
165 -- * Iterated, when the graph has an outstanding vertex iterator
166
167 function Component
168 (G : Directed_Graph;
169 V : Vertex_Id) return Component_Id;
170 -- Obtain the component where vertex V of graph G resides. This action
171 -- raises the following exceptions:
172 --
173 -- * Missing_Vertex, when the vertex is not present in the graph
174
175 function Contains_Component
176 (G : Directed_Graph;
177 Comp : Component_Id) return Boolean;
178 -- Determine whether graph G contains component Comp
179
180 function Contains_Edge
181 (G : Directed_Graph;
182 E : Edge_Id) return Boolean;
183 -- Determine whether graph G contains edge E
184
185 function Contains_Vertex
186 (G : Directed_Graph;
187 V : Vertex_Id) return Boolean;
188 -- Determine whether graph G contains vertex V
189
190 function Create
191 (Initial_Vertices : Positive;
192 Initial_Edges : Positive) return Directed_Graph;
193 -- Create a new graph with vertex capacity Initial_Vertices and edge
194 -- capacity Initial_Edges. This routine must be called at the start of
195 -- a graph's lifetime.
196
197 procedure Delete_Edge
198 (G : Directed_Graph;
199 E : Edge_Id);
200 -- Delete edge E from graph G. This action raises these exceptions:
201 --
202 -- * Iterated, when the graph has an outstanding edge iterator
203 --
204 -- * Missing_Edge, when the edge is not present in the graph
205 --
206 -- * Missing_Vertex, when the source vertex that "owns" the edge is
207 -- not present in the graph.
208
209 function Destination_Vertex
210 (G : Directed_Graph;
211 E : Edge_Id) return Vertex_Id;
212 -- Obtain the destination vertex of edge E of graph G. This action
213 -- raises the following exceptions:
214 --
215 -- * Missing_Edge, when the edge is not present in the graph
216
217 procedure Destroy (G : in out Directed_Graph);
218 -- Destroy the contents of graph G, rendering it unusable. This routine
219 -- must be called at the end of a graph's lifetime. This action raises
220 -- the following exceptions:
221 --
222 -- * Iterated, if the graph has any outstanding iterator
223
224 procedure Find_Components (G : Directed_Graph);
225 -- Find all components of graph G. This action raises the following
226 -- exceptions:
227 --
228 -- * Iterated, when the components or vertices of the graph have an
229 -- outstanding iterator.
230
231 function Is_Empty (G : Directed_Graph) return Boolean;
232 -- Determine whether graph G is empty
233
234 function Number_Of_Component_Vertices
235 (G : Directed_Graph;
236 Comp : Component_Id) return Natural;
237 -- Obtain the total number of vertices of component Comp of graph G
238
239 function Number_Of_Components (G : Directed_Graph) return Natural;
240 -- Obtain the total number of components of graph G
241
242 function Number_Of_Edges (G : Directed_Graph) return Natural;
243 -- Obtain the total number of edges of graph G
244
245 function Number_Of_Outgoing_Edges
246 (G : Directed_Graph;
247 V : Vertex_Id) return Natural;
248 -- Obtain the total number of outgoing edges of vertex V of graph G
249
250 function Number_Of_Vertices (G : Directed_Graph) return Natural;
251 -- Obtain the total number of vertices of graph G
252
253 function Present (G : Directed_Graph) return Boolean;
254 -- Determine whether graph G exists
255
256 function Source_Vertex
257 (G : Directed_Graph;
258 E : Edge_Id) return Vertex_Id;
259 -- Obtain the source vertex that "owns" edge E of graph G. This action
260 -- raises the following exceptions:
261 --
262 -- * Missing_Edge, when the edge is not present in the graph
263
264 -------------------------
265 -- Iterator operations --
266 -------------------------
267
268 -- The following types represent iterators over various attributes of a
269 -- graph. Each iterator locks all mutation operations of its associated
270 -- attribute, and unlocks them once it is exhausted. The iterators must
271 -- be used with the following pattern:
272 --
273 -- Iter : Iterate_XXX (Graph);
274 -- while Has_Next (Iter) loop
275 -- Next (Iter, Element);
276 -- end loop;
277 --
278 -- It is possible to advance the iterators by using Next only, however
279 -- this risks raising Iterator_Exhausted.
280
281 -- The following type represents an iterator over all edges of a graph
282
283 type All_Edge_Iterator is private;
284
285 function Has_Next (Iter : All_Edge_Iterator) return Boolean;
286 -- Determine whether iterator Iter has more edges to examine
287
288 function Iterate_All_Edges (G : Directed_Graph) return All_Edge_Iterator;
289 -- Obtain an iterator over all edges of graph G
290
291 procedure Next
292 (Iter : in out All_Edge_Iterator;
293 E : out Edge_Id);
294 -- Return the current edge referenced by iterator Iter and advance to
295 -- the next available edge. This action raises the following exceptions:
296 --
297 -- * Iterator_Exhausted, when the iterator has been exhausted and
298 -- further attempts are made to advance it.
299
300 -- The following type represents an iterator over all vertices of a
301 -- graph.
302
303 type All_Vertex_Iterator is private;
304
305 function Has_Next (Iter : All_Vertex_Iterator) return Boolean;
306 -- Determine whether iterator Iter has more vertices to examine
307
308 function Iterate_All_Vertices
309 (G : Directed_Graph) return All_Vertex_Iterator;
310 -- Obtain an iterator over all vertices of graph G
311
312 procedure Next
313 (Iter : in out All_Vertex_Iterator;
314 V : out Vertex_Id);
315 -- Return the current vertex referenced by iterator Iter and advance
316 -- to the next available vertex. This action raises the following
317 -- exceptions:
318 --
319 -- * Iterator_Exhausted, when the iterator has been exhausted and
320 -- further attempts are made to advance it.
321
322 -- The following type represents an iterator over all components of a
323 -- graph.
324
325 type Component_Iterator is private;
326
327 function Has_Next (Iter : Component_Iterator) return Boolean;
328 -- Determine whether iterator Iter has more components to examine
329
330 function Iterate_Components
331 (G : Directed_Graph) return Component_Iterator;
332 -- Obtain an iterator over all components of graph G
333
334 procedure Next
335 (Iter : in out Component_Iterator;
336 Comp : out Component_Id);
337 -- Return the current component referenced by iterator Iter and advance
338 -- to the next component. This action raises the following exceptions:
339 --
340 -- * Iterator_Exhausted, when the iterator has been exhausted and
341 -- further attempts are made to advance it.
342
343 -- The following type prepresents an iterator over all vertices of a
344 -- component.
345
346 type Component_Vertex_Iterator is private;
347
348 function Has_Next (Iter : Component_Vertex_Iterator) return Boolean;
349 -- Determine whether iterator Iter has more vertices to examine
350
351 function Iterate_Component_Vertices
352 (G : Directed_Graph;
353 Comp : Component_Id) return Component_Vertex_Iterator;
354 -- Obtain an iterator over all vertices that comprise component Comp of
355 -- graph G.
356
357 procedure Next
358 (Iter : in out Component_Vertex_Iterator;
359 V : out Vertex_Id);
360 -- Return the current vertex referenced by iterator Iter and advance to
361 -- the next vertex. This action raises the following exceptions:
362 --
363 -- * Iterator_Exhausted, when the iterator has been exhausted and
364 -- further attempts are made to advance it.
365
366 -- The following type represents an iterator over all outgoing edges of
367 -- a vertex.
368
369 type Outgoing_Edge_Iterator is private;
370
371 function Has_Next (Iter : Outgoing_Edge_Iterator) return Boolean;
372 -- Determine whether iterator Iter has more outgoing edges to examine
373
374 function Iterate_Outgoing_Edges
375 (G : Directed_Graph;
376 V : Vertex_Id) return Outgoing_Edge_Iterator;
377 -- Obtain an iterator over all the outgoing edges "owned" by vertex V of
378 -- graph G.
379
380 procedure Next
381 (Iter : in out Outgoing_Edge_Iterator;
382 E : out Edge_Id);
383 -- Return the current outgoing edge referenced by iterator Iter and
384 -- advance to the next available outgoing edge. This action raises the
385 -- following exceptions:
386 --
387 -- * Iterator_Exhausted, when the iterator has been exhausted and
388 -- further attempts are made to advance it.
389
390 private
391 pragma Unreferenced (No_Edge);
392
393 --------------
394 -- Edge_Map --
395 --------------
396
397 type Edge_Attributes is record
398 Destination : Vertex_Id := No_Vertex;
399 -- The target of a directed edge
400
401 Source : Vertex_Id := No_Vertex;
402 -- The origin of a directed edge. The source vertex "owns" the edge.
403 end record;
404
405 No_Edge_Attributes : constant Edge_Attributes :=
406 (Destination => No_Vertex,
407 Source => No_Vertex);
408
409 procedure Destroy_Edge_Attributes (Attrs : in out Edge_Attributes);
410 -- Destroy the contents of attributes Attrs
411
412 package Edge_Map is new Dynamic_Hash_Tables
413 (Key_Type => Edge_Id,
414 Value_Type => Edge_Attributes,
415 No_Value => No_Edge_Attributes,
416 Expansion_Threshold => 1.5,
417 Expansion_Factor => 2,
418 Compression_Threshold => 0.3,
419 Compression_Factor => 2,
420 "=" => Same_Edge,
421 Destroy_Value => Destroy_Edge_Attributes,
422 Hash => Hash_Edge);
423
424 --------------
425 -- Edge_Set --
426 --------------
427
428 package Edge_Set is new Membership_Sets
429 (Element_Type => Edge_Id,
430 "=" => "=",
431 Hash => Hash_Edge);
432
433 -----------------
434 -- Vertex_List --
435 -----------------
436
437 procedure Destroy_Vertex (V : in out Vertex_Id);
438 -- Destroy the contents of a vertex
439
440 package Vertex_List is new Doubly_Linked_Lists
441 (Element_Type => Vertex_Id,
442 "=" => Same_Vertex,
443 Destroy_Element => Destroy_Vertex);
444
445 ----------------
446 -- Vertex_Map --
447 ----------------
448
449 type Vertex_Attributes is record
450 Component : Component_Id := No_Component;
451 -- The component where a vertex lives
452
453 Outgoing_Edges : Edge_Set.Membership_Set := Edge_Set.Nil;
454 -- The set of edges that extend out from a vertex
455 end record;
456
457 No_Vertex_Attributes : constant Vertex_Attributes :=
458 (Component => No_Component,
459 Outgoing_Edges => Edge_Set.Nil);
460
461 procedure Destroy_Vertex_Attributes (Attrs : in out Vertex_Attributes);
462 -- Destroy the contents of attributes Attrs
463
464 package Vertex_Map is new Dynamic_Hash_Tables
465 (Key_Type => Vertex_Id,
466 Value_Type => Vertex_Attributes,
467 No_Value => No_Vertex_Attributes,
468 Expansion_Threshold => 1.5,
469 Expansion_Factor => 2,
470 Compression_Threshold => 0.3,
471 Compression_Factor => 2,
472 "=" => Same_Vertex,
473 Destroy_Value => Destroy_Vertex_Attributes,
474 Hash => Hash_Vertex);
475
476 -------------------
477 -- Component_Map --
478 -------------------
479
480 type Component_Attributes is record
481 Vertices : Vertex_List.Doubly_Linked_List := Vertex_List.Nil;
482 end record;
483
484 No_Component_Attributes : constant Component_Attributes :=
485 (Vertices => Vertex_List.Nil);
486
487 procedure Destroy_Component_Attributes
488 (Attrs : in out Component_Attributes);
489 -- Destroy the contents of attributes Attrs
490
491 package Component_Map is new Dynamic_Hash_Tables
492 (Key_Type => Component_Id,
493 Value_Type => Component_Attributes,
494 No_Value => No_Component_Attributes,
495 Expansion_Threshold => 1.5,
496 Expansion_Factor => 2,
497 Compression_Threshold => 0.3,
498 Compression_Factor => 2,
499 "=" => "=",
500 Destroy_Value => Destroy_Component_Attributes,
501 Hash => Hash_Component);
502
503 -----------
504 -- Graph --
505 -----------
506
507 type Directed_Graph_Attributes is record
508 All_Edges : Edge_Map.Dynamic_Hash_Table := Edge_Map.Nil;
509 -- The map of edge -> edge attributes for all edges in the graph
510
511 All_Vertices : Vertex_Map.Dynamic_Hash_Table := Vertex_Map.Nil;
512 -- The map of vertex -> vertex attributes for all vertices in the
513 -- graph.
514
515 Components : Component_Map.Dynamic_Hash_Table := Component_Map.Nil;
516 -- The map of component -> component attributes for all components
517 -- in the graph.
518 end record;
519
520 type Directed_Graph is access Directed_Graph_Attributes;
521 Nil : constant Directed_Graph := null;
522
523 ---------------
524 -- Iterators --
525 ---------------
526
527 type All_Edge_Iterator is new Edge_Map.Iterator;
528 type All_Vertex_Iterator is new Vertex_Map.Iterator;
529 type Component_Iterator is new Component_Map.Iterator;
530 type Component_Vertex_Iterator is new Vertex_List.Iterator;
531 type Outgoing_Edge_Iterator is new Edge_Set.Iterator;
532 end Directed_Graphs;
533
534 private
535 First_Component : constant Component_Id := No_Component + 1;
536
537 end GNAT.Graphs;