File : a-ciorma.ads


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