2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / a-ciorma.ads
blob2882a084bd2b8e75bbecc61056aee23aff11d42a
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 -- 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.Indefinite_Ordered_Maps is
48 pragma Preelaborate;
49 pragma Remote_Types;
51 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
53 type Map is tagged private
54 with Constant_Indexing => Constant_Reference,
55 Variable_Indexing => Reference,
56 Default_Iterator => Iterate,
57 Iterator_Element => Element_Type;
59 pragma Preelaborable_Initialization (Map);
61 type Cursor is private;
62 pragma Preelaborable_Initialization (Cursor);
64 Empty_Map : constant Map;
66 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 procedure (Key : Key_Type;
92 Element : Element_Type));
94 procedure Update_Element
95 (Container : in out Map;
96 Position : Cursor;
97 Process : not null access procedure (Key : Key_Type;
98 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 New_Item : Element_Type);
147 procedure Include
148 (Container : in out Map;
149 Key : Key_Type;
150 New_Item : Element_Type);
152 procedure Replace
153 (Container : in out Map;
154 Key : Key_Type;
155 New_Item : Element_Type);
157 procedure Exclude (Container : in out Map; Key : Key_Type);
159 procedure Delete (Container : in out Map; Key : Key_Type);
161 procedure Delete (Container : in out Map; Position : in out Cursor);
163 procedure Delete_First (Container : in out Map);
165 procedure Delete_Last (Container : in out Map);
167 function First (Container : Map) return Cursor;
169 function First_Element (Container : Map) return Element_Type;
171 function First_Key (Container : Map) return Key_Type;
173 function Last (Container : Map) return Cursor;
175 function Last_Element (Container : Map) return Element_Type;
177 function Last_Key (Container : Map) return Key_Type;
179 function Next (Position : Cursor) return Cursor;
181 procedure Next (Position : in out Cursor);
183 function Previous (Position : Cursor) return Cursor;
185 procedure Previous (Position : in out Cursor);
187 function Find (Container : Map; Key : Key_Type) return Cursor;
189 function Element (Container : Map; Key : Key_Type) return Element_Type;
191 function Floor (Container : Map; Key : Key_Type) return Cursor;
193 function Ceiling (Container : Map; Key : Key_Type) return Cursor;
195 function Contains (Container : Map; Key : Key_Type) return Boolean;
197 function "<" (Left, Right : Cursor) return Boolean;
199 function ">" (Left, Right : Cursor) return Boolean;
201 function "<" (Left : Cursor; Right : Key_Type) return Boolean;
203 function ">" (Left : Cursor; Right : Key_Type) return Boolean;
205 function "<" (Left : Key_Type; Right : Cursor) return Boolean;
207 function ">" (Left : Key_Type; Right : Cursor) return Boolean;
209 procedure Iterate
210 (Container : Map;
211 Process : not null access procedure (Position : Cursor));
213 procedure Reverse_Iterate
214 (Container : Map;
215 Process : not null access procedure (Position : Cursor));
217 -- The map container supports iteration in both the forward and reverse
218 -- directions, hence these constructor functions return an object that
219 -- supports the Reversible_Iterator interface.
221 function Iterate
222 (Container : Map)
223 return Map_Iterator_Interfaces.Reversible_Iterator'Class;
225 function Iterate
226 (Container : Map;
227 Start : Cursor)
228 return Map_Iterator_Interfaces.Reversible_Iterator'Class;
230 private
232 pragma Inline (Next);
233 pragma Inline (Previous);
235 type Node_Type;
236 type Node_Access is access Node_Type;
238 type Key_Access is access Key_Type;
239 type Element_Access is access Element_Type;
241 type Node_Type is limited record
242 Parent : Node_Access;
243 Left : Node_Access;
244 Right : Node_Access;
245 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
246 Key : Key_Access;
247 Element : Element_Access;
248 end record;
250 package Tree_Types is new Red_Black_Trees.Generic_Tree_Types
251 (Node_Type,
252 Node_Access);
254 type Map is new Ada.Finalization.Controlled with record
255 Tree : Tree_Types.Tree_Type;
256 end record;
258 overriding procedure Adjust (Container : in out Map);
260 overriding procedure Finalize (Container : in out Map) renames Clear;
262 use Red_Black_Trees;
263 use Tree_Types;
264 use Ada.Finalization;
265 use Ada.Streams;
267 procedure Write
268 (Stream : not null access Root_Stream_Type'Class;
269 Container : Map);
271 for Map'Write use Write;
273 procedure Read
274 (Stream : not null access Root_Stream_Type'Class;
275 Container : out Map);
277 for Map'Read use Read;
279 type Map_Access is access all Map;
280 for Map_Access'Storage_Size use 0;
282 type Cursor is record
283 Container : Map_Access;
284 Node : Node_Access;
285 end record;
287 procedure Write
288 (Stream : not null access Root_Stream_Type'Class;
289 Item : Cursor);
291 for Cursor'Write use Write;
293 procedure Read
294 (Stream : not null access Root_Stream_Type'Class;
295 Item : out Cursor);
297 for Cursor'Read use Read;
299 type Reference_Control_Type is
300 new Controlled with record
301 Container : Map_Access;
302 end record;
304 overriding procedure Adjust (Control : in out Reference_Control_Type);
305 pragma Inline (Adjust);
307 overriding procedure Finalize (Control : in out Reference_Control_Type);
308 pragma Inline (Finalize);
310 type Constant_Reference_Type
311 (Element : not null access constant Element_Type) is
312 record
313 Control : Reference_Control_Type :=
314 raise Program_Error with "uninitialized reference";
315 -- The RM says, "The default initialization of an object of
316 -- type Constant_Reference_Type or Reference_Type propagates
317 -- Program_Error."
318 end record;
320 procedure Read
321 (Stream : not null access Root_Stream_Type'Class;
322 Item : out Constant_Reference_Type);
324 for Constant_Reference_Type'Read use Read;
326 procedure Write
327 (Stream : not null access Root_Stream_Type'Class;
328 Item : Constant_Reference_Type);
330 for Constant_Reference_Type'Write use Write;
332 type Reference_Type
333 (Element : not null access Element_Type) is
334 record
335 Control : Reference_Control_Type :=
336 raise Program_Error with "uninitialized reference";
337 -- The RM says, "The default initialization of an object of
338 -- type Constant_Reference_Type or Reference_Type propagates
339 -- Program_Error."
340 end record;
342 procedure Read
343 (Stream : not null access Root_Stream_Type'Class;
344 Item : out Reference_Type);
346 for Reference_Type'Read use Read;
348 procedure Write
349 (Stream : not null access Root_Stream_Type'Class;
350 Item : Reference_Type);
352 for Reference_Type'Write use Write;
354 Empty_Map : constant Map :=
355 (Controlled with Tree => (First => null,
356 Last => null,
357 Root => null,
358 Length => 0,
359 Busy => 0,
360 Lock => 0));
362 No_Element : constant Cursor := Cursor'(null, null);
364 type Iterator is new Limited_Controlled and
365 Map_Iterator_Interfaces.Reversible_Iterator with
366 record
367 Container : Map_Access;
368 Node : Node_Access;
369 end record;
371 overriding procedure Finalize (Object : in out Iterator);
373 overriding function First (Object : Iterator) return Cursor;
374 overriding function Last (Object : Iterator) return Cursor;
376 overriding function Next
377 (Object : Iterator;
378 Position : Cursor) return Cursor;
380 overriding function Previous
381 (Object : Iterator;
382 Position : Cursor) return Cursor;
384 end Ada.Containers.Indefinite_Ordered_Maps;