1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.FORMAL_DOUBLY_LINKED_LISTS --
9 -- Copyright (C) 2004-2010, 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;
58 type Element_Type is private;
60 with function "=" (Left, Right : Element_Type)
63 package Ada.Containers.Formal_Doubly_Linked_Lists is
66 type List (Capacity : Count_Type) is tagged private;
67 -- pragma Preelaborable_Initialization (List);
69 type Cursor is private;
70 pragma Preelaborable_Initialization (Cursor);
72 Empty_List : constant List;
74 No_Element : constant Cursor;
76 function "=" (Left, Right : List) return Boolean;
78 function Length (Container : List) return Count_Type;
80 function Is_Empty (Container : List) return Boolean;
82 procedure Clear (Container : in out List);
84 procedure Assign (Target : in out List; Source : List);
86 function Copy (Source : List; Capacity : Count_Type := 0) return List;
88 function Element (Container : List; Position : Cursor) return Element_Type;
90 procedure Replace_Element
91 (Container : in out List;
93 New_Item : Element_Type);
95 procedure Query_Element
96 (Container : List; Position : Cursor;
97 Process : not null access procedure (Element : Element_Type));
99 procedure Update_Element
100 (Container : in out List;
102 Process : not null access procedure (Element : in out Element_Type));
104 procedure Move (Target : in out List; Source : in out List);
107 (Container : in out List;
109 New_Item : Element_Type;
110 Count : Count_Type := 1);
113 (Container : in out List;
115 New_Item : Element_Type;
116 Position : out Cursor;
117 Count : Count_Type := 1);
120 (Container : in out List;
122 Position : out Cursor;
123 Count : Count_Type := 1);
126 (Container : in out List;
127 New_Item : Element_Type;
128 Count : Count_Type := 1);
131 (Container : in out List;
132 New_Item : Element_Type;
133 Count : Count_Type := 1);
136 (Container : in out List;
137 Position : in out Cursor;
138 Count : Count_Type := 1);
140 procedure Delete_First
141 (Container : in out List;
142 Count : Count_Type := 1);
144 procedure Delete_Last
145 (Container : in out List;
146 Count : Count_Type := 1);
148 procedure Reverse_Elements (Container : in out List);
151 (Container : in out List;
155 (Container : in out List;
159 (Target : in out List;
161 Source : in out List);
164 (Target : in out List;
166 Source : in out List;
167 Position : in out Cursor);
170 (Container : in out List;
174 function First (Container : List) return Cursor;
176 function First_Element (Container : List) return Element_Type;
178 function Last (Container : List) return Cursor;
180 function Last_Element (Container : List) return Element_Type;
182 function Next (Container : List; Position : Cursor) return Cursor;
184 procedure Next (Container : List; Position : in out Cursor);
186 function Previous (Container : List; Position : Cursor) return Cursor;
188 procedure Previous (Container : List; Position : in out Cursor);
193 Position : Cursor := No_Element) return Cursor;
195 function Reverse_Find
198 Position : Cursor := No_Element) return Cursor;
202 Item : Element_Type) return Boolean;
204 function Has_Element (Container : List; Position : Cursor) return Boolean;
209 not null access procedure (Container : List; Position : Cursor));
211 procedure Reverse_Iterate
214 not null access procedure (Container : List; Position : Cursor));
217 with function "<" (Left, Right : Element_Type) return Boolean is <>;
218 package Generic_Sorting is
220 function Is_Sorted (Container : List) return Boolean;
222 procedure Sort (Container : in out List);
224 procedure Merge (Target, Source : in out List);
228 function Strict_Equal (Left, Right : List) return Boolean;
229 -- Strict_Equal returns True if the containers are physically equal, i.e.
230 -- they are structurally equal (function "=" returns True) and that they
231 -- have the same set of cursors.
233 function Left (Container : List; Position : Cursor) return List;
234 function Right (Container : List; Position : Cursor) return List;
235 -- Left returns a container containing all elements preceding Position
236 -- (excluded) in Container. Right returns a container containing all
237 -- elements following Position (included) in Container. These two new
238 -- functions can be used to express invariant properties in loops which
239 -- iterate over containers. Left returns the part of the container already
240 -- scanned and Right the part not scanned yet.
244 type Node_Type is record
245 Prev : Count_Type'Base := -1;
247 Element : Element_Type;
249 function "=" (L, R : Node_Type) return Boolean is abstract;
251 type Node_Array is array (Count_Type range <>) of Node_Type;
252 function "=" (L, R : Node_Array) return Boolean is abstract;
254 type List_Access is access all List;
255 for List_Access'Storage_Size use 0;
257 type Kind is (Plain, Part);
259 type Plain_List (Capacity : Count_Type) is record
260 Nodes : Node_Array (1 .. Capacity) := (others => <>);
261 Free : Count_Type'Base := -1;
266 type PList_Access is access Plain_List;
268 type Part_List is record
269 LLength : Count_Type := 0;
270 LFirst : Count_Type := 0;
271 LLast : Count_Type := 0;
274 type List (Capacity : Count_Type) is tagged record
276 Length : Count_Type := 0;
277 First : Count_Type := 0;
278 Last : Count_Type := 0;
280 Plain : PList_Access := new Plain_List'(Capacity, others => <>);
286 (Stream : not null access Root_Stream_Type'Class;
289 for List'Read use Read;
292 (Stream : not null access Root_Stream_Type'Class;
295 for List'Write use Write;
297 type Cursor is record
298 Node : Count_Type := 0;
302 (Stream : not null access Root_Stream_Type'Class;
305 for Cursor'Read use Read;
308 (Stream : not null access Root_Stream_Type'Class;
311 for Cursor'Write use Write;
313 Empty_List : constant List := (0, others => <>);
315 No_Element : constant Cursor := (Node => 0);
317 end Ada.Containers.Formal_Doubly_Linked_Lists;