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-2020, 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
34with Ada.Iterator_Interfaces;
35
36with Ada.Containers.Helpers;
37private with Ada.Containers.Red_Black_Trees;
38private with Ada.Finalization;
39private with Ada.Streams;
40private with Ada.Strings.Text_Output;
41
42generic
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
48package Ada.Containers.Ordered_Sets with
49  SPARK_Mode => Off
50is
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   generic
235      type Key_Type (<>) is private;
236
237      with function Key (Element : Element_Type) return Key_Type;
238
239      with function "<" (Left, Right : Key_Type) return Boolean is <>;
240
241   package Generic_Keys is
242
243      function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
244
245      function Key (Position : Cursor) return Key_Type;
246
247      function Element (Container : Set; Key : Key_Type) return Element_Type;
248
249      procedure Replace
250        (Container : in out Set;
251         Key       : Key_Type;
252         New_Item  : Element_Type);
253
254      procedure Exclude (Container : in out Set; Key : Key_Type);
255
256      procedure Delete (Container : in out Set; Key : Key_Type);
257
258      function Find (Container : Set; Key : Key_Type) return Cursor;
259
260      function Floor (Container : Set; Key : Key_Type) return Cursor;
261
262      function Ceiling (Container : Set; Key : Key_Type) return Cursor;
263
264      function Contains (Container : Set; Key : Key_Type) return Boolean;
265
266      procedure Update_Element_Preserving_Key
267        (Container : in out Set;
268         Position  : Cursor;
269         Process   : not null access
270                       procedure (Element : in out Element_Type));
271
272      type Reference_Type (Element : not null access Element_Type) is private
273      with
274         Implicit_Dereference => Element;
275
276      function Reference_Preserving_Key
277        (Container : aliased in out Set;
278         Position  : Cursor) return Reference_Type;
279
280      function Constant_Reference
281        (Container : aliased Set;
282         Key       : Key_Type) return Constant_Reference_Type;
283
284      function Reference_Preserving_Key
285        (Container : aliased in out Set;
286         Key       : Key_Type) return Reference_Type;
287
288   private
289      type Set_Access is access all Set;
290      for Set_Access'Storage_Size use 0;
291
292      type Key_Access is access all Key_Type;
293
294      package Impl is new Helpers.Generic_Implementation;
295
296      type Reference_Control_Type is
297        new Impl.Reference_Control_Type with
298      record
299         Container : Set_Access;
300         Pos       : Cursor;
301         Old_Key   : Key_Access;
302      end record;
303
304      overriding procedure Finalize (Control : in out Reference_Control_Type);
305      pragma Inline (Finalize);
306
307      type Reference_Type (Element : not null access Element_Type) is record
308         Control  : Reference_Control_Type;
309      end record;
310
311      use Ada.Streams;
312
313      procedure Write
314        (Stream : not null access Root_Stream_Type'Class;
315         Item   : Reference_Type);
316
317      for Reference_Type'Write use Write;
318
319      procedure Read
320        (Stream : not null access Root_Stream_Type'Class;
321         Item   : out Reference_Type);
322
323      for Reference_Type'Read use Read;
324   end Generic_Keys;
325
326private
327
328   pragma Inline (Next);
329   pragma Inline (Previous);
330
331   type Node_Type;
332   type Node_Access is access Node_Type;
333
334   type Node_Type is limited record
335      Parent  : Node_Access;
336      Left    : Node_Access;
337      Right   : Node_Access;
338      Color   : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
339      Element : aliased Element_Type;
340   end record;
341
342   package Tree_Types is
343     new Red_Black_Trees.Generic_Tree_Types (Node_Type, Node_Access);
344
345   type Set is new Ada.Finalization.Controlled with record
346      Tree : Tree_Types.Tree_Type;
347   end record with Put_Image => Put_Image;
348
349   procedure Put_Image
350     (S : in out Ada.Strings.Text_Output.Sink'Class; V : Set);
351
352   overriding procedure Adjust (Container : in out Set);
353
354   overriding procedure Finalize (Container : in out Set) renames Clear;
355
356   use Red_Black_Trees;
357   use Tree_Types, Tree_Types.Implementation;
358   use Ada.Finalization;
359   use Ada.Streams;
360
361   procedure Write
362     (Stream    : not null access Root_Stream_Type'Class;
363      Container : Set);
364
365   for Set'Write use Write;
366
367   procedure Read
368     (Stream    : not null access Root_Stream_Type'Class;
369      Container : out Set);
370
371   for Set'Read use Read;
372
373   type Set_Access is access all Set;
374   for Set_Access'Storage_Size use 0;
375
376   type Cursor is record
377      Container : Set_Access;
378      Node      : Node_Access;
379   end record;
380
381   procedure Write
382     (Stream : not null access Root_Stream_Type'Class;
383      Item   : Cursor);
384
385   for Cursor'Write use Write;
386
387   procedure Read
388     (Stream : not null access Root_Stream_Type'Class;
389      Item   : out Cursor);
390
391   for Cursor'Read use Read;
392
393   subtype Reference_Control_Type is Implementation.Reference_Control_Type;
394   --  It is necessary to rename this here, so that the compiler can find it
395
396   type Constant_Reference_Type
397     (Element : not null access constant Element_Type) is
398      record
399         Control : Reference_Control_Type :=
400           raise Program_Error with "uninitialized reference";
401         --  The RM says, "The default initialization of an object of
402         --  type Constant_Reference_Type or Reference_Type propagates
403         --  Program_Error."
404      end record;
405
406   procedure Write
407     (Stream : not null access Root_Stream_Type'Class;
408      Item   : Constant_Reference_Type);
409
410   for Constant_Reference_Type'Write use Write;
411
412   procedure Read
413     (Stream : not null access Root_Stream_Type'Class;
414      Item   : out Constant_Reference_Type);
415
416   for Constant_Reference_Type'Read use Read;
417
418   --  Three operations are used to optimize in the expansion of "for ... of"
419   --  loops: the Next(Cursor) procedure in the visible part, and the following
420   --  Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for
421   --  details.
422
423   function Pseudo_Reference
424     (Container : aliased Set'Class) return Reference_Control_Type;
425   pragma Inline (Pseudo_Reference);
426   --  Creates an object of type Reference_Control_Type pointing to the
427   --  container, and increments the Lock. Finalization of this object will
428   --  decrement the Lock.
429
430   type Element_Access is access all Element_Type with
431     Storage_Size => 0;
432
433   function Get_Element_Access
434     (Position : Cursor) return not null Element_Access;
435   --  Returns a pointer to the element designated by Position.
436
437   Empty_Set : constant Set := (Controlled with others => <>);
438   function Empty  return Set is (Empty_Set);
439
440   No_Element : constant Cursor := Cursor'(null, null);
441
442   type Iterator is new Limited_Controlled and
443     Set_Iterator_Interfaces.Reversible_Iterator with
444   record
445      Container : Set_Access;
446      Node      : Node_Access;
447   end record
448     with Disable_Controlled => not T_Check;
449
450   overriding procedure Finalize (Object : in out Iterator);
451
452   overriding function First (Object : Iterator) return Cursor;
453   overriding function Last  (Object : Iterator) return Cursor;
454
455   overriding function Next
456     (Object   : Iterator;
457      Position : Cursor) return Cursor;
458
459   overriding function Previous
460     (Object   : Iterator;
461      Position : Cursor) return Cursor;
462
463end Ada.Containers.Ordered_Sets;
464