[multiple changes]
[gcc.git] / gcc / ada / a-cbmutr.ads
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.BOUNDED_MULTIWAY_TREES --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 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.Streams;
36
37 generic
38 type Element_Type is private;
39
40 with function "=" (Left, Right : Element_Type) return Boolean is <>;
41
42 package Ada.Containers.Bounded_Multiway_Trees is
43 pragma Pure;
44 pragma Remote_Types;
45
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);
52
53 type Cursor is private;
54 pragma Preelaborable_Initialization (Cursor);
55
56 Empty_Tree : constant Tree;
57
58 No_Element : constant Cursor;
59 function Has_Element (Position : Cursor) return Boolean;
60
61 package Tree_Iterator_Interfaces is new
62 Ada.Iterator_Interfaces (Cursor, Has_Element);
63
64 function Equal_Subtree
65 (Left_Position : Cursor;
66 Right_Position : Cursor) return Boolean;
67
68 function "=" (Left, Right : Tree) return Boolean;
69
70 function Is_Empty (Container : Tree) return Boolean;
71
72 function Node_Count (Container : Tree) return Count_Type;
73
74 function Subtree_Node_Count (Position : Cursor) return Count_Type;
75
76 function Depth (Position : Cursor) return Count_Type;
77
78 function Is_Root (Position : Cursor) return Boolean;
79
80 function Is_Leaf (Position : Cursor) return Boolean;
81
82 function Root (Container : Tree) return Cursor;
83
84 procedure Clear (Container : in out Tree);
85
86 function Element (Position : Cursor) return Element_Type;
87
88 procedure Replace_Element
89 (Container : in out Tree;
90 Position : Cursor;
91 New_Item : Element_Type);
92
93 procedure Query_Element
94 (Position : Cursor;
95 Process : not null access procedure (Element : Element_Type));
96
97 procedure Update_Element
98 (Container : in out Tree;
99 Position : Cursor;
100 Process : not null access procedure (Element : in out Element_Type));
101
102 type Constant_Reference_Type
103 (Element : not null access constant Element_Type) is private
104 with Implicit_Dereference => Element;
105
106 type Reference_Type
107 (Element : not null access Element_Type) is private
108 with Implicit_Dereference => Element;
109
110 procedure Assign (Target : in out Tree; Source : Tree);
111
112 function Copy (Source : Tree; Capacity : Count_Type := 0) return Tree;
113
114 procedure Move (Target : in out Tree; Source : in out Tree);
115
116 procedure Delete_Leaf
117 (Container : in out Tree;
118 Position : in out Cursor);
119
120 procedure Delete_Subtree
121 (Container : in out Tree;
122 Position : in out Cursor);
123
124 procedure Swap
125 (Container : in out Tree;
126 I, J : Cursor);
127
128 function Find
129 (Container : Tree;
130 Item : Element_Type) return Cursor;
131
132 -- This version of the AI:
133 -- 10-06-02 AI05-0136-1/07
134 -- declares Find_In_Subtree this way:
135 --
136 -- function Find_In_Subtree
137 -- (Container : Tree;
138 -- Item : Element_Type;
139 -- Position : Cursor) return Cursor;
140 --
141 -- It seems that the Container parameter is there by mistake, but we need
142 -- an official ruling from the ARG. ???
143
144 function Find_In_Subtree
145 (Position : Cursor;
146 Item : Element_Type) return Cursor;
147
148 -- This version of the AI:
149 -- 10-06-02 AI05-0136-1/07
150 -- declares Ancestor_Find this way:
151 --
152 -- function Ancestor_Find
153 -- (Container : Tree;
154 -- Item : Element_Type;
155 -- Position : Cursor) return Cursor;
156 --
157 -- It seems that the Container parameter is there by mistake, but we need
158 -- an official ruling from the ARG. ???
159
160 function Ancestor_Find
161 (Position : Cursor;
162 Item : Element_Type) return Cursor;
163
164 function Contains
165 (Container : Tree;
166 Item : Element_Type) return Boolean;
167
168 procedure Iterate
169 (Container : Tree;
170 Process : not null access procedure (Position : Cursor));
171
172 procedure Iterate_Subtree
173 (Position : Cursor;
174 Process : not null access procedure (Position : Cursor));
175
176 function Iterate (Container : Tree)
177 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
178
179 function Iterate_Subtree (Position : Cursor)
180 return Tree_Iterator_Interfaces.Forward_Iterator'Class;
181
182 function Child_Count (Parent : Cursor) return Count_Type;
183
184 function Child_Depth (Parent, Child : Cursor) return Count_Type;
185
186 procedure Insert_Child
187 (Container : in out Tree;
188 Parent : Cursor;
189 Before : Cursor;
190 New_Item : Element_Type;
191 Count : Count_Type := 1);
192
193 procedure Insert_Child
194 (Container : in out Tree;
195 Parent : Cursor;
196 Before : Cursor;
197 New_Item : Element_Type;
198 Position : out Cursor;
199 Count : Count_Type := 1);
200
201 procedure Insert_Child
202 (Container : in out Tree;
203 Parent : Cursor;
204 Before : Cursor;
205 Position : out Cursor;
206 Count : Count_Type := 1);
207
208 procedure Prepend_Child
209 (Container : in out Tree;
210 Parent : Cursor;
211 New_Item : Element_Type;
212 Count : Count_Type := 1);
213
214 procedure Append_Child
215 (Container : in out Tree;
216 Parent : Cursor;
217 New_Item : Element_Type;
218 Count : Count_Type := 1);
219
220 procedure Delete_Children
221 (Container : in out Tree;
222 Parent : Cursor);
223
224 procedure Copy_Subtree
225 (Target : in out Tree;
226 Parent : Cursor;
227 Before : Cursor;
228 Source : Cursor);
229
230 procedure Splice_Subtree
231 (Target : in out Tree;
232 Parent : Cursor;
233 Before : Cursor;
234 Source : in out Tree;
235 Position : in out Cursor);
236
237 procedure Splice_Subtree
238 (Container : in out Tree;
239 Parent : Cursor;
240 Before : Cursor;
241 Position : Cursor);
242
243 procedure Splice_Children
244 (Target : in out Tree;
245 Target_Parent : Cursor;
246 Before : Cursor;
247 Source : in out Tree;
248 Source_Parent : Cursor);
249
250 procedure Splice_Children
251 (Container : in out Tree;
252 Target_Parent : Cursor;
253 Before : Cursor;
254 Source_Parent : Cursor);
255
256 function Parent (Position : Cursor) return Cursor;
257
258 function First_Child (Parent : Cursor) return Cursor;
259
260 function First_Child_Element (Parent : Cursor) return Element_Type;
261
262 function Last_Child (Parent : Cursor) return Cursor;
263
264 function Last_Child_Element (Parent : Cursor) return Element_Type;
265
266 function Next_Sibling (Position : Cursor) return Cursor;
267
268 function Previous_Sibling (Position : Cursor) return Cursor;
269
270 procedure Next_Sibling (Position : in out Cursor);
271
272 procedure Previous_Sibling (Position : in out Cursor);
273
274 -- This version of the AI:
275
276 -- 10-06-02 AI05-0136-1/07
277
278 -- declares Iterate_Children this way:
279
280 -- procedure Iterate_Children
281 -- (Container : Tree;
282 -- Parent : Cursor;
283 -- Process : not null access procedure (Position : Cursor));
284
285 -- It seems that the Container parameter is there by mistake, but we need
286 -- an official ruling from the ARG. ???
287
288 procedure Iterate_Children
289 (Parent : Cursor;
290 Process : not null access procedure (Position : Cursor));
291
292 procedure Reverse_Iterate_Children
293 (Parent : Cursor;
294 Process : not null access procedure (Position : Cursor));
295
296 private
297 use Ada.Streams;
298
299 type Children_Type is record
300 First : Count_Type'Base;
301 Last : Count_Type'Base;
302 end record;
303
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;
309 end record;
310
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;
313
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;
318 Busy : Integer := 0;
319 Lock : Integer := 0;
320 Count : Count_Type := 0;
321 end record;
322
323 procedure Write
324 (Stream : not null access Root_Stream_Type'Class;
325 Container : Tree);
326
327 for Tree'Write use Write;
328
329 procedure Read
330 (Stream : not null access Root_Stream_Type'Class;
331 Container : out Tree);
332
333 for Tree'Read use Read;
334
335 type Tree_Access is access all Tree;
336 for Tree_Access'Storage_Size use 0;
337
338 type Cursor is record
339 Container : Tree_Access;
340 Node : Count_Type'Base := -1;
341 end record;
342
343 procedure Read
344 (Stream : not null access Root_Stream_Type'Class;
345 Position : out Cursor);
346 for Cursor'Read use Read;
347
348 procedure Write
349 (Stream : not null access Root_Stream_Type'Class;
350 Position : Cursor);
351 for Cursor'Write use Write;
352
353 type Constant_Reference_Type
354 (Element : not null access constant Element_Type) is null record;
355
356 procedure Write
357 (Stream : not null access Root_Stream_Type'Class;
358 Item : Constant_Reference_Type);
359 for Constant_Reference_Type'Write use Write;
360
361 procedure Read
362 (Stream : not null access Root_Stream_Type'Class;
363 Item : out Constant_Reference_Type);
364 for Constant_Reference_Type'Read use Read;
365
366 type Reference_Type
367 (Element : not null access Element_Type) is null record;
368 procedure Write
369 (Stream : not null access Root_Stream_Type'Class;
370 Item : Reference_Type);
371 for Reference_Type'Write use Write;
372
373 procedure Read
374 (Stream : not null access Root_Stream_Type'Class;
375 Item : out Reference_Type);
376 for Reference_Type'Read use Read;
377
378 function Constant_Reference
379 (Container : aliased Tree;
380 Position : Cursor)
381 return Constant_Reference_Type;
382
383 function Reference
384 (Container : aliased Tree;
385 Position : Cursor)
386 return Reference_Type;
387
388 Empty_Tree : constant Tree := (Capacity => 0, others => <>);
389
390 No_Element : constant Cursor := Cursor'(others => <>);
391
392 end Ada.Containers.Bounded_Multiway_Trees;