[multiple changes]
[gcc.git] / gcc / ada / a-comutr.ads
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-2011, 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 private with Ada.Finalization;
36 private with Ada.Streams;
37
38 generic
39 type Element_Type is private;
40
41 with function "=" (Left, Right : Element_Type) return Boolean is <>;
42
43 package Ada.Containers.Multiway_Trees is
44 pragma Preelaborate;
45 pragma Remote_Types;
46
47 type Tree is tagged private
48 with Constant_Indexing => Constant_Reference,
49 Variable_Indexing => Reference,
50 Default_Iterator => Iterate,
51 Iterator_Element => Element_Type;
52 pragma Preelaborable_Initialization (Tree);
53
54 type Cursor is private;
55 pragma Preelaborable_Initialization (Cursor);
56
57 Empty_Tree : constant Tree;
58
59 No_Element : constant Cursor;
60 function Has_Element (Position : Cursor) return Boolean;
61
62 package Tree_Iterator_Interfaces is new
63 Ada.Iterator_Interfaces (Cursor, Has_Element);
64
65 function Equal_Subtree
66 (Left_Position : Cursor;
67 Right_Position : Cursor) return Boolean;
68
69 function "=" (Left, Right : Tree) return Boolean;
70
71 function Is_Empty (Container : Tree) return Boolean;
72
73 function Node_Count (Container : Tree) return Count_Type;
74
75 function Subtree_Node_Count (Position : Cursor) return Count_Type;
76
77 function Depth (Position : Cursor) return Count_Type;
78
79 function Is_Root (Position : Cursor) return Boolean;
80
81 function Is_Leaf (Position : Cursor) return Boolean;
82
83 function Root (Container : Tree) return Cursor;
84
85 procedure Clear (Container : in out Tree);
86
87 function Element (Position : Cursor) return Element_Type;
88
89 procedure Replace_Element
90 (Container : in out Tree;
91 Position : Cursor;
92 New_Item : Element_Type);
93
94 procedure Query_Element
95 (Position : Cursor;
96 Process : not null access procedure (Element : Element_Type));
97
98 procedure Update_Element
99 (Container : in out Tree;
100 Position : Cursor;
101 Process : not null access procedure (Element : in out Element_Type));
102
103 type Constant_Reference_Type
104 (Element : not null access constant Element_Type) is private
105 with Implicit_Dereference => Element;
106
107 type Reference_Type
108 (Element : not null access Element_Type) is private
109 with Implicit_Dereference => Element;
110
111 procedure Assign (Target : in out Tree; Source : Tree);
112
113 function Copy (Source : Tree) return Tree;
114
115 procedure Move (Target : in out Tree; Source : in out Tree);
116
117 procedure Delete_Leaf
118 (Container : in out Tree;
119 Position : in out Cursor);
120
121 procedure Delete_Subtree
122 (Container : in out Tree;
123 Position : in out Cursor);
124
125 procedure Swap
126 (Container : in out Tree;
127 I, J : Cursor);
128
129 function Find
130 (Container : Tree;
131 Item : Element_Type) return Cursor;
132
133 -- This version of the AI:
134 -- 10-06-02 AI05-0136-1/07
135 -- declares Find_In_Subtree this way:
136 --
137 -- function Find_In_Subtree
138 -- (Container : Tree;
139 -- Item : Element_Type;
140 -- Position : Cursor) return Cursor;
141 --
142 -- It seems that the Container parameter is there by mistake, but we need
143 -- an official ruling from the ARG. ???
144
145 function Find_In_Subtree
146 (Position : Cursor;
147 Item : Element_Type) return Cursor;
148
149 -- This version of the AI:
150 -- 10-06-02 AI05-0136-1/07
151 -- declares Ancestor_Find this way:
152 --
153 -- function Ancestor_Find
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 Ancestor_Find
162 (Position : Cursor;
163 Item : Element_Type) return Cursor;
164
165 function Contains
166 (Container : Tree;
167 Item : Element_Type) return Boolean;
168
169 procedure Iterate
170 (Container : Tree;
171 Process : not null access procedure (Position : Cursor));
172
173 procedure Iterate_Subtree
174 (Position : Cursor;
175 Process : not null access procedure (Position : Cursor));
176
177 function Iterate (Container : Tree)
178 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
179
180 function Iterate_Subtree (Position : Cursor)
181 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
182
183 function Child_Count (Parent : Cursor) return Count_Type;
184
185 function Child_Depth (Parent, Child : Cursor) return Count_Type;
186
187 procedure Insert_Child
188 (Container : in out Tree;
189 Parent : Cursor;
190 Before : Cursor;
191 New_Item : Element_Type;
192 Count : Count_Type := 1);
193
194 procedure Insert_Child
195 (Container : in out Tree;
196 Parent : Cursor;
197 Before : Cursor;
198 New_Item : Element_Type;
199 Position : out Cursor;
200 Count : Count_Type := 1);
201
202 procedure Insert_Child
203 (Container : in out Tree;
204 Parent : Cursor;
205 Before : Cursor;
206 Position : out Cursor;
207 Count : Count_Type := 1);
208
209 procedure Prepend_Child
210 (Container : in out Tree;
211 Parent : Cursor;
212 New_Item : Element_Type;
213 Count : Count_Type := 1);
214
215 procedure Append_Child
216 (Container : in out Tree;
217 Parent : Cursor;
218 New_Item : Element_Type;
219 Count : Count_Type := 1);
220
221 procedure Delete_Children
222 (Container : in out Tree;
223 Parent : Cursor);
224
225 procedure Copy_Subtree
226 (Target : in out Tree;
227 Parent : Cursor;
228 Before : Cursor;
229 Source : Cursor);
230
231 procedure Splice_Subtree
232 (Target : in out Tree;
233 Parent : Cursor;
234 Before : Cursor;
235 Source : in out Tree;
236 Position : in out Cursor);
237
238 procedure Splice_Subtree
239 (Container : in out Tree;
240 Parent : Cursor;
241 Before : Cursor;
242 Position : Cursor);
243
244 procedure Splice_Children
245 (Target : in out Tree;
246 Target_Parent : Cursor;
247 Before : Cursor;
248 Source : in out Tree;
249 Source_Parent : Cursor);
250
251 procedure Splice_Children
252 (Container : in out Tree;
253 Target_Parent : Cursor;
254 Before : Cursor;
255 Source_Parent : Cursor);
256
257 function Parent (Position : Cursor) return Cursor;
258
259 function First_Child (Parent : Cursor) return Cursor;
260
261 function First_Child_Element (Parent : Cursor) return Element_Type;
262
263 function Last_Child (Parent : Cursor) return Cursor;
264
265 function Last_Child_Element (Parent : Cursor) return Element_Type;
266
267 function Next_Sibling (Position : Cursor) return Cursor;
268
269 function Previous_Sibling (Position : Cursor) return Cursor;
270
271 procedure Next_Sibling (Position : in out Cursor);
272
273 procedure Previous_Sibling (Position : in out Cursor);
274
275 -- This version of the AI:
276 -- 10-06-02 AI05-0136-1/07
277 -- declares Iterate_Children this way:
278 --
279 -- procedure Iterate_Children
280 -- (Container : Tree;
281 -- Parent : Cursor;
282 -- Process : not null access procedure (Position : Cursor));
283 --
284 -- It seems that the Container parameter is there by mistake, but we need
285 -- an official ruling from the ARG. ???
286
287 procedure Iterate_Children
288 (Parent : Cursor;
289 Process : not null access procedure (Position : Cursor));
290
291 procedure Reverse_Iterate_Children
292 (Parent : Cursor;
293 Process : not null access procedure (Position : Cursor));
294
295 private
296
297 -- A node of this multiway tree comprises an element and a list of children
298 -- (that are themselves trees). The root node is distinguished because it
299 -- contains only children: it does not have an element itself.
300 --
301 -- This design feature puts two design goals in tension:
302 -- (1) treat the root node the same as any other node
303 -- (2) not declare any objects of type Element_Type unnecessarily
304 --
305 -- To satisfy (1), we could simply declare the Root node of the tree using
306 -- the normal Tree_Node_Type, but that would mean that (2) is not
307 -- satisfied. To resolve the tension (in favor of (2)), we declare the
308 -- component Root as having a different node type, without an Element
309 -- component (thus satisfying goal (2)) but otherwise identical to a normal
310 -- node, and then use Unchecked_Conversion to convert an access object
311 -- designating the Root node component to the access type designating a
312 -- normal, non-root node (thus satisfying goal (1)). We make an explicit
313 -- check for Root when there is any attempt to manipulate the Element
314 -- component of the node (a check required by the RM anyway).
315 --
316 -- In order to be explicit about node (and pointer) representation, we
317 -- specify that the respective node types have convention C, to ensure that
318 -- the layout of the components of the node records is the same, thus
319 -- guaranteeing that (unchecked) conversions between access types
320 -- designating each kind of node type is a meaningful conversion.
321
322 type Tree_Node_Type;
323 type Tree_Node_Access is access all Tree_Node_Type;
324 pragma Convention (C, Tree_Node_Access);
325
326 type Children_Type is record
327 First : Tree_Node_Access;
328 Last : Tree_Node_Access;
329 end record;
330
331 -- See the comment above. This declaration must exactly match the
332 -- declaration of Root_Node_Type (except for the Element component).
333
334 type Tree_Node_Type is record
335 Parent : Tree_Node_Access;
336 Prev : Tree_Node_Access;
337 Next : Tree_Node_Access;
338 Children : Children_Type;
339 Element : Element_Type;
340 end record;
341 pragma Convention (C, Tree_Node_Type);
342
343 -- See the comment above. This declaration must match the declaration of
344 -- Tree_Node_Type (except for the Element component).
345
346 type Root_Node_Type is record
347 Parent : Tree_Node_Access;
348 Prev : Tree_Node_Access;
349 Next : Tree_Node_Access;
350 Children : Children_Type;
351 end record;
352 pragma Convention (C, Root_Node_Type);
353
354 use Ada.Finalization;
355
356 -- The Count component of type Tree represents the number of nodes that
357 -- have been (dynamically) allocated. It does not include the root node
358 -- itself. As implementors, we decide to cache this value, so that the
359 -- selector function Node_Count can execute in O(1) time, in order to be
360 -- consistent with the behavior of the Length selector function for other
361 -- standard container library units. This does mean, however, that the
362 -- two-container forms for Splice_XXX (that move subtrees across tree
363 -- containers) will execute in O(n) time, because we must count the number
364 -- of nodes in the subtree(s) that get moved. (We resolve the tension
365 -- between Node_Count and Splice_XXX in favor of Node_Count, under the
366 -- assumption that Node_Count is the more common operation).
367
368 type Tree is new Controlled with record
369 Root : aliased Root_Node_Type;
370 Busy : Integer := 0;
371 Lock : Integer := 0;
372 Count : Count_Type := 0;
373 end record;
374
375 overriding procedure Adjust (Container : in out Tree);
376
377 overriding procedure Finalize (Container : in out Tree) renames Clear;
378
379 use Ada.Streams;
380
381 procedure Write
382 (Stream : not null access Root_Stream_Type'Class;
383 Container : Tree);
384
385 for Tree'Write use Write;
386
387 procedure Read
388 (Stream : not null access Root_Stream_Type'Class;
389 Container : out Tree);
390
391 for Tree'Read use Read;
392
393 type Tree_Access is access all Tree;
394 for Tree_Access'Storage_Size use 0;
395
396 type Cursor is record
397 Container : Tree_Access;
398 Node : Tree_Node_Access;
399 end record;
400
401 procedure Write
402 (Stream : not null access Root_Stream_Type'Class;
403 Position : Cursor);
404
405 for Cursor'Write use Write;
406
407 procedure Read
408 (Stream : not null access Root_Stream_Type'Class;
409 Position : out Cursor);
410
411 for Cursor'Read use Read;
412
413 type Constant_Reference_Type
414 (Element : not null access constant Element_Type) is null record;
415
416 procedure Read
417 (Stream : not null access Root_Stream_Type'Class;
418 Item : out Constant_Reference_Type);
419
420 for Constant_Reference_Type'Read use Read;
421
422 procedure Write
423 (Stream : not null access Root_Stream_Type'Class;
424 Item : Constant_Reference_Type);
425
426 for Constant_Reference_Type'Write use Write;
427
428 type Reference_Type
429 (Element : not null access Element_Type) is null record;
430
431 procedure Read
432 (Stream : not null access Root_Stream_Type'Class;
433 Item : out Reference_Type);
434
435 for Reference_Type'Read use Read;
436
437 procedure Write
438 (Stream : not null access Root_Stream_Type'Class;
439 Item : Reference_Type);
440
441 for Reference_Type'Write use Write;
442
443 function Constant_Reference
444 (Container : aliased Tree;
445 Position : Cursor)
446 return Constant_Reference_Type;
447
448 function Reference
449 (Container : aliased Tree;
450 Position : Cursor)
451 return Reference_Type;
452
453 Empty_Tree : constant Tree := (Controlled with others => <>);
454
455 No_Element : constant Cursor := (others => <>);
456
457 end Ada.Containers.Multiway_Trees;