Debian Codegraph @

gcc/ada/libgnat/a-comutr.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . M U L T I W A Y _ T R E E S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2025, Free Software Foundation, Inc. --
10 -- --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
14 -- --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
21 -- --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
25 -- --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
30 -- --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
33
34 with Ada.Iterator_Interfaces;
35
36 with Ada.Containers.Helpers;
37 private with Ada.Finalization;
38 private with Ada.Streams;
39 private with Ada.Strings.Text_Buffers;
40
41 generic
42 type Element_Type is private;
43
44 with function "=" (Left, Right : Element_Type) return Boolean is <>;
45
46 package Ada.Containers.Multiway_Trees with
47 SPARK_Mode => Off
48 is
49 pragma Annotate (CodePeer, Skip_Analysis);
50 pragma Preelaborate;
51 pragma Remote_Types;
52
53 type Tree is tagged private
54 with Constant_Indexing => Constant_Reference,
55 Variable_Indexing => Reference,
56 Default_Iterator => Iterate,
57 Iterator_Element => Element_Type;
58 pragma Preelaborable_Initialization (Tree);
59
60 type Cursor is private;
61 pragma Preelaborable_Initialization (Cursor);
62
63 Empty_Tree : constant Tree;
64
65 No_Element : constant Cursor;
66 function Has_Element (Position : Cursor) return Boolean;
67
68 package Tree_Iterator_Interfaces is new
69 Ada.Iterator_Interfaces (Cursor, Has_Element);
70
71 function Equal_Subtree
72 (Left_Position : Cursor;
73 Right_Position : Cursor) return Boolean;
74
75 function "=" (Left, Right : Tree) return Boolean;
76
77 function Is_Empty (Container : Tree) return Boolean;
78
79 function Node_Count (Container : Tree) return Count_Type;
80
81 function Subtree_Node_Count (Position : Cursor) return Count_Type;
82
83 function Depth (Position : Cursor) return Count_Type;
84
85 function Is_Root (Position : Cursor) return Boolean;
86
87 function Is_Leaf (Position : Cursor) return Boolean;
88
89 function Root (Container : Tree) return Cursor;
90
91 procedure Clear (Container : in out Tree);
92
93 function Element (Position : Cursor) return Element_Type;
94
95 procedure Replace_Element
96 (Container : in out Tree;
97 Position : Cursor;
98 New_Item : Element_Type);
99
100 procedure Query_Element
101 (Position : Cursor;
102 Process : not null access procedure (Element : Element_Type));
103
104 procedure Update_Element
105 (Container : in out Tree;
106 Position : Cursor;
107 Process : not null access procedure (Element : in out Element_Type));
108
109 type Constant_Reference_Type
110 (Element : not null access constant Element_Type) is private
111 with Implicit_Dereference => Element;
112
113 type Reference_Type
114 (Element : not null access Element_Type) is private
115 with Implicit_Dereference => Element;
116
117 function Constant_Reference
118 (Container : aliased Tree;
119 Position : Cursor) return Constant_Reference_Type;
120 pragma Inline (Constant_Reference);
121
122 function Reference
123 (Container : aliased in out Tree;
124 Position : Cursor) return Reference_Type;
125 pragma Inline (Reference);
126
127 procedure Assign (Target : in out Tree; Source : Tree);
128
129 function Copy (Source : Tree) return Tree;
130
131 procedure Move (Target : in out Tree; Source : in out Tree);
132
133 procedure Delete_Leaf
134 (Container : in out Tree;
135 Position : in out Cursor);
136
137 procedure Delete_Subtree
138 (Container : in out Tree;
139 Position : in out Cursor);
140
141 procedure Swap
142 (Container : in out Tree;
143 I, J : Cursor);
144
145 function Find
146 (Container : Tree;
147 Item : Element_Type) return Cursor;
148
149 -- This version of the AI:
150 -- 10-06-02 AI05-0136-1/07
151 -- declares Find_In_Subtree this way:
152 --
153 -- function Find_In_Subtree
154 -- (Container : Tree;
155 -- Item : Element_Type;
156 -- Position : Cursor) return Cursor;
157 --
158 -- It seems that the Container parameter is there by mistake, but we need
159 -- an official ruling from the ARG. ???
160
161 function Find_In_Subtree
162 (Position : Cursor;
163 Item : Element_Type) return Cursor;
164
165 -- This version of the AI:
166 -- 10-06-02 AI05-0136-1/07
167 -- declares Ancestor_Find this way:
168 --
169 -- function Ancestor_Find
170 -- (Container : Tree;
171 -- Item : Element_Type;
172 -- Position : Cursor) return Cursor;
173 --
174 -- It seems that the Container parameter is there by mistake, but we need
175 -- an official ruling from the ARG. ???
176
177 function Ancestor_Find
178 (Position : Cursor;
179 Item : Element_Type) return Cursor;
180
181 function Contains
182 (Container : Tree;
183 Item : Element_Type) return Boolean;
184
185 procedure Iterate
186 (Container : Tree;
187 Process : not null access procedure (Position : Cursor));
188
189 procedure Iterate_Subtree
190 (Position : Cursor;
191 Process : not null access procedure (Position : Cursor));
192
193 function Iterate (Container : Tree)
194 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
195
196 function Iterate_Subtree (Position : Cursor)
197 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
198
199 function Iterate_Children
200 (Container : Tree;
201 Parent : Cursor)
202 return Tree_Iterator_Interfaces.Reversible_Iterator'Class;
203
204 function Child_Count (Parent : Cursor) return Count_Type;
205
206 function Child_Depth (Parent, Child : Cursor) return Count_Type;
207
208 procedure Insert_Child
209 (Container : in out Tree;
210 Parent : Cursor;
211 Before : Cursor;
212 New_Item : Element_Type;
213 Count : Count_Type := 1);
214
215 procedure Insert_Child
216 (Container : in out Tree;
217 Parent : Cursor;
218 Before : Cursor;
219 New_Item : Element_Type;
220 Position : out Cursor;
221 Count : Count_Type := 1);
222
223 procedure Insert_Child
224 (Container : in out Tree;
225 Parent : Cursor;
226 Before : Cursor;
227 Position : out Cursor;
228 Count : Count_Type := 1);
229
230 procedure Prepend_Child
231 (Container : in out Tree;
232 Parent : Cursor;
233 New_Item : Element_Type;
234 Count : Count_Type := 1);
235
236 procedure Append_Child
237 (Container : in out Tree;
238 Parent : Cursor;
239 New_Item : Element_Type;
240 Count : Count_Type := 1);
241
242 procedure Delete_Children
243 (Container : in out Tree;
244 Parent : Cursor);
245
246 procedure Copy_Subtree
247 (Target : in out Tree;
248 Parent : Cursor;
249 Before : Cursor;
250 Source : Cursor);
251
252 procedure Splice_Subtree
253 (Target : in out Tree;
254 Parent : Cursor;
255 Before : Cursor;
256 Source : in out Tree;
257 Position : in out Cursor);
258
259 procedure Splice_Subtree
260 (Container : in out Tree;
261 Parent : Cursor;
262 Before : Cursor;
263 Position : Cursor);
264
265 procedure Splice_Children
266 (Target : in out Tree;
267 Target_Parent : Cursor;
268 Before : Cursor;
269 Source : in out Tree;
270 Source_Parent : Cursor);
271
272 procedure Splice_Children
273 (Container : in out Tree;
274 Target_Parent : Cursor;
275 Before : Cursor;
276 Source_Parent : Cursor);
277
278 function Parent (Position : Cursor) return Cursor;
279
280 function First_Child (Parent : Cursor) return Cursor;
281
282 function First_Child_Element (Parent : Cursor) return Element_Type;
283
284 function Last_Child (Parent : Cursor) return Cursor;
285
286 function Last_Child_Element (Parent : Cursor) return Element_Type;
287
288 function Next_Sibling (Position : Cursor) return Cursor;
289
290 function Previous_Sibling (Position : Cursor) return Cursor;
291
292 procedure Next_Sibling (Position : in out Cursor);
293
294 procedure Previous_Sibling (Position : in out Cursor);
295
296 -- This version of the AI:
297 -- 10-06-02 AI05-0136-1/07
298 -- declares Iterate_Children this way:
299 --
300 -- procedure Iterate_Children
301 -- (Container : Tree;
302 -- Parent : Cursor;
303 -- Process : not null access procedure (Position : Cursor));
304 --
305 -- It seems that the Container parameter is there by mistake, but we need
306 -- an official ruling from the ARG. ???
307
308 procedure Iterate_Children
309 (Parent : Cursor;
310 Process : not null access procedure (Position : Cursor));
311
312 procedure Reverse_Iterate_Children
313 (Parent : Cursor;
314 Process : not null access procedure (Position : Cursor));
315
316 private
317 -- A node of this multiway tree comprises an element and a list of children
318 -- (that are themselves trees). The root node is distinguished because it
319 -- contains only children: it does not have an element itself.
320
321 -- This design feature puts two design goals in tension with one another:
322 -- (1) treat the root node the same as any other node
323 -- (2) not declare any objects of type Element_Type unnecessarily
324
325 -- To satisfy (1), we could simply declare the Root node of the tree
326 -- using the normal Tree_Node_Type, but that would mean that (2) is not
327 -- satisfied. To resolve the tension (in favor of (2)), we declare the
328 -- component Root as having a different node type, without an Element
329 -- component (thus satisfying goal (2)) but otherwise identical to a normal
330 -- node, and then use Unchecked_Conversion to convert an access object
331 -- designating the Root node component to the access type designating a
332 -- normal, non-root node (thus satisfying goal (1)). We make an explicit
333 -- check for Root when there is any attempt to manipulate the Element
334 -- component of the node (a check required by the RM anyway).
335
336 -- In order to be explicit about node (and pointer) representation, we
337 -- specify that the respective node types have convention C, to ensure
338 -- that the layout of the components of the node records is the same,
339 -- thus guaranteeing that (unchecked) conversions between access types
340 -- designating each kind of node type is a meaningful conversion.
341
342 use Ada.Containers.Helpers;
343 package Implementation is new Generic_Implementation;
344 use Implementation;
345
346 type Tree_Node_Type;
347 type Tree_Node_Access is access all Tree_Node_Type;
348 pragma Convention (C, Tree_Node_Access);
349 pragma No_Strict_Aliasing (Tree_Node_Access);
350 -- The above-mentioned Unchecked_Conversion is a violation of the normal
351 -- aliasing rules.
352
353 type Children_Type is record
354 First : Tree_Node_Access;
355 Last : Tree_Node_Access;
356 end record;
357
358 -- See the comment above. This declaration must exactly match the
359 -- declaration of Root_Node_Type (except for the Element component).
360
361 type Tree_Node_Type is record
362 Parent : Tree_Node_Access;
363 Prev : Tree_Node_Access;
364 Next : Tree_Node_Access;
365 Children : Children_Type;
366 Element : aliased Element_Type;
367 end record;
368 pragma Convention (C, Tree_Node_Type);
369
370 -- See the comment above. This declaration must match the declaration of
371 -- Tree_Node_Type (except for the Element component).
372
373 type Root_Node_Type is record
374 Parent : Tree_Node_Access;
375 Prev : Tree_Node_Access;
376 Next : Tree_Node_Access;
377 Children : Children_Type;
378 end record;
379 pragma Convention (C, Root_Node_Type);
380
381 for Root_Node_Type'Alignment use Standard'Maximum_Alignment;
382 -- The alignment has to be large enough to allow Root_Node to Tree_Node
383 -- access value conversions, and Tree_Node_Type's alignment may be bumped
384 -- up by the Element component.
385
386 use Ada.Finalization;
387
388 -- The Count component of type Tree represents the number of nodes that
389 -- have been (dynamically) allocated. It does not include the root node
390 -- itself. As implementors, we decide to cache this value, so that the
391 -- selector function Node_Count can execute in O(1) time, in order to be
392 -- consistent with the behavior of the Length selector function for other
393 -- standard container library units. This does mean, however, that the
394 -- two-container forms for Splice_XXX (that move subtrees across tree
395 -- containers) will execute in O(n) time, because we must count the number
396 -- of nodes in the subtree(s) that get moved. (We resolve the tension
397 -- between Node_Count and Splice_XXX in favor of Node_Count, under the
398 -- assumption that Node_Count is the more common operation).
399
400 type Tree is new Controlled with record
401 Root : aliased Root_Node_Type;
402 TC : aliased Tamper_Counts;
403 Count : Count_Type := 0;
404 end record with Put_Image => Put_Image;
405
406 procedure Put_Image
407 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Tree);
408
409 overriding procedure Adjust (Container : in out Tree);
410
411 overriding procedure Finalize (Container : in out Tree) renames Clear;
412
413 use Ada.Streams;
414
415 procedure Write
416 (Stream : not null access Root_Stream_Type'Class;
417 Container : Tree);
418
419 for Tree'Write use Write;
420
421 procedure Read
422 (Stream : not null access Root_Stream_Type'Class;
423 Container : out Tree);
424
425 for Tree'Read use Read;
426
427 type Tree_Access is access all Tree;
428 for Tree_Access'Storage_Size use 0;
429
430 type Cursor is record
431 Container : Tree_Access;
432 Node : Tree_Node_Access;
433 end record;
434
435 procedure Write
436 (Stream : not null access Root_Stream_Type'Class;
437 Position : Cursor);
438
439 for Cursor'Write use Write;
440
441 procedure Read
442 (Stream : not null access Root_Stream_Type'Class;
443 Position : out Cursor);
444
445 for Cursor'Read use Read;
446
447 subtype Reference_Control_Type is Implementation.Reference_Control_Type;
448 -- It is necessary to rename this here, so that the compiler can find it
449
450 type Constant_Reference_Type
451 (Element : not null access constant Element_Type) is
452 record
453 Control : Reference_Control_Type :=
454 raise Program_Error with "uninitialized reference";
455 -- The RM says, "The default initialization of an object of
456 -- type Constant_Reference_Type or Reference_Type propagates
457 -- Program_Error."
458 end record;
459
460 procedure Read
461 (Stream : not null access Root_Stream_Type'Class;
462 Item : out Constant_Reference_Type);
463
464 for Constant_Reference_Type'Read use Read;
465
466 procedure Write
467 (Stream : not null access Root_Stream_Type'Class;
468 Item : Constant_Reference_Type);
469
470 for Constant_Reference_Type'Write use Write;
471
472 type Reference_Type
473 (Element : not null access Element_Type) is
474 record
475 Control : Reference_Control_Type :=
476 raise Program_Error with "uninitialized reference";
477 -- The RM says, "The default initialization of an object of
478 -- type Constant_Reference_Type or Reference_Type propagates
479 -- Program_Error."
480 end record;
481
482 procedure Read
483 (Stream : not null access Root_Stream_Type'Class;
484 Item : out Reference_Type);
485
486 for Reference_Type'Read use Read;
487
488 procedure Write
489 (Stream : not null access Root_Stream_Type'Class;
490 Item : Reference_Type);
491
492 for Reference_Type'Write use Write;
493
494 -- Base iterator type for shared functionality between Child_Iterator
495 -- and Subtree_Iterator - namely finalization.
496 type Root_Iterator is abstract new Limited_Controlled and
497 Tree_Iterator_Interfaces.Forward_Iterator with
498 record
499 Container : Tree_Access;
500 Subtree : Tree_Node_Access;
501 end record
502 with Disable_Controlled => not T_Check;
503
504 overriding procedure Finalize (Object : in out Root_Iterator);
505
506 -- Iterator to handle traversal within a specific subtree.
507 type Subtree_Iterator is new Root_Iterator with null record;
508
509 overriding function First (Object : Subtree_Iterator) return Cursor;
510
511 overriding function Next
512 (Object : Subtree_Iterator;
513 Position : Cursor) return Cursor;
514
515 -- Iterator to handle bidirectional traversal of a node's immediate
516 -- children for operations like reverse enumeration and selective
517 -- insertion.
518 type Child_Iterator is new Root_Iterator and
519 Tree_Iterator_Interfaces.Reversible_Iterator with null record
520 with Disable_Controlled => not T_Check;
521
522 overriding function First (Object : Child_Iterator) return Cursor;
523
524 overriding function Next
525 (Object : Child_Iterator;
526 Position : Cursor) return Cursor;
527
528 overriding function Last (Object : Child_Iterator) return Cursor;
529
530 overriding function Previous
531 (Object : Child_Iterator;
532 Position : Cursor) return Cursor;
533 -- See Ada.Containers.Vectors for documentation on the following
534
535 function Pseudo_Reference
536 (Container : aliased Tree'Class) return Reference_Control_Type;
537 pragma Inline (Pseudo_Reference);
538 -- Creates an object of type Reference_Control_Type pointing to the
539 -- container, and increments the Lock. Finalization of this object will
540 -- decrement the Lock.
541
542 type Element_Access is access all Element_Type with
543 Storage_Size => 0;
544
545 function Get_Element_Access
546 (Position : Cursor) return not null Element_Access;
547 -- Returns a pointer to the element designated by Position.
548
549 Empty_Tree : constant Tree := (Controlled with others => <>);
550
551 No_Element : constant Cursor := (others => <>);
552
553 end Ada.Containers.Multiway_Trees;