2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / a-convec.ads
blobfb801b8aaaed76ad974f625a2b260dddc22fab8f
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . V E C T O R S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2015, 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 -- --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
34 with Ada.Iterator_Interfaces;
36 private with Ada.Finalization;
37 private with Ada.Streams;
39 generic
40 type Index_Type is range <>;
41 type Element_Type is private;
43 with function "=" (Left, Right : Element_Type) return Boolean is <>;
45 package Ada.Containers.Vectors is
46 pragma Preelaborate;
47 pragma Remote_Types;
49 subtype Extended_Index is Index_Type'Base
50 range Index_Type'First - 1 ..
51 Index_Type'Min (Index_Type'Base'Last - 1, Index_Type'Last) + 1;
53 No_Index : constant Extended_Index := Extended_Index'First;
55 type Vector is tagged private
56 with
57 Constant_Indexing => Constant_Reference,
58 Variable_Indexing => Reference,
59 Default_Iterator => Iterate,
60 Iterator_Element => Element_Type;
61 pragma Preelaborable_Initialization (Vector);
63 type Cursor is private;
64 pragma Preelaborable_Initialization (Cursor);
66 No_Element : constant Cursor;
68 function Has_Element (Position : Cursor) return Boolean;
70 package Vector_Iterator_Interfaces is new
71 Ada.Iterator_Interfaces (Cursor, Has_Element);
73 Empty_Vector : constant Vector;
75 overriding function "=" (Left, Right : Vector) return Boolean;
77 function To_Vector (Length : Count_Type) return Vector;
79 function To_Vector
80 (New_Item : Element_Type;
81 Length : Count_Type) return Vector;
83 function "&" (Left, Right : Vector) return Vector;
85 function "&" (Left : Vector; Right : Element_Type) return Vector;
87 function "&" (Left : Element_Type; Right : Vector) return Vector;
89 function "&" (Left, Right : Element_Type) return Vector;
91 function Capacity (Container : Vector) return Count_Type;
93 procedure Reserve_Capacity
94 (Container : in out Vector;
95 Capacity : Count_Type);
97 function Length (Container : Vector) return Count_Type;
99 procedure Set_Length
100 (Container : in out Vector;
101 Length : Count_Type);
103 function Is_Empty (Container : Vector) return Boolean;
105 procedure Clear (Container : in out Vector);
107 function To_Cursor
108 (Container : Vector;
109 Index : Extended_Index) return Cursor;
111 function To_Index (Position : Cursor) return Extended_Index;
113 function Element
114 (Container : Vector;
115 Index : Index_Type) return Element_Type;
117 function Element (Position : Cursor) return Element_Type;
119 procedure Replace_Element
120 (Container : in out Vector;
121 Index : Index_Type;
122 New_Item : Element_Type);
124 procedure Replace_Element
125 (Container : in out Vector;
126 Position : Cursor;
127 New_Item : Element_Type);
129 procedure Query_Element
130 (Container : Vector;
131 Index : Index_Type;
132 Process : not null access procedure (Element : Element_Type));
134 procedure Query_Element
135 (Position : Cursor;
136 Process : not null access procedure (Element : Element_Type));
138 procedure Update_Element
139 (Container : in out Vector;
140 Index : Index_Type;
141 Process : not null access procedure (Element : in out Element_Type));
143 procedure Update_Element
144 (Container : in out Vector;
145 Position : Cursor;
146 Process : not null access procedure (Element : in out Element_Type));
148 type Constant_Reference_Type
149 (Element : not null access constant Element_Type) is
150 private
151 with
152 Implicit_Dereference => Element;
154 type Reference_Type (Element : not null access Element_Type) is private
155 with
156 Implicit_Dereference => Element;
158 function Constant_Reference
159 (Container : aliased Vector;
160 Position : Cursor) return Constant_Reference_Type;
161 pragma Inline (Constant_Reference);
163 function Reference
164 (Container : aliased in out Vector;
165 Position : Cursor) return Reference_Type;
166 pragma Inline (Reference);
168 function Constant_Reference
169 (Container : aliased Vector;
170 Index : Index_Type) return Constant_Reference_Type;
171 pragma Inline (Constant_Reference);
173 function Reference
174 (Container : aliased in out Vector;
175 Index : Index_Type) return Reference_Type;
176 pragma Inline (Reference);
178 procedure Assign (Target : in out Vector; Source : Vector);
180 function Copy (Source : Vector; Capacity : Count_Type := 0) return Vector;
182 procedure Move (Target : in out Vector; Source : in out Vector);
184 procedure Insert
185 (Container : in out Vector;
186 Before : Extended_Index;
187 New_Item : Vector);
189 procedure Insert
190 (Container : in out Vector;
191 Before : Cursor;
192 New_Item : Vector);
194 procedure Insert
195 (Container : in out Vector;
196 Before : Cursor;
197 New_Item : Vector;
198 Position : out Cursor);
200 procedure Insert
201 (Container : in out Vector;
202 Before : Extended_Index;
203 New_Item : Element_Type;
204 Count : Count_Type := 1);
206 procedure Insert
207 (Container : in out Vector;
208 Before : Cursor;
209 New_Item : Element_Type;
210 Count : Count_Type := 1);
212 procedure Insert
213 (Container : in out Vector;
214 Before : Cursor;
215 New_Item : Element_Type;
216 Position : out Cursor;
217 Count : Count_Type := 1);
219 procedure Insert
220 (Container : in out Vector;
221 Before : Extended_Index;
222 Count : Count_Type := 1);
224 procedure Insert
225 (Container : in out Vector;
226 Before : Cursor;
227 Position : out Cursor;
228 Count : Count_Type := 1);
230 procedure Prepend
231 (Container : in out Vector;
232 New_Item : Vector);
234 procedure Prepend
235 (Container : in out Vector;
236 New_Item : Element_Type;
237 Count : Count_Type := 1);
239 procedure Append
240 (Container : in out Vector;
241 New_Item : Vector);
243 procedure Append
244 (Container : in out Vector;
245 New_Item : Element_Type;
246 Count : Count_Type := 1);
248 procedure Insert_Space
249 (Container : in out Vector;
250 Before : Extended_Index;
251 Count : Count_Type := 1);
253 procedure Insert_Space
254 (Container : in out Vector;
255 Before : Cursor;
256 Position : out Cursor;
257 Count : Count_Type := 1);
259 procedure Delete
260 (Container : in out Vector;
261 Index : Extended_Index;
262 Count : Count_Type := 1);
264 procedure Delete
265 (Container : in out Vector;
266 Position : in out Cursor;
267 Count : Count_Type := 1);
269 procedure Delete_First
270 (Container : in out Vector;
271 Count : Count_Type := 1);
273 procedure Delete_Last
274 (Container : in out Vector;
275 Count : Count_Type := 1);
277 procedure Reverse_Elements (Container : in out Vector);
279 procedure Swap (Container : in out Vector; I, J : Index_Type);
281 procedure Swap (Container : in out Vector; I, J : Cursor);
283 function First_Index (Container : Vector) return Index_Type;
285 function First (Container : Vector) return Cursor;
287 function First_Element (Container : Vector) return Element_Type;
289 function Last_Index (Container : Vector) return Extended_Index;
291 function Last (Container : Vector) return Cursor;
293 function Last_Element (Container : Vector) return Element_Type;
295 function Next (Position : Cursor) return Cursor;
297 procedure Next (Position : in out Cursor);
299 function Previous (Position : Cursor) return Cursor;
301 procedure Previous (Position : in out Cursor);
303 function Find_Index
304 (Container : Vector;
305 Item : Element_Type;
306 Index : Index_Type := Index_Type'First) return Extended_Index;
308 function Find
309 (Container : Vector;
310 Item : Element_Type;
311 Position : Cursor := No_Element) return Cursor;
313 function Reverse_Find_Index
314 (Container : Vector;
315 Item : Element_Type;
316 Index : Index_Type := Index_Type'Last) return Extended_Index;
318 function Reverse_Find
319 (Container : Vector;
320 Item : Element_Type;
321 Position : Cursor := No_Element) return Cursor;
323 function Contains
324 (Container : Vector;
325 Item : Element_Type) return Boolean;
327 procedure Iterate
328 (Container : Vector;
329 Process : not null access procedure (Position : Cursor));
331 procedure Reverse_Iterate
332 (Container : Vector;
333 Process : not null access procedure (Position : Cursor));
335 function Iterate (Container : Vector)
336 return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
338 function Iterate (Container : Vector; Start : Cursor)
339 return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
341 generic
342 with function "<" (Left, Right : Element_Type) return Boolean is <>;
343 package Generic_Sorting is
345 function Is_Sorted (Container : Vector) return Boolean;
347 procedure Sort (Container : in out Vector);
349 procedure Merge (Target : in out Vector; Source : in out Vector);
351 end Generic_Sorting;
353 private
355 pragma Inline (Append);
356 pragma Inline (First_Index);
357 pragma Inline (Last_Index);
358 pragma Inline (Element);
359 pragma Inline (First_Element);
360 pragma Inline (Last_Element);
361 pragma Inline (Query_Element);
362 pragma Inline (Update_Element);
363 pragma Inline (Replace_Element);
364 pragma Inline (Is_Empty);
365 pragma Inline (Contains);
366 pragma Inline (Next);
367 pragma Inline (Previous);
369 type Elements_Array is array (Index_Type range <>) of aliased Element_Type;
370 function "=" (L, R : Elements_Array) return Boolean is abstract;
372 type Elements_Type (Last : Extended_Index) is limited record
373 EA : Elements_Array (Index_Type'First .. Last);
374 end record;
376 type Elements_Access is access all Elements_Type;
378 use Ada.Finalization;
379 use Ada.Streams;
381 type Vector is new Controlled with record
382 Elements : Elements_Access := null;
383 Last : Extended_Index := No_Index;
384 Busy : Natural := 0;
385 Lock : Natural := 0;
386 end record;
388 overriding procedure Adjust (Container : in out Vector);
389 overriding procedure Finalize (Container : in out Vector);
391 procedure Write
392 (Stream : not null access Root_Stream_Type'Class;
393 Container : Vector);
395 for Vector'Write use Write;
397 procedure Read
398 (Stream : not null access Root_Stream_Type'Class;
399 Container : out Vector);
401 for Vector'Read use Read;
403 type Vector_Access is access all Vector;
404 for Vector_Access'Storage_Size use 0;
406 type Cursor is record
407 Container : Vector_Access;
408 Index : Index_Type := Index_Type'First;
409 end record;
411 procedure Read
412 (Stream : not null access Root_Stream_Type'Class;
413 Position : out Cursor);
415 for Cursor'Read use Read;
417 procedure Write
418 (Stream : not null access Root_Stream_Type'Class;
419 Position : Cursor);
421 for Cursor'Write use Write;
423 type Reference_Control_Type is
424 new Controlled with record
425 Container : Vector_Access;
426 end record;
428 overriding procedure Adjust (Control : in out Reference_Control_Type);
429 pragma Inline (Adjust);
431 overriding procedure Finalize (Control : in out Reference_Control_Type);
432 pragma Inline (Finalize);
434 type Constant_Reference_Type
435 (Element : not null access constant Element_Type) is
436 record
437 Control : Reference_Control_Type :=
438 raise Program_Error with "uninitialized reference";
439 -- The RM says, "The default initialization of an object of
440 -- type Constant_Reference_Type or Reference_Type propagates
441 -- Program_Error."
442 end record;
444 procedure Write
445 (Stream : not null access Root_Stream_Type'Class;
446 Item : Constant_Reference_Type);
448 for Constant_Reference_Type'Write use Write;
450 procedure Read
451 (Stream : not null access Root_Stream_Type'Class;
452 Item : out Constant_Reference_Type);
454 for Constant_Reference_Type'Read use Read;
456 type Reference_Type
457 (Element : not null access Element_Type) is
458 record
459 Control : Reference_Control_Type :=
460 raise Program_Error with "uninitialized reference";
461 -- The RM says, "The default initialization of an object of
462 -- type Constant_Reference_Type or Reference_Type propagates
463 -- Program_Error."
464 end record;
466 procedure Write
467 (Stream : not null access Root_Stream_Type'Class;
468 Item : Reference_Type);
470 for Reference_Type'Write use Write;
472 procedure Read
473 (Stream : not null access Root_Stream_Type'Class;
474 Item : out Reference_Type);
476 for Reference_Type'Read use Read;
478 -- Three operations are used to optimize in the expansion of "for ... of"
479 -- loops: the Next(Cursor) procedure in the visible part, and the following
480 -- Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for
481 -- details.
483 function Pseudo_Reference
484 (Container : aliased Vector'Class) return Reference_Control_Type;
485 pragma Inline (Pseudo_Reference);
486 -- Creates an object of type Reference_Control_Type pointing to the
487 -- container, and increments the Lock. Finalization of this object will
488 -- decrement the Lock.
490 type Element_Access is access all Element_Type;
492 function Get_Element_Access
493 (Position : Cursor) return not null Element_Access;
494 -- Returns a pointer to the element designated by Position.
496 No_Element : constant Cursor := Cursor'(null, Index_Type'First);
498 Empty_Vector : constant Vector := (Controlled with others => <>);
500 Count_Type_Last : constant := Count_Type'Last;
501 -- Count_Type'Last as a universal_integer, so we can compare Index_Type
502 -- values against this without type conversions that might overflow.
504 end Ada.Containers.Vectors;