1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.BOUNDED_MULTIWAY_TREES --
9 -- Copyright (C) 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.Streams;
38 type Element_Type is private;
40 with function "=" (Left, Right : Element_Type) return Boolean is <>;
42 package Ada.Containers.Bounded_Multiway_Trees is
46 type Tree (Capacity : Count_Type) is tagged private
47 with Constant_Indexing => Constant_Reference,
48 Variable_Indexing => Reference,
49 Default_Iterator => Iterate,
50 Iterator_Element => Element_Type;
51 pragma Preelaborable_Initialization (Tree);
53 type Cursor is private;
54 pragma Preelaborable_Initialization (Cursor);
56 Empty_Tree : constant Tree;
58 No_Element : constant Cursor;
59 function Has_Element (Position : Cursor) return Boolean;
61 package Tree_Iterator_Interfaces is new
62 Ada.Iterator_Interfaces (Cursor, Has_Element);
64 function Equal_Subtree
65 (Left_Position : Cursor;
66 Right_Position : Cursor) return Boolean;
68 function "=" (Left, Right : Tree) return Boolean;
70 function Is_Empty (Container : Tree) return Boolean;
72 function Node_Count (Container : Tree) return Count_Type;
74 function Subtree_Node_Count (Position : Cursor) return Count_Type;
76 function Depth (Position : Cursor) return Count_Type;
78 function Is_Root (Position : Cursor) return Boolean;
80 function Is_Leaf (Position : Cursor) return Boolean;
82 function Root (Container : Tree) return Cursor;
84 procedure Clear (Container : in out Tree);
86 function Element (Position : Cursor) return Element_Type;
88 procedure Replace_Element
89 (Container : in out Tree;
91 New_Item : Element_Type);
93 procedure Query_Element
95 Process : not null access procedure (Element : Element_Type));
97 procedure Update_Element
98 (Container : in out Tree;
100 Process : not null access procedure (Element : in out Element_Type));
102 type Constant_Reference_Type
103 (Element : not null access constant Element_Type) is private
104 with Implicit_Dereference => Element;
107 (Element : not null access Element_Type) is private
108 with Implicit_Dereference => Element;
110 procedure Assign (Target : in out Tree; Source : Tree);
112 function Copy (Source : Tree; Capacity : Count_Type := 0) return Tree;
114 procedure Move (Target : in out Tree; Source : in out Tree);
116 procedure Delete_Leaf
117 (Container : in out Tree;
118 Position : in out Cursor);
120 procedure Delete_Subtree
121 (Container : in out Tree;
122 Position : in out Cursor);
125 (Container : in out Tree;
130 Item : Element_Type) return Cursor;
132 -- This version of the AI:
133 -- 10-06-02 AI05-0136-1/07
134 -- declares Find_In_Subtree this way:
136 -- function Find_In_Subtree
137 -- (Container : Tree;
138 -- Item : Element_Type;
139 -- Position : Cursor) return Cursor;
141 -- It seems that the Container parameter is there by mistake, but we need
142 -- an official ruling from the ARG. ???
144 function Find_In_Subtree
146 Item : Element_Type) return Cursor;
148 -- This version of the AI:
149 -- 10-06-02 AI05-0136-1/07
150 -- declares Ancestor_Find this way:
152 -- function Ancestor_Find
153 -- (Container : Tree;
154 -- Item : Element_Type;
155 -- Position : Cursor) return Cursor;
157 -- It seems that the Container parameter is there by mistake, but we need
158 -- an official ruling from the ARG. ???
160 function Ancestor_Find
162 Item : Element_Type) return Cursor;
166 Item : Element_Type) return Boolean;
170 Process : not null access procedure (Position : Cursor));
172 procedure Iterate_Subtree
174 Process : not null access procedure (Position : Cursor));
176 function Iterate (Container : Tree)
177 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
179 function Iterate_Subtree (Position : Cursor)
180 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
182 function Child_Count (Parent : Cursor) return Count_Type;
184 function Child_Depth (Parent, Child : Cursor) return Count_Type;
186 procedure Insert_Child
187 (Container : in out Tree;
190 New_Item : Element_Type;
191 Count : Count_Type := 1);
193 procedure Insert_Child
194 (Container : in out Tree;
197 New_Item : Element_Type;
198 Position : out Cursor;
199 Count : Count_Type := 1);
201 procedure Insert_Child
202 (Container : in out Tree;
205 Position : out Cursor;
206 Count : Count_Type := 1);
208 procedure Prepend_Child
209 (Container : in out Tree;
211 New_Item : Element_Type;
212 Count : Count_Type := 1);
214 procedure Append_Child
215 (Container : in out Tree;
217 New_Item : Element_Type;
218 Count : Count_Type := 1);
220 procedure Delete_Children
221 (Container : in out Tree;
224 procedure Copy_Subtree
225 (Target : in out Tree;
230 procedure Splice_Subtree
231 (Target : in out Tree;
234 Source : in out Tree;
235 Position : in out Cursor);
237 procedure Splice_Subtree
238 (Container : in out Tree;
243 procedure Splice_Children
244 (Target : in out Tree;
245 Target_Parent : Cursor;
247 Source : in out Tree;
248 Source_Parent : Cursor);
250 procedure Splice_Children
251 (Container : in out Tree;
252 Target_Parent : Cursor;
254 Source_Parent : Cursor);
256 function Parent (Position : Cursor) return Cursor;
258 function First_Child (Parent : Cursor) return Cursor;
260 function First_Child_Element (Parent : Cursor) return Element_Type;
262 function Last_Child (Parent : Cursor) return Cursor;
264 function Last_Child_Element (Parent : Cursor) return Element_Type;
266 function Next_Sibling (Position : Cursor) return Cursor;
268 function Previous_Sibling (Position : Cursor) return Cursor;
270 procedure Next_Sibling (Position : in out Cursor);
272 procedure Previous_Sibling (Position : in out Cursor);
274 -- This version of the AI:
276 -- 10-06-02 AI05-0136-1/07
278 -- declares Iterate_Children this way:
280 -- procedure Iterate_Children
281 -- (Container : Tree;
283 -- Process : not null access procedure (Position : Cursor));
285 -- It seems that the Container parameter is there by mistake, but we need
286 -- an official ruling from the ARG. ???
288 procedure Iterate_Children
290 Process : not null access procedure (Position : Cursor));
292 procedure Reverse_Iterate_Children
294 Process : not null access procedure (Position : Cursor));
299 type Children_Type is record
300 First : Count_Type'Base;
301 Last : Count_Type'Base;
304 type Tree_Node_Type is record
305 Parent : Count_Type'Base;
306 Prev : Count_Type'Base;
307 Next : Count_Type'Base;
308 Children : Children_Type;
311 type Tree_Node_Array is array (Count_Type range <>) of Tree_Node_Type;
312 type Element_Array is array (Count_Type range <>) of aliased Element_Type;
314 type Tree (Capacity : Count_Type) is tagged record
315 Nodes : Tree_Node_Array (0 .. Capacity) := (others => <>);
316 Elements : Element_Array (1 .. Capacity) := (others => <>);
317 Free : Count_Type'Base := -1;
320 Count : Count_Type := 0;
324 (Stream : not null access Root_Stream_Type'Class;
327 for Tree'Write use Write;
330 (Stream : not null access Root_Stream_Type'Class;
331 Container : out Tree);
333 for Tree'Read use Read;
335 type Tree_Access is access all Tree;
336 for Tree_Access'Storage_Size use 0;
338 type Cursor is record
339 Container : Tree_Access;
340 Node : Count_Type'Base := -1;
344 (Stream : not null access Root_Stream_Type'Class;
345 Position : out Cursor);
346 for Cursor'Read use Read;
349 (Stream : not null access Root_Stream_Type'Class;
351 for Cursor'Write use Write;
353 type Constant_Reference_Type
354 (Element : not null access constant Element_Type) is null record;
357 (Stream : not null access Root_Stream_Type'Class;
358 Item : Constant_Reference_Type);
359 for Constant_Reference_Type'Write use Write;
362 (Stream : not null access Root_Stream_Type'Class;
363 Item : out Constant_Reference_Type);
364 for Constant_Reference_Type'Read use Read;
367 (Element : not null access Element_Type) is null record;
369 (Stream : not null access Root_Stream_Type'Class;
370 Item : Reference_Type);
371 for Reference_Type'Write use Write;
374 (Stream : not null access Root_Stream_Type'Class;
375 Item : out Reference_Type);
376 for Reference_Type'Read use Read;
378 function Constant_Reference
379 (Container : aliased Tree;
381 return Constant_Reference_Type;
384 (Container : aliased Tree;
386 return Reference_Type;
388 Empty_Tree : constant Tree := (Capacity => 0, others => <>);
390 No_Element : constant Cursor := Cursor'(others => <>);
392 end Ada.Containers.Bounded_Multiway_Trees;