annotate gcc/ada/libgnat/a-comutr.ads @ 131:84e7813d76e9

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