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