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-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.Streams;
39private with Ada.Finalization;
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.Bounded_Ordered_Sets with
49  SPARK_Mode => Off
50is
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
64   pragma Preelaborable_Initialization (Set);
65
66   type Cursor is private;
67   pragma Preelaborable_Initialization (Cursor);
68
69   Empty_Set : constant Set;
70
71   function Empty (Capacity : Count_Type := 10) return Set;
72
73   No_Element : constant Cursor;
74
75   function Has_Element (Position : Cursor) return Boolean;
76
77   package Set_Iterator_Interfaces is new
78     Ada.Iterator_Interfaces (Cursor, Has_Element);
79
80   function "=" (Left, Right : Set) return Boolean;
81
82   function Equivalent_Sets (Left, Right : Set) return Boolean;
83
84   function To_Set (New_Item : Element_Type) return Set;
85
86   function Length (Container : Set) return Count_Type;
87
88   function Is_Empty (Container : Set) return Boolean;
89
90   procedure Clear (Container : in out Set);
91
92   function Element (Position : Cursor) return Element_Type;
93
94   procedure Replace_Element
95     (Container : in out Set;
96      Position  : Cursor;
97      New_Item  : Element_Type);
98
99   procedure Query_Element
100     (Position : Cursor;
101      Process  : not null access procedure (Element : Element_Type));
102
103   type Constant_Reference_Type
104      (Element : not null access constant Element_Type) is
105   private
106   with
107      Implicit_Dereference => Element;
108
109   function Constant_Reference
110     (Container : aliased Set;
111      Position  : Cursor) return Constant_Reference_Type;
112
113   procedure Assign (Target : in out Set; Source : Set);
114
115   function Copy (Source : Set; Capacity : Count_Type := 0) 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      use Ada.Streams;
295
296      package Impl is new Helpers.Generic_Implementation;
297
298      type Reference_Control_Type is
299        new Impl.Reference_Control_Type with
300      record
301         Container : Set_Access;
302         Pos       : Cursor;
303         Old_Key   : Key_Access;
304      end record;
305
306      overriding procedure Finalize (Control : in out Reference_Control_Type);
307      pragma Inline (Finalize);
308
309      type Reference_Type (Element : not null access Element_Type) is record
310         Control  : Reference_Control_Type;
311      end record;
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
319      procedure Write
320        (Stream : not null access Root_Stream_Type'Class;
321         Item   : Reference_Type);
322
323      for Reference_Type'Write use Write;
324
325   end Generic_Keys;
326
327private
328
329   pragma Inline (Next);
330   pragma Inline (Previous);
331
332   type Node_Type is record
333      Parent  : Count_Type;
334      Left    : Count_Type;
335      Right   : Count_Type;
336      Color   : Red_Black_Trees.Color_Type := Red_Black_Trees.Red;
337      Element : aliased Element_Type;
338   end record;
339
340   package Tree_Types is
341     new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
342
343   type Set (Capacity : Count_Type) is
344     new Tree_Types.Tree_Type (Capacity)
345      with null record with Put_Image => Put_Image;
346
347   procedure Put_Image
348     (S : in out Ada.Strings.Text_Output.Sink'Class; V : Set);
349
350   use Tree_Types, Tree_Types.Implementation;
351   use Ada.Finalization;
352   use Ada.Streams;
353
354   procedure Write
355     (Stream    : not null access Root_Stream_Type'Class;
356      Container : Set);
357
358   for Set'Write use Write;
359
360   procedure Read
361     (Stream    : not null access Root_Stream_Type'Class;
362      Container : out Set);
363
364   for Set'Read use Read;
365
366   type Set_Access is access all Set;
367   for Set_Access'Storage_Size use 0;
368
369   --  Note: If a Cursor object has no explicit initialization expression,
370   --  it must default initialize to the same value as constant No_Element.
371   --  The Node component of type Cursor has scalar type Count_Type, so it
372   --  requires an explicit initialization expression of its own declaration,
373   --  in order for objects of record type Cursor to properly initialize.
374
375   type Cursor is record
376      Container : Set_Access;
377      Node      : Count_Type := 0;
378   end record;
379
380   procedure Write
381     (Stream : not null access Root_Stream_Type'Class;
382      Item   : Cursor);
383
384   for Cursor'Write use Write;
385
386   procedure Read
387     (Stream : not null access Root_Stream_Type'Class;
388      Item   : out Cursor);
389
390   for Cursor'Read use Read;
391
392   subtype Reference_Control_Type is Implementation.Reference_Control_Type;
393   --  It is necessary to rename this here, so that the compiler can find it
394
395   type Constant_Reference_Type
396     (Element : not null access constant Element_Type) is
397      record
398         Control : Reference_Control_Type :=
399           raise Program_Error with "uninitialized reference";
400         --  The RM says, "The default initialization of an object of
401         --  type Constant_Reference_Type or Reference_Type propagates
402         --  Program_Error."
403      end record;
404
405   procedure Read
406     (Stream : not null access Root_Stream_Type'Class;
407      Item   : out Constant_Reference_Type);
408
409   for Constant_Reference_Type'Read use Read;
410
411   procedure Write
412     (Stream : not null access Root_Stream_Type'Class;
413      Item   : Constant_Reference_Type);
414
415   for Constant_Reference_Type'Write use Write;
416
417   --  Three operations are used to optimize in the expansion of "for ... of"
418   --  loops: the Next(Cursor) procedure in the visible part, and the following
419   --  Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for
420   --  details.
421
422   function Pseudo_Reference
423     (Container : aliased Set'Class) return Reference_Control_Type;
424   pragma Inline (Pseudo_Reference);
425   --  Creates an object of type Reference_Control_Type pointing to the
426   --  container, and increments the Lock. Finalization of this object will
427   --  decrement the Lock.
428
429   type Element_Access is access all Element_Type with
430     Storage_Size => 0;
431
432   function Get_Element_Access
433     (Position : Cursor) return not null Element_Access;
434   --  Returns a pointer to the element designated by Position.
435
436   Empty_Set : constant Set := Set'(Tree_Type with Capacity => 0);
437
438   No_Element : constant Cursor := Cursor'(null, 0);
439
440   type Iterator is new Limited_Controlled and
441     Set_Iterator_Interfaces.Reversible_Iterator with
442   record
443      Container : Set_Access;
444      Node      : Count_Type;
445   end record
446     with Disable_Controlled => not T_Check;
447
448   overriding procedure Finalize (Object : in out Iterator);
449
450   overriding function First (Object : Iterator) return Cursor;
451   overriding function Last  (Object : Iterator) return Cursor;
452
453   overriding function Next
454     (Object   : Iterator;
455      Position : Cursor) return Cursor;
456
457   overriding function Previous
458     (Object   : Iterator;
459      Position : Cursor) return Cursor;
460
461end Ada.Containers.Bounded_Ordered_Sets;
462