1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.BOUNDED_MULTIWAY_TREES --
9 -- Copyright (C) 2014, 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
;
35 private with Ada
.Streams
;
36 private with Ada
.Finalization
;
39 type Element_Type
is private;
41 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
43 package Ada
.Containers
.Bounded_Multiway_Trees
is
47 type Tree
(Capacity
: Count_Type
) is tagged private
48 with Constant_Indexing
=> Constant_Reference
,
49 Variable_Indexing
=> Reference
,
50 Default_Iterator
=> Iterate
,
51 Iterator_Element
=> Element_Type
;
52 pragma Preelaborable_Initialization
(Tree
);
54 type Cursor
is private;
55 pragma Preelaborable_Initialization
(Cursor
);
57 Empty_Tree
: constant Tree
;
59 No_Element
: constant Cursor
;
60 function Has_Element
(Position
: Cursor
) return Boolean;
62 package Tree_Iterator_Interfaces
is new
63 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
65 function Equal_Subtree
66 (Left_Position
: Cursor
;
67 Right_Position
: Cursor
) return Boolean;
69 function "=" (Left
, Right
: Tree
) return Boolean;
71 function Is_Empty
(Container
: Tree
) return Boolean;
73 function Node_Count
(Container
: Tree
) return Count_Type
;
75 function Subtree_Node_Count
(Position
: Cursor
) return Count_Type
;
77 function Depth
(Position
: Cursor
) return Count_Type
;
79 function Is_Root
(Position
: Cursor
) return Boolean;
81 function Is_Leaf
(Position
: Cursor
) return Boolean;
83 function Root
(Container
: Tree
) return Cursor
;
85 procedure Clear
(Container
: in out Tree
);
87 function Element
(Position
: Cursor
) return Element_Type
;
89 procedure Replace_Element
90 (Container
: in out Tree
;
92 New_Item
: Element_Type
);
94 procedure Query_Element
96 Process
: not null access procedure (Element
: Element_Type
));
98 procedure Update_Element
99 (Container
: in out Tree
;
101 Process
: not null access procedure (Element
: in out Element_Type
));
103 type Constant_Reference_Type
104 (Element
: not null access constant Element_Type
) is private
105 with Implicit_Dereference
=> Element
;
108 (Element
: not null access Element_Type
) is private
109 with Implicit_Dereference
=> Element
;
111 function Constant_Reference
112 (Container
: aliased Tree
;
113 Position
: Cursor
) return Constant_Reference_Type
;
116 (Container
: aliased in out Tree
;
117 Position
: Cursor
) return Reference_Type
;
119 procedure Assign
(Target
: in out Tree
; Source
: Tree
);
121 function Copy
(Source
: Tree
; Capacity
: Count_Type
:= 0) return Tree
;
123 procedure Move
(Target
: in out Tree
; Source
: in out Tree
);
125 procedure Delete_Leaf
126 (Container
: in out Tree
;
127 Position
: in out Cursor
);
129 procedure Delete_Subtree
130 (Container
: in out Tree
;
131 Position
: in out Cursor
);
134 (Container
: in out Tree
;
139 Item
: Element_Type
) return Cursor
;
141 function Find_In_Subtree
143 Item
: Element_Type
) return Cursor
;
145 function Ancestor_Find
147 Item
: Element_Type
) return Cursor
;
151 Item
: Element_Type
) return Boolean;
155 Process
: not null access procedure (Position
: Cursor
));
157 procedure Iterate_Subtree
159 Process
: not null access procedure (Position
: Cursor
));
161 function Iterate
(Container
: Tree
)
162 return Tree_Iterator_Interfaces
.Forward_Iterator
'Class;
164 function Iterate_Subtree
(Position
: Cursor
)
165 return Tree_Iterator_Interfaces
.Forward_Iterator
'Class;
167 function Iterate_Children
170 return Tree_Iterator_Interfaces
.Reversible_Iterator
'Class;
172 function Child_Count
(Parent
: Cursor
) return Count_Type
;
174 function Child_Depth
(Parent
, Child
: Cursor
) return Count_Type
;
176 procedure Insert_Child
177 (Container
: in out Tree
;
180 New_Item
: Element_Type
;
181 Count
: Count_Type
:= 1);
183 procedure Insert_Child
184 (Container
: in out Tree
;
187 New_Item
: Element_Type
;
188 Position
: out Cursor
;
189 Count
: Count_Type
:= 1);
191 procedure Insert_Child
192 (Container
: in out Tree
;
195 Position
: out Cursor
;
196 Count
: Count_Type
:= 1);
198 procedure Prepend_Child
199 (Container
: in out Tree
;
201 New_Item
: Element_Type
;
202 Count
: Count_Type
:= 1);
204 procedure Append_Child
205 (Container
: in out Tree
;
207 New_Item
: Element_Type
;
208 Count
: Count_Type
:= 1);
210 procedure Delete_Children
211 (Container
: in out Tree
;
214 procedure Copy_Subtree
215 (Target
: in out Tree
;
220 procedure Splice_Subtree
221 (Target
: in out Tree
;
224 Source
: in out Tree
;
225 Position
: in out Cursor
);
227 procedure Splice_Subtree
228 (Container
: in out Tree
;
233 procedure Splice_Children
234 (Target
: in out Tree
;
235 Target_Parent
: Cursor
;
237 Source
: in out Tree
;
238 Source_Parent
: Cursor
);
240 procedure Splice_Children
241 (Container
: in out Tree
;
242 Target_Parent
: Cursor
;
244 Source_Parent
: Cursor
);
246 function Parent
(Position
: Cursor
) return Cursor
;
248 function First_Child
(Parent
: Cursor
) return Cursor
;
250 function First_Child_Element
(Parent
: Cursor
) return Element_Type
;
252 function Last_Child
(Parent
: Cursor
) return Cursor
;
254 function Last_Child_Element
(Parent
: Cursor
) return Element_Type
;
256 function Next_Sibling
(Position
: Cursor
) return Cursor
;
258 function Previous_Sibling
(Position
: Cursor
) return Cursor
;
260 procedure Next_Sibling
(Position
: in out Cursor
);
262 procedure Previous_Sibling
(Position
: in out Cursor
);
264 procedure Iterate_Children
266 Process
: not null access procedure (Position
: Cursor
));
268 procedure Reverse_Iterate_Children
270 Process
: not null access procedure (Position
: Cursor
));
274 use Ada
.Finalization
;
276 No_Node
: constant Count_Type
'Base := -1;
277 -- Need to document all global declarations such as this ???
279 -- Following decls also need much more documentation ???
281 type Children_Type
is record
282 First
: Count_Type
'Base;
283 Last
: Count_Type
'Base;
286 type Tree_Node_Type
is record
287 Parent
: Count_Type
'Base;
288 Prev
: Count_Type
'Base;
289 Next
: Count_Type
'Base;
290 Children
: Children_Type
;
293 type Tree_Node_Array
is array (Count_Type
range <>) of Tree_Node_Type
;
294 type Element_Array
is array (Count_Type
range <>) of aliased Element_Type
;
296 type Tree
(Capacity
: Count_Type
) is tagged record
297 Nodes
: Tree_Node_Array
(0 .. Capacity
) := (others => <>);
298 Elements
: Element_Array
(1 .. Capacity
) := (others => <>);
299 Free
: Count_Type
'Base := No_Node
;
302 Count
: Count_Type
:= 0;
306 (Stream
: not null access Root_Stream_Type
'Class;
309 for Tree
'Write use Write
;
312 (Stream
: not null access Root_Stream_Type
'Class;
313 Container
: out Tree
);
315 for Tree
'Read use Read
;
317 type Tree_Access
is access all Tree
;
318 for Tree_Access
'Storage_Size use 0;
320 type Cursor
is record
321 Container
: Tree_Access
;
322 Node
: Count_Type
'Base := No_Node
;
326 (Stream
: not null access Root_Stream_Type
'Class;
327 Position
: out Cursor
);
328 for Cursor
'Read use Read
;
331 (Stream
: not null access Root_Stream_Type
'Class;
333 for Cursor
'Write use Write
;
335 type Reference_Control_Type
is
336 new Controlled
with record
337 Container
: Tree_Access
;
340 overriding
procedure Adjust
(Control
: in out Reference_Control_Type
);
341 pragma Inline
(Adjust
);
343 overriding
procedure Finalize
(Control
: in out Reference_Control_Type
);
344 pragma Inline
(Finalize
);
346 type Constant_Reference_Type
347 (Element
: not null access constant Element_Type
) is
349 Control
: Reference_Control_Type
;
353 (Stream
: not null access Root_Stream_Type
'Class;
354 Item
: Constant_Reference_Type
);
355 for Constant_Reference_Type
'Write use Write
;
358 (Stream
: not null access Root_Stream_Type
'Class;
359 Item
: out Constant_Reference_Type
);
360 for Constant_Reference_Type
'Read use Read
;
363 (Element
: not null access Element_Type
) is
365 Control
: Reference_Control_Type
;
369 (Stream
: not null access Root_Stream_Type
'Class;
370 Item
: Reference_Type
);
371 for Reference_Type
'Write use Write
;
374 (Stream
: not null access Root_Stream_Type
'Class;
375 Item
: out Reference_Type
);
376 for Reference_Type
'Read use Read
;
378 Empty_Tree
: constant Tree
:= (Capacity
=> 0, others => <>);
380 No_Element
: constant Cursor
:= Cursor
'(others => <>);
382 end Ada.Containers.Bounded_Multiway_Trees;