2014-07-30 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / a-cborse.ads
blobaee0bf968a1010a380d0324f60a3d3f2921b9fd5
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . B O U N D E D _ O R D E R E D _ S E T S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2014, 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.Streams;
38 private with Ada.Finalization;
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.Bounded_Ordered_Sets is
47 pragma Pure;
48 pragma Remote_Types;
50 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
52 type Set (Capacity : Count_Type) 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 Empty_Set : constant Set;
64 No_Element : constant Cursor;
66 function Has_Element (Position : Cursor) return Boolean;
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;
104 procedure Assign (Target : in out Set; Source : Set);
106 function Copy (Source : Set; Capacity : Count_Type := 0) return Set;
108 procedure Move (Target : in out Set; Source : in out Set);
110 procedure Insert
111 (Container : in out Set;
112 New_Item : Element_Type;
113 Position : out Cursor;
114 Inserted : out Boolean);
116 procedure Insert
117 (Container : in out Set;
118 New_Item : Element_Type);
120 procedure Include
121 (Container : in out Set;
122 New_Item : Element_Type);
124 procedure Replace
125 (Container : in out Set;
126 New_Item : Element_Type);
128 procedure Exclude
129 (Container : in out Set;
130 Item : Element_Type);
132 procedure Delete
133 (Container : in out Set;
134 Item : Element_Type);
136 procedure Delete
137 (Container : in out Set;
138 Position : in out Cursor);
140 procedure Delete_First (Container : in out Set);
142 procedure Delete_Last (Container : in out Set);
144 procedure Union (Target : in out Set; Source : Set);
146 function Union (Left, Right : Set) return Set;
148 function "or" (Left, Right : Set) return Set renames Union;
150 procedure Intersection (Target : in out Set; Source : Set);
152 function Intersection (Left, Right : Set) return Set;
154 function "and" (Left, Right : Set) return Set renames Intersection;
156 procedure Difference (Target : in out Set; Source : Set);
158 function Difference (Left, Right : Set) return Set;
160 function "-" (Left, Right : Set) return Set renames Difference;
162 procedure Symmetric_Difference (Target : in out Set; Source : Set);
164 function Symmetric_Difference (Left, Right : Set) return Set;
166 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
168 function Overlap (Left, Right : Set) return Boolean;
170 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
172 function First (Container : Set) return Cursor;
174 function First_Element (Container : Set) return Element_Type;
176 function Last (Container : Set) return Cursor;
178 function Last_Element (Container : Set) return Element_Type;
180 function Next (Position : Cursor) return Cursor;
182 procedure Next (Position : in out Cursor);
184 function Previous (Position : Cursor) return Cursor;
186 procedure Previous (Position : in out Cursor);
188 function Find (Container : Set; Item : Element_Type) return Cursor;
190 function Floor (Container : Set; Item : Element_Type) return Cursor;
192 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
194 function Contains (Container : Set; Item : Element_Type) return Boolean;
196 function "<" (Left, Right : Cursor) return Boolean;
198 function ">" (Left, Right : Cursor) return Boolean;
200 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
202 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
204 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
206 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
208 procedure Iterate
209 (Container : Set;
210 Process : not null access procedure (Position : Cursor));
212 procedure Reverse_Iterate
213 (Container : Set;
214 Process : not null access procedure (Position : Cursor));
216 function Iterate
217 (Container : Set)
218 return Set_Iterator_Interfaces.Reversible_Iterator'class;
220 function Iterate
221 (Container : Set;
222 Start : Cursor)
223 return Set_Iterator_Interfaces.Reversible_Iterator'class;
225 generic
226 type Key_Type (<>) is private;
228 with function Key (Element : Element_Type) return Key_Type;
230 with function "<" (Left, Right : Key_Type) return Boolean is <>;
232 package Generic_Keys is
234 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
236 function Key (Position : Cursor) return Key_Type;
238 function Element (Container : Set; Key : Key_Type) return Element_Type;
240 procedure Replace
241 (Container : in out Set;
242 Key : Key_Type;
243 New_Item : Element_Type);
245 procedure Exclude (Container : in out Set; Key : Key_Type);
247 procedure Delete (Container : in out Set; Key : Key_Type);
249 function Find (Container : Set; Key : Key_Type) return Cursor;
251 function Floor (Container : Set; Key : Key_Type) return Cursor;
253 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
255 function Contains (Container : Set; Key : Key_Type) return Boolean;
257 procedure Update_Element_Preserving_Key
258 (Container : in out Set;
259 Position : Cursor;
260 Process : not null access
261 procedure (Element : in out Element_Type));
263 type Reference_Type (Element : not null access Element_Type) is private
264 with
265 Implicit_Dereference => Element;
267 function Reference_Preserving_Key
268 (Container : aliased in out Set;
269 Position : Cursor) return Reference_Type;
271 function Constant_Reference
272 (Container : aliased Set;
273 Key : Key_Type) return Constant_Reference_Type;
275 function Reference_Preserving_Key
276 (Container : aliased in out Set;
277 Key : Key_Type) return Reference_Type;
279 private
280 type Set_Access is access all Set;
281 for Set_Access'Storage_Size use 0;
283 type Key_Access is access all Key_Type;
285 use Ada.Streams;
287 type Reference_Control_Type is
288 new Ada.Finalization.Controlled with
289 record
290 Container : Set_Access;
291 Pos : Cursor;
292 Old_Key : Key_Access;
293 end record;
295 overriding procedure
296 Adjust (Control : in out Reference_Control_Type);
297 pragma Inline (Adjust);
299 overriding procedure
300 Finalize (Control : in out Reference_Control_Type);
301 pragma Inline (Finalize);
303 type Reference_Type (Element : not null access Element_Type) is record
304 Control : Reference_Control_Type;
305 end record;
307 procedure Read
308 (Stream : not null access Root_Stream_Type'Class;
309 Item : out Reference_Type);
311 for Reference_Type'Read use Read;
313 procedure Write
314 (Stream : not null access Root_Stream_Type'Class;
315 Item : Reference_Type);
317 for Reference_Type'Write use Write;
319 end Generic_Keys;
321 private
323 pragma Inline (Next);
324 pragma Inline (Previous);
326 type Node_Type is record
327 Parent : Count_Type;
328 Left : Count_Type;
329 Right : Count_Type;
330 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
331 Element : aliased Element_Type;
332 end record;
334 package Tree_Types is
335 new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
337 type Set (Capacity : Count_Type) is
338 new Tree_Types.Tree_Type (Capacity) with null record;
340 use Tree_Types;
341 use Ada.Finalization;
342 use Ada.Streams;
344 procedure Write
345 (Stream : not null access Root_Stream_Type'Class;
346 Container : Set);
348 for Set'Write use Write;
350 procedure Read
351 (Stream : not null access Root_Stream_Type'Class;
352 Container : out Set);
354 for Set'Read use Read;
356 type Set_Access is access all Set;
357 for Set_Access'Storage_Size use 0;
359 -- Note: If a Cursor object has no explicit initialization expression,
360 -- it must default initialize to the same value as constant No_Element.
361 -- The Node component of type Cursor has scalar type Count_Type, so it
362 -- requires an explicit initialization expression of its own declaration,
363 -- in order for objects of record type Cursor to properly initialize.
365 type Cursor is record
366 Container : Set_Access;
367 Node : Count_Type := 0;
368 end record;
370 procedure Write
371 (Stream : not null access Root_Stream_Type'Class;
372 Item : Cursor);
374 for Cursor'Write use Write;
376 procedure Read
377 (Stream : not null access Root_Stream_Type'Class;
378 Item : out Cursor);
380 for Cursor'Read use Read;
382 type Reference_Control_Type is new Controlled with record
383 Container : Set_Access;
384 end record;
386 overriding procedure Adjust (Control : in out Reference_Control_Type);
387 pragma Inline (Adjust);
389 overriding procedure Finalize (Control : in out Reference_Control_Type);
390 pragma Inline (Finalize);
392 type Constant_Reference_Type
393 (Element : not null access constant Element_Type) is
394 record
395 Control : Reference_Control_Type;
396 end record;
398 procedure Read
399 (Stream : not null access Root_Stream_Type'Class;
400 Item : out Constant_Reference_Type);
402 for Constant_Reference_Type'Read use Read;
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 Empty_Set : constant Set := Set'(Tree_Type with Capacity => 0);
412 No_Element : constant Cursor := Cursor'(null, 0);
414 type Iterator is new Limited_Controlled and
415 Set_Iterator_Interfaces.Reversible_Iterator with
416 record
417 Container : Set_Access;
418 Node : Count_Type;
419 end record;
421 overriding procedure Finalize (Object : in out Iterator);
423 overriding function First (Object : Iterator) return Cursor;
424 overriding function Last (Object : Iterator) return Cursor;
426 overriding function Next
427 (Object : Iterator;
428 Position : Cursor) return Cursor;
430 overriding function Previous
431 (Object : Iterator;
432 Position : Cursor) return Cursor;
434 end Ada.Containers.Bounded_Ordered_Sets;