Debian Codegraph @

gcc/ada/libgnat/a-ciorse.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.INDEFINITE_ORDERED_SETS --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2025, 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 ------------------------------------------------------------------------------
33
34 with Ada.Iterator_Interfaces;
35
36 with Ada.Containers.Helpers;
37 private with Ada.Containers.Red_Black_Trees;
38 private with Ada.Finalization;
39 private with Ada.Streams;
40 private with Ada.Strings.Text_Buffers;
41
42 generic
43 type Element_Type (<>) is private;
44
45 with function "<" (Left, Right : Element_Type) return Boolean is <>;
46 with function "=" (Left, Right : Element_Type) return Boolean is <>;
47
48 package Ada.Containers.Indefinite_Ordered_Sets with
49 SPARK_Mode => Off
50 is
51 pragma Annotate (CodePeer, Skip_Analysis);
52 pragma Preelaborate;
53 pragma Remote_Types;
54
55 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
56
57 type Set is tagged private with
58 Constant_Indexing => Constant_Reference,
59 Default_Iterator => Iterate,
60 Iterator_Element => Element_Type,
61 Aggregate => (Empty => Empty,
62 Add_Unnamed => Include);
63
64 pragma Preelaborable_Initialization (Set);
65
66 type Cursor is private;
67 pragma Preelaborable_Initialization (Cursor);
68
69 Empty_Set : constant Set;
70 function Empty return Set;
71
72 No_Element : constant Cursor;
73
74 function Has_Element (Position : Cursor) return Boolean;
75
76 package Set_Iterator_Interfaces is new
77 Ada.Iterator_Interfaces (Cursor, Has_Element);
78
79 function "=" (Left, Right : Set) return Boolean;
80
81 function Equivalent_Sets (Left, Right : Set) return Boolean;
82
83 function To_Set (New_Item : Element_Type) return Set;
84
85 function Length (Container : Set) return Count_Type;
86
87 function Is_Empty (Container : Set) return Boolean;
88
89 procedure Clear (Container : in out Set);
90
91 function Element (Position : Cursor) return Element_Type;
92
93 procedure Replace_Element
94 (Container : in out Set;
95 Position : Cursor;
96 New_Item : Element_Type);
97
98 procedure Query_Element
99 (Position : Cursor;
100 Process : not null access procedure (Element : Element_Type));
101
102 type Constant_Reference_Type
103 (Element : not null access constant Element_Type) is
104 private with
105 Implicit_Dereference => Element;
106
107 function Constant_Reference
108 (Container : aliased Set;
109 Position : Cursor) return Constant_Reference_Type;
110 pragma Inline (Constant_Reference);
111
112 procedure Assign (Target : in out Set; Source : Set);
113
114 function Copy (Source : Set) return Set;
115
116 procedure Move (Target : in out Set; Source : in out Set);
117
118 procedure Insert
119 (Container : in out Set;
120 New_Item : Element_Type;
121 Position : out Cursor;
122 Inserted : out Boolean);
123
124 procedure Insert
125 (Container : in out Set;
126 New_Item : Element_Type);
127
128 procedure Include
129 (Container : in out Set;
130 New_Item : Element_Type);
131
132 procedure Replace
133 (Container : in out Set;
134 New_Item : Element_Type);
135
136 procedure Exclude
137 (Container : in out Set;
138 Item : Element_Type);
139
140 procedure Delete
141 (Container : in out Set;
142 Item : Element_Type);
143
144 procedure Delete
145 (Container : in out Set;
146 Position : in out Cursor);
147
148 procedure Delete_First (Container : in out Set);
149
150 procedure Delete_Last (Container : in out Set);
151
152 procedure Union (Target : in out Set; Source : Set);
153
154 function Union (Left, Right : Set) return Set;
155
156 function "or" (Left, Right : Set) return Set renames Union;
157
158 procedure Intersection (Target : in out Set; Source : Set);
159
160 function Intersection (Left, Right : Set) return Set;
161
162 function "and" (Left, Right : Set) return Set renames Intersection;
163
164 procedure Difference (Target : in out Set; Source : Set);
165
166 function Difference (Left, Right : Set) return Set;
167
168 function "-" (Left, Right : Set) return Set renames Difference;
169
170 procedure Symmetric_Difference (Target : in out Set; Source : Set);
171
172 function Symmetric_Difference (Left, Right : Set) return Set;
173
174 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
175
176 function Overlap (Left, Right : Set) return Boolean;
177
178 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
179
180 function First (Container : Set) return Cursor;
181
182 function First_Element (Container : Set) return Element_Type;
183
184 function Last (Container : Set) return Cursor;
185
186 function Last_Element (Container : Set) return Element_Type;
187
188 function Next (Position : Cursor) return Cursor;
189
190 procedure Next (Position : in out Cursor);
191
192 function Previous (Position : Cursor) return Cursor;
193
194 procedure Previous (Position : in out Cursor);
195
196 function Find
197 (Container : Set;
198 Item : Element_Type) return Cursor;
199
200 function Floor
201 (Container : Set;
202 Item : Element_Type) return Cursor;
203
204 function Ceiling
205 (Container : Set;
206 Item : Element_Type) return Cursor;
207
208 function Contains
209 (Container : Set;
210 Item : Element_Type) return Boolean;
211
212 function "<" (Left, Right : Cursor) return Boolean;
213
214 function ">" (Left, Right : Cursor) return Boolean;
215
216 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
217
218 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
219
220 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
221
222 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
223
224 procedure Iterate
225 (Container : Set;
226 Process : not null access procedure (Position : Cursor));
227
228 procedure Reverse_Iterate
229 (Container : Set;
230 Process : not null access procedure (Position : Cursor));
231
232 function Iterate
233 (Container : Set)
234 return Set_Iterator_Interfaces.Reversible_Iterator'class;
235
236 function Iterate
237 (Container : Set;
238 Start : Cursor)
239 return Set_Iterator_Interfaces.Reversible_Iterator'class;
240
241 -- Ada 2022 features:
242
243 function Has_Element (Container : Set; Position : Cursor) return Boolean;
244
245 function Tampering_With_Cursors_Prohibited (Container : Set) return Boolean;
246
247 function Element (Container : Set; Position : Cursor) return Element_Type;
248
249 procedure Query_Element
250 (Container : Set;
251 Position : Cursor;
252 Process : not null access procedure (Element : Element_Type));
253
254 function Next (Container : Set; Position : Cursor) return Cursor;
255
256 procedure Next (Container : Set; Position : in out Cursor);
257
258 ----------------
259
260 generic
261 type Key_Type (<>) is private;
262
263 with function Key (Element : Element_Type) return Key_Type;
264
265 with function "<" (Left, Right : Key_Type) return Boolean is <>;
266
267 package Generic_Keys is
268
269 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
270
271 function Key (Position : Cursor) return Key_Type;
272
273 function Key (Container : Set; Position : Cursor) return Key_Type is
274 (Key (Element (Container, Position)));
275
276 function Element (Container : Set; Key : Key_Type) return Element_Type;
277
278 procedure Replace
279 (Container : in out Set;
280 Key : Key_Type;
281 New_Item : Element_Type);
282
283 procedure Exclude (Container : in out Set; Key : Key_Type);
284
285 procedure Delete (Container : in out Set; Key : Key_Type);
286
287 function Find
288 (Container : Set;
289 Key : Key_Type) return Cursor;
290
291 function Floor
292 (Container : Set;
293 Key : Key_Type) return Cursor;
294
295 function Ceiling
296 (Container : Set;
297 Key : Key_Type) return Cursor;
298
299 function Contains
300 (Container : Set;
301 Key : Key_Type) return Boolean;
302
303 procedure Update_Element_Preserving_Key
304 (Container : in out Set;
305 Position : Cursor;
306 Process : not null access
307 procedure (Element : in out Element_Type));
308
309 type Reference_Type (Element : not null access Element_Type) is private
310 with
311 Implicit_Dereference => Element;
312
313 function Reference_Preserving_Key
314 (Container : aliased in out Set;
315 Position : Cursor) return Reference_Type;
316
317 function Constant_Reference
318 (Container : aliased Set;
319 Key : Key_Type) return Constant_Reference_Type;
320
321 function Reference_Preserving_Key
322 (Container : aliased in out Set;
323 Key : Key_Type) return Reference_Type;
324
325 private
326 type Set_Access is access all Set;
327 for Set_Access'Storage_Size use 0;
328
329 type Key_Access is access all Key_Type;
330
331 package Impl is new Helpers.Generic_Implementation;
332
333 type Reference_Control_Type is
334 new Impl.Reference_Control_Type with
335 record
336 Container : Set_Access;
337 Pos : Cursor;
338 Old_Key : Key_Access;
339 end record;
340
341 overriding procedure Adjust (Control : in out Reference_Control_Type);
342 pragma Inline (Adjust);
343
344 overriding procedure Finalize (Control : in out Reference_Control_Type);
345 pragma Inline (Finalize);
346
347 type Reference_Type (Element : not null access Element_Type) is record
348 Control : Reference_Control_Type;
349 end record;
350
351 use Ada.Streams;
352
353 procedure Write
354 (Stream : not null access Root_Stream_Type'Class;
355 Item : Reference_Type);
356
357 for Reference_Type'Write use Write;
358
359 procedure Read
360 (Stream : not null access Root_Stream_Type'Class;
361 Item : out Reference_Type);
362
363 for Reference_Type'Read use Read;
364 end Generic_Keys;
365
366 private
367 pragma Inline (Next);
368 pragma Inline (Previous);
369
370 type Node_Type;
371 type Node_Access is access Node_Type;
372
373 type Element_Access is access all Element_Type;
374
375 type Node_Type is limited record
376 Parent : Node_Access;
377 Left : Node_Access;
378 Right : Node_Access;
379 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
380 Element : Element_Access;
381 end record;
382
383 package Tree_Types is new Red_Black_Trees.Generic_Tree_Types
384 (Node_Type,
385 Node_Access);
386
387 type Set is new Ada.Finalization.Controlled with record
388 Tree : Tree_Types.Tree_Type;
389 end record with Put_Image => Put_Image;
390
391 procedure Put_Image
392 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Set);
393
394 overriding procedure Adjust (Container : in out Set);
395
396 overriding procedure Finalize (Container : in out Set) renames Clear;
397
398 use Red_Black_Trees;
399 use Tree_Types, Tree_Types.Implementation;
400 use Ada.Finalization;
401 use Ada.Streams;
402
403 procedure Write
404 (Stream : not null access Root_Stream_Type'Class;
405 Container : Set);
406
407 for Set'Write use Write;
408
409 procedure Read
410 (Stream : not null access Root_Stream_Type'Class;
411 Container : out Set);
412
413 for Set'Read use Read;
414
415 type Set_Access is access all Set;
416 for Set_Access'Storage_Size use 0;
417
418 type Cursor is record
419 Container : Set_Access;
420 Node : Node_Access;
421 end record;
422
423 procedure Write
424 (Stream : not null access Root_Stream_Type'Class;
425 Item : Cursor);
426
427 for Cursor'Write use Write;
428
429 procedure Read
430 (Stream : not null access Root_Stream_Type'Class;
431 Item : out Cursor);
432
433 for Cursor'Read use Read;
434
435 subtype Reference_Control_Type is Implementation.Reference_Control_Type;
436 -- It is necessary to rename this here, so that the compiler can find it
437
438 type Constant_Reference_Type
439 (Element : not null access constant Element_Type) is
440 record
441 Control : Reference_Control_Type :=
442 raise Program_Error with "uninitialized reference";
443 -- The RM says, "The default initialization of an object of
444 -- type Constant_Reference_Type or Reference_Type propagates
445 -- Program_Error."
446 end record;
447
448 procedure Read
449 (Stream : not null access Root_Stream_Type'Class;
450 Item : out Constant_Reference_Type);
451
452 for Constant_Reference_Type'Read use Read;
453
454 procedure Write
455 (Stream : not null access Root_Stream_Type'Class;
456 Item : Constant_Reference_Type);
457
458 for Constant_Reference_Type'Write use Write;
459
460 -- See Ada.Containers.Vectors for documentation on the following
461
462 procedure _Next (Position : in out Cursor) renames Next;
463 procedure _Previous (Position : in out Cursor) renames Previous;
464
465 function Pseudo_Reference
466 (Container : aliased Set'Class) return Reference_Control_Type;
467 pragma Inline (Pseudo_Reference);
468 -- Creates an object of type Reference_Control_Type pointing to the
469 -- container, and increments the Lock. Finalization of this object will
470 -- decrement the Lock.
471
472 function Get_Element_Access
473 (Position : Cursor) return not null Element_Access;
474 -- Returns a pointer to the element designated by Position.
475
476 Empty_Set : constant Set := (Controlled with others => <>);
477 function Empty return Set is (Empty_Set);
478
479 No_Element : constant Cursor := Cursor'(null, null);
480
481 type Iterator is new Limited_Controlled and
482 Set_Iterator_Interfaces.Reversible_Iterator with
483 record
484 Container : Set_Access;
485 Node : Node_Access;
486 end record
487 with Disable_Controlled => not T_Check;
488
489 overriding procedure Finalize (Object : in out Iterator);
490
491 overriding function First (Object : Iterator) return Cursor;
492 overriding function Last (Object : Iterator) return Cursor;
493
494 overriding function Next
495 (Object : Iterator;
496 Position : Cursor) return Cursor;
497
498 overriding function Previous
499 (Object : Iterator;
500 Position : Cursor) return Cursor;
501
502 end Ada.Containers.Indefinite_Ordered_Sets;