Debian Codegraph @

gcc/ada/libgnat/a-cborse.ads

raw
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-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.Streams;
39 private with Ada.Finalization;
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.Bounded_Ordered_Sets with
49 SPARK_Mode => Off
50 is
51 pragma Annotate (CodePeer, Skip_Analysis);
52 pragma Pure;
53 pragma Remote_Types;
54
55 function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
56
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;
65
66 type Cursor is private with Preelaborable_Initialization;
67
68 Empty_Set : constant Set;
69
70 function Empty (Capacity : Count_Type := 10) 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
105 with
106 Implicit_Dereference => Element;
107
108 function Constant_Reference
109 (Container : aliased Set;
110 Position : Cursor) return Constant_Reference_Type;
111
112 procedure Assign (Target : in out Set; Source : Set);
113
114 function Copy (Source : Set; Capacity : Count_Type := 0) 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 (Container : Set; Item : Element_Type) return Cursor;
197
198 function Floor (Container : Set; Item : Element_Type) return Cursor;
199
200 function Ceiling (Container : Set; Item : Element_Type) return Cursor;
201
202 function Contains (Container : Set; Item : Element_Type) return Boolean;
203
204 function "<" (Left, Right : Cursor) return Boolean;
205
206 function ">" (Left, Right : Cursor) return Boolean;
207
208 function "<" (Left : Cursor; Right : Element_Type) return Boolean;
209
210 function ">" (Left : Cursor; Right : Element_Type) return Boolean;
211
212 function "<" (Left : Element_Type; Right : Cursor) return Boolean;
213
214 function ">" (Left : Element_Type; Right : Cursor) return Boolean;
215
216 procedure Iterate
217 (Container : Set;
218 Process : not null access procedure (Position : Cursor));
219
220 procedure Reverse_Iterate
221 (Container : Set;
222 Process : not null access procedure (Position : Cursor));
223
224 function Iterate
225 (Container : Set)
226 return Set_Iterator_Interfaces.Reversible_Iterator'class;
227
228 function Iterate
229 (Container : Set;
230 Start : Cursor)
231 return Set_Iterator_Interfaces.Reversible_Iterator'class;
232
233 -- Ada 2022 features:
234
235 function Has_Element (Container : Set; Position : Cursor) return Boolean;
236
237 function Tampering_With_Cursors_Prohibited (Container : Set) return Boolean;
238
239 function Element (Container : Set; Position : Cursor) return Element_Type;
240
241 procedure Query_Element
242 (Container : Set;
243 Position : Cursor;
244 Process : not null access procedure (Element : Element_Type));
245
246 function Next (Container : Set; Position : Cursor) return Cursor;
247
248 procedure Next (Container : Set; Position : in out Cursor);
249
250 ----------------
251
252 generic
253 type Key_Type (<>) is private;
254
255 with function Key (Element : Element_Type) return Key_Type;
256
257 with function "<" (Left, Right : Key_Type) return Boolean is <>;
258
259 package Generic_Keys is
260
261 function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
262
263 function Key (Position : Cursor) return Key_Type;
264
265 function Key (Container : Set; Position : Cursor) return Key_Type is
266 (Key (Element (Container, Position)));
267
268 function Element (Container : Set; Key : Key_Type) return Element_Type;
269
270 procedure Replace
271 (Container : in out Set;
272 Key : Key_Type;
273 New_Item : Element_Type);
274
275 procedure Exclude (Container : in out Set; Key : Key_Type);
276
277 procedure Delete (Container : in out Set; Key : Key_Type);
278
279 function Find (Container : Set; Key : Key_Type) return Cursor;
280
281 function Floor (Container : Set; Key : Key_Type) return Cursor;
282
283 function Ceiling (Container : Set; Key : Key_Type) return Cursor;
284
285 function Contains (Container : Set; Key : Key_Type) return Boolean;
286
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));
292
293 type Reference_Type (Element : not null access Element_Type) is private
294 with
295 Implicit_Dereference => Element;
296
297 function Reference_Preserving_Key
298 (Container : aliased in out Set;
299 Position : Cursor) return Reference_Type;
300
301 function Constant_Reference
302 (Container : aliased Set;
303 Key : Key_Type) return Constant_Reference_Type;
304
305 function Reference_Preserving_Key
306 (Container : aliased in out Set;
307 Key : Key_Type) return Reference_Type;
308
309 private
310 type Set_Access is access all Set;
311 for Set_Access'Storage_Size use 0;
312
313 type Key_Access is access all Key_Type;
314
315 use Ada.Streams;
316
317 package Impl is new Helpers.Generic_Implementation;
318
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;
326
327 overriding procedure Adjust (Control : in out Reference_Control_Type);
328 pragma Inline (Adjust);
329
330 overriding procedure Finalize (Control : in out Reference_Control_Type);
331 pragma Inline (Finalize);
332
333 type Reference_Type (Element : not null access Element_Type) is record
334 Control : Reference_Control_Type;
335 end record;
336
337 procedure Read
338 (Stream : not null access Root_Stream_Type'Class;
339 Item : out Reference_Type);
340
341 for Reference_Type'Read use Read;
342
343 procedure Write
344 (Stream : not null access Root_Stream_Type'Class;
345 Item : Reference_Type);
346
347 for Reference_Type'Write use Write;
348
349 end Generic_Keys;
350
351 private
352
353 pragma Inline (Next);
354 pragma Inline (Previous);
355
356 type Node_Type is record
357 Parent : Count_Type;
358 Left : Count_Type;
359 Right : Count_Type;
360 Color : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
361 Element : aliased Element_Type;
362 end record;
363
364 package Tree_Types is
365 new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
366
367 type Set (Capacity : Count_Type) is
368 new Tree_Types.Tree_Type (Capacity)
369 with null record with Put_Image => Put_Image;
370
371 procedure Put_Image
372 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Set);
373
374 use Tree_Types, Tree_Types.Implementation;
375 use Ada.Finalization;
376 use Ada.Streams;
377
378 procedure Write
379 (Stream : not null access Root_Stream_Type'Class;
380 Container : Set);
381
382 for Set'Write use Write;
383
384 procedure Read
385 (Stream : not null access Root_Stream_Type'Class;
386 Container : out Set);
387
388 for Set'Read use Read;
389
390 type Set_Access is access all Set;
391 for Set_Access'Storage_Size use 0;
392
393 -- Note: If a Cursor object has no explicit initialization expression,
394 -- it must default initialize to the same value as constant No_Element.
395 -- The Node component of type Cursor has scalar type Count_Type, so it
396 -- requires an explicit initialization expression of its own declaration,
397 -- in order for objects of record type Cursor to properly initialize.
398
399 type Cursor is record
400 Container : Set_Access;
401 Node : Count_Type := 0;
402 end record;
403
404 procedure Write
405 (Stream : not null access Root_Stream_Type'Class;
406 Item : Cursor);
407
408 for Cursor'Write use Write;
409
410 procedure Read
411 (Stream : not null access Root_Stream_Type'Class;
412 Item : out Cursor);
413
414 for Cursor'Read use Read;
415
416 subtype Reference_Control_Type is Implementation.Reference_Control_Type;
417 -- It is necessary to rename this here, so that the compiler can find it
418
419 type Constant_Reference_Type
420 (Element : not null access constant Element_Type) is
421 record
422 Control : Reference_Control_Type :=
423 raise Program_Error with "uninitialized reference";
424 -- The RM says, "The default initialization of an object of
425 -- type Constant_Reference_Type or Reference_Type propagates
426 -- Program_Error."
427 end record;
428
429 procedure Read
430 (Stream : not null access Root_Stream_Type'Class;
431 Item : out Constant_Reference_Type);
432
433 for Constant_Reference_Type'Read use Read;
434
435 procedure Write
436 (Stream : not null access Root_Stream_Type'Class;
437 Item : Constant_Reference_Type);
438
439 for Constant_Reference_Type'Write use Write;
440
441 -- See Ada.Containers.Vectors for documentation on the following
442
443 procedure _Next (Position : in out Cursor) renames Next;
444 procedure _Previous (Position : in out Cursor) renames Previous;
445
446 function Pseudo_Reference
447 (Container : aliased Set'Class) return Reference_Control_Type;
448 pragma Inline (Pseudo_Reference);
449 -- Creates an object of type Reference_Control_Type pointing to the
450 -- container, and increments the Lock. Finalization of this object will
451 -- decrement the Lock.
452
453 type Element_Access is access all Element_Type with
454 Storage_Size => 0;
455
456 function Get_Element_Access
457 (Position : Cursor) return not null Element_Access;
458 -- Returns a pointer to the element designated by Position.
459
460 Empty_Set : constant Set := Set'(Tree_Type with Capacity => 0);
461
462 No_Element : constant Cursor := Cursor'(null, 0);
463
464 type Iterator is new Limited_Controlled and
465 Set_Iterator_Interfaces.Reversible_Iterator with
466 record
467 Container : Set_Access;
468 Node : Count_Type;
469 end record
470 with Disable_Controlled => not T_Check;
471
472 overriding procedure Finalize (Object : in out Iterator);
473
474 overriding function First (Object : Iterator) return Cursor;
475 overriding function Last (Object : Iterator) return Cursor;
476
477 overriding function Next
478 (Object : Iterator;
479 Position : Cursor) return Cursor;
480
481 overriding function Previous
482 (Object : Iterator;
483 Position : Cursor) return Cursor;
484
485 end Ada.Containers.Bounded_Ordered_Sets;