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