1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.BOUNDED_MULTIWAY_TREES --
9 -- Copyright (C) 2014-2015, 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
;
36 with Ada
.Containers
.Helpers
;
37 private with Ada
.Streams
;
40 type Element_Type
is private;
42 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
44 package Ada
.Containers
.Bounded_Multiway_Trees
is
45 pragma Annotate
(CodePeer
, Skip_Analysis
);
49 type Tree
(Capacity
: Count_Type
) is tagged private
50 with Constant_Indexing
=> Constant_Reference
,
51 Variable_Indexing
=> Reference
,
52 Default_Iterator
=> Iterate
,
53 Iterator_Element
=> Element_Type
;
54 pragma Preelaborable_Initialization
(Tree
);
56 type Cursor
is private;
57 pragma Preelaborable_Initialization
(Cursor
);
59 Empty_Tree
: constant Tree
;
61 No_Element
: constant Cursor
;
62 function Has_Element
(Position
: Cursor
) return Boolean;
64 package Tree_Iterator_Interfaces
is new
65 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
67 function Equal_Subtree
68 (Left_Position
: Cursor
;
69 Right_Position
: Cursor
) return Boolean;
71 function "=" (Left
, Right
: Tree
) return Boolean;
73 function Is_Empty
(Container
: Tree
) return Boolean;
75 function Node_Count
(Container
: Tree
) return Count_Type
;
77 function Subtree_Node_Count
(Position
: Cursor
) return Count_Type
;
79 function Depth
(Position
: Cursor
) return Count_Type
;
81 function Is_Root
(Position
: Cursor
) return Boolean;
83 function Is_Leaf
(Position
: Cursor
) return Boolean;
85 function Root
(Container
: Tree
) return Cursor
;
87 procedure Clear
(Container
: in out Tree
);
89 function Element
(Position
: Cursor
) return Element_Type
;
91 procedure Replace_Element
92 (Container
: in out Tree
;
94 New_Item
: Element_Type
);
96 procedure Query_Element
98 Process
: not null access procedure (Element
: Element_Type
));
100 procedure Update_Element
101 (Container
: in out Tree
;
103 Process
: not null access procedure (Element
: in out Element_Type
));
105 type Constant_Reference_Type
106 (Element
: not null access constant Element_Type
) is private
107 with Implicit_Dereference
=> Element
;
110 (Element
: not null access Element_Type
) is private
111 with Implicit_Dereference
=> Element
;
113 function Constant_Reference
114 (Container
: aliased Tree
;
115 Position
: Cursor
) return Constant_Reference_Type
;
118 (Container
: aliased in out Tree
;
119 Position
: Cursor
) return Reference_Type
;
121 procedure Assign
(Target
: in out Tree
; Source
: Tree
);
123 function Copy
(Source
: Tree
; Capacity
: Count_Type
:= 0) return Tree
;
125 procedure Move
(Target
: in out Tree
; Source
: in out Tree
);
127 procedure Delete_Leaf
128 (Container
: in out Tree
;
129 Position
: in out Cursor
);
131 procedure Delete_Subtree
132 (Container
: in out Tree
;
133 Position
: in out Cursor
);
136 (Container
: in out Tree
;
141 Item
: Element_Type
) return Cursor
;
143 function Find_In_Subtree
145 Item
: Element_Type
) return Cursor
;
147 function Ancestor_Find
149 Item
: Element_Type
) return Cursor
;
153 Item
: Element_Type
) return Boolean;
157 Process
: not null access procedure (Position
: Cursor
));
159 procedure Iterate_Subtree
161 Process
: not null access procedure (Position
: Cursor
));
163 function Iterate
(Container
: Tree
)
164 return Tree_Iterator_Interfaces
.Forward_Iterator
'Class;
166 function Iterate_Subtree
(Position
: Cursor
)
167 return Tree_Iterator_Interfaces
.Forward_Iterator
'Class;
169 function Iterate_Children
172 return Tree_Iterator_Interfaces
.Reversible_Iterator
'Class;
174 function Child_Count
(Parent
: Cursor
) return Count_Type
;
176 function Child_Depth
(Parent
, Child
: Cursor
) return Count_Type
;
178 procedure Insert_Child
179 (Container
: in out Tree
;
182 New_Item
: Element_Type
;
183 Count
: Count_Type
:= 1);
185 procedure Insert_Child
186 (Container
: in out Tree
;
189 New_Item
: Element_Type
;
190 Position
: out Cursor
;
191 Count
: Count_Type
:= 1);
193 procedure Insert_Child
194 (Container
: in out Tree
;
197 Position
: out Cursor
;
198 Count
: Count_Type
:= 1);
200 procedure Prepend_Child
201 (Container
: in out Tree
;
203 New_Item
: Element_Type
;
204 Count
: Count_Type
:= 1);
206 procedure Append_Child
207 (Container
: in out Tree
;
209 New_Item
: Element_Type
;
210 Count
: Count_Type
:= 1);
212 procedure Delete_Children
213 (Container
: in out Tree
;
216 procedure Copy_Subtree
217 (Target
: in out Tree
;
222 procedure Splice_Subtree
223 (Target
: in out Tree
;
226 Source
: in out Tree
;
227 Position
: in out Cursor
);
229 procedure Splice_Subtree
230 (Container
: in out Tree
;
235 procedure Splice_Children
236 (Target
: in out Tree
;
237 Target_Parent
: Cursor
;
239 Source
: in out Tree
;
240 Source_Parent
: Cursor
);
242 procedure Splice_Children
243 (Container
: in out Tree
;
244 Target_Parent
: Cursor
;
246 Source_Parent
: Cursor
);
248 function Parent
(Position
: Cursor
) return Cursor
;
250 function First_Child
(Parent
: Cursor
) return Cursor
;
252 function First_Child_Element
(Parent
: Cursor
) return Element_Type
;
254 function Last_Child
(Parent
: Cursor
) return Cursor
;
256 function Last_Child_Element
(Parent
: Cursor
) return Element_Type
;
258 function Next_Sibling
(Position
: Cursor
) return Cursor
;
260 function Previous_Sibling
(Position
: Cursor
) return Cursor
;
262 procedure Next_Sibling
(Position
: in out Cursor
);
264 procedure Previous_Sibling
(Position
: in out Cursor
);
266 procedure Iterate_Children
268 Process
: not null access procedure (Position
: Cursor
));
270 procedure Reverse_Iterate_Children
272 Process
: not null access procedure (Position
: Cursor
));
276 use Ada
.Containers
.Helpers
;
277 package Implementation
is new Generic_Implementation
;
282 No_Node
: constant Count_Type
'Base := -1;
283 -- Need to document all global declarations such as this ???
285 -- Following decls also need much more documentation ???
287 type Children_Type
is record
288 First
: Count_Type
'Base;
289 Last
: Count_Type
'Base;
292 type Tree_Node_Type
is record
293 Parent
: Count_Type
'Base;
294 Prev
: Count_Type
'Base;
295 Next
: Count_Type
'Base;
296 Children
: Children_Type
;
299 type Tree_Node_Array
is array (Count_Type
range <>) of Tree_Node_Type
;
300 type Element_Array
is array (Count_Type
range <>) of aliased Element_Type
;
302 type Tree
(Capacity
: Count_Type
) is tagged record
303 Nodes
: Tree_Node_Array
(0 .. Capacity
) := (others => <>);
304 Elements
: Element_Array
(1 .. Capacity
) := (others => <>);
305 Free
: Count_Type
'Base := No_Node
;
306 TC
: aliased Tamper_Counts
;
307 Count
: Count_Type
:= 0;
311 (Stream
: not null access Root_Stream_Type
'Class;
314 for Tree
'Write use Write
;
317 (Stream
: not null access Root_Stream_Type
'Class;
318 Container
: out Tree
);
320 for Tree
'Read use Read
;
322 type Tree_Access
is access all Tree
;
323 for Tree_Access
'Storage_Size use 0;
325 type Cursor
is record
326 Container
: Tree_Access
;
327 Node
: Count_Type
'Base := No_Node
;
331 (Stream
: not null access Root_Stream_Type
'Class;
332 Position
: out Cursor
);
333 for Cursor
'Read use Read
;
336 (Stream
: not null access Root_Stream_Type
'Class;
338 for Cursor
'Write use Write
;
340 subtype Reference_Control_Type
is Implementation
.Reference_Control_Type
;
341 -- It is necessary to rename this here, so that the compiler can find it
343 type Constant_Reference_Type
344 (Element
: not null access constant Element_Type
) is
346 Control
: Reference_Control_Type
:=
347 raise Program_Error
with "uninitialized reference";
348 -- The RM says, "The default initialization of an object of
349 -- type Constant_Reference_Type or Reference_Type propagates
354 (Stream
: not null access Root_Stream_Type
'Class;
355 Item
: Constant_Reference_Type
);
356 for Constant_Reference_Type
'Write use Write
;
359 (Stream
: not null access Root_Stream_Type
'Class;
360 Item
: out Constant_Reference_Type
);
361 for Constant_Reference_Type
'Read use Read
;
364 (Element
: not null access Element_Type
) is
366 Control
: Reference_Control_Type
:=
367 raise Program_Error
with "uninitialized reference";
368 -- The RM says, "The default initialization of an object of
369 -- type Constant_Reference_Type or Reference_Type propagates
374 (Stream
: not null access Root_Stream_Type
'Class;
375 Item
: Reference_Type
);
376 for Reference_Type
'Write use Write
;
379 (Stream
: not null access Root_Stream_Type
'Class;
380 Item
: out Reference_Type
);
381 for Reference_Type
'Read use Read
;
383 -- Three operations are used to optimize in the expansion of "for ... of"
384 -- loops: the Next(Cursor) procedure in the visible part, and the following
385 -- Pseudo_Reference and Get_Element_Access functions. See Exp_Ch5 for
388 function Pseudo_Reference
389 (Container
: aliased Tree
'Class) return Reference_Control_Type
;
390 pragma Inline
(Pseudo_Reference
);
391 -- Creates an object of type Reference_Control_Type pointing to the
392 -- container, and increments the Lock. Finalization of this object will
393 -- decrement the Lock.
395 type Element_Access
is access all Element_Type
with
398 function Get_Element_Access
399 (Position
: Cursor
) return not null Element_Access
;
400 -- Returns a pointer to the element designated by Position.
402 Empty_Tree
: constant Tree
:= (Capacity
=> 0, others => <>);
404 No_Element
: constant Cursor
:= Cursor
'(others => <>);
406 end Ada.Containers.Bounded_Multiway_Trees;