1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.FORMAL_DOUBLY_LINKED_LISTS --
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 Ada.Containers.Bounded_Doubly_Linked_Lists 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.
38 -- The modifications are:
40 -- A parameter for the container is added to every function reading the
41 -- contents of a container: Next, Previous, Query_Element, Has_Element,
42 -- Iterate, Reverse_Iterate, Element. This change is motivated by the need
43 -- 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
48 -- There are three new functions:
50 -- function Strict_Equal (Left, Right : List) return Boolean;
51 -- function First_To_Previous (Container : List; Current : Cursor)
53 -- function Current_To_Last (Container : List; Current : Cursor)
56 -- See subprogram specifications that follow for details
59 type Element_Type
is private;
61 with function "=" (Left
, Right
: Element_Type
)
64 package Ada
.Containers
.Formal_Doubly_Linked_Lists
is
65 pragma Annotate
(GNATprove
, External_Axiomatization
);
68 type List
(Capacity
: Count_Type
) is private with
69 Iterable
=> (First
=> First
,
71 Has_Element
=> Has_Element
,
73 pragma Preelaborable_Initialization
(List
);
75 type Cursor
is private;
76 pragma Preelaborable_Initialization
(Cursor
);
78 Empty_List
: constant List
;
80 No_Element
: constant Cursor
;
82 function "=" (Left
, Right
: List
) return Boolean with
85 function Length
(Container
: List
) return Count_Type
with
88 function Is_Empty
(Container
: List
) return Boolean with
91 procedure Clear
(Container
: in out List
) with
94 procedure Assign
(Target
: in out List
; Source
: List
) with
96 Pre
=> Target
.Capacity
>= Length
(Source
);
98 function Copy
(Source
: List
; Capacity
: Count_Type
:= 0) return List
with
100 Pre
=> Capacity
= 0 or else Capacity
>= Source
.Capacity
;
104 Position
: Cursor
) return Element_Type
107 Pre
=> Has_Element
(Container
, Position
);
109 procedure Replace_Element
110 (Container
: in out List
;
112 New_Item
: Element_Type
)
115 Pre
=> Has_Element
(Container
, Position
);
117 procedure Move
(Target
: in out List
; Source
: in out List
) with
119 Pre
=> Target
.Capacity
>= Length
(Source
);
122 (Container
: in out List
;
124 New_Item
: Element_Type
;
125 Count
: Count_Type
:= 1)
128 Pre
=> Length
(Container
) + Count
<= Container
.Capacity
129 and then (Has_Element
(Container
, Before
)
130 or else Before
= No_Element
);
133 (Container
: in out List
;
135 New_Item
: Element_Type
;
136 Position
: out Cursor
;
137 Count
: Count_Type
:= 1)
140 Pre
=> Length
(Container
) + Count
<= Container
.Capacity
141 and then (Has_Element
(Container
, Before
)
142 or else Before
= No_Element
);
145 (Container
: in out List
;
147 Position
: out Cursor
;
148 Count
: Count_Type
:= 1)
151 Pre
=> Length
(Container
) + Count
<= Container
.Capacity
152 and then (Has_Element
(Container
, Before
)
153 or else Before
= No_Element
);
156 (Container
: in out List
;
157 New_Item
: Element_Type
;
158 Count
: Count_Type
:= 1)
161 Pre
=> Length
(Container
) + Count
<= Container
.Capacity
;
164 (Container
: in out List
;
165 New_Item
: Element_Type
;
166 Count
: Count_Type
:= 1)
169 Pre
=> Length
(Container
) + Count
<= Container
.Capacity
;
172 (Container
: in out List
;
173 Position
: in out Cursor
;
174 Count
: Count_Type
:= 1)
177 Pre
=> Has_Element
(Container
, Position
);
179 procedure Delete_First
180 (Container
: in out List
;
181 Count
: Count_Type
:= 1)
185 procedure Delete_Last
186 (Container
: in out List
;
187 Count
: Count_Type
:= 1)
191 procedure Reverse_Elements
(Container
: in out List
) with
195 (Container
: in out List
;
199 Pre
=> Has_Element
(Container
, I
) and then Has_Element
(Container
, J
);
202 (Container
: in out List
;
206 Pre
=> Has_Element
(Container
, I
) and then Has_Element
(Container
, J
);
209 (Target
: in out List
;
211 Source
: in out List
)
214 Pre
=> Length
(Source
) + Length
(Target
) <= Target
.Capacity
215 and then (Has_Element
(Target
, Before
)
216 or else Before
= No_Element
);
219 (Target
: in out List
;
221 Source
: in out List
;
222 Position
: in out Cursor
)
225 Pre
=> Length
(Source
) + Length
(Target
) <= Target
.Capacity
226 and then (Has_Element
(Target
, Before
)
227 or else Before
= No_Element
)
228 and then Has_Element
(Source
, Position
);
231 (Container
: in out List
;
236 Pre
=> 2 * Length
(Container
) <= Container
.Capacity
237 and then (Has_Element
(Container
, Before
)
238 or else Before
= No_Element
)
239 and then Has_Element
(Container
, Position
);
241 function First
(Container
: List
) return Cursor
with
244 function First_Element
(Container
: List
) return Element_Type
with
246 Pre
=> not Is_Empty
(Container
);
248 function Last
(Container
: List
) return Cursor
with
251 function Last_Element
(Container
: List
) return Element_Type
with
253 Pre
=> not Is_Empty
(Container
);
255 function Next
(Container
: List
; Position
: Cursor
) return Cursor
with
257 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
259 procedure Next
(Container
: List
; Position
: in out Cursor
) with
261 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
263 function Previous
(Container
: List
; Position
: Cursor
) return Cursor
with
265 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
267 procedure Previous
(Container
: List
; Position
: in out Cursor
) with
269 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
274 Position
: Cursor
:= No_Element
) return Cursor
277 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
279 function Reverse_Find
282 Position
: Cursor
:= No_Element
) return Cursor
285 Pre
=> Has_Element
(Container
, Position
) or else Position
= No_Element
;
289 Item
: Element_Type
) return Boolean
293 function Has_Element
(Container
: List
; Position
: Cursor
) return Boolean
298 with function "<" (Left
, Right
: Element_Type
) return Boolean is <>;
299 package Generic_Sorting
is
301 function Is_Sorted
(Container
: List
) return Boolean with
304 procedure Sort
(Container
: in out List
) with
307 procedure Merge
(Target
, Source
: in out List
) with
312 function Strict_Equal
(Left
, Right
: List
) return Boolean with
314 -- Strict_Equal returns True if the containers are physically equal, i.e.
315 -- they are structurally equal (function "=" returns True) and that they
316 -- have the same set of cursors.
318 function First_To_Previous
(Container
: List
; Current
: Cursor
) return List
321 Pre
=> Has_Element
(Container
, Current
) or else Current
= No_Element
;
322 function Current_To_Last
(Container
: List
; Current
: Cursor
) return List
325 Pre
=> Has_Element
(Container
, Current
) or else Current
= No_Element
;
326 -- First_To_Previous returns a container containing all elements preceding
327 -- Current (excluded) in Container. Current_To_Last returns a container
328 -- containing all elements following Current (included) in Container.
329 -- These two new functions can be used to express invariant properties in
330 -- loops which iterate over containers. First_To_Previous returns the part
331 -- of the container already scanned and Current_To_Last the part not
336 type Node_Type
is record
337 Prev
: Count_Type
'Base := -1;
339 Element
: Element_Type
;
342 function "=" (L
, R
: Node_Type
) return Boolean is abstract;
344 type Node_Array
is array (Count_Type
range <>) of Node_Type
;
345 function "=" (L
, R
: Node_Array
) return Boolean is abstract;
347 type List
(Capacity
: Count_Type
) is tagged record
348 Nodes
: Node_Array
(1 .. Capacity
) := (others => <>);
349 Free
: Count_Type
'Base := -1;
350 Length
: Count_Type
:= 0;
351 First
: Count_Type
:= 0;
352 Last
: Count_Type
:= 0;
355 type Cursor
is record
356 Node
: Count_Type
:= 0;
359 Empty_List
: constant List
:= (0, others => <>);
361 No_Element
: constant Cursor
:= (Node
=> 0);
363 end Ada
.Containers
.Formal_Doubly_Linked_Lists
;