ada: Further cleanup in finalization machinery
[official-gcc.git] / gcc / ada / libgnat / a-cborse.ads
blob26d0305fa7ee55baf76b22538b1de5a8d3947a5c
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-2023, 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 with Ada.Containers.Helpers;
37 private with Ada.Containers.Red_Black_Trees;
38 private with Ada.Streams;
39 private with Ada.Finalization;
40 private with Ada.Strings.Text_Buffers;
42 generic
43 type Element_Type is private;
45 with function "<" (Left, Right : Element_Type) return Boolean is <>;
46 with function "=" (Left, Right : Element_Type) return Boolean is <>;
48 package Ada.Containers.Bounded_Ordered_Sets with
49 SPARK_Mode => Off
51 pragma Annotate (CodePeer, Skip_Analysis);
52 pragma Pure;
53 pragma Remote_Types;
55 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
57 type Set (Capacity : Count_Type) is tagged private
58 with Constant_Indexing => Constant_Reference,
59 Default_Iterator => Iterate,
60 Iterator_Element => Element_Type,
61 Aggregate => (Empty => Empty,
62 Add_Unnamed => Include),
63 Preelaborable_Initialization
64 => Element_Type'Preelaborable_Initialization;
66 type Cursor is private with Preelaborable_Initialization;
68 Empty_Set : constant Set;
70 function Empty (Capacity : Count_Type := 10) return Set;
72 No_Element : constant Cursor;
74 function Has_Element (Position : Cursor) return Boolean;
76 package Set_Iterator_Interfaces is new
77 Ada.Iterator_Interfaces (Cursor, Has_Element);
79 function "=" (Left, Right : Set) return Boolean;
81 function Equivalent_Sets (Left, Right : Set) return Boolean;
83 function To_Set (New_Item : Element_Type) return Set;
85 function Length (Container : Set) return Count_Type;
87 function Is_Empty (Container : Set) return Boolean;
89 procedure Clear (Container : in out Set);
91 function Element (Position : Cursor) return Element_Type;
93 procedure Replace_Element
94 (Container : in out Set;
95 Position : Cursor;
96 New_Item : Element_Type);
98 procedure Query_Element
99 (Position : Cursor;
100 Process : not null access procedure (Element : Element_Type));
102 type Constant_Reference_Type
103 (Element : not null access constant Element_Type) is
104 private
105 with
106 Implicit_Dereference => Element;
108 function Constant_Reference
109 (Container : aliased Set;
110 Position : Cursor) return Constant_Reference_Type;
112 procedure Assign (Target : in out Set; Source : Set);
114 function Copy (Source : Set; Capacity : Count_Type := 0) return Set;
116 procedure Move (Target : in out Set; Source : in out Set);
118 procedure Insert
119 (Container : in out Set;
120 New_Item : Element_Type;
121 Position : out Cursor;
122 Inserted : out Boolean);
124 procedure Insert
125 (Container : in out Set;
126 New_Item : Element_Type);
128 procedure Include
129 (Container : in out Set;
130 New_Item : Element_Type);
132 procedure Replace
133 (Container : in out Set;
134 New_Item : Element_Type);
136 procedure Exclude
137 (Container : in out Set;
138 Item : Element_Type);
140 procedure Delete
141 (Container : in out Set;
142 Item : Element_Type);
144 procedure Delete
145 (Container : in out Set;
146 Position : in out Cursor);
148 procedure Delete_First (Container : in out Set);
150 procedure Delete_Last (Container : in out Set);
152 procedure Union (Target : in out Set; Source : Set);
154 function Union (Left, Right : Set) return Set;
156 function "or" (Left, Right : Set) return Set renames Union;
158 procedure Intersection (Target : in out Set; Source : Set);
160 function Intersection (Left, Right : Set) return Set;
162 function "and" (Left, Right : Set) return Set renames Intersection;
164 procedure Difference (Target : in out Set; Source : Set);
166 function Difference (Left, Right : Set) return Set;
168 function "-" (Left, Right : Set) return Set renames Difference;
170 procedure Symmetric_Difference (Target : in out Set; Source : Set);
172 function Symmetric_Difference (Left, Right : Set) return Set;
174 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
176 function Overlap (Left, Right : Set) return Boolean;
178 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
180 function First (Container : Set) return Cursor;
182 function First_Element (Container : Set) return Element_Type;
184 function Last (Container : Set) return Cursor;
186 function Last_Element (Container : Set) return Element_Type;
188 function Next (Position : Cursor) return Cursor;
190 procedure Next (Position : in out Cursor);
192 function Previous (Position : Cursor) return Cursor;
194 procedure Previous (Position : in out Cursor);
196 function Find (Container : Set; Item : Element_Type) return Cursor;
198 function Floor (Container : Set; Item : Element_Type) return Cursor;
200 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
202 function Contains (Container : Set; Item : Element_Type) return Boolean;
204 function "<" (Left, Right : Cursor) return Boolean;
206 function ">" (Left, Right : Cursor) return Boolean;
208 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
210 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
212 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
214 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
216 procedure Iterate
217 (Container : Set;
218 Process : not null access procedure (Position : Cursor));
220 procedure Reverse_Iterate
221 (Container : Set;
222 Process : not null access procedure (Position : Cursor));
224 function Iterate
225 (Container : Set)
226 return Set_Iterator_Interfaces.Reversible_Iterator'class;
228 function Iterate
229 (Container : Set;
230 Start : Cursor)
231 return Set_Iterator_Interfaces.Reversible_Iterator'class;
233 -- Ada 2022 features:
235 function Has_Element (Container : Set; Position : Cursor) return Boolean;
237 function Tampering_With_Cursors_Prohibited (Container : Set) return Boolean;
239 function Element (Container : Set; Position : Cursor) return Element_Type;
241 procedure Query_Element
242 (Container : Set;
243 Position : Cursor;
244 Process : not null access procedure (Element : Element_Type));
246 function Next (Container : Set; Position : Cursor) return Cursor;
248 procedure Next (Container : Set; Position : in out Cursor);
250 ----------------
252 generic
253 type Key_Type (<>) is private;
255 with function Key (Element : Element_Type) return Key_Type;
257 with function "<" (Left, Right : Key_Type) return Boolean is <>;
259 package Generic_Keys is
261 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
263 function Key (Position : Cursor) return Key_Type;
265 function Key (Container : Set; Position : Cursor) return Key_Type is
266 (Key (Element (Container, Position)));
268 function Element (Container : Set; Key : Key_Type) return Element_Type;
270 procedure Replace
271 (Container : in out Set;
272 Key : Key_Type;
273 New_Item : Element_Type);
275 procedure Exclude (Container : in out Set; Key : Key_Type);
277 procedure Delete (Container : in out Set; Key : Key_Type);
279 function Find (Container : Set; Key : Key_Type) return Cursor;
281 function Floor (Container : Set; Key : Key_Type) return Cursor;
283 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
285 function Contains (Container : Set; Key : Key_Type) return Boolean;
287 procedure Update_Element_Preserving_Key
288 (Container : in out Set;
289 Position : Cursor;
290 Process : not null access
291 procedure (Element : in out Element_Type));
293 type Reference_Type (Element : not null access Element_Type) is private
294 with
295 Implicit_Dereference => Element;
297 function Reference_Preserving_Key
298 (Container : aliased in out Set;
299 Position : Cursor) return Reference_Type;
301 function Constant_Reference
302 (Container : aliased Set;
303 Key : Key_Type) return Constant_Reference_Type;
305 function Reference_Preserving_Key
306 (Container : aliased in out Set;
307 Key : Key_Type) return Reference_Type;
309 private
310 type Set_Access is access all Set;
311 for Set_Access'Storage_Size use 0;
313 type Key_Access is access all Key_Type;
315 use Ada.Streams;
317 package Impl is new Helpers.Generic_Implementation;
319 type Reference_Control_Type is
320 new Impl.Reference_Control_Type with
321 record
322 Container : Set_Access;
323 Pos : Cursor;
324 Old_Key : Key_Access;
325 end record;
327 overriding procedure Finalize (Control : in out Reference_Control_Type);
328 pragma Inline (Finalize);
330 type Reference_Type (Element : not null access Element_Type) is record
331 Control : Reference_Control_Type;
332 end record;
334 procedure Read
335 (Stream : not null access Root_Stream_Type'Class;
336 Item : out Reference_Type);
338 for Reference_Type'Read use Read;
340 procedure Write
341 (Stream : not null access Root_Stream_Type'Class;
342 Item : Reference_Type);
344 for Reference_Type'Write use Write;
346 end Generic_Keys;
348 private
350 pragma Inline (Next);
351 pragma Inline (Previous);
353 type Node_Type is record
354 Parent : Count_Type;
355 Left : Count_Type;
356 Right : Count_Type;
357 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
358 Element : aliased Element_Type;
359 end record;
361 package Tree_Types is
362 new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
364 type Set (Capacity : Count_Type) is
365 new Tree_Types.Tree_Type (Capacity)
366 with null record with Put_Image => Put_Image;
368 procedure Put_Image
369 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Set);
371 use Tree_Types, Tree_Types.Implementation;
372 use Ada.Finalization;
373 use Ada.Streams;
375 procedure Write
376 (Stream : not null access Root_Stream_Type'Class;
377 Container : Set);
379 for Set'Write use Write;
381 procedure Read
382 (Stream : not null access Root_Stream_Type'Class;
383 Container : out Set);
385 for Set'Read use Read;
387 type Set_Access is access all Set;
388 for Set_Access'Storage_Size use 0;
390 -- Note: If a Cursor object has no explicit initialization expression,
391 -- it must default initialize to the same value as constant No_Element.
392 -- The Node component of type Cursor has scalar type Count_Type, so it
393 -- requires an explicit initialization expression of its own declaration,
394 -- in order for objects of record type Cursor to properly initialize.
396 type Cursor is record
397 Container : Set_Access;
398 Node : Count_Type := 0;
399 end record;
401 procedure Write
402 (Stream : not null access Root_Stream_Type'Class;
403 Item : Cursor);
405 for Cursor'Write use Write;
407 procedure Read
408 (Stream : not null access Root_Stream_Type'Class;
409 Item : out Cursor);
411 for Cursor'Read use Read;
413 subtype Reference_Control_Type is Implementation.Reference_Control_Type;
414 -- It is necessary to rename this here, so that the compiler can find it
416 type Constant_Reference_Type
417 (Element : not null access constant Element_Type) is
418 record
419 Control : Reference_Control_Type :=
420 raise Program_Error with "uninitialized reference";
421 -- The RM says, "The default initialization of an object of
422 -- type Constant_Reference_Type or Reference_Type propagates
423 -- Program_Error."
424 end record;
426 procedure Read
427 (Stream : not null access Root_Stream_Type'Class;
428 Item : out Constant_Reference_Type);
430 for Constant_Reference_Type'Read use Read;
432 procedure Write
433 (Stream : not null access Root_Stream_Type'Class;
434 Item : Constant_Reference_Type);
436 for Constant_Reference_Type'Write use Write;
438 -- See Ada.Containers.Vectors for documentation on the following
440 procedure _Next (Position : in out Cursor) renames Next;
441 procedure _Previous (Position : in out Cursor) renames Previous;
443 function Pseudo_Reference
444 (Container : aliased Set'Class) return Reference_Control_Type;
445 pragma Inline (Pseudo_Reference);
446 -- Creates an object of type Reference_Control_Type pointing to the
447 -- container, and increments the Lock. Finalization of this object will
448 -- decrement the Lock.
450 type Element_Access is access all Element_Type with
451 Storage_Size => 0;
453 function Get_Element_Access
454 (Position : Cursor) return not null Element_Access;
455 -- Returns a pointer to the element designated by Position.
457 Empty_Set : constant Set := Set'(Tree_Type with Capacity => 0);
459 No_Element : constant Cursor := Cursor'(null, 0);
461 type Iterator is new Limited_Controlled and
462 Set_Iterator_Interfaces.Reversible_Iterator with
463 record
464 Container : Set_Access;
465 Node : Count_Type;
466 end record
467 with Disable_Controlled => not T_Check;
469 overriding procedure Finalize (Object : in out Iterator);
471 overriding function First (Object : Iterator) return Cursor;
472 overriding function Last (Object : Iterator) return Cursor;
474 overriding function Next
475 (Object : Iterator;
476 Position : Cursor) return Cursor;
478 overriding function Previous
479 (Object : Iterator;
480 Position : Cursor) return Cursor;
482 end Ada.Containers.Bounded_Ordered_Sets;