2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / a-coorse.ads
blobf574f3c92ca2d8cba95c2a1350ad236e35076656
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 _ S E T 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 Element_Type is private;
43 with function "<" (Left, Right : Element_Type) return Boolean is <>;
44 with function "=" (Left, Right : Element_Type) return Boolean is <>;
46 package Ada.Containers.Ordered_Sets is
47 pragma Preelaborate;
48 pragma Remote_Types;
50 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
52 type Set is tagged private
53 with Constant_Indexing => Constant_Reference,
54 Default_Iterator => Iterate,
55 Iterator_Element => Element_Type;
57 pragma Preelaborable_Initialization (Set);
59 type Cursor is private;
60 pragma Preelaborable_Initialization (Cursor);
62 function Has_Element (Position : Cursor) return Boolean;
64 Empty_Set : constant Set;
66 No_Element : constant Cursor;
68 package Set_Iterator_Interfaces is new
69 Ada.Iterator_Interfaces (Cursor, Has_Element);
71 function "=" (Left, Right : Set) return Boolean;
73 function Equivalent_Sets (Left, Right : Set) return Boolean;
75 function To_Set (New_Item : Element_Type) return Set;
77 function Length (Container : Set) return Count_Type;
79 function Is_Empty (Container : Set) return Boolean;
81 procedure Clear (Container : in out Set);
83 function Element (Position : Cursor) return Element_Type;
85 procedure Replace_Element
86 (Container : in out Set;
87 Position : Cursor;
88 New_Item : Element_Type);
90 procedure Query_Element
91 (Position : Cursor;
92 Process : not null access procedure (Element : Element_Type));
94 type Constant_Reference_Type
95 (Element : not null access constant Element_Type) is
96 private
97 with
98 Implicit_Dereference => Element;
100 function Constant_Reference
101 (Container : aliased Set;
102 Position : Cursor) return Constant_Reference_Type;
103 pragma Inline (Constant_Reference);
105 procedure Assign (Target : in out Set; Source : Set);
107 function Copy (Source : Set) return Set;
109 procedure Move (Target : in out Set; Source : in out Set);
111 procedure Insert
112 (Container : in out Set;
113 New_Item : Element_Type;
114 Position : out Cursor;
115 Inserted : out Boolean);
117 procedure Insert
118 (Container : in out Set;
119 New_Item : Element_Type);
121 procedure Include
122 (Container : in out Set;
123 New_Item : Element_Type);
125 procedure Replace
126 (Container : in out Set;
127 New_Item : Element_Type);
129 procedure Exclude
130 (Container : in out Set;
131 Item : Element_Type);
133 procedure Delete
134 (Container : in out Set;
135 Item : Element_Type);
137 procedure Delete
138 (Container : in out Set;
139 Position : in out Cursor);
141 procedure Delete_First (Container : in out Set);
143 procedure Delete_Last (Container : in out Set);
145 procedure Union (Target : in out Set; Source : Set);
147 function Union (Left, Right : Set) return Set;
149 function "or" (Left, Right : Set) return Set renames Union;
151 procedure Intersection (Target : in out Set; Source : Set);
153 function Intersection (Left, Right : Set) return Set;
155 function "and" (Left, Right : Set) return Set renames Intersection;
157 procedure Difference (Target : in out Set; Source : Set);
159 function Difference (Left, Right : Set) return Set;
161 function "-" (Left, Right : Set) return Set renames Difference;
163 procedure Symmetric_Difference (Target : in out Set; Source : Set);
165 function Symmetric_Difference (Left, Right : Set) return Set;
167 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
169 function Overlap (Left, Right : Set) return Boolean;
171 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
173 function First (Container : Set) return Cursor;
175 function First_Element (Container : Set) return Element_Type;
177 function Last (Container : Set) return Cursor;
179 function Last_Element (Container : Set) return Element_Type;
181 function Next (Position : Cursor) return Cursor;
183 procedure Next (Position : in out Cursor);
185 function Previous (Position : Cursor) return Cursor;
187 procedure Previous (Position : in out Cursor);
189 function Find (Container : Set; Item : Element_Type) return Cursor;
191 function Floor (Container : Set; Item : Element_Type) return Cursor;
193 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
195 function Contains (Container : Set; Item : Element_Type) return Boolean;
197 function "<" (Left, Right : Cursor) return Boolean;
199 function ">" (Left, Right : Cursor) return Boolean;
201 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
203 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
205 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
207 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
209 procedure Iterate
210 (Container : Set;
211 Process : not null access procedure (Position : Cursor));
213 procedure Reverse_Iterate
214 (Container : Set;
215 Process : not null access procedure (Position : Cursor));
217 function Iterate
218 (Container : Set)
219 return Set_Iterator_Interfaces.Reversible_Iterator'class;
221 function Iterate
222 (Container : Set;
223 Start : Cursor)
224 return Set_Iterator_Interfaces.Reversible_Iterator'class;
226 generic
227 type Key_Type (<>) is private;
229 with function Key (Element : Element_Type) return Key_Type;
231 with function "<" (Left, Right : Key_Type) return Boolean is <>;
233 package Generic_Keys is
235 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
237 function Key (Position : Cursor) return Key_Type;
239 function Element (Container : Set; Key : Key_Type) return Element_Type;
241 procedure Replace
242 (Container : in out Set;
243 Key : Key_Type;
244 New_Item : Element_Type);
246 procedure Exclude (Container : in out Set; Key : Key_Type);
248 procedure Delete (Container : in out Set; Key : Key_Type);
250 function Find (Container : Set; Key : Key_Type) return Cursor;
252 function Floor (Container : Set; Key : Key_Type) return Cursor;
254 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
256 function Contains (Container : Set; Key : Key_Type) return Boolean;
258 procedure Update_Element_Preserving_Key
259 (Container : in out Set;
260 Position : Cursor;
261 Process : not null access
262 procedure (Element : in out Element_Type));
264 type Reference_Type (Element : not null access Element_Type) is private
265 with
266 Implicit_Dereference => Element;
268 function Reference_Preserving_Key
269 (Container : aliased in out Set;
270 Position : Cursor) return Reference_Type;
272 function Constant_Reference
273 (Container : aliased Set;
274 Key : Key_Type) return Constant_Reference_Type;
276 function Reference_Preserving_Key
277 (Container : aliased in out Set;
278 Key : Key_Type) return Reference_Type;
280 private
281 type Set_Access is access all Set;
282 for Set_Access'Storage_Size use 0;
284 type Key_Access is access all Key_Type;
286 type Reference_Control_Type is
287 new Ada.Finalization.Controlled with
288 record
289 Container : Set_Access;
290 Pos : Cursor;
291 Old_Key : Key_Access;
292 end record;
294 overriding procedure Adjust (Control : in out Reference_Control_Type);
295 pragma Inline (Adjust);
297 overriding procedure Finalize (Control : in out Reference_Control_Type);
298 pragma Inline (Finalize);
300 type Reference_Type (Element : not null access Element_Type) is record
301 Control : Reference_Control_Type;
302 end record;
304 use Ada.Streams;
306 procedure Write
307 (Stream : not null access Root_Stream_Type'Class;
308 Item : Reference_Type);
310 for Reference_Type'Write use Write;
312 procedure Read
313 (Stream : not null access Root_Stream_Type'Class;
314 Item : out Reference_Type);
316 for Reference_Type'Read use Read;
317 end Generic_Keys;
319 private
321 pragma Inline (Next);
322 pragma Inline (Previous);
324 type Node_Type;
325 type Node_Access is access Node_Type;
327 type Node_Type is limited record
328 Parent : Node_Access;
329 Left : Node_Access;
330 Right : Node_Access;
331 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
332 Element : aliased Element_Type;
333 end record;
335 package Tree_Types is
336 new Red_Black_Trees.Generic_Tree_Types (Node_Type, Node_Access);
338 type Set is new Ada.Finalization.Controlled with record
339 Tree : Tree_Types.Tree_Type;
340 end record;
342 overriding procedure Adjust (Container : in out Set);
344 overriding procedure Finalize (Container : in out Set) renames Clear;
346 use Red_Black_Trees;
347 use Tree_Types;
348 use Ada.Finalization;
349 use Ada.Streams;
351 procedure Write
352 (Stream : not null access Root_Stream_Type'Class;
353 Container : Set);
355 for Set'Write use Write;
357 procedure Read
358 (Stream : not null access Root_Stream_Type'Class;
359 Container : out Set);
361 for Set'Read use Read;
363 type Set_Access is access all Set;
364 for Set_Access'Storage_Size use 0;
366 type Cursor is record
367 Container : Set_Access;
368 Node : Node_Access;
369 end record;
371 procedure Write
372 (Stream : not null access Root_Stream_Type'Class;
373 Item : Cursor);
375 for Cursor'Write use Write;
377 procedure Read
378 (Stream : not null access Root_Stream_Type'Class;
379 Item : out Cursor);
381 for Cursor'Read use Read;
383 type Reference_Control_Type is
384 new Controlled with record
385 Container : Set_Access;
386 end record;
388 overriding procedure Adjust (Control : in out Reference_Control_Type);
389 pragma Inline (Adjust);
391 overriding procedure Finalize (Control : in out Reference_Control_Type);
392 pragma Inline (Finalize);
394 type Constant_Reference_Type
395 (Element : not null access constant Element_Type) is
396 record
397 Control : Reference_Control_Type :=
398 raise Program_Error with "uninitialized reference";
399 -- The RM says, "The default initialization of an object of
400 -- type Constant_Reference_Type or Reference_Type propagates
401 -- Program_Error."
402 end record;
404 procedure Write
405 (Stream : not null access Root_Stream_Type'Class;
406 Item : Constant_Reference_Type);
408 for Constant_Reference_Type'Write use Write;
410 procedure Read
411 (Stream : not null access Root_Stream_Type'Class;
412 Item : out Constant_Reference_Type);
414 for Constant_Reference_Type'Read use Read;
416 -- Three operations are used to optimize in the expansion of "for ... of"
417 -- loops: the Next(Cursor) procedure in the visible part, and the following
418 -- Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for
419 -- details.
421 function Pseudo_Reference
422 (Container : aliased Set'Class) return Reference_Control_Type;
423 pragma Inline (Pseudo_Reference);
424 -- Creates an object of type Reference_Control_Type pointing to the
425 -- container, and increments the Lock. Finalization of this object will
426 -- decrement the Lock.
428 type Element_Access is access all Element_Type;
430 function Get_Element_Access
431 (Position : Cursor) return not null Element_Access;
432 -- Returns a pointer to the element designated by Position.
434 Empty_Set : constant Set :=
435 (Controlled with Tree => (First => null,
436 Last => null,
437 Root => null,
438 Length => 0,
439 Busy => 0,
440 Lock => 0));
442 No_Element : constant Cursor := Cursor'(null, null);
444 type Iterator is new Limited_Controlled and
445 Set_Iterator_Interfaces.Reversible_Iterator with
446 record
447 Container : Set_Access;
448 Node : Node_Access;
449 end record;
451 overriding procedure Finalize (Object : in out Iterator);
453 overriding function First (Object : Iterator) return Cursor;
454 overriding function Last (Object : Iterator) return Cursor;
456 overriding function Next
457 (Object : Iterator;
458 Position : Cursor) return Cursor;
460 overriding function Previous
461 (Object : Iterator;
462 Position : Cursor) return Cursor;
464 end Ada.Containers.Ordered_Sets;