[gcc]
[official-gcc.git] / gcc / ada / a-coorma.ads
blob3034a2ec8501d8b8a8f022b87f34f37f84db9c4a
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 ------------------------------------------------------------------------------
34 with Ada.Iterator_Interfaces;
36 private with Ada.Containers.Red_Black_Trees;
37 private with Ada.Finalization;
38 private with Ada.Streams;
40 generic
41 type Key_Type is private;
42 type Element_Type is private;
44 with function "<" (Left, Right : Key_Type) return Boolean is <>;
45 with function "=" (Left, Right : Element_Type) return Boolean is <>;
47 package Ada.Containers.Ordered_Maps is
48 pragma Annotate (CodePeer, Skip_Analysis);
49 pragma Preelaborate;
50 pragma Remote_Types;
52 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
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;
60 type Cursor is private;
61 pragma Preelaborable_Initialization (Cursor);
63 Empty_Map : constant Map;
65 No_Element : constant Cursor;
67 function Has_Element (Position : Cursor) return Boolean;
69 package Map_Iterator_Interfaces is new
70 Ada.Iterator_Interfaces (Cursor, Has_Element);
72 function "=" (Left, Right : Map) return Boolean;
74 function Length (Container : Map) return Count_Type;
76 function Is_Empty (Container : Map) return Boolean;
78 procedure Clear (Container : in out Map);
80 function Key (Position : Cursor) return Key_Type;
82 function Element (Position : Cursor) return Element_Type;
84 procedure Replace_Element
85 (Container : in out Map;
86 Position : Cursor;
87 New_Item : Element_Type);
89 procedure Query_Element
90 (Position : Cursor;
91 Process : not null access
92 procedure (Key : Key_Type; Element : Element_Type));
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));
100 type Constant_Reference_Type
101 (Element : not null access constant Element_Type) is private
102 with
103 Implicit_Dereference => Element;
105 type Reference_Type (Element : not null access Element_Type) is private
106 with
107 Implicit_Dereference => Element;
109 function Constant_Reference
110 (Container : aliased Map;
111 Position : Cursor) return Constant_Reference_Type;
112 pragma Inline (Constant_Reference);
114 function Reference
115 (Container : aliased in out Map;
116 Position : Cursor) return Reference_Type;
117 pragma Inline (Reference);
119 function Constant_Reference
120 (Container : aliased Map;
121 Key : Key_Type) return Constant_Reference_Type;
122 pragma Inline (Constant_Reference);
124 function Reference
125 (Container : aliased in out Map;
126 Key : Key_Type) return Reference_Type;
127 pragma Inline (Reference);
129 procedure Assign (Target : in out Map; Source : Map);
131 function Copy (Source : Map) return Map;
133 procedure Move (Target : in out Map; Source : in out Map);
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);
142 procedure Insert
143 (Container : in out Map;
144 Key : Key_Type;
145 Position : out Cursor;
146 Inserted : out Boolean);
148 procedure Insert
149 (Container : in out Map;
150 Key : Key_Type;
151 New_Item : Element_Type);
153 procedure Include
154 (Container : in out Map;
155 Key : Key_Type;
156 New_Item : Element_Type);
158 procedure Replace
159 (Container : in out Map;
160 Key : Key_Type;
161 New_Item : Element_Type);
163 procedure Exclude (Container : in out Map; Key : Key_Type);
165 procedure Delete (Container : in out Map; Key : Key_Type);
167 procedure Delete (Container : in out Map; Position : in out Cursor);
169 procedure Delete_First (Container : in out Map);
171 procedure Delete_Last (Container : in out Map);
173 function First (Container : Map) return Cursor;
175 function First_Element (Container : Map) return Element_Type;
177 function First_Key (Container : Map) return Key_Type;
179 function Last (Container : Map) return Cursor;
181 function Last_Element (Container : Map) return Element_Type;
183 function Last_Key (Container : Map) return Key_Type;
185 function Next (Position : Cursor) return Cursor;
187 procedure Next (Position : in out Cursor);
189 function Previous (Position : Cursor) return Cursor;
191 procedure Previous (Position : in out Cursor);
193 function Find (Container : Map; Key : Key_Type) return Cursor;
195 function Element (Container : Map; Key : Key_Type) return Element_Type;
197 function Floor (Container : Map; Key : Key_Type) return Cursor;
199 function Ceiling (Container : Map; Key : Key_Type) return Cursor;
201 function Contains (Container : Map; Key : Key_Type) return Boolean;
203 function "<" (Left, Right : Cursor) return Boolean;
205 function ">" (Left, Right : Cursor) return Boolean;
207 function "<" (Left : Cursor; Right : Key_Type) return Boolean;
209 function ">" (Left : Cursor; Right : Key_Type) return Boolean;
211 function "<" (Left : Key_Type; Right : Cursor) return Boolean;
213 function ">" (Left : Key_Type; Right : Cursor) return Boolean;
215 procedure Iterate
216 (Container : Map;
217 Process : not null access procedure (Position : Cursor));
219 procedure Reverse_Iterate
220 (Container : Map;
221 Process : not null access procedure (Position : Cursor));
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.
227 function Iterate
228 (Container : Map)
229 return Map_Iterator_Interfaces.Reversible_Iterator'class;
231 function Iterate
232 (Container : Map;
233 Start : Cursor)
234 return Map_Iterator_Interfaces.Reversible_Iterator'class;
236 private
238 pragma Inline (Next);
239 pragma Inline (Previous);
241 type Node_Type;
242 type Node_Access is access Node_Type;
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;
253 package Tree_Types is
254 new Red_Black_Trees.Generic_Tree_Types (Node_Type, Node_Access);
256 type Map is new Ada.Finalization.Controlled with record
257 Tree : Tree_Types.Tree_Type;
258 end record;
260 overriding procedure Adjust (Container : in out Map);
262 overriding procedure Finalize (Container : in out Map) renames Clear;
264 use Red_Black_Trees;
265 use Tree_Types, Tree_Types.Implementation;
266 use Ada.Finalization;
267 use Ada.Streams;
269 procedure Write
270 (Stream : not null access Root_Stream_Type'Class;
271 Container : Map);
273 for Map'Write use Write;
275 procedure Read
276 (Stream : not null access Root_Stream_Type'Class;
277 Container : out Map);
279 for Map'Read use Read;
281 type Map_Access is access all Map;
282 for Map_Access'Storage_Size use 0;
284 type Cursor is record
285 Container : Map_Access;
286 Node : Node_Access;
287 end record;
289 procedure Write
290 (Stream : not null access Root_Stream_Type'Class;
291 Item : Cursor);
293 for Cursor'Write use Write;
295 procedure Read
296 (Stream : not null access Root_Stream_Type'Class;
297 Item : out Cursor);
299 for Cursor'Read use Read;
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
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;
314 procedure Read
315 (Stream : not null access Root_Stream_Type'Class;
316 Item : out Constant_Reference_Type);
318 for Constant_Reference_Type'Read use Read;
320 procedure Write
321 (Stream : not null access Root_Stream_Type'Class;
322 Item : Constant_Reference_Type);
324 for Constant_Reference_Type'Write use Write;
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;
336 procedure Read
337 (Stream : not null access Root_Stream_Type'Class;
338 Item : out Reference_Type);
340 for Reference_Type'Read use Read;
342 procedure Write
343 (Stream : not null access Root_Stream_Type'Class;
344 Item : Reference_Type);
346 for Reference_Type'Write use Write;
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.
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.
360 type Element_Access is access all Element_Type with
361 Storage_Size => 0;
363 function Get_Element_Access
364 (Position : Cursor) return not null Element_Access;
365 -- Returns a pointer to the element designated by Position.
367 Empty_Map : constant Map := (Controlled with others => <>);
369 No_Element : constant Cursor := Cursor'(null, null);
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;
379 overriding procedure Finalize (Object : in out Iterator);
381 overriding function First (Object : Iterator) return Cursor;
382 overriding function Last (Object : Iterator) return Cursor;
384 overriding function Next
385 (Object : Iterator;
386 Position : Cursor) return Cursor;
388 overriding function Previous
389 (Object : Iterator;
390 Position : Cursor) return Cursor;
392 end Ada.Containers.Ordered_Maps;