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