Debian Codegraph @

gcc/ada/libgnat/a-coorse.ads

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