818cde28a1c7628c20b9a2a1e56367ace23f1cf8
[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 private with Ada.Streams;
35
36 generic
37 type Element_Type is private;
38
39 with function "=" (Left, Right : Element_Type) return Boolean is <>;
40
41 package Ada.Containers.Bounded_Multiway_Trees is
42 pragma Pure;
43 pragma Remote_Types;
44
45 type Tree (Capacity : Count_Type) is tagged private;
46 pragma Preelaborable_Initialization (Tree);
47
48 type Cursor is private;
49 pragma Preelaborable_Initialization (Cursor);
50
51 Empty_Tree : constant Tree;
52
53 No_Element : constant Cursor;
54
55 function Equal_Subtree
56 (Left_Position : Cursor;
57 Right_Position : Cursor) return Boolean;
58
59 function "=" (Left, Right : Tree) return Boolean;
60
61 function Is_Empty (Container : Tree) return Boolean;
62
63 function Node_Count (Container : Tree) return Count_Type;
64
65 function Subtree_Node_Count (Position : Cursor) return Count_Type;
66
67 function Depth (Position : Cursor) return Count_Type;
68
69 function Is_Root (Position : Cursor) return Boolean;
70
71 function Is_Leaf (Position : Cursor) return Boolean;
72
73 function Root (Container : Tree) return Cursor;
74
75 procedure Clear (Container : in out Tree);
76
77 function Element (Position : Cursor) return Element_Type;
78
79 procedure Replace_Element
80 (Container : in out Tree;
81 Position : Cursor;
82 New_Item : Element_Type);
83
84 procedure Query_Element
85 (Position : Cursor;
86 Process : not null access procedure (Element : Element_Type));
87
88 procedure Update_Element
89 (Container : in out Tree;
90 Position : Cursor;
91 Process : not null access procedure (Element : in out Element_Type));
92
93 procedure Assign (Target : in out Tree; Source : Tree);
94
95 function Copy (Source : Tree; Capacity : Count_Type := 0) return Tree;
96
97 procedure Move (Target : in out Tree; Source : in out Tree);
98
99 procedure Delete_Leaf
100 (Container : in out Tree;
101 Position : in out Cursor);
102
103 procedure Delete_Subtree
104 (Container : in out Tree;
105 Position : in out Cursor);
106
107 procedure Swap
108 (Container : in out Tree;
109 I, J : Cursor);
110
111 function Find
112 (Container : Tree;
113 Item : Element_Type) return Cursor;
114
115 -- This version of the AI:
116 -- 10-06-02 AI05-0136-1/07
117 -- declares Find_In_Subtree this way:
118 --
119 -- function Find_In_Subtree
120 -- (Container : Tree;
121 -- Item : Element_Type;
122 -- Position : Cursor) return Cursor;
123 --
124 -- It seems that the Container parameter is there by mistake, but we need
125 -- an official ruling from the ARG. ???
126
127 function Find_In_Subtree
128 (Position : Cursor;
129 Item : Element_Type) return Cursor;
130
131 -- This version of the AI:
132 -- 10-06-02 AI05-0136-1/07
133 -- declares Ancestor_Find this way:
134 --
135 -- function Ancestor_Find
136 -- (Container : Tree;
137 -- Item : Element_Type;
138 -- Position : Cursor) return Cursor;
139 --
140 -- It seems that the Container parameter is there by mistake, but we need
141 -- an official ruling from the ARG. ???
142
143 function Ancestor_Find
144 (Position : Cursor;
145 Item : Element_Type) return Cursor;
146
147 function Contains
148 (Container : Tree;
149 Item : Element_Type) return Boolean;
150
151 function Has_Element (Position : Cursor) return Boolean;
152
153 procedure Iterate
154 (Container : Tree;
155 Process : not null access procedure (Position : Cursor));
156
157 procedure Iterate_Subtree
158 (Position : Cursor;
159 Process : not null access procedure (Position : Cursor));
160
161 function Child_Count (Parent : Cursor) return Count_Type;
162
163 function Child_Depth (Parent, Child : Cursor) return Count_Type;
164
165 procedure Insert_Child
166 (Container : in out Tree;
167 Parent : Cursor;
168 Before : Cursor;
169 New_Item : Element_Type;
170 Count : Count_Type := 1);
171
172 procedure Insert_Child
173 (Container : in out Tree;
174 Parent : Cursor;
175 Before : Cursor;
176 New_Item : Element_Type;
177 Position : out Cursor;
178 Count : Count_Type := 1);
179
180 procedure Insert_Child
181 (Container : in out Tree;
182 Parent : Cursor;
183 Before : Cursor;
184 Position : out Cursor;
185 Count : Count_Type := 1);
186
187 procedure Prepend_Child
188 (Container : in out Tree;
189 Parent : Cursor;
190 New_Item : Element_Type;
191 Count : Count_Type := 1);
192
193 procedure Append_Child
194 (Container : in out Tree;
195 Parent : Cursor;
196 New_Item : Element_Type;
197 Count : Count_Type := 1);
198
199 procedure Delete_Children
200 (Container : in out Tree;
201 Parent : Cursor);
202
203 procedure Copy_Subtree
204 (Target : in out Tree;
205 Parent : Cursor;
206 Before : Cursor;
207 Source : Cursor);
208
209 procedure Splice_Subtree
210 (Target : in out Tree;
211 Parent : Cursor;
212 Before : Cursor;
213 Source : in out Tree;
214 Position : in out Cursor);
215
216 procedure Splice_Subtree
217 (Container : in out Tree;
218 Parent : Cursor;
219 Before : Cursor;
220 Position : Cursor);
221
222 procedure Splice_Children
223 (Target : in out Tree;
224 Target_Parent : Cursor;
225 Before : Cursor;
226 Source : in out Tree;
227 Source_Parent : Cursor);
228
229 procedure Splice_Children
230 (Container : in out Tree;
231 Target_Parent : Cursor;
232 Before : Cursor;
233 Source_Parent : Cursor);
234
235 function Parent (Position : Cursor) return Cursor;
236
237 function First_Child (Parent : Cursor) return Cursor;
238
239 function First_Child_Element (Parent : Cursor) return Element_Type;
240
241 function Last_Child (Parent : Cursor) return Cursor;
242
243 function Last_Child_Element (Parent : Cursor) return Element_Type;
244
245 function Next_Sibling (Position : Cursor) return Cursor;
246
247 function Previous_Sibling (Position : Cursor) return Cursor;
248
249 procedure Next_Sibling (Position : in out Cursor);
250
251 procedure Previous_Sibling (Position : in out Cursor);
252
253 -- This version of the AI:
254
255 -- 10-06-02 AI05-0136-1/07
256
257 -- declares Iterate_Children this way:
258
259 -- procedure Iterate_Children
260 -- (Container : Tree;
261 -- Parent : Cursor;
262 -- Process : not null access procedure (Position : Cursor));
263
264 -- It seems that the Container parameter is there by mistake, but we need
265 -- an official ruling from the ARG. ???
266
267 procedure Iterate_Children
268 (Parent : Cursor;
269 Process : not null access procedure (Position : Cursor));
270
271 procedure Reverse_Iterate_Children
272 (Parent : Cursor;
273 Process : not null access procedure (Position : Cursor));
274
275 private
276
277 type Children_Type is record
278 First : Count_Type'Base;
279 Last : Count_Type'Base;
280 end record;
281
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;
287 end record;
288
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;
291
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;
296 Busy : Integer := 0;
297 Lock : Integer := 0;
298 Count : Count_Type := 0;
299 end record;
300
301 use Ada.Streams;
302
303 procedure Write
304 (Stream : not null access Root_Stream_Type'Class;
305 Container : Tree);
306
307 for Tree'Write use Write;
308
309 procedure Read
310 (Stream : not null access Root_Stream_Type'Class;
311 Container : out Tree);
312
313 for Tree'Read use Read;
314
315 type Tree_Access is access all Tree;
316 for Tree_Access'Storage_Size use 0;
317
318 type Cursor is record
319 Container : Tree_Access;
320 Node : Count_Type'Base := -1;
321 end record;
322
323 procedure Write
324 (Stream : not null access Root_Stream_Type'Class;
325 Position : Cursor);
326
327 for Cursor'Write use Write;
328
329 procedure Read
330 (Stream : not null access Root_Stream_Type'Class;
331 Position : out Cursor);
332
333 for Cursor'Read use Read;
334
335 Empty_Tree : constant Tree := Tree'(Capacity => 0, others => <>);
336
337 No_Element : constant Cursor := Cursor'(others => <>);
338
339 end Ada.Containers.Bounded_Multiway_Trees;