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 _ V E C T O R S --
9 -- Copyright (C) 2004-2011, 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_Vectors in the Ada
33 -- 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 -- content of a container: Element, Next, Query_Element, Previous, Iterate,
40 -- Has_Element, Reverse_Iterate. 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 two new functions:
48 -- function Left (Container : Vector; Position : Cursor) return Vector;
49 -- function Right (Container : Vector; Position : Cursor) return Vector;
51 -- Left returns a container containing all elements preceding Position
52 -- (excluded) in Container. Right returns a container containing all
53 -- elements following Position (included) in Container. These two new
54 -- functions are useful to express invariant properties in loops which
55 -- iterate over containers. Left returns the part of the container already
56 -- scanned and Right the part not scanned yet.
58 private with Ada
.Streams
;
63 type Index_Type
is range <>;
64 type Element_Type
is private;
66 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
68 package Ada
.Containers
.Formal_Vectors
is
71 subtype Extended_Index
is Index_Type
'Base
72 range Index_Type
'First - 1 ..
73 Index_Type
'Min (Index_Type
'Base'Last - 1, Index_Type'Last) + 1;
75 -- ??? i don't think we can do this...
76 -- TODO: we need the ARG to either figure out how to declare this subtype,
77 -- or eliminate the requirement that it be present.
78 -- subtype Capacity_Subtype is Count_Type -- correct name???
79 -- range 0 .. Count_Type'Max (0,
80 -- Index_Type'Pos (Index_Type'Last) -
81 -- Index_Type'Pos (Index_Type'First) + 1);
84 subtype Capacity_Subtype is Count_Type;
86 No_Index : constant Extended_Index := Extended_Index'First;
88 type Vector (Capacity : Capacity_Subtype) is tagged private;
89 -- pragma Preelaborable_Initialization (Vector);
91 type Cursor is private;
92 pragma Preelaborable_Initialization (Cursor);
94 Empty_Vector : constant Vector;
96 No_Element : constant Cursor;
98 function "=" (Left, Right : Vector) return Boolean;
100 function To_Vector (Length : Capacity_Subtype) return Vector;
103 (New_Item : Element_Type;
104 Length : Capacity_Subtype) return Vector;
106 function "&" (Left, Right : Vector) return Vector;
108 function "&" (Left : Vector; Right : Element_Type) return Vector;
110 function "&" (Left : Element_Type; Right : Vector) return Vector;
112 function "&" (Left, Right : Element_Type) return Vector;
114 function Capacity (Container : Vector) return Capacity_Subtype;
116 procedure Reserve_Capacity
117 (Container : in out Vector;
118 Capacity : Capacity_Subtype);
120 function Length (Container : Vector) return Capacity_Subtype;
123 (Container : in out Vector;
124 Length : Capacity_Subtype);
126 function Is_Empty (Container : Vector) return Boolean;
128 procedure Clear (Container : in out Vector);
130 procedure Assign (Target : in out Vector; Source : Vector);
134 Capacity : Capacity_Subtype := 0) return Vector;
138 Index : Extended_Index) return Cursor;
140 function To_Index (Position : Cursor) return Extended_Index;
144 Index : Index_Type) return Element_Type;
148 Position : Cursor) return Element_Type;
150 procedure Replace_Element
151 (Container : in out Vector;
153 New_Item : Element_Type);
155 procedure Replace_Element
156 (Container : in out Vector;
158 New_Item : Element_Type);
160 procedure Query_Element
163 Process : not null access procedure (Element : Element_Type));
165 procedure Query_Element
168 Process : not null access procedure (Element : Element_Type));
170 procedure Update_Element
171 (Container : in out Vector;
173 Process : not null access procedure (Element : in out Element_Type));
175 procedure Update_Element
176 (Container : in out Vector;
178 Process : not null access procedure (Element : in out Element_Type));
180 procedure Move (Target : in out Vector; Source : in out Vector);
183 (Container : in out Vector;
184 Before : Extended_Index;
188 (Container : in out Vector;
193 (Container : in out Vector;
196 Position : out Cursor);
199 (Container : in out Vector;
200 Before : Extended_Index;
201 New_Item : Element_Type;
202 Count : Count_Type := 1);
205 (Container : in out Vector;
207 New_Item : Element_Type;
208 Count : Count_Type := 1);
211 (Container : in out Vector;
213 New_Item : Element_Type;
214 Position : out Cursor;
215 Count : Count_Type := 1);
218 (Container : in out Vector;
219 Before : Extended_Index;
220 Count : Count_Type := 1);
223 (Container : in out Vector;
225 Position : out Cursor;
226 Count : Count_Type := 1);
229 (Container : in out Vector;
233 (Container : in out Vector;
234 New_Item : Element_Type;
235 Count : Count_Type := 1);
238 (Container : in out Vector;
242 (Container : in out Vector;
243 New_Item : Element_Type;
244 Count : Count_Type := 1);
246 procedure Insert_Space
247 (Container : in out Vector;
248 Before : Extended_Index;
249 Count : Count_Type := 1);
251 procedure Insert_Space
252 (Container : in out Vector;
254 Position : out Cursor;
255 Count : Count_Type := 1);
258 (Container : in out Vector;
259 Index : Extended_Index;
260 Count : Count_Type := 1);
263 (Container : in out Vector;
264 Position : in out Cursor;
265 Count : Count_Type := 1);
267 procedure Delete_First
268 (Container : in out Vector;
269 Count : Count_Type := 1);
271 procedure Delete_Last
272 (Container : in out Vector;
273 Count : Count_Type := 1);
275 procedure Reverse_Elements (Container : in out Vector);
277 procedure Swap (Container : in out Vector; I, J : Index_Type);
279 procedure Swap (Container : in out Vector; I, J : Cursor);
281 function First_Index (Container : Vector) return Index_Type;
283 function First (Container : Vector) return Cursor;
285 function First_Element (Container : Vector) return Element_Type;
287 function Last_Index (Container : Vector) return Extended_Index;
289 function Last (Container : Vector) return Cursor;
291 function Last_Element (Container : Vector) return Element_Type;
293 function Next (Container : Vector; Position : Cursor) return Cursor;
295 procedure Next (Container : Vector; Position : in out Cursor);
297 function Previous (Container : Vector; Position : Cursor) return Cursor;
299 procedure Previous (Container : Vector; Position : in out Cursor);
304 Index : Index_Type := Index_Type'First) return Extended_Index;
309 Position : Cursor := No_Element) return Cursor;
311 function Reverse_Find_Index
314 Index : Index_Type := Index_Type'Last) return Extended_Index;
316 function Reverse_Find
319 Position : Cursor := No_Element) return Cursor;
323 Item : Element_Type) return Boolean;
325 function Has_Element (Container : Vector; Position : Cursor) return Boolean;
329 Process : not null access
330 procedure (Container : Vector; Position : Cursor));
332 procedure Reverse_Iterate
334 Process : not null access
335 procedure (Container : Vector; Position : Cursor));
338 with function "<" (Left, Right : Element_Type) return Boolean is <>;
339 package Generic_Sorting is
341 function Is_Sorted (Container : Vector) return Boolean;
343 procedure Sort (Container : in out Vector);
345 procedure Merge (Target : in out Vector; Source : in out Vector);
349 function Left (Container : Vector; Position : Cursor) return Vector;
351 function Right (Container : Vector; Position : Cursor) return Vector;
355 pragma Inline (First_Index);
356 pragma Inline (Last_Index);
357 pragma Inline (Element);
358 pragma Inline (First_Element);
359 pragma Inline (Last_Element);
360 pragma Inline (Query_Element);
361 pragma Inline (Update_Element);
362 pragma Inline (Replace_Element);
363 pragma Inline (Contains);
364 pragma Inline (Next);
365 pragma Inline (Previous);
367 type Elements_Array is array (Count_Type range <>) of Element_Type;
368 function "=" (L, R : Elements_Array) return Boolean is abstract;
370 type Vector (Capacity : Capacity_Subtype) is tagged record
371 Elements : Elements_Array (1 .. Capacity);
372 Last : Extended_Index := No_Index;
380 (Stream : not null access Root_Stream_Type'Class;
383 for Vector'Write use Write;
386 (Stream : not null access Root_Stream_Type'Class;
387 Container : out Vector);
389 for Vector'Read use Read;
391 type Cursor is record
392 Valid : Boolean := True;
393 Index : Index_Type := Index_Type'First;
397 (Stream : not null access Root_Stream_Type'Class;
400 for Cursor'Write use Write;
403 (Stream : not null access Root_Stream_Type'Class;
404 Position : out Cursor);
406 for Cursor'Read use Read;
408 Empty_Vector : constant Vector := (Capacity => 0, others => <>);
410 No_Element : constant Cursor := (Valid => False, Index => Index_Type'First);
412 end Ada.Containers.Formal_Vectors;