OSDN Git Service

2012-01-05 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-cimutr.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --                   ADA.CONTAINERS.INDEFINITE_MULTIWAY_TREES               --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-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.Finalization;
36 private with Ada.Streams;
37
38 generic
39    type Element_Type (<>) is private;
40
41    with function "=" (Left, Right : Element_Type) return Boolean is <>;
42
43 package Ada.Containers.Indefinite_Multiway_Trees is
44    pragma Preelaborate;
45    pragma Remote_Types;
46
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
53    pragma Preelaborable_Initialization (Tree);
54
55    type Cursor is private;
56    pragma Preelaborable_Initialization (Cursor);
57
58    Empty_Tree : constant Tree;
59
60    No_Element : constant Cursor;
61    function Has_Element (Position : Cursor) return Boolean;
62
63    package Tree_Iterator_Interfaces is new
64      Ada.Iterator_Interfaces (Cursor, Has_Element);
65
66    function Equal_Subtree
67      (Left_Position  : Cursor;
68       Right_Position : Cursor) return Boolean;
69
70    function "=" (Left, Right : Tree) return Boolean;
71
72    function Is_Empty (Container : Tree) return Boolean;
73
74    function Node_Count (Container : Tree) return Count_Type;
75
76    function Subtree_Node_Count (Position : Cursor) return Count_Type;
77
78    function Depth (Position : Cursor) return Count_Type;
79
80    function Is_Root (Position : Cursor) return Boolean;
81
82    function Is_Leaf (Position : Cursor) return Boolean;
83
84    function Root (Container : Tree) return Cursor;
85
86    procedure Clear (Container : in out Tree);
87
88    function Element (Position : Cursor) return Element_Type;
89
90    procedure Replace_Element
91      (Container : in out Tree;
92       Position  : Cursor;
93       New_Item  : Element_Type);
94
95    procedure Query_Element
96      (Position : Cursor;
97       Process  : not null access procedure (Element : Element_Type));
98
99    procedure Update_Element
100      (Container : in out Tree;
101       Position  : Cursor;
102       Process   : not null access procedure (Element : in out Element_Type));
103
104    type Constant_Reference_Type
105      (Element : not null access constant Element_Type) is private
106         with Implicit_Dereference => Element;
107
108    type Reference_Type
109      (Element : not null access Element_Type) is private
110         with Implicit_Dereference => Element;
111
112    procedure Assign (Target : in out Tree; Source : Tree);
113
114    function Copy (Source : Tree) return Tree;
115
116    procedure Move (Target : in out Tree; Source : in out Tree);
117
118    procedure Delete_Leaf
119      (Container : in out Tree;
120       Position  : in out Cursor);
121
122    procedure Delete_Subtree
123      (Container : in out Tree;
124       Position  : in out Cursor);
125
126    procedure Swap
127      (Container : in out Tree;
128       I, J      : Cursor);
129
130    function Find
131      (Container : Tree;
132       Item      : Element_Type) return Cursor;
133
134    --  This version of the AI:
135    --   10-06-02  AI05-0136-1/07
136    --  declares Find_In_Subtree this way:
137    --
138    --  function Find_In_Subtree
139    --    (Container : Tree;
140    --     Item      : Element_Type;
141    --     Position  : Cursor) return Cursor;
142    --
143    --  It seems that the Container parameter is there by mistake, but we need
144    --  an official ruling from the ARG. ???
145
146    function Find_In_Subtree
147      (Position : Cursor;
148       Item     : Element_Type) return Cursor;
149
150    --  This version of the AI:
151    --   10-06-02  AI05-0136-1/07
152    --  declares Ancestor_Find this way:
153    --
154    --  function Ancestor_Find
155    --    (Container : Tree;
156    --     Item      : Element_Type;
157    --     Position  : Cursor) return Cursor;
158    --
159    --  It seems that the Container parameter is there by mistake, but we need
160    --  an official ruling from the ARG. ???
161
162    function Ancestor_Find
163      (Position : Cursor;
164       Item     : Element_Type) return Cursor;
165
166    function Contains
167      (Container : Tree;
168       Item      : Element_Type) return Boolean;
169
170    procedure Iterate
171      (Container : Tree;
172       Process   : not null access procedure (Position : Cursor));
173
174    procedure Iterate_Subtree
175      (Position  : Cursor;
176       Process   : not null access procedure (Position : Cursor));
177
178    function Iterate (Container : Tree)
179      return Tree_Iterator_Interfaces.Forward_Iterator'Class;
180
181    function Iterate_Subtree (Position : Cursor)
182      return Tree_Iterator_Interfaces.Forward_Iterator'Class;
183
184    function Iterate_Children
185      (Container : Tree;
186       Parent    : Cursor)
187      return Tree_Iterator_Interfaces.Reversible_Iterator'Class;
188
189    function Child_Count (Parent : Cursor) return Count_Type;
190
191    function Child_Depth (Parent, Child : Cursor) return Count_Type;
192
193    procedure Insert_Child
194      (Container : in out Tree;
195       Parent    : Cursor;
196       Before    : Cursor;
197       New_Item  : Element_Type;
198       Count     : Count_Type := 1);
199
200    procedure Insert_Child
201      (Container : in out Tree;
202       Parent    : Cursor;
203       Before    : Cursor;
204       New_Item  : Element_Type;
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    --   10-06-02  AI05-0136-1/07
276    --  declares Iterate_Children this way:
277    --
278    --  procedure Iterate_Children
279    --    (Container : Tree;
280    --     Parent    : Cursor;
281    --     Process   : not null access procedure (Position : Cursor));
282    --
283    --  It seems that the Container parameter is there by mistake, but we need
284    --  an official ruling from the ARG. ???
285
286    procedure Iterate_Children
287      (Parent  : Cursor;
288       Process : not null access procedure (Position : Cursor));
289
290    procedure Reverse_Iterate_Children
291      (Parent  : Cursor;
292       Process : not null access procedure (Position : Cursor));
293
294 private
295
296    type Tree_Node_Type;
297    type Tree_Node_Access is access all Tree_Node_Type;
298
299    type Children_Type is record
300       First : Tree_Node_Access;
301       Last  : Tree_Node_Access;
302    end record;
303
304    type Element_Access is access Element_Type;
305
306    type Tree_Node_Type is record
307       Parent   : Tree_Node_Access;
308       Prev     : Tree_Node_Access;
309       Next     : Tree_Node_Access;
310       Children : Children_Type;
311       Element  : Element_Access;
312    end record;
313
314    use Ada.Finalization;
315
316    --  The Count component of type Tree represents the number of nodes that
317    --  have been (dynamically) allocated. It does not include the root node
318    --  itself. As implementors, we decide to cache this value, so that the
319    --  selector function Node_Count can execute in O(1) time, in order to be
320    --  consistent with the behavior of the Length selector function for other
321    --  standard container library units. This does mean, however, that the
322    --  two-container forms for Splice_XXX (that move subtrees across tree
323    --  containers) will execute in O(n) time, because we must count the number
324    --  of nodes in the subtree(s) that get moved. (We resolve the tension
325    --  between Node_Count and Splice_XXX in favor of Node_Count, under the
326    --  assumption that Node_Count is the more common operation).
327
328    type Tree is new Controlled with record
329       Root  : aliased Tree_Node_Type;
330       Busy  : Natural := 0;
331       Lock  : Natural := 0;
332       Count : Count_Type := 0;
333    end record;
334
335    overriding procedure Adjust (Container : in out Tree);
336
337    overriding procedure Finalize (Container : in out Tree) renames Clear;
338
339    use Ada.Streams;
340
341    procedure Write
342      (Stream    : not null access Root_Stream_Type'Class;
343       Container : Tree);
344
345    for Tree'Write use Write;
346
347    procedure Read
348      (Stream    : not null access Root_Stream_Type'Class;
349       Container : out Tree);
350
351    for Tree'Read use Read;
352
353    type Tree_Access is access all Tree;
354    for Tree_Access'Storage_Size use 0;
355
356    type Cursor is record
357       Container : Tree_Access;
358       Node      : Tree_Node_Access;
359    end record;
360
361    procedure Write
362      (Stream   : not null access Root_Stream_Type'Class;
363       Position : Cursor);
364
365    for Cursor'Write use Write;
366
367    procedure Read
368      (Stream   : not null access Root_Stream_Type'Class;
369       Position : out Cursor);
370
371    for Cursor'Read use Read;
372
373    type Constant_Reference_Type
374      (Element : not null access constant Element_Type) is null record;
375
376    procedure Read
377      (Stream : not null access Root_Stream_Type'Class;
378       Item   : out Constant_Reference_Type);
379
380    for Constant_Reference_Type'Read use Read;
381
382    procedure Write
383      (Stream : not null access Root_Stream_Type'Class;
384       Item   : Constant_Reference_Type);
385
386    for Constant_Reference_Type'Write use Write;
387
388    type Reference_Type
389      (Element : not null access Element_Type) is null record;
390
391    procedure Read
392      (Stream : not null access Root_Stream_Type'Class;
393       Item   : out Reference_Type);
394
395    for Reference_Type'Read use Read;
396
397    procedure Write
398      (Stream : not null access Root_Stream_Type'Class;
399       Item   : Reference_Type);
400
401    for Reference_Type'Write use Write;
402
403    function Constant_Reference
404      (Container : aliased Tree;
405       Position  : Cursor) return Constant_Reference_Type;
406
407    function Reference
408      (Container : aliased Tree;
409       Position  : Cursor) return Reference_Type;
410
411    Empty_Tree : constant Tree := (Controlled with others => <>);
412
413    No_Element : constant Cursor := (others => <>);
414
415 end Ada.Containers.Indefinite_Multiway_Trees;