1------------------------------------------------------------------------------ 2-- -- 3-- GNAT LIBRARY COMPONENTS -- 4-- -- 5-- ADA.CONTAINERS.INDEFINITE_ORDERED_MAPS -- 6-- -- 7-- S p e c -- 8-- -- 9-- Copyright (C) 2004-2021, 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 34with Ada.Iterator_Interfaces; 35 36private with Ada.Containers.Red_Black_Trees; 37private with Ada.Finalization; 38private with Ada.Streams; 39private with Ada.Strings.Text_Buffers; 40 41generic 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 48package Ada.Containers.Indefinite_Ordered_Maps with 49 SPARK_Mode => Off 50is 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 58 with 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 65 pragma Preelaborable_Initialization (Map); 66 67 type Cursor is private; 68 pragma Preelaborable_Initialization (Cursor); 69 70 Empty_Map : constant Map; 71 72 function Empty return Map; 73 pragma Ada_2022 (Empty); 74 75 No_Element : constant Cursor; 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 procedure (Key : Key_Type; 101 Element : Element_Type)); 102 103 procedure Update_Element 104 (Container : in out Map; 105 Position : Cursor; 106 Process : not null access procedure (Key : Key_Type; 107 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 New_Item : Element_Type); 155 156 procedure Include 157 (Container : in out Map; 158 Key : Key_Type; 159 New_Item : Element_Type); 160 161 procedure Replace 162 (Container : in out Map; 163 Key : Key_Type; 164 New_Item : Element_Type); 165 166 procedure Exclude (Container : in out Map; Key : Key_Type); 167 168 procedure Delete (Container : in out Map; Key : Key_Type); 169 170 procedure Delete (Container : in out Map; Position : in out Cursor); 171 172 procedure Delete_First (Container : in out Map); 173 174 procedure Delete_Last (Container : in out Map); 175 176 function First (Container : Map) return Cursor; 177 178 function First_Element (Container : Map) return Element_Type; 179 180 function First_Key (Container : Map) return Key_Type; 181 182 function Last (Container : Map) return Cursor; 183 184 function Last_Element (Container : Map) return Element_Type; 185 186 function Last_Key (Container : Map) return Key_Type; 187 188 function Next (Position : Cursor) return Cursor; 189 190 procedure Next (Position : in out Cursor); 191 192 function Previous (Position : Cursor) return Cursor; 193 194 procedure Previous (Position : in out Cursor); 195 196 function Find (Container : Map; Key : Key_Type) return Cursor; 197 198 function Element (Container : Map; Key : Key_Type) return Element_Type; 199 200 function Floor (Container : Map; Key : Key_Type) return Cursor; 201 202 function Ceiling (Container : Map; Key : Key_Type) return Cursor; 203 204 function Contains (Container : Map; Key : Key_Type) return Boolean; 205 206 function "<" (Left, Right : Cursor) return Boolean; 207 208 function ">" (Left, Right : Cursor) return Boolean; 209 210 function "<" (Left : Cursor; Right : Key_Type) return Boolean; 211 212 function ">" (Left : Cursor; Right : Key_Type) return Boolean; 213 214 function "<" (Left : Key_Type; Right : Cursor) return Boolean; 215 216 function ">" (Left : Key_Type; Right : Cursor) return Boolean; 217 218 procedure Iterate 219 (Container : Map; 220 Process : not null access procedure (Position : Cursor)); 221 222 procedure Reverse_Iterate 223 (Container : Map; 224 Process : not null access procedure (Position : Cursor)); 225 226 -- The map container supports iteration in both the forward and reverse 227 -- directions, hence these constructor functions return an object that 228 -- supports the Reversible_Iterator interface. 229 230 function Iterate 231 (Container : Map) 232 return Map_Iterator_Interfaces.Reversible_Iterator'Class; 233 234 function Iterate 235 (Container : Map; 236 Start : Cursor) 237 return Map_Iterator_Interfaces.Reversible_Iterator'Class; 238 239private 240 241 pragma Inline (Next); 242 pragma Inline (Previous); 243 244 type Node_Type; 245 type Node_Access is access Node_Type; 246 247 type Key_Access is access Key_Type; 248 type Element_Access is access all Element_Type; 249 250 type Node_Type is limited record 251 Parent : Node_Access; 252 Left : Node_Access; 253 Right : Node_Access; 254 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red; 255 Key : Key_Access; 256 Element : Element_Access; 257 end record; 258 259 package Tree_Types is new Red_Black_Trees.Generic_Tree_Types 260 (Node_Type, 261 Node_Access); 262 263 type Map is new Ada.Finalization.Controlled with record 264 Tree : Tree_Types.Tree_Type; 265 end record with Put_Image => Put_Image; 266 267 procedure Put_Image 268 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Map); 269 270 overriding procedure Adjust (Container : in out Map); 271 272 overriding procedure Finalize (Container : in out Map) renames Clear; 273 274 use Red_Black_Trees; 275 use Tree_Types, Tree_Types.Implementation; 276 use Ada.Finalization; 277 use Ada.Streams; 278 279 procedure Write 280 (Stream : not null access Root_Stream_Type'Class; 281 Container : Map); 282 283 for Map'Write use Write; 284 285 procedure Read 286 (Stream : not null access Root_Stream_Type'Class; 287 Container : out Map); 288 289 for Map'Read use Read; 290 291 type Map_Access is access all Map; 292 for Map_Access'Storage_Size use 0; 293 294 type Cursor is record 295 Container : Map_Access; 296 Node : Node_Access; 297 end record; 298 299 procedure Write 300 (Stream : not null access Root_Stream_Type'Class; 301 Item : Cursor); 302 303 for Cursor'Write use Write; 304 305 procedure Read 306 (Stream : not null access Root_Stream_Type'Class; 307 Item : out Cursor); 308 309 for Cursor'Read use Read; 310 311 subtype Reference_Control_Type is Implementation.Reference_Control_Type; 312 -- It is necessary to rename this here, so that the compiler can find it 313 314 type Constant_Reference_Type 315 (Element : not null access constant Element_Type) is 316 record 317 Control : Reference_Control_Type := 318 raise Program_Error with "uninitialized reference"; 319 -- The RM says, "The default initialization of an object of 320 -- type Constant_Reference_Type or Reference_Type propagates 321 -- Program_Error." 322 end record; 323 324 procedure Read 325 (Stream : not null access Root_Stream_Type'Class; 326 Item : out Constant_Reference_Type); 327 328 for Constant_Reference_Type'Read use Read; 329 330 procedure Write 331 (Stream : not null access Root_Stream_Type'Class; 332 Item : Constant_Reference_Type); 333 334 for Constant_Reference_Type'Write use Write; 335 336 type Reference_Type 337 (Element : not null access Element_Type) is 338 record 339 Control : Reference_Control_Type := 340 raise Program_Error with "uninitialized reference"; 341 -- The RM says, "The default initialization of an object of 342 -- type Constant_Reference_Type or Reference_Type propagates 343 -- Program_Error." 344 end record; 345 346 procedure Read 347 (Stream : not null access Root_Stream_Type'Class; 348 Item : out Reference_Type); 349 350 for Reference_Type'Read use Read; 351 352 procedure Write 353 (Stream : not null access Root_Stream_Type'Class; 354 Item : Reference_Type); 355 356 for Reference_Type'Write use Write; 357 358 -- Three operations are used to optimize in the expansion of "for ... of" 359 -- loops: the Next(Cursor) procedure in the visible part, and the following 360 -- Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for 361 -- details. 362 363 function Pseudo_Reference 364 (Container : aliased Map'Class) return Reference_Control_Type; 365 pragma Inline (Pseudo_Reference); 366 -- Creates an object of type Reference_Control_Type pointing to the 367 -- container, and increments the Lock. Finalization of this object will 368 -- decrement the Lock. 369 370 function Get_Element_Access 371 (Position : Cursor) return not null Element_Access; 372 -- Returns a pointer to the element designated by Position. 373 374 Empty_Map : constant Map := (Controlled with others => <>); 375 function Empty return Map is (Empty_Map); 376 377 No_Element : constant Cursor := Cursor'(null, null); 378 379 type Iterator is new Limited_Controlled and 380 Map_Iterator_Interfaces.Reversible_Iterator with 381 record 382 Container : Map_Access; 383 Node : Node_Access; 384 end record 385 with Disable_Controlled => not T_Check; 386 387 overriding procedure Finalize (Object : in out Iterator); 388 389 overriding function First (Object : Iterator) return Cursor; 390 overriding function Last (Object : Iterator) return Cursor; 391 392 overriding function Next 393 (Object : Iterator; 394 Position : Cursor) return Cursor; 395 396 overriding function Previous 397 (Object : Iterator; 398 Position : Cursor) return Cursor; 399 400end Ada.Containers.Indefinite_Ordered_Maps; 401