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 private with Ada.Streams;
37 type Element_Type is private;
39 with function "=" (Left, Right : Element_Type) return Boolean is <>;
41 package Ada.Containers.Bounded_Multiway_Trees is
45 type Tree (Capacity : Count_Type) is tagged private;
46 pragma Preelaborable_Initialization (Tree);
48 type Cursor is private;
49 pragma Preelaborable_Initialization (Cursor);
51 Empty_Tree : constant Tree;
53 No_Element : constant Cursor;
55 function Equal_Subtree
56 (Left_Position : Cursor;
57 Right_Position : Cursor) return Boolean;
59 function "=" (Left, Right : Tree) return Boolean;
61 function Is_Empty (Container : Tree) return Boolean;
63 function Node_Count (Container : Tree) return Count_Type;
65 function Subtree_Node_Count (Position : Cursor) return Count_Type;
67 function Depth (Position : Cursor) return Count_Type;
69 function Is_Root (Position : Cursor) return Boolean;
71 function Is_Leaf (Position : Cursor) return Boolean;
73 function Root (Container : Tree) return Cursor;
75 procedure Clear (Container : in out Tree);
77 function Element (Position : Cursor) return Element_Type;
79 procedure Replace_Element
80 (Container : in out Tree;
82 New_Item : Element_Type);
84 procedure Query_Element
86 Process : not null access procedure (Element : Element_Type));
88 procedure Update_Element
89 (Container : in out Tree;
91 Process : not null access procedure (Element : in out Element_Type));
93 procedure Assign (Target : in out Tree; Source : Tree);
95 function Copy (Source : Tree; Capacity : Count_Type := 0) return Tree;
97 procedure Move (Target : in out Tree; Source : in out Tree);
100 (Container : in out Tree;
101 Position : in out Cursor);
103 procedure Delete_Subtree
104 (Container : in out Tree;
105 Position : in out Cursor);
108 (Container : in out Tree;
113 Item : Element_Type) return Cursor;
115 -- This version of the AI:
116 -- 10-06-02 AI05-0136-1/07
117 -- declares Find_In_Subtree this way:
119 -- function Find_In_Subtree
120 -- (Container : Tree;
121 -- Item : Element_Type;
122 -- Position : Cursor) return Cursor;
124 -- It seems that the Container parameter is there by mistake, but we need
125 -- an official ruling from the ARG. ???
127 function Find_In_Subtree
129 Item : Element_Type) return Cursor;
131 -- This version of the AI:
132 -- 10-06-02 AI05-0136-1/07
133 -- declares Ancestor_Find this way:
135 -- function Ancestor_Find
136 -- (Container : Tree;
137 -- Item : Element_Type;
138 -- Position : Cursor) return Cursor;
140 -- It seems that the Container parameter is there by mistake, but we need
141 -- an official ruling from the ARG. ???
143 function Ancestor_Find
145 Item : Element_Type) return Cursor;
149 Item : Element_Type) return Boolean;
151 function Has_Element (Position : Cursor) return Boolean;
155 Process : not null access procedure (Position : Cursor));
157 procedure Iterate_Subtree
159 Process : not null access procedure (Position : Cursor));
161 function Child_Count (Parent : Cursor) return Count_Type;
163 function Child_Depth (Parent, Child : Cursor) return Count_Type;
165 procedure Insert_Child
166 (Container : in out Tree;
169 New_Item : Element_Type;
170 Count : Count_Type := 1);
172 procedure Insert_Child
173 (Container : in out Tree;
176 New_Item : Element_Type;
177 Position : out Cursor;
178 Count : Count_Type := 1);
180 procedure Insert_Child
181 (Container : in out Tree;
184 Position : out Cursor;
185 Count : Count_Type := 1);
187 procedure Prepend_Child
188 (Container : in out Tree;
190 New_Item : Element_Type;
191 Count : Count_Type := 1);
193 procedure Append_Child
194 (Container : in out Tree;
196 New_Item : Element_Type;
197 Count : Count_Type := 1);
199 procedure Delete_Children
200 (Container : in out Tree;
203 procedure Copy_Subtree
204 (Target : in out Tree;
209 procedure Splice_Subtree
210 (Target : in out Tree;
213 Source : in out Tree;
214 Position : in out Cursor);
216 procedure Splice_Subtree
217 (Container : in out Tree;
222 procedure Splice_Children
223 (Target : in out Tree;
224 Target_Parent : Cursor;
226 Source : in out Tree;
227 Source_Parent : Cursor);
229 procedure Splice_Children
230 (Container : in out Tree;
231 Target_Parent : Cursor;
233 Source_Parent : Cursor);
235 function Parent (Position : Cursor) return Cursor;
237 function First_Child (Parent : Cursor) return Cursor;
239 function First_Child_Element (Parent : Cursor) return Element_Type;
241 function Last_Child (Parent : Cursor) return Cursor;
243 function Last_Child_Element (Parent : Cursor) return Element_Type;
245 function Next_Sibling (Position : Cursor) return Cursor;
247 function Previous_Sibling (Position : Cursor) return Cursor;
249 procedure Next_Sibling (Position : in out Cursor);
251 procedure Previous_Sibling (Position : in out Cursor);
253 -- This version of the AI:
255 -- 10-06-02 AI05-0136-1/07
257 -- declares Iterate_Children this way:
259 -- procedure Iterate_Children
260 -- (Container : Tree;
262 -- Process : not null access procedure (Position : Cursor));
264 -- It seems that the Container parameter is there by mistake, but we need
265 -- an official ruling from the ARG. ???
267 procedure Iterate_Children
269 Process : not null access procedure (Position : Cursor));
271 procedure Reverse_Iterate_Children
273 Process : not null access procedure (Position : Cursor));
277 type Children_Type is record
278 First : Count_Type'Base;
279 Last : Count_Type'Base;
282 type Tree_Node_Type is record
283 Parent : Count_Type'Base;
284 Prev : Count_Type'Base;
285 Next : Count_Type'Base;
286 Children : Children_Type;
289 type Tree_Node_Array is array (Count_Type range <>) of Tree_Node_Type;
290 type Element_Array is array (Count_Type range <>) of Element_Type;
292 type Tree (Capacity : Count_Type) is tagged record
293 Nodes : Tree_Node_Array (0 .. Capacity) := (others => <>);
294 Elements : Element_Array (1 .. Capacity) := (others => <>);
295 Free : Count_Type'Base := -1;
298 Count : Count_Type := 0;
304 (Stream : not null access Root_Stream_Type'Class;
307 for Tree'Write use Write;
310 (Stream : not null access Root_Stream_Type'Class;
311 Container : out Tree);
313 for Tree'Read use Read;
315 type Tree_Access is access all Tree;
316 for Tree_Access'Storage_Size use 0;
318 type Cursor is record
319 Container : Tree_Access;
320 Node : Count_Type'Base := -1;
324 (Stream : not null access Root_Stream_Type'Class;
327 for Cursor'Write use Write;
330 (Stream : not null access Root_Stream_Type'Class;
331 Position : out Cursor);
333 for Cursor'Read use Read;
335 Empty_Tree : constant Tree := Tree'(Capacity => 0, others => <>);
337 No_Element : constant Cursor := Cursor'(others => <>);
339 end Ada.Containers.Bounded_Multiway_Trees;