2013-03-08 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / ada / a-cofove.ads
blob24e2944fb7e062c6fdf9719c264fda14d7eab6d6
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
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 --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2011, Free Software Foundation, Inc. --
10 -- --
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. --
14 -- --
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. --
21 -- --
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. --
25 -- --
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
44 -- container.
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;
59 with Ada.Containers;
60 use Ada.Containers;
62 generic
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
69 pragma Pure;
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);
83 -- so for now:
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;
102 function To_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;
122 procedure Set_Length
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);
132 function Copy
133 (Source : Vector;
134 Capacity : Capacity_Subtype := 0) return Vector;
136 function To_Cursor
137 (Container : Vector;
138 Index : Extended_Index) return Cursor;
140 function To_Index (Position : Cursor) return Extended_Index;
142 function Element
143 (Container : Vector;
144 Index : Index_Type) return Element_Type;
146 function Element
147 (Container : Vector;
148 Position : Cursor) return Element_Type;
150 procedure Replace_Element
151 (Container : in out Vector;
152 Index : Index_Type;
153 New_Item : Element_Type);
155 procedure Replace_Element
156 (Container : in out Vector;
157 Position : Cursor;
158 New_Item : Element_Type);
160 procedure Query_Element
161 (Container : Vector;
162 Index : Index_Type;
163 Process : not null access procedure (Element : Element_Type));
165 procedure Query_Element
166 (Container : Vector;
167 Position : Cursor;
168 Process : not null access procedure (Element : Element_Type));
170 procedure Update_Element
171 (Container : in out Vector;
172 Index : Index_Type;
173 Process : not null access procedure (Element : in out Element_Type));
175 procedure Update_Element
176 (Container : in out Vector;
177 Position : Cursor;
178 Process : not null access procedure (Element : in out Element_Type));
180 procedure Move (Target : in out Vector; Source : in out Vector);
182 procedure Insert
183 (Container : in out Vector;
184 Before : Extended_Index;
185 New_Item : Vector);
187 procedure Insert
188 (Container : in out Vector;
189 Before : Cursor;
190 New_Item : Vector);
192 procedure Insert
193 (Container : in out Vector;
194 Before : Cursor;
195 New_Item : Vector;
196 Position : out Cursor);
198 procedure Insert
199 (Container : in out Vector;
200 Before : Extended_Index;
201 New_Item : Element_Type;
202 Count : Count_Type := 1);
204 procedure Insert
205 (Container : in out Vector;
206 Before : Cursor;
207 New_Item : Element_Type;
208 Count : Count_Type := 1);
210 procedure Insert
211 (Container : in out Vector;
212 Before : Cursor;
213 New_Item : Element_Type;
214 Position : out Cursor;
215 Count : Count_Type := 1);
217 procedure Insert
218 (Container : in out Vector;
219 Before : Extended_Index;
220 Count : Count_Type := 1);
222 procedure Insert
223 (Container : in out Vector;
224 Before : Cursor;
225 Position : out Cursor;
226 Count : Count_Type := 1);
228 procedure Prepend
229 (Container : in out Vector;
230 New_Item : Vector);
232 procedure Prepend
233 (Container : in out Vector;
234 New_Item : Element_Type;
235 Count : Count_Type := 1);
237 procedure Append
238 (Container : in out Vector;
239 New_Item : Vector);
241 procedure Append
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;
253 Before : Cursor;
254 Position : out Cursor;
255 Count : Count_Type := 1);
257 procedure Delete
258 (Container : in out Vector;
259 Index : Extended_Index;
260 Count : Count_Type := 1);
262 procedure Delete
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);
301 function Find_Index
302 (Container : Vector;
303 Item : Element_Type;
304 Index : Index_Type := Index_Type'First) return Extended_Index;
306 function Find
307 (Container : Vector;
308 Item : Element_Type;
309 Position : Cursor := No_Element) return Cursor;
311 function Reverse_Find_Index
312 (Container : Vector;
313 Item : Element_Type;
314 Index : Index_Type := Index_Type'Last) return Extended_Index;
316 function Reverse_Find
317 (Container : Vector;
318 Item : Element_Type;
319 Position : Cursor := No_Element) return Cursor;
321 function Contains
322 (Container : Vector;
323 Item : Element_Type) return Boolean;
325 function Has_Element (Container : Vector; Position : Cursor) return Boolean;
327 procedure Iterate
328 (Container : Vector;
329 Process : not null access
330 procedure (Container : Vector; Position : Cursor));
332 procedure Reverse_Iterate
333 (Container : Vector;
334 Process : not null access
335 procedure (Container : Vector; Position : Cursor));
337 generic
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);
347 end Generic_Sorting;
349 function Left (Container : Vector; Position : Cursor) return Vector;
351 function Right (Container : Vector; Position : Cursor) return Vector;
353 private
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;
373 Busy : Natural := 0;
374 Lock : Natural := 0;
375 end record;
377 use Ada.Streams;
379 procedure Write
380 (Stream : not null access Root_Stream_Type'Class;
381 Container : Vector);
383 for Vector'Write use Write;
385 procedure Read
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;
394 end record;
396 procedure Write
397 (Stream : not null access Root_Stream_Type'Class;
398 Position : Cursor);
400 for Cursor'Write use Write;
402 procedure Read
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;