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