1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
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 --
9 -- Copyright (C) 2004-2011, Free Software Foundation, Inc. --
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. --
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. --
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. --
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/>. --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
34 with Ada.Iterator_Interfaces;
35 private with Ada.Finalization;
36 private with Ada.Streams;
39 type Element_Type is private;
41 with function "=" (Left, Right : Element_Type) return Boolean is <>;
43 package Ada.Containers.Multiway_Trees is
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);
54 type Cursor is private;
55 pragma Preelaborable_Initialization (Cursor);
57 Empty_Tree : constant Tree;
59 No_Element : constant Cursor;
60 function Has_Element (Position : Cursor) return Boolean;
62 package Tree_Iterator_Interfaces is new
63 Ada.Iterator_Interfaces (Cursor, Has_Element);
65 function Equal_Subtree
66 (Left_Position : Cursor;
67 Right_Position : Cursor) return Boolean;
69 function "=" (Left, Right : Tree) return Boolean;
71 function Is_Empty (Container : Tree) return Boolean;
73 function Node_Count (Container : Tree) return Count_Type;
75 function Subtree_Node_Count (Position : Cursor) return Count_Type;
77 function Depth (Position : Cursor) return Count_Type;
79 function Is_Root (Position : Cursor) return Boolean;
81 function Is_Leaf (Position : Cursor) return Boolean;
83 function Root (Container : Tree) return Cursor;
85 procedure Clear (Container : in out Tree);
87 function Element (Position : Cursor) return Element_Type;
89 procedure Replace_Element
90 (Container : in out Tree;
92 New_Item : Element_Type);
94 procedure Query_Element
96 Process : not null access procedure (Element : Element_Type));
98 procedure Update_Element
99 (Container : in out Tree;
101 Process : not null access procedure (Element : in out Element_Type));
103 type Constant_Reference_Type
104 (Element : not null access constant Element_Type) is private
105 with Implicit_Dereference => Element;
108 (Element : not null access Element_Type) is private
109 with Implicit_Dereference => Element;
111 procedure Assign (Target : in out Tree; Source : Tree);
113 function Copy (Source : Tree) return Tree;
115 procedure Move (Target : in out Tree; Source : in out Tree);
117 procedure Delete_Leaf
118 (Container : in out Tree;
119 Position : in out Cursor);
121 procedure Delete_Subtree
122 (Container : in out Tree;
123 Position : in out Cursor);
126 (Container : in out Tree;
131 Item : Element_Type) return Cursor;
133 -- This version of the AI:
134 -- 10-06-02 AI05-0136-1/07
135 -- declares Find_In_Subtree this way:
137 -- function Find_In_Subtree
138 -- (Container : Tree;
139 -- Item : Element_Type;
140 -- Position : Cursor) return Cursor;
142 -- It seems that the Container parameter is there by mistake, but we need
143 -- an official ruling from the ARG. ???
145 function Find_In_Subtree
147 Item : Element_Type) return Cursor;
149 -- This version of the AI:
150 -- 10-06-02 AI05-0136-1/07
151 -- declares Ancestor_Find this way:
153 -- function Ancestor_Find
154 -- (Container : Tree;
155 -- Item : Element_Type;
156 -- Position : Cursor) return Cursor;
158 -- It seems that the Container parameter is there by mistake, but we need
159 -- an official ruling from the ARG. ???
161 function Ancestor_Find
163 Item : Element_Type) return Cursor;
167 Item : Element_Type) return Boolean;
171 Process : not null access procedure (Position : Cursor));
173 procedure Iterate_Subtree
175 Process : not null access procedure (Position : Cursor));
177 function Iterate (Container : Tree)
178 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
180 function Iterate_Subtree (Position : Cursor)
181 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
183 function Child_Count (Parent : Cursor) return Count_Type;
185 function Child_Depth (Parent, Child : Cursor) return Count_Type;
187 procedure Insert_Child
188 (Container : in out Tree;
191 New_Item : Element_Type;
192 Count : Count_Type := 1);
194 procedure Insert_Child
195 (Container : in out Tree;
198 New_Item : Element_Type;
199 Position : out Cursor;
200 Count : Count_Type := 1);
202 procedure Insert_Child
203 (Container : in out Tree;
206 Position : out Cursor;
207 Count : Count_Type := 1);
209 procedure Prepend_Child
210 (Container : in out Tree;
212 New_Item : Element_Type;
213 Count : Count_Type := 1);
215 procedure Append_Child
216 (Container : in out Tree;
218 New_Item : Element_Type;
219 Count : Count_Type := 1);
221 procedure Delete_Children
222 (Container : in out Tree;
225 procedure Copy_Subtree
226 (Target : in out Tree;
231 procedure Splice_Subtree
232 (Target : in out Tree;
235 Source : in out Tree;
236 Position : in out Cursor);
238 procedure Splice_Subtree
239 (Container : in out Tree;
244 procedure Splice_Children
245 (Target : in out Tree;
246 Target_Parent : Cursor;
248 Source : in out Tree;
249 Source_Parent : Cursor);
251 procedure Splice_Children
252 (Container : in out Tree;
253 Target_Parent : Cursor;
255 Source_Parent : Cursor);
257 function Parent (Position : Cursor) return Cursor;
259 function First_Child (Parent : Cursor) return Cursor;
261 function First_Child_Element (Parent : Cursor) return Element_Type;
263 function Last_Child (Parent : Cursor) return Cursor;
265 function Last_Child_Element (Parent : Cursor) return Element_Type;
267 function Next_Sibling (Position : Cursor) return Cursor;
269 function Previous_Sibling (Position : Cursor) return Cursor;
271 procedure Next_Sibling (Position : in out Cursor);
273 procedure Previous_Sibling (Position : in out Cursor);
275 -- This version of the AI:
276 -- 10-06-02 AI05-0136-1/07
277 -- declares Iterate_Children this way:
279 -- procedure Iterate_Children
280 -- (Container : Tree;
282 -- Process : not null access procedure (Position : Cursor));
284 -- It seems that the Container parameter is there by mistake, but we need
285 -- an official ruling from the ARG. ???
287 procedure Iterate_Children
289 Process : not null access procedure (Position : Cursor));
291 procedure Reverse_Iterate_Children
293 Process : not null access procedure (Position : Cursor));
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.
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
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).
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.
323 type Tree_Node_Access is access all Tree_Node_Type;
324 pragma Convention (C, Tree_Node_Access);
326 type Children_Type is record
327 First : Tree_Node_Access;
328 Last : Tree_Node_Access;
331 -- See the comment above. This declaration must exactly match the
332 -- declaration of Root_Node_Type (except for the Element component).
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;
341 pragma Convention (C, Tree_Node_Type);
343 -- See the comment above. This declaration must match the declaration of
344 -- Tree_Node_Type (except for the Element component).
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;
352 pragma Convention (C, Root_Node_Type);
354 use Ada.Finalization;
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).
368 type Tree is new Controlled with record
369 Root : aliased Root_Node_Type;
372 Count : Count_Type := 0;
375 overriding procedure Adjust (Container : in out Tree);
377 overriding procedure Finalize (Container : in out Tree) renames Clear;
382 (Stream : not null access Root_Stream_Type'Class;
385 for Tree'Write use Write;
388 (Stream : not null access Root_Stream_Type'Class;
389 Container : out Tree);
391 for Tree'Read use Read;
393 type Tree_Access is access all Tree;
394 for Tree_Access'Storage_Size use 0;
396 type Cursor is record
397 Container : Tree_Access;
398 Node : Tree_Node_Access;
402 (Stream : not null access Root_Stream_Type'Class;
405 for Cursor'Write use Write;
408 (Stream : not null access Root_Stream_Type'Class;
409 Position : out Cursor);
411 for Cursor'Read use Read;
413 type Constant_Reference_Type
414 (Element : not null access constant Element_Type) is null record;
417 (Stream : not null access Root_Stream_Type'Class;
418 Item : out Constant_Reference_Type);
420 for Constant_Reference_Type'Read use Read;
423 (Stream : not null access Root_Stream_Type'Class;
424 Item : Constant_Reference_Type);
426 for Constant_Reference_Type'Write use Write;
429 (Element : not null access Element_Type) is null record;
432 (Stream : not null access Root_Stream_Type'Class;
433 Item : out Reference_Type);
435 for Reference_Type'Read use Read;
438 (Stream : not null access Root_Stream_Type'Class;
439 Item : Reference_Type);
441 for Reference_Type'Write use Write;
443 function Constant_Reference
444 (Container : aliased Tree;
446 return Constant_Reference_Type;
449 (Container : aliased Tree;
451 return Reference_Type;
453 Empty_Tree : constant Tree := (Controlled with others => <>);
455 No_Element : constant Cursor := (others => <>);
457 end Ada.Containers.Multiway_Trees;