2014-10-31 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / a-cforse.ads
blobb3e9ff566194e1c1b078b5b85ce7d8334d6c5d26
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-2014, 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 ------------------------------------------------------------------------------
32 -- This spec is derived from package Ada.Containers.Bounded_Ordered_Sets in
33 -- the Ada 2012 RM. The modifications are meant to facilitate formal proofs by
34 -- making it easier to express properties, and by making the specification of
35 -- this unit compatible with SPARK 2014. Note that the API of this unit may be
36 -- subject to incompatible changes as SPARK 2014 evolves.
38 -- The modifications are:
40 -- A parameter for the container is added to every function reading the
41 -- content of a container: Key, Element, Next, Query_Element, Previous,
42 -- Has_Element, Iterate, Reverse_Iterate. This change is motivated by the
43 -- need to have cursors which are valid on different containers (typically
44 -- a container C and its previous version C'Old) for expressing properties,
45 -- which is not possible if cursors encapsulate an access to the underlying
46 -- container. The operators "<" and ">" that could not be modified that way
47 -- have been removed.
49 -- There are three new functions:
51 -- function Strict_Equal (Left, Right : Set) return Boolean;
52 -- function First_To_Previous (Container : Set; Current : Cursor)
53 -- return Set;
54 -- function Current_To_Last (Container : Set; Current : Cursor)
55 -- return Set;
57 -- See detailed specifications for these subprograms
59 private with Ada.Containers.Red_Black_Trees;
61 generic
62 type Element_Type is private;
64 with function "<" (Left, Right : Element_Type) return Boolean is <>;
65 with function "=" (Left, Right : Element_Type) return Boolean is <>;
67 package Ada.Containers.Formal_Ordered_Sets is
68 pragma Annotate (GNATprove, External_Axiomatization);
69 pragma Pure;
70 pragma SPARK_Mode (On);
72 function Equivalent_Elements (Left, Right : Element_Type) return Boolean
73 with
74 Global => null;
76 type Set (Capacity : Count_Type) is private with
77 Iterable => (First => First,
78 Next => Next,
79 Has_Element => Has_Element,
80 Element => Element),
81 Default_Initial_Condition;
82 pragma Preelaborable_Initialization (Set);
84 type Cursor is private;
85 pragma Preelaborable_Initialization (Cursor);
87 Empty_Set : constant Set;
89 No_Element : constant Cursor;
91 function "=" (Left, Right : Set) return Boolean with
92 Global => null;
94 function Equivalent_Sets (Left, Right : Set) return Boolean with
95 Global => null;
97 function To_Set (New_Item : Element_Type) return Set with
98 Global => null;
100 function Length (Container : Set) return Count_Type with
101 Global => null;
103 function Is_Empty (Container : Set) return Boolean with
104 Global => null;
106 procedure Clear (Container : in out Set) with
107 Global => null;
109 procedure Assign (Target : in out Set; Source : Set) with
110 Pre => Target.Capacity >= Length (Source);
112 function Copy (Source : Set; Capacity : Count_Type := 0) return Set with
113 Global => null,
114 Pre => Capacity = 0 or else Capacity >= Source.Capacity;
116 function Element
117 (Container : Set;
118 Position : Cursor) return Element_Type
119 with
120 Global => null,
121 Pre => Has_Element (Container, Position);
123 procedure Replace_Element
124 (Container : in out Set;
125 Position : Cursor;
126 New_Item : Element_Type)
127 with
128 Global => null,
129 Pre => Has_Element (Container, Position);
131 procedure Move (Target : in out Set; Source : in out Set) with
132 Global => null,
133 Pre => Target.Capacity >= Length (Source);
135 procedure Insert
136 (Container : in out Set;
137 New_Item : Element_Type;
138 Position : out Cursor;
139 Inserted : out Boolean)
140 with
141 Global => null,
142 Pre => Length (Container) < Container.Capacity;
144 procedure Insert
145 (Container : in out Set;
146 New_Item : Element_Type)
147 with
148 Global => null,
149 Pre => Length (Container) < Container.Capacity
150 and then (not Contains (Container, New_Item));
152 procedure Include
153 (Container : in out Set;
154 New_Item : Element_Type)
155 with
156 Global => null,
157 Pre => Length (Container) < Container.Capacity;
159 procedure Replace
160 (Container : in out Set;
161 New_Item : Element_Type)
162 with
163 Global => null,
164 Pre => Contains (Container, New_Item);
166 procedure Exclude
167 (Container : in out Set;
168 Item : Element_Type)
169 with
170 Global => null;
172 procedure Delete
173 (Container : in out Set;
174 Item : Element_Type)
175 with
176 Global => null,
177 Pre => Contains (Container, Item);
179 procedure Delete
180 (Container : in out Set;
181 Position : in out Cursor)
182 with
183 Global => null,
184 Pre => Has_Element (Container, Position);
186 procedure Delete_First (Container : in out Set) with
187 Global => null;
189 procedure Delete_Last (Container : in out Set) with
190 Global => null;
192 procedure Union (Target : in out Set; Source : Set) with
193 Global => null,
194 Pre => Length (Target) + Length (Source) -
195 Length (Intersection (Target, Source)) <= Target.Capacity;
197 function Union (Left, Right : Set) return Set with
198 Global => null,
199 Pre => Length (Left) + Length (Right) -
200 Length (Intersection (Left, Right)) <= Count_Type'Last;
202 function "or" (Left, Right : Set) return Set renames Union;
204 procedure Intersection (Target : in out Set; Source : Set) with
205 Global => null;
207 function Intersection (Left, Right : Set) return Set with
208 Global => null;
210 function "and" (Left, Right : Set) return Set renames Intersection;
212 procedure Difference (Target : in out Set; Source : Set) with
213 Global => null;
215 function Difference (Left, Right : Set) return Set with
216 Global => null;
218 function "-" (Left, Right : Set) return Set renames Difference;
220 procedure Symmetric_Difference (Target : in out Set; Source : Set) with
221 Global => null,
222 Pre => Length (Target) + Length (Source) -
223 2 * Length (Intersection (Target, Source)) <= Target.Capacity;
225 function Symmetric_Difference (Left, Right : Set) return Set with
226 Global => null,
227 Pre => Length (Left) + Length (Right) -
228 2 * Length (Intersection (Left, Right)) <= Count_Type'Last;
230 function "xor" (Left, Right : Set) return Set renames Symmetric_Difference;
232 function Overlap (Left, Right : Set) return Boolean with
233 Global => null;
235 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean with
236 Global => null;
238 function First (Container : Set) return Cursor with
239 Global => null;
241 function First_Element (Container : Set) return Element_Type with
242 Global => null,
243 Pre => not Is_Empty (Container);
245 function Last (Container : Set) return Cursor;
247 function Last_Element (Container : Set) return Element_Type with
248 Global => null,
249 Pre => not Is_Empty (Container);
251 function Next (Container : Set; Position : Cursor) return Cursor with
252 Global => null,
253 Pre => Has_Element (Container, Position) or else Position = No_Element;
255 procedure Next (Container : Set; Position : in out Cursor) with
256 Global => null,
257 Pre => Has_Element (Container, Position) or else Position = No_Element;
259 function Previous (Container : Set; Position : Cursor) return Cursor with
260 Global => null,
261 Pre => Has_Element (Container, Position) or else Position = No_Element;
263 procedure Previous (Container : Set; Position : in out Cursor) with
264 Global => null,
265 Pre => Has_Element (Container, Position) or else Position = No_Element;
267 function Find (Container : Set; Item : Element_Type) return Cursor with
268 Global => null;
270 function Floor (Container : Set; Item : Element_Type) return Cursor with
271 Global => null;
273 function Ceiling (Container : Set; Item : Element_Type) return Cursor with
274 Global => null;
276 function Contains (Container : Set; Item : Element_Type) return Boolean with
277 Global => null;
279 function Has_Element (Container : Set; Position : Cursor) return Boolean
280 with
281 Global => null;
283 generic
284 type Key_Type (<>) is private;
286 with function Key (Element : Element_Type) return Key_Type;
288 with function "<" (Left, Right : Key_Type) return Boolean is <>;
290 package Generic_Keys is
292 function Equivalent_Keys (Left, Right : Key_Type) return Boolean with
293 Global => null;
295 function Key (Container : Set; Position : Cursor) return Key_Type with
296 Global => null;
298 function Element (Container : Set; Key : Key_Type) return Element_Type
299 with
300 Global => null;
302 procedure Replace
303 (Container : in out Set;
304 Key : Key_Type;
305 New_Item : Element_Type)
306 with
307 Global => null;
309 procedure Exclude (Container : in out Set; Key : Key_Type) with
310 Global => null;
312 procedure Delete (Container : in out Set; Key : Key_Type) with
313 Global => null;
315 function Find (Container : Set; Key : Key_Type) return Cursor with
316 Global => null;
318 function Floor (Container : Set; Key : Key_Type) return Cursor with
319 Global => null;
321 function Ceiling (Container : Set; Key : Key_Type) return Cursor with
322 Global => null;
324 function Contains (Container : Set; Key : Key_Type) return Boolean with
325 Global => null;
327 end Generic_Keys;
329 function Strict_Equal (Left, Right : Set) return Boolean with
330 Global => null;
331 -- Strict_Equal returns True if the containers are physically equal, i.e.
332 -- they are structurally equal (function "=" returns True) and that they
333 -- have the same set of cursors.
335 function First_To_Previous (Container : Set; Current : Cursor) return Set
336 with
337 Global => null,
338 Pre => Has_Element (Container, Current) or else Current = No_Element;
339 function Current_To_Last (Container : Set; Current : Cursor) return Set
340 with
341 Global => null,
342 Pre => Has_Element (Container, Current) or else Current = No_Element;
343 -- First_To_Previous returns a container containing all elements preceding
344 -- Current (excluded) in Container. Current_To_Last returns a container
345 -- containing all elements following Current (included) in Container.
346 -- These two new functions can be used to express invariant properties in
347 -- loops which iterate over containers. First_To_Previous returns the part
348 -- of the container already scanned and Current_To_Last the part not
349 -- scanned yet.
351 private
352 pragma Inline (Next);
353 pragma Inline (Previous);
354 pragma SPARK_Mode (Off);
356 type Node_Type is record
357 Has_Element : Boolean := False;
358 Parent : Count_Type := 0;
359 Left : Count_Type := 0;
360 Right : Count_Type := 0;
361 Color : Red_Black_Trees.Color_Type;
362 Element : Element_Type;
363 end record;
365 package Tree_Types is
366 new Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
368 type Set (Capacity : Count_Type) is
369 new Tree_Types.Tree_Type (Capacity) with null record;
371 use Red_Black_Trees;
373 type Cursor is record
374 Node : Count_Type;
375 end record;
377 No_Element : constant Cursor := (Node => 0);
379 Empty_Set : constant Set := (Capacity => 0, others => <>);
381 end Ada.Containers.Formal_Ordered_Sets;