1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
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 --
9 -- Copyright (C) 2004-2013, Free Software Foundation, Inc. --
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. --
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. --
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. --
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_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.
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 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
49 -- There are four new functions:
51 -- function Strict_Equal (Left, Right : Map) return Boolean;
52 -- function Overlap (Left, Right : Map) return Boolean;
53 -- function Left (Container : Map; Position : Cursor) return Map;
54 -- function Right (Container : Map; Position : Cursor) return Map;
56 -- See detailed specifications for these subprograms
58 private with Ada
.Containers
.Red_Black_Trees
;
61 type Key_Type
is private;
62 type Element_Type
is private;
64 with function "<" (Left
, Right
: Key_Type
) return Boolean is <>;
65 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
67 package Ada
.Containers
.Formal_Ordered_Maps
is
70 function Equivalent_Keys
(Left
, Right
: Key_Type
) return Boolean;
72 type Map
(Capacity
: Count_Type
) is private;
73 pragma Preelaborable_Initialization
(Map
);
75 type Cursor
is private;
76 pragma Preelaborable_Initialization
(Cursor
);
78 Empty_Map
: constant Map
;
80 No_Element
: constant Cursor
;
82 function "=" (Left
, Right
: Map
) return Boolean;
84 function Length
(Container
: Map
) return Count_Type
;
86 function Is_Empty
(Container
: Map
) return Boolean;
88 procedure Clear
(Container
: in out Map
);
90 procedure Assign
(Target
: in out Map
; Source
: Map
) with
91 Pre
=> Target
.Capacity
>= Length
(Source
);
93 function Copy
(Source
: Map
; Capacity
: Count_Type
:= 0) return Map
with
94 Pre
=> Capacity
>= Source
.Capacity
;
96 function Key
(Container
: Map
; Position
: Cursor
) return Key_Type
with
97 Pre
=> Has_Element
(Container
, Position
);
101 Position
: Cursor
) return Element_Type
103 Pre
=> Has_Element
(Container
, Position
);
105 procedure Replace_Element
106 (Container
: in out Map
;
108 New_Item
: Element_Type
)
110 Pre
=> Has_Element
(Container
, Position
);
112 procedure Move
(Target
: in out Map
; Source
: in out Map
) with
113 Pre
=> Target
.Capacity
>= Length
(Source
);
116 (Container
: in out Map
;
118 New_Item
: Element_Type
;
119 Position
: out Cursor
;
120 Inserted
: out Boolean)
122 Pre
=> Length
(Container
) < Container
.Capacity
;
125 (Container
: in out Map
;
127 New_Item
: Element_Type
)
129 Pre
=> Length
(Container
) < Container
.Capacity
130 and then (not Contains
(Container
, Key
));
133 (Container
: in out Map
;
135 New_Item
: Element_Type
)
137 Pre
=> Length
(Container
) < Container
.Capacity
;
140 (Container
: in out Map
;
142 New_Item
: Element_Type
)
144 Pre
=> Contains
(Container
, Key
);
146 procedure Exclude
(Container
: in out Map
; Key
: Key_Type
);
148 procedure Delete
(Container
: in out Map
; Key
: Key_Type
) with
149 Pre
=> Contains
(Container
, Key
);
151 procedure Delete
(Container
: in out Map
; Position
: in out Cursor
) with
152 Pre
=> Has_Element
(Container
, Position
);
154 procedure Delete_First
(Container
: in out Map
);
156 procedure Delete_Last
(Container
: in out Map
);
158 function First
(Container
: Map
) return Cursor
;
160 function First_Element
(Container
: Map
) return Element_Type
with
161 Pre
=> not Is_Empty
(Container
);
163 function First_Key
(Container
: Map
) return Key_Type
with
164 Pre
=> not Is_Empty
(Container
);
166 function Last
(Container
: Map
) return Cursor
;
168 function Last_Element
(Container
: Map
) return Element_Type
with
169 Pre
=> not Is_Empty
(Container
);
171 function Last_Key
(Container
: Map
) return Key_Type
with
172 Pre
=> not Is_Empty
(Container
);
174 function Next
(Container
: Map
; Position
: Cursor
) return Cursor
with
175 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
177 procedure Next
(Container
: Map
; Position
: in out Cursor
) with
178 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
180 function Previous
(Container
: Map
; Position
: Cursor
) return Cursor
with
181 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
183 procedure Previous
(Container
: Map
; Position
: in out Cursor
) with
184 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
186 function Find
(Container
: Map
; Key
: Key_Type
) return Cursor
;
188 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
with
189 Pre
=> Contains
(Container
, Key
);
191 function Floor
(Container
: Map
; Key
: Key_Type
) return Cursor
;
193 function Ceiling
(Container
: Map
; Key
: Key_Type
) return Cursor
;
195 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean;
197 function Has_Element
(Container
: Map
; Position
: Cursor
) return Boolean;
199 function Strict_Equal
(Left
, Right
: Map
) return Boolean;
200 -- Strict_Equal returns True if the containers are physically equal, i.e.
201 -- they are structurally equal (function "=" returns True) and that they
202 -- have the same set of cursors.
204 function Left
(Container
: Map
; Position
: Cursor
) return Map
with
205 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
206 function Right
(Container
: Map
; Position
: Cursor
) return Map
with
207 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
208 -- Left returns a container containing all elements preceding Position
209 -- (excluded) in Container. Right returns a container containing all
210 -- elements following Position (included) in Container. These two new
211 -- functions can be used to express invariant properties in loops which
212 -- iterate over containers. Left returns the part of the container already
213 -- scanned and Right the part not scanned yet.
215 function Overlap
(Left
, Right
: Map
) return Boolean;
216 -- Overlap returns True if the containers have common keys
219 pragma Inline
(Next
);
220 pragma Inline
(Previous
);
222 subtype Node_Access
is Count_Type
;
226 type Node_Type
is record
227 Has_Element
: Boolean := False;
228 Parent
: Node_Access
:= 0;
229 Left
: Node_Access
:= 0;
230 Right
: Node_Access
:= 0;
231 Color
: Red_Black_Trees
.Color_Type
:= Red
;
233 Element
: Element_Type
;
236 package Tree_Types
is
237 new Ada
.Containers
.Red_Black_Trees
.Generic_Bounded_Tree_Types
(Node_Type
);
239 type Map
(Capacity
: Count_Type
) is
240 new Tree_Types
.Tree_Type
(Capacity
) with null record;
242 type Cursor
is record
246 Empty_Map
: constant Map
:= (Capacity
=> 0, others => <>);
248 No_Element
: constant Cursor
:= (Node
=> 0);
250 end Ada
.Containers
.Formal_Ordered_Maps
;