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 _ H A S H E D _ M A P S     --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2013, 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_Hashed_Maps in the
33--  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.
37
38--  The modifications are:
39
40--    A parameter for the container is added to every function reading the
41--    contents of a container: Key, Element, Next, Query_Element, Has_Element,
42--    Iterate, Equivalent_Keys. This change is motivated by the need to have
43--    cursors which are valid on different containers (typically a container C
44--    and its previous version C'Old) for expressing properties, which is not
45--    possible if cursors encapsulate an access to the underlying container.
46
47--    There are four new functions:
48
49--      function Strict_Equal (Left, Right : Map) return Boolean;
50--      function Overlap (Left, Right : Map) return Boolean;
51--      function First_To_Previous (Container : Map; Current : Cursor)
52--         return Map;
53--      function Current_To_Last (Container : Map; Current : Cursor)
54--         return Map;
55
56--    See detailed specifications for these subprograms
57
58private with Ada.Containers.Hash_Tables;
59
60generic
61   type Key_Type is private;
62   type Element_Type is private;
63
64   with function Hash (Key : Key_Type) return Hash_Type;
65   with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
66   with function "=" (Left, Right : Element_Type) return Boolean is <>;
67
68package Ada.Containers.Formal_Hashed_Maps is
69   pragma Annotate (GNATprove, External_Axiomatization);
70   pragma Pure;
71
72   type Map (Capacity : Count_Type; Modulus : Hash_Type) is private with
73     Iterable => (First       => First,
74                  Next        => Next,
75                  Has_Element => Has_Element,
76                  Element     => Element);
77   pragma Preelaborable_Initialization (Map);
78
79   type Cursor is private;
80   pragma Preelaborable_Initialization (Cursor);
81
82   Empty_Map : constant Map;
83
84   No_Element : constant Cursor;
85
86   function "=" (Left, Right : Map) return Boolean with
87     Global => null;
88
89   function Capacity (Container : Map) return Count_Type with
90     Global => null;
91
92   procedure Reserve_Capacity
93     (Container : in out Map;
94      Capacity  : Count_Type)
95   with
96     Global => null,
97     Pre    => Capacity <= Container.Capacity;
98
99   function Length (Container : Map) return Count_Type with
100     Global => null;
101
102   function Is_Empty (Container : Map) return Boolean with
103     Global => null;
104
105   procedure Clear (Container : in out Map) with
106     Global => null;
107
108   procedure Assign (Target : in out Map; Source : Map) with
109     Global => null,
110     Pre    => Target.Capacity >= Length (Source);
111
112   function Copy
113     (Source   : Map;
114      Capacity : Count_Type := 0) return Map
115   with
116     Global => null,
117     Pre    => Capacity = 0 or else Capacity >= Source.Capacity;
118   --  Copy returns a container stricty equal to Source. It must have
119   --  the same cursors associated with each element. Therefore:
120   --  - capacity=0 means use container.capacity as capacity of target
121   --  - the modulus cannot be changed.
122
123   function Key (Container : Map; Position : Cursor) return Key_Type with
124     Global => null,
125     Pre    => Has_Element (Container, Position);
126
127   function Element
128     (Container : Map;
129      Position  : Cursor) return Element_Type
130   with
131     Global => null,
132     Pre    => Has_Element (Container, Position);
133
134   procedure Replace_Element
135     (Container : in out Map;
136      Position  : Cursor;
137      New_Item  : Element_Type)
138   with
139     Global => null,
140     Pre    => Has_Element (Container, Position);
141
142   procedure Move (Target : in out Map; Source : in out Map) with
143     Global => null,
144     Pre    => Target.Capacity >= Length (Source);
145
146   procedure Insert
147     (Container : in out Map;
148      Key       : Key_Type;
149      New_Item  : Element_Type;
150      Position  : out Cursor;
151      Inserted  : out Boolean)
152   with
153     Global => null,
154     Pre    => Length (Container) < Container.Capacity;
155
156   procedure Insert
157     (Container : in out Map;
158      Key       : Key_Type;
159      New_Item  : Element_Type)
160   with
161     Global => null,
162     Pre    => Length (Container) < Container.Capacity
163                 and then (not Contains (Container, Key));
164
165   procedure Include
166     (Container : in out Map;
167      Key       : Key_Type;
168      New_Item  : Element_Type)
169   with
170     Global => null,
171     Pre    => Length (Container) < Container.Capacity;
172
173   procedure Replace
174     (Container : in out Map;
175      Key       : Key_Type;
176      New_Item  : Element_Type)
177   with
178     Global => null,
179     Pre    => Contains (Container, Key);
180
181   procedure Exclude (Container : in out Map; Key : Key_Type) with
182     Global => null;
183
184   procedure Delete (Container : in out Map; Key : Key_Type) with
185     Global => null,
186     Pre    => Contains (Container, Key);
187
188   procedure Delete (Container : in out Map; Position : in out Cursor) with
189     Global => null,
190     Pre    => Has_Element (Container, Position);
191
192   function First (Container : Map) return Cursor with
193     Global => null;
194
195   function Next (Container : Map; Position : Cursor) return Cursor with
196     Global => null,
197     Pre    => Has_Element (Container, Position) or else Position = No_Element;
198
199   procedure Next (Container : Map; Position : in out Cursor) with
200     Global => null,
201     Pre    => Has_Element (Container, Position) or else Position = No_Element;
202
203   function Find (Container : Map; Key : Key_Type) return Cursor with
204     Global => null;
205
206   function Contains (Container : Map; Key : Key_Type) return Boolean with
207     Global => null;
208
209   function Element (Container : Map; Key : Key_Type) return Element_Type with
210     Global => null,
211     Pre    => Contains (Container, Key);
212
213   function Has_Element (Container : Map; Position : Cursor) return Boolean
214   with
215     Global => null;
216
217   function Equivalent_Keys
218     (Left   : Map;
219      CLeft  : Cursor;
220      Right  : Map;
221      CRight : Cursor) return Boolean
222   with
223     Global => null;
224
225   function Equivalent_Keys
226     (Left  : Map;
227      CLeft : Cursor;
228      Right : Key_Type) return Boolean
229   with
230     Global => null;
231
232   function Equivalent_Keys
233     (Left   : Key_Type;
234      Right  : Map;
235      CRight : Cursor) return Boolean
236   with
237     Global => null;
238
239   function Default_Modulus (Capacity : Count_Type) return Hash_Type with
240     Global => null;
241
242   function Strict_Equal (Left, Right : Map) return Boolean with
243     Global => null;
244   --  Strict_Equal returns True if the containers are physically equal, i.e.
245   --  they are structurally equal (function "=" returns True) and that they
246   --  have the same set of cursors.
247
248   function First_To_Previous (Container : Map; Current : Cursor) return Map
249   with
250     Global => null,
251     Pre    => Has_Element (Container, Current) or else Current = No_Element;
252   function Current_To_Last (Container : Map; Current : Cursor) return Map
253   with
254     Global => null,
255     Pre    => Has_Element (Container, Current) or else Current = No_Element;
256   --  First_To_Previous returns a container containing all elements preceding
257   --  Current (excluded) in Container. Current_To_Last returns a container
258   --  containing all elements following Current (included) in Container.
259   --  These two new functions can be used to express invariant properties in
260   --  loops which iterate over containers. First_To_Previous returns the part
261   --  of the container already scanned and Current_To_Last the part not
262   --  scanned yet.
263
264   function Overlap (Left, Right : Map) return Boolean with
265     Global => null;
266   --  Overlap returns True if the containers have common keys
267
268private
269   pragma Inline (Length);
270   pragma Inline (Is_Empty);
271   pragma Inline (Clear);
272   pragma Inline (Key);
273   pragma Inline (Element);
274   pragma Inline (Contains);
275   pragma Inline (Capacity);
276   pragma Inline (Has_Element);
277   pragma Inline (Equivalent_Keys);
278   pragma Inline (Next);
279
280   type Node_Type is record
281      Key         : Key_Type;
282      Element     : Element_Type;
283      Next        : Count_Type;
284      Has_Element : Boolean := False;
285   end record;
286
287   package HT_Types is new
288     Ada.Containers.Hash_Tables.Generic_Bounded_Hash_Table_Types
289       (Node_Type);
290
291   type Map (Capacity : Count_Type; Modulus : Hash_Type) is
292      new HT_Types.Hash_Table_Type (Capacity, Modulus) with null record;
293
294   use HT_Types;
295
296   type Cursor is record
297      Node : Count_Type;
298   end record;
299
300   Empty_Map : constant Map := (Capacity => 0, Modulus => 0, others => <>);
301
302   No_Element : constant Cursor := (Node => 0);
303
304end Ada.Containers.Formal_Hashed_Maps;
305