1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.FORMAL_DOUBLY_LINKED_LISTS --
9 -- Copyright (C) 2004-2012, 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 to facilitate formal proofs by making it
34 -- easier to express properties.
36 -- The modifications are:
38 -- A parameter for the container is added to every function reading the
39 -- contents of a container: Next, Previous, Query_Element, Has_Element,
40 -- Iterate, Reverse_Iterate, Element. This change is motivated by the need
41 -- to have cursors which are valid on different containers (typically a
42 -- container C and its previous version C'Old) for expressing properties,
43 -- which is not possible if cursors encapsulate an access to the underlying
46 -- There are three new functions:
48 -- function Strict_Equal (Left, Right : List) return Boolean;
49 -- function Left (Container : List; Position : Cursor) return List;
50 -- function Right (Container : List; Position : Cursor) return List;
52 -- See detailed specifications for these subprograms
54 private with Ada
.Streams
;
56 with Ada
.Iterator_Interfaces
;
59 type Element_Type
is private;
61 with function "=" (Left
, Right
: Element_Type
)
64 package Ada
.Containers
.Formal_Doubly_Linked_Lists
is
67 type List
(Capacity
: Count_Type
) is tagged private with
68 Constant_Indexing
=> Constant_Reference
,
69 Default_Iterator
=> Iterate
,
70 Iterator_Element
=> Element_Type
;
71 -- pragma Preelaborable_Initialization (List);
73 type Cursor
is private;
74 pragma Preelaborable_Initialization
(Cursor
);
76 Empty_List
: constant List
;
78 No_Element
: constant Cursor
;
80 function Not_No_Element
(Position
: Cursor
) return Boolean;
82 package List_Iterator_Interfaces
is new
83 Ada
.Iterator_Interfaces
(Cursor
=> Cursor
, Has_Element
=> Not_No_Element
);
85 function Iterate
(Container
: List
; Start
: Cursor
)
86 return List_Iterator_Interfaces
.Reversible_Iterator
'Class;
88 function Iterate
(Container
: List
)
89 return List_Iterator_Interfaces
.Reversible_Iterator
'Class;
91 function "=" (Left
, Right
: List
) return Boolean;
93 function Length
(Container
: List
) return Count_Type
;
95 function Is_Empty
(Container
: List
) return Boolean;
97 procedure Clear
(Container
: in out List
);
99 procedure Assign
(Target
: in out List
; Source
: List
);
101 function Copy
(Source
: List
; Capacity
: Count_Type
:= 0) return List
;
103 function Element
(Container
: List
; Position
: Cursor
) return Element_Type
;
105 procedure Replace_Element
106 (Container
: in out List
;
108 New_Item
: Element_Type
);
110 procedure Query_Element
111 (Container
: List
; Position
: Cursor
;
112 Process
: not null access procedure (Element
: Element_Type
));
114 procedure Update_Element
115 (Container
: in out List
;
117 Process
: not null access procedure (Element
: in out Element_Type
));
119 procedure Move
(Target
: in out List
; Source
: in out List
);
122 (Container
: in out List
;
124 New_Item
: Element_Type
;
125 Count
: Count_Type
:= 1);
128 (Container
: in out List
;
130 New_Item
: Element_Type
;
131 Position
: out Cursor
;
132 Count
: Count_Type
:= 1);
135 (Container
: in out List
;
137 Position
: out Cursor
;
138 Count
: Count_Type
:= 1);
141 (Container
: in out List
;
142 New_Item
: Element_Type
;
143 Count
: Count_Type
:= 1);
146 (Container
: in out List
;
147 New_Item
: Element_Type
;
148 Count
: Count_Type
:= 1);
151 (Container
: in out List
;
152 Position
: in out Cursor
;
153 Count
: Count_Type
:= 1);
155 procedure Delete_First
156 (Container
: in out List
;
157 Count
: Count_Type
:= 1);
159 procedure Delete_Last
160 (Container
: in out List
;
161 Count
: Count_Type
:= 1);
163 procedure Reverse_Elements
(Container
: in out List
);
166 (Container
: in out List
;
170 (Container
: in out List
;
174 (Target
: in out List
;
176 Source
: in out List
);
179 (Target
: in out List
;
181 Source
: in out List
;
182 Position
: in out Cursor
);
185 (Container
: in out List
;
189 function First
(Container
: List
) return Cursor
;
191 function First_Element
(Container
: List
) return Element_Type
;
193 function Last
(Container
: List
) return Cursor
;
195 function Last_Element
(Container
: List
) return Element_Type
;
197 function Next
(Container
: List
; Position
: Cursor
) return Cursor
;
199 procedure Next
(Container
: List
; Position
: in out Cursor
);
201 function Previous
(Container
: List
; Position
: Cursor
) return Cursor
;
203 procedure Previous
(Container
: List
; Position
: in out Cursor
);
208 Position
: Cursor
:= No_Element
) return Cursor
;
210 function Reverse_Find
213 Position
: Cursor
:= No_Element
) return Cursor
;
217 Item
: Element_Type
) return Boolean;
219 function Has_Element
(Container
: List
; Position
: Cursor
) return Boolean;
224 not null access procedure (Container
: List
; Position
: Cursor
));
226 procedure Reverse_Iterate
229 not null access procedure (Container
: List
; Position
: Cursor
));
232 with function "<" (Left
, Right
: Element_Type
) return Boolean is <>;
233 package Generic_Sorting
is
235 function Is_Sorted
(Container
: List
) return Boolean;
237 procedure Sort
(Container
: in out List
);
239 procedure Merge
(Target
, Source
: in out List
);
243 type Constant_Reference_Type
244 (Element
: not null access constant Element_Type
) is private
246 Implicit_Dereference
=> Element
;
248 function Constant_Reference
249 (Container
: List
; -- SHOULD BE ALIASED ???
250 Position
: Cursor
) return Constant_Reference_Type
;
252 function Strict_Equal
(Left
, Right
: List
) return Boolean;
253 -- Strict_Equal returns True if the containers are physically equal, i.e.
254 -- they are structurally equal (function "=" returns True) and that they
255 -- have the same set of cursors.
257 function Left
(Container
: List
; Position
: Cursor
) return List
;
258 function Right
(Container
: List
; Position
: Cursor
) return List
;
259 -- Left returns a container containing all elements preceding Position
260 -- (excluded) in Container. Right returns a container containing all
261 -- elements following Position (included) in Container. These two new
262 -- functions can be used to express invariant properties in loops which
263 -- iterate over containers. Left returns the part of the container already
264 -- scanned and Right the part not scanned yet.
268 type Node_Type
is record
269 Prev
: Count_Type
'Base := -1;
271 Element
: aliased Element_Type
;
274 function "=" (L
, R
: Node_Type
) return Boolean is abstract;
276 type Node_Array
is array (Count_Type
range <>) of Node_Type
;
277 function "=" (L
, R
: Node_Array
) return Boolean is abstract;
279 type List
(Capacity
: Count_Type
) is tagged record
280 Nodes
: Node_Array
(1 .. Capacity
) := (others => <>);
281 Free
: Count_Type
'Base := -1;
284 Length
: Count_Type
:= 0;
285 First
: Count_Type
:= 0;
286 Last
: Count_Type
:= 0;
292 (Stream
: not null access Root_Stream_Type
'Class;
295 for List
'Read use Read
;
298 (Stream
: not null access Root_Stream_Type
'Class;
301 for List
'Write use Write
;
303 type List_Access
is access all List
;
304 for List_Access
'Storage_Size use 0;
306 type Cursor
is record
307 Node
: Count_Type
:= 0;
310 type Constant_Reference_Type
311 (Element
: not null access constant Element_Type
) is null record;
314 (Stream
: not null access Root_Stream_Type
'Class;
317 for Cursor
'Read use Read
;
320 (Stream
: not null access Root_Stream_Type
'Class;
323 for Cursor
'Write use Write
;
325 Empty_List
: constant List
:= (0, others => <>);
327 No_Element
: constant Cursor
:= (Node
=> 0);
329 end Ada
.Containers
.Formal_Doubly_Linked_Lists
;