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