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