OSDN Git Service

2011-08-02 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-cforse.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --   A D A . C O N T A I N E R S . F O R M A L _ O R D E R E D _ S E T S    --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2010, 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
32 --  This spec is derived from package Ada.Containers.Bounded_Ordered_Sets in
33 --  the Ada 2012 RM. The modifications are to facilitate formal proofs by
34 --  making it easier to express properties.
35
36 --  The modifications are:
37
38 --    A parameter for the container is added to every function reading the
39 --    content of a container: Key, Element, Next, Query_Element, Previous,
40 --    Has_Element, Iterate, Reverse_Iterate. This change is motivated by the
41 --    need to have cursors which are valid on different containers (typically
42 --    a container C and its previous version C'Old) for expressing properties,
43 --    which is not possible if cursors encapsulate an access to the underlying
44 --    container. The operators "<" and ">" that could not be modified that way
45 --    have been removed.
46
47 --    There are two new functions:
48
49 --      function Left  (Container : Set; Position : Cursor) return Set;
50 --      function Right (Container : Set; Position : Cursor) return Set;
51
52 --      Left returns a container containing all elements preceding Position
53 --      (excluded) in Container. Right returns a container containing all
54 --      elements following Position (included) in Container. These two new
55 --      functions are useful to express invariant properties in loops which
56 --      iterate over containers. Left returns the part of the container already
57 --      scanned and Right the part not scanned yet.
58
59 private with Ada.Containers.Red_Black_Trees;
60 private with Ada.Streams;
61
62 with Ada.Containers;
63
64 generic
65    type Element_Type is private;
66
67    with function "<" (Left, Right : Element_Type) return Boolean is <>;
68    with function "=" (Left, Right : Element_Type) return Boolean is <>;
69
70 package Ada.Containers.Formal_Ordered_Sets is
71    pragma Pure;
72
73    function Equivalent_Elements (Left, Right : Element_Type) return Boolean;
74
75    type Set (Capacity : Count_Type) is tagged private;
76    --  pragma Preelaborable_Initialization (Set);
77
78    type Cursor is private;
79    pragma Preelaborable_Initialization (Cursor);
80
81    Empty_Set : constant Set;
82
83    No_Element : constant Cursor;
84
85    function "=" (Left, Right : Set) return Boolean;
86
87    function Equivalent_Sets (Left, Right : Set) return Boolean;
88
89    function To_Set (New_Item : Element_Type) return Set;
90
91    function Length (Container : Set) return Count_Type;
92
93    function Is_Empty (Container : Set) return Boolean;
94
95    procedure Clear (Container : in out Set);
96
97    procedure Assign (Target : in out Set; Source : Set);
98
99    function Copy (Source : Set; Capacity : Count_Type := 0) return Set;
100
101    function Element (Container : Set; Position : Cursor) return Element_Type;
102
103    procedure Replace_Element
104      (Container : in out Set;
105       Position  : Cursor;
106       New_Item  : Element_Type);
107
108    procedure Query_Element
109      (Container : in out Set;
110       Position  : Cursor;
111       Process   : not null access procedure (Element : Element_Type));
112
113    procedure Move (Target : in out Set; Source : in out Set);
114
115    procedure Insert
116      (Container : in out Set;
117       New_Item  : Element_Type;
118       Position  : out Cursor;
119       Inserted  : out Boolean);
120
121    procedure Insert
122      (Container : in out Set;
123       New_Item  : Element_Type);
124
125    procedure Include
126      (Container : in out Set;
127       New_Item  : Element_Type);
128
129    procedure Replace
130      (Container : in out Set;
131       New_Item  : Element_Type);
132
133    procedure Exclude
134      (Container : in out Set;
135       Item      : Element_Type);
136
137    procedure Delete
138      (Container : in out Set;
139       Item      : Element_Type);
140
141    procedure Delete
142      (Container : in out Set;
143       Position  : in out Cursor);
144
145    procedure Delete_First (Container : in out Set);
146
147    procedure Delete_Last (Container : in out Set);
148
149    procedure Union (Target : in out Set; Source : Set);
150
151    function Union (Left, Right : Set) return Set;
152
153    function "or" (Left, Right : Set) return Set renames Union;
154
155    procedure Intersection (Target : in out Set; Source : Set);
156
157    function Intersection (Left, Right : Set) return Set;
158
159    function "and" (Left, Right : Set) return Set renames Intersection;
160
161    procedure Difference (Target : in out Set; Source : Set);
162
163    function Difference (Left, Right : Set) return Set;
164
165    function "-" (Left, Right : Set) return Set renames Difference;
166
167    procedure Symmetric_Difference (Target : in out Set; Source : Set);
168
169    function Symmetric_Difference (Left, Right : Set) return Set;
170
171    function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
172
173    function Overlap (Left, Right : Set) return Boolean;
174
175    function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
176
177    function First (Container : Set) return Cursor;
178
179    function First_Element (Container : Set) return Element_Type;
180
181    function Last (Container : Set) return Cursor;
182
183    function Last_Element (Container : Set) return Element_Type;
184
185    function Next (Container : Set; Position : Cursor) return Cursor;
186
187    procedure Next (Container : Set; Position : in out Cursor);
188
189    function Previous (Container : Set; Position : Cursor) return Cursor;
190
191    procedure Previous (Container : Set; Position : in out Cursor);
192
193    function Find (Container : Set; Item : Element_Type) return Cursor;
194
195    function Floor (Container : Set; Item : Element_Type) return Cursor;
196
197    function Ceiling (Container : Set; Item : Element_Type) return Cursor;
198
199    function Contains (Container : Set; Item : Element_Type) return Boolean;
200
201    function Has_Element (Container : Set; Position : Cursor) return Boolean;
202
203    procedure Iterate
204      (Container : Set;
205       Process   :
206         not null access procedure (Container : Set; Position : Cursor));
207
208    procedure Reverse_Iterate
209      (Container : Set;
210       Process   : not null access
211                     procedure (Container : Set; Position : Cursor));
212
213    generic
214       type Key_Type (<>) is private;
215
216       with function Key (Element : Element_Type) return Key_Type;
217
218       with function "<" (Left, Right : Key_Type) return Boolean is <>;
219
220    package Generic_Keys is
221
222       function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
223
224       function Key (Container : Set; Position : Cursor) return Key_Type;
225
226       function Element (Container : Set; Key : Key_Type) return Element_Type;
227
228       procedure Replace
229         (Container : in out Set;
230          Key       : Key_Type;
231          New_Item  : Element_Type);
232
233       procedure Exclude (Container : in out Set; Key : Key_Type);
234
235       procedure Delete (Container : in out Set; Key : Key_Type);
236
237       function Find (Container : Set; Key : Key_Type) return Cursor;
238
239       function Floor (Container : Set; Key : Key_Type) return Cursor;
240
241       function Ceiling (Container : Set; Key : Key_Type) return Cursor;
242
243       function Contains (Container : Set; Key : Key_Type) return Boolean;
244
245       procedure Update_Element_Preserving_Key
246         (Container : in out Set;
247          Position  : Cursor;
248          Process   : not null access
249                        procedure (Element : in out Element_Type));
250
251    end Generic_Keys;
252
253    function Strict_Equal (Left, Right : Set) return Boolean;
254
255    function Left (Container : Set; Position : Cursor) return Set;
256
257    function Right (Container : Set; Position : Cursor) return Set;
258
259 private
260
261    pragma Inline (Next);
262    pragma Inline (Previous);
263
264    type Node_Type is record
265       Has_Element : Boolean := False;
266       Parent  : Count_Type;
267       Left    : Count_Type;
268       Right   : Count_Type;
269       Color   : Red_Black_Trees.Color_Type;
270       Element : Element_Type;
271    end record;
272
273    type Kind is (Plain, Part);
274
275    package Tree_Types is
276      new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
277
278    type Tree_Type_Access is access all Tree_Types.Tree_Type;
279
280    type Set (Capacity : Count_Type) is tagged record
281       Tree   : Tree_Type_Access := new Tree_Types.Tree_Type (Capacity);
282       K      : Kind := Plain;
283       Length : Count_Type := 0;
284       First  : Count_Type := 0;
285       Last   : Count_Type := 0;
286    end record;
287
288    use Red_Black_Trees;
289    use Ada.Streams;
290
291    type Set_Access is access all Set;
292    for Set_Access'Storage_Size use 0;
293
294    type Cursor is record
295       Node : Count_Type;
296    end record;
297
298    procedure Write
299      (Stream : not null access Root_Stream_Type'Class;
300       Item   : Cursor);
301
302    for Cursor'Write use Write;
303
304    procedure Read
305      (Stream : not null access Root_Stream_Type'Class;
306       Item   : out Cursor);
307
308    for Cursor'Read use Read;
309
310    No_Element : constant Cursor := (Node => 0);
311
312    procedure Write
313      (Stream    : not null access Root_Stream_Type'Class;
314       Container : Set);
315
316    for Set'Write use Write;
317
318    procedure Read
319      (Stream    : not null access Root_Stream_Type'Class;
320       Container : out Set);
321
322    for Set'Read use Read;
323
324    Empty_Set : constant Set := (Capacity => 0, others => <>);
325
326 end Ada.Containers.Formal_Ordered_Sets;