| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT LIBRARY COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- ADA.CONTAINERS.BOUNDED_MULTIWAY_TREES -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2014-2025, 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 |
|
| 36 |
with Ada.Containers.Helpers; |
| 37 |
private with Ada.Streams; |
| 38 |
private with Ada.Strings.Text_Buffers; |
| 39 |
|
| 40 |
generic |
| 41 |
type Element_Type is private; |
| 42 |
|
| 43 |
with function "=" (Left, Right : Element_Type) return Boolean is <>; |
| 44 |
|
| 45 |
package Ada.Containers.Bounded_Multiway_Trees with |
| 46 |
SPARK_Mode => Off |
| 47 |
is |
| 48 |
pragma Annotate (CodePeer, Skip_Analysis); |
| 49 |
pragma Pure; |
| 50 |
pragma Remote_Types; |
| 51 |
|
| 52 |
type Tree (Capacity : Count_Type) is tagged private |
| 53 |
with Constant_Indexing => Constant_Reference, |
| 54 |
Variable_Indexing => Reference, |
| 55 |
Default_Iterator => Iterate, |
| 56 |
Iterator_Element => Element_Type, |
| 57 |
Preelaborable_Initialization |
| 58 |
=> Element_Type'Preelaborable_Initialization; |
| 59 |
|
| 60 |
type Cursor is private with Preelaborable_Initialization; |
| 61 |
|
| 62 |
Empty_Tree : constant Tree; |
| 63 |
|
| 64 |
No_Element : constant Cursor; |
| 65 |
function Has_Element (Position : Cursor) return Boolean; |
| 66 |
|
| 67 |
package Tree_Iterator_Interfaces is new |
| 68 |
Ada.Iterator_Interfaces (Cursor, Has_Element); |
| 69 |
|
| 70 |
function Equal_Subtree |
| 71 |
(Left_Position : Cursor; |
| 72 |
Right_Position : Cursor) return Boolean; |
| 73 |
|
| 74 |
function "=" (Left, Right : Tree) return Boolean; |
| 75 |
|
| 76 |
function Is_Empty (Container : Tree) return Boolean; |
| 77 |
|
| 78 |
function Node_Count (Container : Tree) return Count_Type; |
| 79 |
|
| 80 |
function Subtree_Node_Count (Position : Cursor) return Count_Type; |
| 81 |
|
| 82 |
function Depth (Position : Cursor) return Count_Type; |
| 83 |
|
| 84 |
function Is_Root (Position : Cursor) return Boolean; |
| 85 |
|
| 86 |
function Is_Leaf (Position : Cursor) return Boolean; |
| 87 |
|
| 88 |
function Root (Container : Tree) return Cursor; |
| 89 |
|
| 90 |
procedure Clear (Container : in out Tree); |
| 91 |
|
| 92 |
function Element (Position : Cursor) return Element_Type; |
| 93 |
|
| 94 |
procedure Replace_Element |
| 95 |
(Container : in out Tree; |
| 96 |
Position : Cursor; |
| 97 |
New_Item : Element_Type); |
| 98 |
|
| 99 |
procedure Query_Element |
| 100 |
(Position : Cursor; |
| 101 |
Process : not null access procedure (Element : Element_Type)); |
| 102 |
|
| 103 |
procedure Update_Element |
| 104 |
(Container : in out Tree; |
| 105 |
Position : Cursor; |
| 106 |
Process : not null access procedure (Element : in out Element_Type)); |
| 107 |
|
| 108 |
type Constant_Reference_Type |
| 109 |
(Element : not null access constant Element_Type) is private |
| 110 |
with Implicit_Dereference => Element; |
| 111 |
|
| 112 |
type Reference_Type |
| 113 |
(Element : not null access Element_Type) is private |
| 114 |
with Implicit_Dereference => Element; |
| 115 |
|
| 116 |
function Constant_Reference |
| 117 |
(Container : aliased Tree; |
| 118 |
Position : Cursor) return Constant_Reference_Type; |
| 119 |
|
| 120 |
function Reference |
| 121 |
(Container : aliased in out Tree; |
| 122 |
Position : Cursor) return Reference_Type; |
| 123 |
|
| 124 |
procedure Assign (Target : in out Tree; Source : Tree); |
| 125 |
|
| 126 |
function Copy (Source : Tree; Capacity : Count_Type := 0) return Tree; |
| 127 |
|
| 128 |
procedure Move (Target : in out Tree; Source : in out Tree); |
| 129 |
|
| 130 |
procedure Delete_Leaf |
| 131 |
(Container : in out Tree; |
| 132 |
Position : in out Cursor); |
| 133 |
|
| 134 |
procedure Delete_Subtree |
| 135 |
(Container : in out Tree; |
| 136 |
Position : in out Cursor); |
| 137 |
|
| 138 |
procedure Swap |
| 139 |
(Container : in out Tree; |
| 140 |
I, J : Cursor); |
| 141 |
|
| 142 |
function Find |
| 143 |
(Container : Tree; |
| 144 |
Item : Element_Type) return Cursor; |
| 145 |
|
| 146 |
function Find_In_Subtree |
| 147 |
(Position : Cursor; |
| 148 |
Item : Element_Type) return Cursor; |
| 149 |
|
| 150 |
function Ancestor_Find |
| 151 |
(Position : Cursor; |
| 152 |
Item : Element_Type) return Cursor; |
| 153 |
|
| 154 |
function Contains |
| 155 |
(Container : Tree; |
| 156 |
Item : Element_Type) return Boolean; |
| 157 |
|
| 158 |
procedure Iterate |
| 159 |
(Container : Tree; |
| 160 |
Process : not null access procedure (Position : Cursor)); |
| 161 |
|
| 162 |
procedure Iterate_Subtree |
| 163 |
(Position : Cursor; |
| 164 |
Process : not null access procedure (Position : Cursor)); |
| 165 |
|
| 166 |
function Iterate (Container : Tree) |
| 167 |
return Tree_Iterator_Interfaces.Forward_Iterator'Class; |
| 168 |
|
| 169 |
function Iterate_Subtree (Position : Cursor) |
| 170 |
return Tree_Iterator_Interfaces.Forward_Iterator'Class; |
| 171 |
|
| 172 |
function Iterate_Children |
| 173 |
(Container : Tree; |
| 174 |
Parent : Cursor) |
| 175 |
return Tree_Iterator_Interfaces.Reversible_Iterator'Class; |
| 176 |
|
| 177 |
function Child_Count (Parent : Cursor) return Count_Type; |
| 178 |
|
| 179 |
function Child_Depth (Parent, Child : Cursor) return Count_Type; |
| 180 |
|
| 181 |
procedure Insert_Child |
| 182 |
(Container : in out Tree; |
| 183 |
Parent : Cursor; |
| 184 |
Before : Cursor; |
| 185 |
New_Item : Element_Type; |
| 186 |
Count : Count_Type := 1); |
| 187 |
|
| 188 |
procedure Insert_Child |
| 189 |
(Container : in out Tree; |
| 190 |
Parent : Cursor; |
| 191 |
Before : Cursor; |
| 192 |
New_Item : Element_Type; |
| 193 |
Position : out Cursor; |
| 194 |
Count : Count_Type := 1); |
| 195 |
|
| 196 |
procedure Insert_Child |
| 197 |
(Container : in out Tree; |
| 198 |
Parent : Cursor; |
| 199 |
Before : Cursor; |
| 200 |
Position : out Cursor; |
| 201 |
Count : Count_Type := 1); |
| 202 |
|
| 203 |
procedure Prepend_Child |
| 204 |
(Container : in out Tree; |
| 205 |
Parent : Cursor; |
| 206 |
New_Item : Element_Type; |
| 207 |
Count : Count_Type := 1); |
| 208 |
|
| 209 |
procedure Append_Child |
| 210 |
(Container : in out Tree; |
| 211 |
Parent : Cursor; |
| 212 |
New_Item : Element_Type; |
| 213 |
Count : Count_Type := 1); |
| 214 |
|
| 215 |
procedure Delete_Children |
| 216 |
(Container : in out Tree; |
| 217 |
Parent : Cursor); |
| 218 |
|
| 219 |
procedure Copy_Subtree |
| 220 |
(Target : in out Tree; |
| 221 |
Parent : Cursor; |
| 222 |
Before : Cursor; |
| 223 |
Source : Cursor); |
| 224 |
|
| 225 |
procedure Splice_Subtree |
| 226 |
(Target : in out Tree; |
| 227 |
Parent : Cursor; |
| 228 |
Before : Cursor; |
| 229 |
Source : in out Tree; |
| 230 |
Position : in out Cursor); |
| 231 |
|
| 232 |
procedure Splice_Subtree |
| 233 |
(Container : in out Tree; |
| 234 |
Parent : Cursor; |
| 235 |
Before : Cursor; |
| 236 |
Position : Cursor); |
| 237 |
|
| 238 |
procedure Splice_Children |
| 239 |
(Target : in out Tree; |
| 240 |
Target_Parent : Cursor; |
| 241 |
Before : Cursor; |
| 242 |
Source : in out Tree; |
| 243 |
Source_Parent : Cursor); |
| 244 |
|
| 245 |
procedure Splice_Children |
| 246 |
(Container : in out Tree; |
| 247 |
Target_Parent : Cursor; |
| 248 |
Before : Cursor; |
| 249 |
Source_Parent : Cursor); |
| 250 |
|
| 251 |
function Parent (Position : Cursor) return Cursor; |
| 252 |
|
| 253 |
function First_Child (Parent : Cursor) return Cursor; |
| 254 |
|
| 255 |
function First_Child_Element (Parent : Cursor) return Element_Type; |
| 256 |
|
| 257 |
function Last_Child (Parent : Cursor) return Cursor; |
| 258 |
|
| 259 |
function Last_Child_Element (Parent : Cursor) return Element_Type; |
| 260 |
|
| 261 |
function Next_Sibling (Position : Cursor) return Cursor; |
| 262 |
|
| 263 |
function Previous_Sibling (Position : Cursor) return Cursor; |
| 264 |
|
| 265 |
procedure Next_Sibling (Position : in out Cursor); |
| 266 |
|
| 267 |
procedure Previous_Sibling (Position : in out Cursor); |
| 268 |
|
| 269 |
procedure Iterate_Children |
| 270 |
(Parent : Cursor; |
| 271 |
Process : not null access procedure (Position : Cursor)); |
| 272 |
|
| 273 |
procedure Reverse_Iterate_Children |
| 274 |
(Parent : Cursor; |
| 275 |
Process : not null access procedure (Position : Cursor)); |
| 276 |
|
| 277 |
private |
| 278 |
|
| 279 |
use Ada.Containers.Helpers; |
| 280 |
package Implementation is new Generic_Implementation; |
| 281 |
use Implementation; |
| 282 |
|
| 283 |
use Ada.Streams; |
| 284 |
|
| 285 |
No_Node : constant Count_Type'Base := -1; |
| 286 |
-- Need to document all global declarations such as this ??? |
| 287 |
|
| 288 |
-- Following decls also need much more documentation ??? |
| 289 |
|
| 290 |
type Children_Type is record |
| 291 |
First : Count_Type'Base; |
| 292 |
Last : Count_Type'Base; |
| 293 |
end record; |
| 294 |
|
| 295 |
type Tree_Node_Type is record |
| 296 |
Parent : Count_Type'Base; |
| 297 |
Prev : Count_Type'Base; |
| 298 |
Next : Count_Type'Base; |
| 299 |
Children : Children_Type; |
| 300 |
end record; |
| 301 |
|
| 302 |
type Tree_Node_Array is array (Count_Type range <>) of Tree_Node_Type; |
| 303 |
type Element_Array is array (Count_Type range <>) of aliased Element_Type; |
| 304 |
|
| 305 |
type Tree (Capacity : Count_Type) is tagged record |
| 306 |
Nodes : Tree_Node_Array (0 .. Capacity); |
| 307 |
Elements : Element_Array (1 .. Capacity); |
| 308 |
Free : Count_Type'Base := No_Node; |
| 309 |
TC : aliased Tamper_Counts; |
| 310 |
Count : Count_Type := 0; |
| 311 |
end record with Put_Image => Put_Image; |
| 312 |
|
| 313 |
procedure Put_Image |
| 314 |
(S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Tree); |
| 315 |
|
| 316 |
procedure Write |
| 317 |
(Stream : not null access Root_Stream_Type'Class; |
| 318 |
Container : Tree); |
| 319 |
|
| 320 |
for Tree'Write use Write; |
| 321 |
|
| 322 |
procedure Read |
| 323 |
(Stream : not null access Root_Stream_Type'Class; |
| 324 |
Container : out Tree); |
| 325 |
|
| 326 |
for Tree'Read use Read; |
| 327 |
|
| 328 |
type Tree_Access is access all Tree; |
| 329 |
for Tree_Access'Storage_Size use 0; |
| 330 |
|
| 331 |
type Cursor is record |
| 332 |
Container : Tree_Access; |
| 333 |
Node : Count_Type'Base := No_Node; |
| 334 |
end record; |
| 335 |
|
| 336 |
procedure Read |
| 337 |
(Stream : not null access Root_Stream_Type'Class; |
| 338 |
Position : out Cursor); |
| 339 |
for Cursor'Read use Read; |
| 340 |
|
| 341 |
procedure Write |
| 342 |
(Stream : not null access Root_Stream_Type'Class; |
| 343 |
Position : Cursor); |
| 344 |
for Cursor'Write use Write; |
| 345 |
|
| 346 |
subtype Reference_Control_Type is Implementation.Reference_Control_Type; |
| 347 |
-- It is necessary to rename this here, so that the compiler can find it |
| 348 |
|
| 349 |
type Constant_Reference_Type |
| 350 |
(Element : not null access constant Element_Type) is |
| 351 |
record |
| 352 |
Control : Reference_Control_Type := |
| 353 |
raise Program_Error with "uninitialized reference"; |
| 354 |
-- The RM says, "The default initialization of an object of |
| 355 |
-- type Constant_Reference_Type or Reference_Type propagates |
| 356 |
-- Program_Error." |
| 357 |
end record; |
| 358 |
|
| 359 |
procedure Write |
| 360 |
(Stream : not null access Root_Stream_Type'Class; |
| 361 |
Item : Constant_Reference_Type); |
| 362 |
for Constant_Reference_Type'Write use Write; |
| 363 |
|
| 364 |
procedure Read |
| 365 |
(Stream : not null access Root_Stream_Type'Class; |
| 366 |
Item : out Constant_Reference_Type); |
| 367 |
for Constant_Reference_Type'Read use Read; |
| 368 |
|
| 369 |
type Reference_Type |
| 370 |
(Element : not null access Element_Type) is |
| 371 |
record |
| 372 |
Control : Reference_Control_Type := |
| 373 |
raise Program_Error with "uninitialized reference"; |
| 374 |
-- The RM says, "The default initialization of an object of |
| 375 |
-- type Constant_Reference_Type or Reference_Type propagates |
| 376 |
-- Program_Error." |
| 377 |
end record; |
| 378 |
|
| 379 |
procedure Write |
| 380 |
(Stream : not null access Root_Stream_Type'Class; |
| 381 |
Item : Reference_Type); |
| 382 |
for Reference_Type'Write use Write; |
| 383 |
|
| 384 |
procedure Read |
| 385 |
(Stream : not null access Root_Stream_Type'Class; |
| 386 |
Item : out Reference_Type); |
| 387 |
for Reference_Type'Read use Read; |
| 388 |
|
| 389 |
-- See Ada.Containers.Vectors for documentation on the following |
| 390 |
|
| 391 |
function Pseudo_Reference |
| 392 |
(Container : aliased Tree'Class) return Reference_Control_Type; |
| 393 |
pragma Inline (Pseudo_Reference); |
| 394 |
-- Creates an object of type Reference_Control_Type pointing to the |
| 395 |
-- container, and increments the Lock. Finalization of this object will |
| 396 |
-- decrement the Lock. |
| 397 |
|
| 398 |
type Element_Access is access all Element_Type with |
| 399 |
Storage_Size => 0; |
| 400 |
|
| 401 |
function Get_Element_Access |
| 402 |
(Position : Cursor) return not null Element_Access; |
| 403 |
-- Returns a pointer to the element designated by Position. |
| 404 |
|
| 405 |
Empty_Tree : constant Tree := (Capacity => 0, others => <>); |
| 406 |
|
| 407 |
No_Element : constant Cursor := Cursor'(others => <>); |
| 408 |
|
| 409 |
end Ada.Containers.Bounded_Multiway_Trees; |