1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . I N D E F I N I T E _ V E C T O R S --
9 -- Copyright (C) 2004-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada.Containers.Generic_Array_Sort;
31 with Ada.Unchecked_Deallocation;
33 with System; use type System.Address;
35 package body Ada.Containers.Indefinite_Vectors is
38 new Ada.Unchecked_Deallocation (Elements_Type, Elements_Access);
41 new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
43 type Iterator is new Limited_Controlled and
44 Vector_Iterator_Interfaces.Reversible_Iterator with
46 Container : Vector_Access;
47 Index : Index_Type'Base;
50 overriding procedure Finalize (Object : in out Iterator);
52 overriding function First (Object : Iterator) return Cursor;
53 overriding function Last (Object : Iterator) return Cursor;
55 overriding function Next
57 Position : Cursor) return Cursor;
59 overriding function Previous
61 Position : Cursor) return Cursor;
67 function "&" (Left, Right : Vector) return Vector is
68 LN : constant Count_Type := Length (Left);
69 RN : constant Count_Type := Length (Right);
70 N : Count_Type'Base; -- length of result
71 J : Count_Type'Base; -- for computing intermediate values
72 Last : Index_Type'Base; -- Last index of result
75 -- We decide that the capacity of the result is the sum of the lengths
76 -- of the vector parameters. We could decide to make it larger, but we
77 -- have no basis for knowing how much larger, so we just allocate the
78 -- minimum amount of storage.
80 -- Here we handle the easy cases first, when one of the vector
81 -- parameters is empty. (We say "easy" because there's nothing to
82 -- compute, that can potentially overflow.)
90 RE : Elements_Array renames
91 Right.Elements.EA (Index_Type'First .. Right.Last);
93 Elements : Elements_Access :=
94 new Elements_Type (Right.Last);
97 -- Elements of an indefinite vector are allocated, so we cannot
98 -- use simple slice assignment to give a value to our result.
99 -- Hence we must walk the array of the Right vector, and copy
100 -- each source element individually.
102 for I in Elements.EA'Range loop
104 if RE (I) /= null then
105 Elements.EA (I) := new Element_Type'(RE (I).all);
110 for J in Index_Type'First .. I - 1 loop
111 Free (Elements.EA (J));
119 return (Controlled with Elements, Right.Last, 0, 0);
126 LE : Elements_Array renames
127 Left.Elements.EA (Index_Type'First .. Left.Last);
129 Elements : Elements_Access :=
130 new Elements_Type (Left.Last);
133 -- Elements of an indefinite vector are allocated, so we cannot
134 -- use simple slice assignment to give a value to our result.
135 -- Hence we must walk the array of the Left vector, and copy
136 -- each source element individually.
138 for I in Elements.EA'Range loop
140 if LE (I) /= null then
141 Elements.EA (I) := new Element_Type'(LE (I).all);
146 for J in Index_Type'First .. I - 1 loop
147 Free (Elements.EA (J));
155 return (Controlled with Elements, Left.Last, 0, 0);
159 -- Neither of the vector parameters is empty, so we must compute the
160 -- length of the result vector and its last index. (This is the harder
161 -- case, because our computations must avoid overflow.)
163 -- There are two constraints we need to satisfy. The first constraint is
164 -- that a container cannot have more than Count_Type'Last elements, so
165 -- we must check the sum of the combined lengths. Note that we cannot
166 -- simply add the lengths, because of the possibility of overflow.
168 if LN > Count_Type'Last - RN then
169 raise Constraint_Error with "new length is out of range";
172 -- It is now safe compute the length of the new vector.
176 -- The second constraint is that the new Last index value cannot
177 -- exceed Index_Type'Last. We use the wider of Index_Type'Base and
178 -- Count_Type'Base as the type for intermediate values.
180 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
182 -- We perform a two-part test. First we determine whether the
183 -- computed Last value lies in the base range of the type, and then
184 -- determine whether it lies in the range of the index (sub)type.
186 -- Last must satisfy this relation:
187 -- First + Length - 1 <= Last
189 -- First - 1 <= Last - Length
190 -- Which can rewrite as:
191 -- No_Index <= Last - Length
193 if Index_Type'Base'Last - Index_Type'Base (N) < No_Index then
194 raise Constraint_Error with "new length is out of range";
197 -- We now know that the computed value of Last is within the base
198 -- range of the type, so it is safe to compute its value:
200 Last := No_Index + Index_Type'Base (N);
202 -- Finally we test whether the value is within the range of the
203 -- generic actual index subtype:
205 if Last > Index_Type'Last then
206 raise Constraint_Error with "new length is out of range";
209 elsif Index_Type'First <= 0 then
211 -- Here we can compute Last directly, in the normal way. We know that
212 -- No_Index is less than 0, so there is no danger of overflow when
213 -- adding the (positive) value of length.
215 J := Count_Type'Base (No_Index) + N; -- Last
217 if J > Count_Type'Base (Index_Type'Last) then
218 raise Constraint_Error with "new length is out of range";
221 -- We know that the computed value (having type Count_Type) of Last
222 -- is within the range of the generic actual index subtype, so it is
223 -- safe to convert to Index_Type:
225 Last := Index_Type'Base (J);
228 -- Here Index_Type'First (and Index_Type'Last) is positive, so we
229 -- must test the length indirectly (by working backwards from the
230 -- largest possible value of Last), in order to prevent overflow.
232 J := Count_Type'Base (Index_Type'Last) - N; -- No_Index
234 if J < Count_Type'Base (No_Index) then
235 raise Constraint_Error with "new length is out of range";
238 -- We have determined that the result length would not create a Last
239 -- index value outside of the range of Index_Type, so we can now
240 -- safely compute its value.
242 Last := Index_Type'Base (Count_Type'Base (No_Index) + N);
246 LE : Elements_Array renames
247 Left.Elements.EA (Index_Type'First .. Left.Last);
249 RE : Elements_Array renames
250 Right.Elements.EA (Index_Type'First .. Right.Last);
252 Elements : Elements_Access := new Elements_Type (Last);
254 I : Index_Type'Base := No_Index;
257 -- Elements of an indefinite vector are allocated, so we cannot use
258 -- simple slice assignment to give a value to our result. Hence we
259 -- must walk the array of each vector parameter, and copy each source
260 -- element individually.
262 for LI in LE'Range loop
266 if LE (LI) /= null then
267 Elements.EA (I) := new Element_Type'(LE (LI).all);
272 for J in Index_Type'First .. I - 1 loop
273 Free (Elements.EA (J));
281 for RI in RE'Range loop
285 if RE (RI) /= null then
286 Elements.EA (I) := new Element_Type'(RE (RI).all);
291 for J in Index_Type'First .. I - 1 loop
292 Free (Elements.EA (J));
300 return (Controlled with Elements, Last, 0, 0);
304 function "&" (Left : Vector; Right : Element_Type) return Vector is
306 -- We decide that the capacity of the result is the sum of the lengths
307 -- of the parameters. We could decide to make it larger, but we have no
308 -- basis for knowing how much larger, so we just allocate the minimum
309 -- amount of storage.
311 -- Here we handle the easy case first, when the vector parameter (Left)
314 if Left.Is_Empty then
316 Elements : Elements_Access := new Elements_Type (Index_Type'First);
320 Elements.EA (Index_Type'First) := new Element_Type'(Right);
327 return (Controlled with Elements, Index_Type'First, 0, 0);
331 -- The vector parameter is not empty, so we must compute the length of
332 -- the result vector and its last index, but in such a way that overflow
333 -- is avoided. We must satisfy two constraints: the new length cannot
334 -- exceed Count_Type'Last, and the new Last index cannot exceed
337 if Left.Length = Count_Type'Last then
338 raise Constraint_Error with "new length is out of range";
341 if Left.Last >= Index_Type'Last then
342 raise Constraint_Error with "new length is out of range";
346 Last : constant Index_Type := Left.Last + 1;
348 LE : Elements_Array renames
349 Left.Elements.EA (Index_Type'First .. Left.Last);
351 Elements : Elements_Access :=
352 new Elements_Type (Last);
355 for I in LE'Range loop
357 if LE (I) /= null then
358 Elements.EA (I) := new Element_Type'(LE (I).all);
363 for J in Index_Type'First .. I - 1 loop
364 Free (Elements.EA (J));
373 Elements.EA (Last) := new Element_Type'(Right);
377 for J in Index_Type'First .. Last - 1 loop
378 Free (Elements.EA (J));
385 return (Controlled with Elements, Last, 0, 0);
389 function "&" (Left : Element_Type; Right : Vector) return Vector is
391 -- We decide that the capacity of the result is the sum of the lengths
392 -- of the parameters. We could decide to make it larger, but we have no
393 -- basis for knowing how much larger, so we just allocate the minimum
394 -- amount of storage.
396 -- Here we handle the easy case first, when the vector parameter (Right)
399 if Right.Is_Empty then
401 Elements : Elements_Access := new Elements_Type (Index_Type'First);
405 Elements.EA (Index_Type'First) := new Element_Type'(Left);
412 return (Controlled with Elements, Index_Type'First, 0, 0);
416 -- The vector parameter is not empty, so we must compute the length of
417 -- the result vector and its last index, but in such a way that overflow
418 -- is avoided. We must satisfy two constraints: the new length cannot
419 -- exceed Count_Type'Last, and the new Last index cannot exceed
422 if Right.Length = Count_Type'Last then
423 raise Constraint_Error with "new length is out of range";
426 if Right.Last >= Index_Type'Last then
427 raise Constraint_Error with "new length is out of range";
431 Last : constant Index_Type := Right.Last + 1;
433 RE : Elements_Array renames
434 Right.Elements.EA (Index_Type'First .. Right.Last);
436 Elements : Elements_Access :=
437 new Elements_Type (Last);
439 I : Index_Type'Base := Index_Type'First;
443 Elements.EA (I) := new Element_Type'(Left);
450 for RI in RE'Range loop
454 if RE (RI) /= null then
455 Elements.EA (I) := new Element_Type'(RE (RI).all);
460 for J in Index_Type'First .. I - 1 loop
461 Free (Elements.EA (J));
469 return (Controlled with Elements, Last, 0, 0);
473 function "&" (Left, Right : Element_Type) return Vector is
475 -- We decide that the capacity of the result is the sum of the lengths
476 -- of the parameters. We could decide to make it larger, but we have no
477 -- basis for knowing how much larger, so we just allocate the minimum
478 -- amount of storage.
480 -- We must compute the length of the result vector and its last index,
481 -- but in such a way that overflow is avoided. We must satisfy two
482 -- constraints: the new length cannot exceed Count_Type'Last (here, we
483 -- know that that condition is satisfied), and the new Last index cannot
484 -- exceed Index_Type'Last.
486 if Index_Type'First >= Index_Type'Last then
487 raise Constraint_Error with "new length is out of range";
491 Last : constant Index_Type := Index_Type'First + 1;
492 Elements : Elements_Access := new Elements_Type (Last);
496 Elements.EA (Index_Type'First) := new Element_Type'(Left);
504 Elements.EA (Last) := new Element_Type'(Right);
507 Free (Elements.EA (Index_Type'First));
512 return (Controlled with Elements, Last, 0, 0);
520 overriding function "=" (Left, Right : Vector) return Boolean is
522 if Left'Address = Right'Address then
526 if Left.Last /= Right.Last then
530 for J in Index_Type'First .. Left.Last loop
531 if Left.Elements.EA (J) = null then
532 if Right.Elements.EA (J) /= null then
536 elsif Right.Elements.EA (J) = null then
539 elsif Left.Elements.EA (J).all /= Right.Elements.EA (J).all then
551 procedure Adjust (Container : in out Vector) is
553 if Container.Last = No_Index then
554 Container.Elements := null;
559 L : constant Index_Type := Container.Last;
560 E : Elements_Array renames
561 Container.Elements.EA (Index_Type'First .. L);
564 Container.Elements := null;
565 Container.Last := No_Index;
569 Container.Elements := new Elements_Type (L);
571 for I in E'Range loop
572 if E (I) /= null then
573 Container.Elements.EA (I) := new Element_Type'(E (I).all);
585 procedure Append (Container : in out Vector; New_Item : Vector) is
587 if Is_Empty (New_Item) then
591 if Container.Last = Index_Type'Last then
592 raise Constraint_Error with "vector is already at its maximum length";
602 (Container : in out Vector;
603 New_Item : Element_Type;
604 Count : Count_Type := 1)
611 if Container.Last = Index_Type'Last then
612 raise Constraint_Error with "vector is already at its maximum length";
626 procedure Assign (Target : in out Vector; Source : Vector) is
628 if Target'Address = Source'Address then
633 Target.Append (Source);
640 function Capacity (Container : Vector) return Count_Type is
642 if Container.Elements = null then
646 return Container.Elements.EA'Length;
653 procedure Clear (Container : in out Vector) is
655 if Container.Busy > 0 then
656 raise Program_Error with
657 "attempt to tamper with cursors (vector is busy)";
660 while Container.Last >= Index_Type'First loop
662 X : Element_Access := Container.Elements.EA (Container.Last);
664 Container.Elements.EA (Container.Last) := null;
665 Container.Last := Container.Last - 1;
671 ------------------------
672 -- Constant_Reference --
673 ------------------------
675 function Constant_Reference
676 (Container : aliased Vector;
677 Position : Cursor) return Constant_Reference_Type
682 if Position.Container = null then
683 raise Constraint_Error with "Position cursor has no element";
686 if Position.Container /= Container'Unrestricted_Access then
687 raise Program_Error with "Position cursor denotes wrong container";
690 if Position.Index > Position.Container.Last then
691 raise Constraint_Error with "Position cursor is out of range";
694 E := Container.Elements.EA (Position.Index);
697 raise Constraint_Error with "element at Position is empty";
700 return (Element => E.all'Access);
701 end Constant_Reference;
703 function Constant_Reference
704 (Container : aliased Vector;
705 Index : Index_Type) return Constant_Reference_Type
710 if Index > Container.Last then
711 raise Constraint_Error with "Index is out of range";
714 E := Container.Elements.EA (Index);
717 raise Constraint_Error with "element at Index is empty";
720 return (Element => E.all'Access);
721 end Constant_Reference;
729 Item : Element_Type) return Boolean
732 return Find_Index (Container, Item) /= No_Index;
741 Capacity : Count_Type := 0) return Vector
749 elsif Capacity >= Source.Length then
754 with "Requested capacity is less than Source length";
757 return Target : Vector do
758 Target.Reserve_Capacity (C);
759 Target.Assign (Source);
768 (Container : in out Vector;
769 Index : Extended_Index;
770 Count : Count_Type := 1)
772 Old_Last : constant Index_Type'Base := Container.Last;
773 New_Last : Index_Type'Base;
774 Count2 : Count_Type'Base; -- count of items from Index to Old_Last
775 J : Index_Type'Base; -- first index of items that slide down
778 -- Delete removes items from the vector, the number of which is the
779 -- minimum of the specified Count and the items (if any) that exist from
780 -- Index to Container.Last. There are no constraints on the specified
781 -- value of Count (it can be larger than what's available at this
782 -- position in the vector, for example), but there are constraints on
783 -- the allowed values of the Index.
785 -- As a precondition on the generic actual Index_Type, the base type
786 -- must include Index_Type'Pred (Index_Type'First); this is the value
787 -- that Container.Last assumes when the vector is empty. However, we do
788 -- not allow that as the value for Index when specifying which items
789 -- should be deleted, so we must manually check. (That the user is
790 -- allowed to specify the value at all here is a consequence of the
791 -- declaration of the Extended_Index subtype, which includes the values
792 -- in the base range that immediately precede and immediately follow the
793 -- values in the Index_Type.)
795 if Index < Index_Type'First then
796 raise Constraint_Error with "Index is out of range (too small)";
799 -- We do allow a value greater than Container.Last to be specified as
800 -- the Index, but only if it's immediately greater. This allows the
801 -- corner case of deleting no items from the back end of the vector to
802 -- be treated as a no-op. (It is assumed that specifying an index value
803 -- greater than Last + 1 indicates some deeper flaw in the caller's
804 -- algorithm, so that case is treated as a proper error.)
806 if Index > Old_Last then
807 if Index > Old_Last + 1 then
808 raise Constraint_Error with "Index is out of range (too large)";
814 -- Here and elsewhere we treat deleting 0 items from the container as a
815 -- no-op, even when the container is busy, so we simply return.
821 -- The internal elements array isn't guaranteed to exist unless we have
822 -- elements, so we handle that case here in order to avoid having to
823 -- check it later. (Note that an empty vector can never be busy, so
824 -- there's no semantic harm in returning early.)
826 if Container.Is_Empty then
830 -- The tampering bits exist to prevent an item from being deleted (or
831 -- otherwise harmfully manipulated) while it is being visited. Query,
832 -- Update, and Iterate increment the busy count on entry, and decrement
833 -- the count on exit. Delete checks the count to determine whether it is
834 -- being called while the associated callback procedure is executing.
836 if Container.Busy > 0 then
837 raise Program_Error with
838 "attempt to tamper with cursors (vector is busy)";
841 -- We first calculate what's available for deletion starting at
842 -- Index. Here and elsewhere we use the wider of Index_Type'Base and
843 -- Count_Type'Base as the type for intermediate values. (See function
844 -- Length for more information.)
846 if Count_Type'Base'Last >= Index_Type'Pos (Index_Type'Base'Last) then
847 Count2 := Count_Type'Base (Old_Last) - Count_Type'Base (Index) + 1;
850 Count2 := Count_Type'Base (Old_Last - Index + 1);
853 -- If the number of elements requested (Count) for deletion is equal to
854 -- (or greater than) the number of elements available (Count2) for
855 -- deletion beginning at Index, then everything from Index to
856 -- Container.Last is deleted (this is equivalent to Delete_Last).
858 if Count >= Count2 then
859 -- Elements in an indefinite vector are allocated, so we must iterate
860 -- over the loop and deallocate elements one-at-a-time. We work from
861 -- back to front, deleting the last element during each pass, in
862 -- order to gracefully handle deallocation failures.
865 EA : Elements_Array renames Container.Elements.EA;
868 while Container.Last >= Index loop
870 K : constant Index_Type := Container.Last;
871 X : Element_Access := EA (K);
874 -- We first isolate the element we're deleting, removing it
875 -- from the vector before we attempt to deallocate it, in
876 -- case the deallocation fails.
879 Container.Last := K - 1;
881 -- Container invariants have been restored, so it is now
882 -- safe to attempt to deallocate the element.
892 -- There are some elements that aren't being deleted (the requested
893 -- count was less than the available count), so we must slide them down
894 -- to Index. We first calculate the index values of the respective array
895 -- slices, using the wider of Index_Type'Base and Count_Type'Base as the
896 -- type for intermediate calculations. For the elements that slide down,
897 -- index value New_Last is the last index value of their new home, and
898 -- index value J is the first index of their old home.
900 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
901 New_Last := Old_Last - Index_Type'Base (Count);
902 J := Index + Index_Type'Base (Count);
905 New_Last := Index_Type'Base (Count_Type'Base (Old_Last) - Count);
906 J := Index_Type'Base (Count_Type'Base (Index) + Count);
909 -- The internal elements array isn't guaranteed to exist unless we have
910 -- elements, but we have that guarantee here because we know we have
911 -- elements to slide. The array index values for each slice have
912 -- already been determined, so what remains to be done is to first
913 -- deallocate the elements that are being deleted, and then slide down
914 -- to Index the elements that aren't being deleted.
917 EA : Elements_Array renames Container.Elements.EA;
920 -- Before we can slide down the elements that aren't being deleted,
921 -- we need to deallocate the elements that are being deleted.
923 for K in Index .. J - 1 loop
925 X : Element_Access := EA (K);
928 -- First we remove the element we're about to deallocate from
929 -- the vector, in case the deallocation fails, in order to
930 -- preserve representation invariants.
934 -- The element has been removed from the vector, so it is now
935 -- safe to attempt to deallocate it.
941 EA (Index .. New_Last) := EA (J .. Old_Last);
942 Container.Last := New_Last;
947 (Container : in out Vector;
948 Position : in out Cursor;
949 Count : Count_Type := 1)
951 pragma Warnings (Off, Position);
954 if Position.Container = null then
955 raise Constraint_Error with "Position cursor has no element";
958 if Position.Container /= Container'Unrestricted_Access then
959 raise Program_Error with "Position cursor denotes wrong container";
962 if Position.Index > Container.Last then
963 raise Program_Error with "Position index is out of range";
966 Delete (Container, Position.Index, Count);
968 Position := No_Element;
975 procedure Delete_First
976 (Container : in out Vector;
977 Count : Count_Type := 1)
984 if Count >= Length (Container) then
989 Delete (Container, Index_Type'First, Count);
996 procedure Delete_Last
997 (Container : in out Vector;
998 Count : Count_Type := 1)
1001 -- It is not permitted to delete items while the container is busy (for
1002 -- example, we're in the middle of a passive iteration). However, we
1003 -- always treat deleting 0 items as a no-op, even when we're busy, so we
1004 -- simply return without checking.
1010 -- We cannot simply subsume the empty case into the loop below (the loop
1011 -- would iterate 0 times), because we rename the internal array object
1012 -- (which is allocated), but an empty vector isn't guaranteed to have
1013 -- actually allocated an array. (Note that an empty vector can never be
1014 -- busy, so there's no semantic harm in returning early here.)
1016 if Container.Is_Empty then
1020 -- The tampering bits exist to prevent an item from being deleted (or
1021 -- otherwise harmfully manipulated) while it is being visited. Query,
1022 -- Update, and Iterate increment the busy count on entry, and decrement
1023 -- the count on exit. Delete_Last checks the count to determine whether
1024 -- it is being called while the associated callback procedure is
1027 if Container.Busy > 0 then
1028 raise Program_Error with
1029 "attempt to tamper with cursors (vector is busy)";
1032 -- Elements in an indefinite vector are allocated, so we must iterate
1033 -- over the loop and deallocate elements one-at-a-time. We work from
1034 -- back to front, deleting the last element during each pass, in order
1035 -- to gracefully handle deallocation failures.
1038 E : Elements_Array renames Container.Elements.EA;
1041 for Indx in 1 .. Count_Type'Min (Count, Container.Length) loop
1043 J : constant Index_Type := Container.Last;
1044 X : Element_Access := E (J);
1047 -- Note that we first isolate the element we're deleting,
1048 -- removing it from the vector, before we actually deallocate
1049 -- it, in order to preserve representation invariants even if
1050 -- the deallocation fails.
1053 Container.Last := J - 1;
1055 -- Container invariants have been restored, so it is now safe
1056 -- to deallocate the element.
1069 (Container : Vector;
1070 Index : Index_Type) return Element_Type
1073 if Index > Container.Last then
1074 raise Constraint_Error with "Index is out of range";
1078 EA : constant Element_Access := Container.Elements.EA (Index);
1082 raise Constraint_Error with "element is empty";
1089 function Element (Position : Cursor) return Element_Type is
1091 if Position.Container = null then
1092 raise Constraint_Error with "Position cursor has no element";
1095 if Position.Index > Position.Container.Last then
1096 raise Constraint_Error with "Position cursor is out of range";
1100 EA : constant Element_Access :=
1101 Position.Container.Elements.EA (Position.Index);
1105 raise Constraint_Error with "element is empty";
1116 procedure Finalize (Container : in out Vector) is
1118 Clear (Container); -- Checks busy-bit
1121 X : Elements_Access := Container.Elements;
1123 Container.Elements := null;
1128 procedure Finalize (Object : in out Iterator) is
1129 B : Natural renames Object.Container.Busy;
1139 (Container : Vector;
1140 Item : Element_Type;
1141 Position : Cursor := No_Element) return Cursor
1144 if Position.Container /= null then
1145 if Position.Container /= Container'Unrestricted_Access then
1146 raise Program_Error with "Position cursor denotes wrong container";
1149 if Position.Index > Container.Last then
1150 raise Program_Error with "Position index is out of range";
1154 for J in Position.Index .. Container.Last loop
1155 if Container.Elements.EA (J) /= null
1156 and then Container.Elements.EA (J).all = Item
1158 return (Container'Unrestricted_Access, J);
1170 (Container : Vector;
1171 Item : Element_Type;
1172 Index : Index_Type := Index_Type'First) return Extended_Index
1175 for Indx in Index .. Container.Last loop
1176 if Container.Elements.EA (Indx) /= null
1177 and then Container.Elements.EA (Indx).all = Item
1190 function First (Container : Vector) return Cursor is
1192 if Is_Empty (Container) then
1196 return (Container'Unrestricted_Access, Index_Type'First);
1199 function First (Object : Iterator) return Cursor is
1201 -- The value of the iterator object's Index component influences the
1202 -- behavior of the First (and Last) selector function.
1204 -- When the Index component is No_Index, this means the iterator
1205 -- object was constructed without a start expression, in which case the
1206 -- (forward) iteration starts from the (logical) beginning of the entire
1207 -- sequence of items (corresponding to Container.First, for a forward
1210 -- Otherwise, this is iteration over a partial sequence of items.
1211 -- When the Index component isn't No_Index, the iterator object was
1212 -- constructed with a start expression, that specifies the position
1213 -- from which the (forward) partial iteration begins.
1215 if Object.Index = No_Index then
1216 return First (Object.Container.all);
1218 return Cursor'(Object.Container, Object.Index);
1226 function First_Element (Container : Vector) return Element_Type is
1228 if Container.Last = No_Index then
1229 raise Constraint_Error with "Container is empty";
1233 EA : constant Element_Access :=
1234 Container.Elements.EA (Index_Type'First);
1238 raise Constraint_Error with "first element is empty";
1249 function First_Index (Container : Vector) return Index_Type is
1250 pragma Unreferenced (Container);
1252 return Index_Type'First;
1255 ---------------------
1256 -- Generic_Sorting --
1257 ---------------------
1259 package body Generic_Sorting is
1261 -----------------------
1262 -- Local Subprograms --
1263 -----------------------
1265 function Is_Less (L, R : Element_Access) return Boolean;
1266 pragma Inline (Is_Less);
1272 function Is_Less (L, R : Element_Access) return Boolean is
1279 return L.all < R.all;
1287 function Is_Sorted (Container : Vector) return Boolean is
1289 if Container.Last <= Index_Type'First then
1294 E : Elements_Array renames Container.Elements.EA;
1296 for I in Index_Type'First .. Container.Last - 1 loop
1297 if Is_Less (E (I + 1), E (I)) then
1310 procedure Merge (Target, Source : in out Vector) is
1311 I, J : Index_Type'Base;
1315 -- The semantics of Merge changed slightly per AI05-0021. It was
1316 -- originally the case that if Target and Source denoted the same
1317 -- container object, then the GNAT implementation of Merge did
1318 -- nothing. However, it was argued that RM05 did not precisely
1319 -- specify the semantics for this corner case. The decision of the
1320 -- ARG was that if Target and Source denote the same non-empty
1321 -- container object, then Program_Error is raised.
1323 if Source.Last < Index_Type'First then -- Source is empty
1327 if Target'Address = Source'Address then
1328 raise Program_Error with
1329 "Target and Source denote same non-empty container";
1332 if Target.Last < Index_Type'First then -- Target is empty
1333 Move (Target => Target, Source => Source);
1337 if Source.Busy > 0 then
1338 raise Program_Error with
1339 "attempt to tamper with cursors (vector is busy)";
1342 I := Target.Last; -- original value (before Set_Length)
1343 Target.Set_Length (Length (Target) + Length (Source));
1345 J := Target.Last; -- new value (after Set_Length)
1346 while Source.Last >= Index_Type'First loop
1348 (Source.Last <= Index_Type'First
1349 or else not (Is_Less
1350 (Source.Elements.EA (Source.Last),
1351 Source.Elements.EA (Source.Last - 1))));
1353 if I < Index_Type'First then
1355 Src : Elements_Array renames
1356 Source.Elements.EA (Index_Type'First .. Source.Last);
1359 Target.Elements.EA (Index_Type'First .. J) := Src;
1360 Src := (others => null);
1363 Source.Last := No_Index;
1368 (I <= Index_Type'First
1369 or else not (Is_Less
1370 (Target.Elements.EA (I),
1371 Target.Elements.EA (I - 1))));
1374 Src : Element_Access renames Source.Elements.EA (Source.Last);
1375 Tgt : Element_Access renames Target.Elements.EA (I);
1378 if Is_Less (Src, Tgt) then
1379 Target.Elements.EA (J) := Tgt;
1384 Target.Elements.EA (J) := Src;
1386 Source.Last := Source.Last - 1;
1398 procedure Sort (Container : in out Vector) is
1400 procedure Sort is new Generic_Array_Sort
1401 (Index_Type => Index_Type,
1402 Element_Type => Element_Access,
1403 Array_Type => Elements_Array,
1406 -- Start of processing for Sort
1409 if Container.Last <= Index_Type'First then
1413 if Container.Lock > 0 then
1414 raise Program_Error with
1415 "attempt to tamper with elements (vector is locked)";
1418 Sort (Container.Elements.EA (Index_Type'First .. Container.Last));
1421 end Generic_Sorting;
1427 function Has_Element (Position : Cursor) return Boolean is
1429 if Position.Container = null then
1433 return Position.Index <= Position.Container.Last;
1441 (Container : in out Vector;
1442 Before : Extended_Index;
1443 New_Item : Element_Type;
1444 Count : Count_Type := 1)
1446 Old_Length : constant Count_Type := Container.Length;
1448 Max_Length : Count_Type'Base; -- determined from range of Index_Type
1449 New_Length : Count_Type'Base; -- sum of current length and Count
1450 New_Last : Index_Type'Base; -- last index of vector after insertion
1452 Index : Index_Type'Base; -- scratch for intermediate values
1453 J : Count_Type'Base; -- scratch
1455 New_Capacity : Count_Type'Base; -- length of new, expanded array
1456 Dst_Last : Index_Type'Base; -- last index of new, expanded array
1457 Dst : Elements_Access; -- new, expanded internal array
1460 -- As a precondition on the generic actual Index_Type, the base type
1461 -- must include Index_Type'Pred (Index_Type'First); this is the value
1462 -- that Container.Last assumes when the vector is empty. However, we do
1463 -- not allow that as the value for Index when specifying where the new
1464 -- items should be inserted, so we must manually check. (That the user
1465 -- is allowed to specify the value at all here is a consequence of the
1466 -- declaration of the Extended_Index subtype, which includes the values
1467 -- in the base range that immediately precede and immediately follow the
1468 -- values in the Index_Type.)
1470 if Before < Index_Type'First then
1471 raise Constraint_Error with
1472 "Before index is out of range (too small)";
1475 -- We do allow a value greater than Container.Last to be specified as
1476 -- the Index, but only if it's immediately greater. This allows for the
1477 -- case of appending items to the back end of the vector. (It is assumed
1478 -- that specifying an index value greater than Last + 1 indicates some
1479 -- deeper flaw in the caller's algorithm, so that case is treated as a
1482 if Before > Container.Last
1483 and then Before > Container.Last + 1
1485 raise Constraint_Error with
1486 "Before index is out of range (too large)";
1489 -- We treat inserting 0 items into the container as a no-op, even when
1490 -- the container is busy, so we simply return.
1496 -- There are two constraints we need to satisfy. The first constraint is
1497 -- that a container cannot have more than Count_Type'Last elements, so
1498 -- we must check the sum of the current length and the insertion count.
1499 -- Note that we cannot simply add these values, because of the
1500 -- possibility of overflow.
1502 if Old_Length > Count_Type'Last - Count then
1503 raise Constraint_Error with "Count is out of range";
1506 -- It is now safe compute the length of the new vector, without fear of
1509 New_Length := Old_Length + Count;
1511 -- The second constraint is that the new Last index value cannot exceed
1512 -- Index_Type'Last. In each branch below, we calculate the maximum
1513 -- length (computed from the range of values in Index_Type), and then
1514 -- compare the new length to the maximum length. If the new length is
1515 -- acceptable, then we compute the new last index from that.
1517 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1519 -- We have to handle the case when there might be more values in the
1520 -- range of Index_Type than in the range of Count_Type.
1522 if Index_Type'First <= 0 then
1524 -- We know that No_Index (the same as Index_Type'First - 1) is
1525 -- less than 0, so it is safe to compute the following sum without
1526 -- fear of overflow.
1528 Index := No_Index + Index_Type'Base (Count_Type'Last);
1530 if Index <= Index_Type'Last then
1532 -- We have determined that range of Index_Type has at least as
1533 -- many values as in Count_Type, so Count_Type'Last is the
1534 -- maximum number of items that are allowed.
1536 Max_Length := Count_Type'Last;
1539 -- The range of Index_Type has fewer values than in Count_Type,
1540 -- so the maximum number of items is computed from the range of
1543 Max_Length := Count_Type'Base (Index_Type'Last - No_Index);
1547 -- No_Index is equal or greater than 0, so we can safely compute
1548 -- the difference without fear of overflow (which we would have to
1549 -- worry about if No_Index were less than 0, but that case is
1552 Max_Length := Count_Type'Base (Index_Type'Last - No_Index);
1555 elsif Index_Type'First <= 0 then
1557 -- We know that No_Index (the same as Index_Type'First - 1) is less
1558 -- than 0, so it is safe to compute the following sum without fear of
1561 J := Count_Type'Base (No_Index) + Count_Type'Last;
1563 if J <= Count_Type'Base (Index_Type'Last) then
1565 -- We have determined that range of Index_Type has at least as
1566 -- many values as in Count_Type, so Count_Type'Last is the maximum
1567 -- number of items that are allowed.
1569 Max_Length := Count_Type'Last;
1572 -- The range of Index_Type has fewer values than Count_Type does,
1573 -- so the maximum number of items is computed from the range of
1577 Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index);
1581 -- No_Index is equal or greater than 0, so we can safely compute the
1582 -- difference without fear of overflow (which we would have to worry
1583 -- about if No_Index were less than 0, but that case is handled
1587 Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index);
1590 -- We have just computed the maximum length (number of items). We must
1591 -- now compare the requested length to the maximum length, as we do not
1592 -- allow a vector expand beyond the maximum (because that would create
1593 -- an internal array with a last index value greater than
1594 -- Index_Type'Last, with no way to index those elements).
1596 if New_Length > Max_Length then
1597 raise Constraint_Error with "Count is out of range";
1600 -- New_Last is the last index value of the items in the container after
1601 -- insertion. Use the wider of Index_Type'Base and Count_Type'Base to
1602 -- compute its value from the New_Length.
1604 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1605 New_Last := No_Index + Index_Type'Base (New_Length);
1608 New_Last := Index_Type'Base (Count_Type'Base (No_Index) + New_Length);
1611 if Container.Elements = null then
1612 pragma Assert (Container.Last = No_Index);
1614 -- This is the simplest case, with which we must always begin: we're
1615 -- inserting items into an empty vector that hasn't allocated an
1616 -- internal array yet. Note that we don't need to check the busy bit
1617 -- here, because an empty container cannot be busy.
1619 -- In an indefinite vector, elements are allocated individually, and
1620 -- stored as access values on the internal array (the length of which
1621 -- represents the vector "capacity"), which is separately allocated.
1623 Container.Elements := new Elements_Type (New_Last);
1625 -- The element backbone has been successfully allocated, so now we
1626 -- allocate the elements.
1628 for Idx in Container.Elements.EA'Range loop
1630 -- In order to preserve container invariants, we always attempt
1631 -- the element allocation first, before setting the Last index
1632 -- value, in case the allocation fails (either because there is no
1633 -- storage available, or because element initialization fails).
1635 Container.Elements.EA (Idx) := new Element_Type'(New_Item);
1637 -- The allocation of the element succeeded, so it is now safe to
1638 -- update the Last index, restoring container invariants.
1640 Container.Last := Idx;
1646 -- The tampering bits exist to prevent an item from being harmfully
1647 -- manipulated while it is being visited. Query, Update, and Iterate
1648 -- increment the busy count on entry, and decrement the count on
1649 -- exit. Insert checks the count to determine whether it is being called
1650 -- while the associated callback procedure is executing.
1652 if Container.Busy > 0 then
1653 raise Program_Error with
1654 "attempt to tamper with cursors (vector is busy)";
1657 if New_Length <= Container.Elements.EA'Length then
1659 -- In this case, we're inserting elements into a vector that has
1660 -- already allocated an internal array, and the existing array has
1661 -- enough unused storage for the new items.
1664 E : Elements_Array renames Container.Elements.EA;
1665 K : Index_Type'Base;
1668 if Before > Container.Last then
1670 -- The new items are being appended to the vector, so no
1671 -- sliding of existing elements is required.
1673 for Idx in Before .. New_Last loop
1675 -- In order to preserve container invariants, we always
1676 -- attempt the element allocation first, before setting the
1677 -- Last index value, in case the allocation fails (either
1678 -- because there is no storage available, or because element
1679 -- initialization fails).
1681 E (Idx) := new Element_Type'(New_Item);
1683 -- The allocation of the element succeeded, so it is now
1684 -- safe to update the Last index, restoring container
1687 Container.Last := Idx;
1691 -- The new items are being inserted before some existing
1692 -- elements, so we must slide the existing elements up to their
1693 -- new home. We use the wider of Index_Type'Base and
1694 -- Count_Type'Base as the type for intermediate index values.
1696 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1697 Index := Before + Index_Type'Base (Count);
1699 Index := Index_Type'Base (Count_Type'Base (Before) + Count);
1702 -- The new items are being inserted in the middle of the array,
1703 -- in the range [Before, Index). Copy the existing elements to
1704 -- the end of the array, to make room for the new items.
1706 E (Index .. New_Last) := E (Before .. Container.Last);
1707 Container.Last := New_Last;
1709 -- We have copied the existing items up to the end of the
1710 -- array, to make room for the new items in the middle of
1711 -- the array. Now we actually allocate the new items.
1713 -- Note: initialize K outside loop to make it clear that
1714 -- K always has a value if the exception handler triggers.
1718 while K < Index loop
1719 E (K) := new Element_Type'(New_Item);
1726 -- Values in the range [Before, K) were successfully
1727 -- allocated, but values in the range [K, Index) are
1728 -- stale (these array positions contain copies of the
1729 -- old items, that did not get assigned a new item,
1730 -- because the allocation failed). We must finish what
1731 -- we started by clearing out all of the stale values,
1732 -- leaving a "hole" in the middle of the array.
1734 E (K .. Index - 1) := (others => null);
1743 -- In this case, we're inserting elements into a vector that has already
1744 -- allocated an internal array, but the existing array does not have
1745 -- enough storage, so we must allocate a new, longer array. In order to
1746 -- guarantee that the amortized insertion cost is O(1), we always
1747 -- allocate an array whose length is some power-of-two factor of the
1748 -- current array length. (The new array cannot have a length less than
1749 -- the New_Length of the container, but its last index value cannot be
1750 -- greater than Index_Type'Last.)
1752 New_Capacity := Count_Type'Max (1, Container.Elements.EA'Length);
1753 while New_Capacity < New_Length loop
1754 if New_Capacity > Count_Type'Last / 2 then
1755 New_Capacity := Count_Type'Last;
1759 New_Capacity := 2 * New_Capacity;
1762 if New_Capacity > Max_Length then
1764 -- We have reached the limit of capacity, so no further expansion
1765 -- will occur. (This is not a problem, as there is never a need to
1766 -- have more capacity than the maximum container length.)
1768 New_Capacity := Max_Length;
1771 -- We have computed the length of the new internal array (and this is
1772 -- what "vector capacity" means), so use that to compute its last index.
1774 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1775 Dst_Last := No_Index + Index_Type'Base (New_Capacity);
1779 Index_Type'Base (Count_Type'Base (No_Index) + New_Capacity);
1782 -- Now we allocate the new, longer internal array. If the allocation
1783 -- fails, we have not changed any container state, so no side-effect
1784 -- will occur as a result of propagating the exception.
1786 Dst := new Elements_Type (Dst_Last);
1788 -- We have our new internal array. All that needs to be done now is to
1789 -- copy the existing items (if any) from the old array (the "source"
1790 -- array) to the new array (the "destination" array), and then
1791 -- deallocate the old array.
1794 Src : Elements_Access := Container.Elements;
1797 Dst.EA (Index_Type'First .. Before - 1) :=
1798 Src.EA (Index_Type'First .. Before - 1);
1800 if Before > Container.Last then
1802 -- The new items are being appended to the vector, so no
1803 -- sliding of existing elements is required.
1805 -- We have copied the elements from to the old, source array to
1806 -- the new, destination array, so we can now deallocate the old
1809 Container.Elements := Dst;
1812 -- Now we append the new items.
1814 for Idx in Before .. New_Last loop
1816 -- In order to preserve container invariants, we always
1817 -- attempt the element allocation first, before setting the
1818 -- Last index value, in case the allocation fails (either
1819 -- because there is no storage available, or because element
1820 -- initialization fails).
1822 Dst.EA (Idx) := new Element_Type'(New_Item);
1824 -- The allocation of the element succeeded, so it is now safe
1825 -- to update the Last index, restoring container invariants.
1827 Container.Last := Idx;
1831 -- The new items are being inserted before some existing elements,
1832 -- so we must slide the existing elements up to their new home.
1834 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1835 Index := Before + Index_Type'Base (Count);
1838 Index := Index_Type'Base (Count_Type'Base (Before) + Count);
1841 Dst.EA (Index .. New_Last) := Src.EA (Before .. Container.Last);
1843 -- We have copied the elements from to the old, source array to
1844 -- the new, destination array, so we can now deallocate the old
1847 Container.Elements := Dst;
1848 Container.Last := New_Last;
1851 -- The new array has a range in the middle containing null access
1852 -- values. We now fill in that partition of the array with the new
1855 for Idx in Before .. Index - 1 loop
1857 -- Note that container invariants have already been satisfied
1858 -- (in particular, the Last index value of the vector has
1859 -- already been updated), so if this allocation fails we simply
1860 -- let it propagate.
1862 Dst.EA (Idx) := new Element_Type'(New_Item);
1869 (Container : in out Vector;
1870 Before : Extended_Index;
1873 N : constant Count_Type := Length (New_Item);
1874 J : Index_Type'Base;
1877 -- Use Insert_Space to create the "hole" (the destination slice) into
1878 -- which we copy the source items.
1880 Insert_Space (Container, Before, Count => N);
1884 -- There's nothing else to do here (vetting of parameters was
1885 -- performed already in Insert_Space), so we simply return.
1890 if Container'Address /= New_Item'Address then
1892 -- This is the simple case. New_Item denotes an object different
1893 -- from Container, so there's nothing special we need to do to copy
1894 -- the source items to their destination, because all of the source
1895 -- items are contiguous.
1898 subtype Src_Index_Subtype is Index_Type'Base range
1899 Index_Type'First .. New_Item.Last;
1901 Src : Elements_Array renames
1902 New_Item.Elements.EA (Src_Index_Subtype);
1904 Dst : Elements_Array renames Container.Elements.EA;
1906 Dst_Index : Index_Type'Base;
1909 Dst_Index := Before - 1;
1910 for Src_Index in Src'Range loop
1911 Dst_Index := Dst_Index + 1;
1913 if Src (Src_Index) /= null then
1914 Dst (Dst_Index) := new Element_Type'(Src (Src_Index).all);
1922 -- New_Item denotes the same object as Container, so an insertion has
1923 -- potentially split the source items. The first source slice is
1924 -- [Index_Type'First, Before), and the second source slice is
1925 -- [J, Container.Last], where index value J is the first index of the
1926 -- second slice. (J gets computed below, but only after we have
1927 -- determined that the second source slice is non-empty.) The
1928 -- destination slice is always the range [Before, J). We perform the
1929 -- copy in two steps, using each of the two slices of the source items.
1932 L : constant Index_Type'Base := Before - 1;
1934 subtype Src_Index_Subtype is Index_Type'Base range
1935 Index_Type'First .. L;
1937 Src : Elements_Array renames
1938 Container.Elements.EA (Src_Index_Subtype);
1940 Dst : Elements_Array renames Container.Elements.EA;
1942 Dst_Index : Index_Type'Base;
1945 -- We first copy the source items that precede the space we
1946 -- inserted. (If Before equals Index_Type'First, then this first
1947 -- source slice will be empty, which is harmless.)
1949 Dst_Index := Before - 1;
1950 for Src_Index in Src'Range loop
1951 Dst_Index := Dst_Index + 1;
1953 if Src (Src_Index) /= null then
1954 Dst (Dst_Index) := new Element_Type'(Src (Src_Index).all);
1958 if Src'Length = N then
1960 -- The new items were effectively appended to the container, so we
1961 -- have already copied all of the items that need to be copied.
1962 -- We return early here, even though the source slice below is
1963 -- empty (so the assignment would be harmless), because we want to
1964 -- avoid computing J, which will overflow if J is greater than
1965 -- Index_Type'Base'Last.
1971 -- Index value J is the first index of the second source slice. (It is
1972 -- also 1 greater than the last index of the destination slice.) Note:
1973 -- avoid computing J if J is greater than Index_Type'Base'Last, in order
1974 -- to avoid overflow. Prevent that by returning early above, immediately
1975 -- after copying the first slice of the source, and determining that
1976 -- this second slice of the source is empty.
1978 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1979 J := Before + Index_Type'Base (N);
1982 J := Index_Type'Base (Count_Type'Base (Before) + N);
1986 subtype Src_Index_Subtype is Index_Type'Base range
1987 J .. Container.Last;
1989 Src : Elements_Array renames
1990 Container.Elements.EA (Src_Index_Subtype);
1992 Dst : Elements_Array renames Container.Elements.EA;
1994 Dst_Index : Index_Type'Base;
1997 -- We next copy the source items that follow the space we inserted.
1998 -- Index value Dst_Index is the first index of that portion of the
1999 -- destination that receives this slice of the source. (For the
2000 -- reasons given above, this slice is guaranteed to be non-empty.)
2002 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2003 Dst_Index := J - Index_Type'Base (Src'Length);
2006 Dst_Index := Index_Type'Base (Count_Type'Base (J) - Src'Length);
2009 for Src_Index in Src'Range loop
2010 if Src (Src_Index) /= null then
2011 Dst (Dst_Index) := new Element_Type'(Src (Src_Index).all);
2014 Dst_Index := Dst_Index + 1;
2020 (Container : in out Vector;
2024 Index : Index_Type'Base;
2027 if Before.Container /= null
2028 and then Before.Container /= Container'Unrestricted_Access
2030 raise Program_Error with "Before cursor denotes wrong container";
2033 if Is_Empty (New_Item) then
2037 if Before.Container = null
2038 or else Before.Index > Container.Last
2040 if Container.Last = Index_Type'Last then
2041 raise Constraint_Error with
2042 "vector is already at its maximum length";
2045 Index := Container.Last + 1;
2048 Index := Before.Index;
2051 Insert (Container, Index, New_Item);
2055 (Container : in out Vector;
2058 Position : out Cursor)
2060 Index : Index_Type'Base;
2063 if Before.Container /= null
2064 and then Before.Container /=
2065 Vector_Access'(Container'Unrestricted_Access)
2067 raise Program_Error with "Before cursor denotes wrong container";
2070 if Is_Empty (New_Item) then
2071 if Before.Container = null
2072 or else Before.Index > Container.Last
2074 Position := No_Element;
2076 Position := (Container'Unrestricted_Access, Before.Index);
2082 if Before.Container = null
2083 or else Before.Index > Container.Last
2085 if Container.Last = Index_Type'Last then
2086 raise Constraint_Error with
2087 "vector is already at its maximum length";
2090 Index := Container.Last + 1;
2093 Index := Before.Index;
2096 Insert (Container, Index, New_Item);
2098 Position := Cursor'(Container'Unrestricted_Access, Index);
2102 (Container : in out Vector;
2104 New_Item : Element_Type;
2105 Count : Count_Type := 1)
2107 Index : Index_Type'Base;
2110 if Before.Container /= null
2111 and then Before.Container /= Container'Unrestricted_Access
2113 raise Program_Error with "Before cursor denotes wrong container";
2120 if Before.Container = null
2121 or else Before.Index > Container.Last
2123 if Container.Last = Index_Type'Last then
2124 raise Constraint_Error with
2125 "vector is already at its maximum length";
2128 Index := Container.Last + 1;
2131 Index := Before.Index;
2134 Insert (Container, Index, New_Item, Count);
2138 (Container : in out Vector;
2140 New_Item : Element_Type;
2141 Position : out Cursor;
2142 Count : Count_Type := 1)
2144 Index : Index_Type'Base;
2147 if Before.Container /= null
2148 and then Before.Container /= Container'Unrestricted_Access
2150 raise Program_Error with "Before cursor denotes wrong container";
2154 if Before.Container = null
2155 or else Before.Index > Container.Last
2157 Position := No_Element;
2159 Position := (Container'Unrestricted_Access, Before.Index);
2165 if Before.Container = null
2166 or else Before.Index > Container.Last
2168 if Container.Last = Index_Type'Last then
2169 raise Constraint_Error with
2170 "vector is already at its maximum length";
2173 Index := Container.Last + 1;
2176 Index := Before.Index;
2179 Insert (Container, Index, New_Item, Count);
2181 Position := (Container'Unrestricted_Access, Index);
2188 procedure Insert_Space
2189 (Container : in out Vector;
2190 Before : Extended_Index;
2191 Count : Count_Type := 1)
2193 Old_Length : constant Count_Type := Container.Length;
2195 Max_Length : Count_Type'Base; -- determined from range of Index_Type
2196 New_Length : Count_Type'Base; -- sum of current length and Count
2197 New_Last : Index_Type'Base; -- last index of vector after insertion
2199 Index : Index_Type'Base; -- scratch for intermediate values
2200 J : Count_Type'Base; -- scratch
2202 New_Capacity : Count_Type'Base; -- length of new, expanded array
2203 Dst_Last : Index_Type'Base; -- last index of new, expanded array
2204 Dst : Elements_Access; -- new, expanded internal array
2207 -- As a precondition on the generic actual Index_Type, the base type
2208 -- must include Index_Type'Pred (Index_Type'First); this is the value
2209 -- that Container.Last assumes when the vector is empty. However, we do
2210 -- not allow that as the value for Index when specifying where the new
2211 -- items should be inserted, so we must manually check. (That the user
2212 -- is allowed to specify the value at all here is a consequence of the
2213 -- declaration of the Extended_Index subtype, which includes the values
2214 -- in the base range that immediately precede and immediately follow the
2215 -- values in the Index_Type.)
2217 if Before < Index_Type'First then
2218 raise Constraint_Error with
2219 "Before index is out of range (too small)";
2222 -- We do allow a value greater than Container.Last to be specified as
2223 -- the Index, but only if it's immediately greater. This allows for the
2224 -- case of appending items to the back end of the vector. (It is assumed
2225 -- that specifying an index value greater than Last + 1 indicates some
2226 -- deeper flaw in the caller's algorithm, so that case is treated as a
2229 if Before > Container.Last
2230 and then Before > Container.Last + 1
2232 raise Constraint_Error with
2233 "Before index is out of range (too large)";
2236 -- We treat inserting 0 items into the container as a no-op, even when
2237 -- the container is busy, so we simply return.
2243 -- There are two constraints we need to satisfy. The first constraint is
2244 -- that a container cannot have more than Count_Type'Last elements, so
2245 -- we must check the sum of the current length and the insertion
2246 -- count. Note that we cannot simply add these values, because of the
2247 -- possibility of overflow.
2249 if Old_Length > Count_Type'Last - Count then
2250 raise Constraint_Error with "Count is out of range";
2253 -- It is now safe compute the length of the new vector, without fear of
2256 New_Length := Old_Length + Count;
2258 -- The second constraint is that the new Last index value cannot exceed
2259 -- Index_Type'Last. In each branch below, we calculate the maximum
2260 -- length (computed from the range of values in Index_Type), and then
2261 -- compare the new length to the maximum length. If the new length is
2262 -- acceptable, then we compute the new last index from that.
2264 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2265 -- We have to handle the case when there might be more values in the
2266 -- range of Index_Type than in the range of Count_Type.
2268 if Index_Type'First <= 0 then
2270 -- We know that No_Index (the same as Index_Type'First - 1) is
2271 -- less than 0, so it is safe to compute the following sum without
2272 -- fear of overflow.
2274 Index := No_Index + Index_Type'Base (Count_Type'Last);
2276 if Index <= Index_Type'Last then
2278 -- We have determined that range of Index_Type has at least as
2279 -- many values as in Count_Type, so Count_Type'Last is the
2280 -- maximum number of items that are allowed.
2282 Max_Length := Count_Type'Last;
2285 -- The range of Index_Type has fewer values than in Count_Type,
2286 -- so the maximum number of items is computed from the range of
2289 Max_Length := Count_Type'Base (Index_Type'Last - No_Index);
2293 -- No_Index is equal or greater than 0, so we can safely compute
2294 -- the difference without fear of overflow (which we would have to
2295 -- worry about if No_Index were less than 0, but that case is
2298 Max_Length := Count_Type'Base (Index_Type'Last - No_Index);
2301 elsif Index_Type'First <= 0 then
2303 -- We know that No_Index (the same as Index_Type'First - 1) is less
2304 -- than 0, so it is safe to compute the following sum without fear of
2307 J := Count_Type'Base (No_Index) + Count_Type'Last;
2309 if J <= Count_Type'Base (Index_Type'Last) then
2311 -- We have determined that range of Index_Type has at least as
2312 -- many values as in Count_Type, so Count_Type'Last is the maximum
2313 -- number of items that are allowed.
2315 Max_Length := Count_Type'Last;
2318 -- The range of Index_Type has fewer values than Count_Type does,
2319 -- so the maximum number of items is computed from the range of
2323 Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index);
2327 -- No_Index is equal or greater than 0, so we can safely compute the
2328 -- difference without fear of overflow (which we would have to worry
2329 -- about if No_Index were less than 0, but that case is handled
2333 Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index);
2336 -- We have just computed the maximum length (number of items). We must
2337 -- now compare the requested length to the maximum length, as we do not
2338 -- allow a vector expand beyond the maximum (because that would create
2339 -- an internal array with a last index value greater than
2340 -- Index_Type'Last, with no way to index those elements).
2342 if New_Length > Max_Length then
2343 raise Constraint_Error with "Count is out of range";
2346 -- New_Last is the last index value of the items in the container after
2347 -- insertion. Use the wider of Index_Type'Base and Count_Type'Base to
2348 -- compute its value from the New_Length.
2350 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2351 New_Last := No_Index + Index_Type'Base (New_Length);
2354 New_Last := Index_Type'Base (Count_Type'Base (No_Index) + New_Length);
2357 if Container.Elements = null then
2358 pragma Assert (Container.Last = No_Index);
2360 -- This is the simplest case, with which we must always begin: we're
2361 -- inserting items into an empty vector that hasn't allocated an
2362 -- internal array yet. Note that we don't need to check the busy bit
2363 -- here, because an empty container cannot be busy.
2365 -- In an indefinite vector, elements are allocated individually, and
2366 -- stored as access values on the internal array (the length of which
2367 -- represents the vector "capacity"), which is separately allocated.
2368 -- We have no elements here (because we're inserting "space"), so all
2369 -- we need to do is allocate the backbone.
2371 Container.Elements := new Elements_Type (New_Last);
2372 Container.Last := New_Last;
2377 -- The tampering bits exist to prevent an item from being harmfully
2378 -- manipulated while it is being visited. Query, Update, and Iterate
2379 -- increment the busy count on entry, and decrement the count on exit.
2380 -- Insert checks the count to determine whether it is being called while
2381 -- the associated callback procedure is executing.
2383 if Container.Busy > 0 then
2384 raise Program_Error with
2385 "attempt to tamper with cursors (vector is busy)";
2388 if New_Length <= Container.Elements.EA'Length then
2389 -- In this case, we're inserting elements into a vector that has
2390 -- already allocated an internal array, and the existing array has
2391 -- enough unused storage for the new items.
2394 E : Elements_Array renames Container.Elements.EA;
2397 if Before <= Container.Last then
2399 -- The new space is being inserted before some existing
2400 -- elements, so we must slide the existing elements up to their
2401 -- new home. We use the wider of Index_Type'Base and
2402 -- Count_Type'Base as the type for intermediate index values.
2404 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2405 Index := Before + Index_Type'Base (Count);
2408 Index := Index_Type'Base (Count_Type'Base (Before) + Count);
2411 E (Index .. New_Last) := E (Before .. Container.Last);
2412 E (Before .. Index - 1) := (others => null);
2416 Container.Last := New_Last;
2420 -- In this case, we're inserting elements into a vector that has already
2421 -- allocated an internal array, but the existing array does not have
2422 -- enough storage, so we must allocate a new, longer array. In order to
2423 -- guarantee that the amortized insertion cost is O(1), we always
2424 -- allocate an array whose length is some power-of-two factor of the
2425 -- current array length. (The new array cannot have a length less than
2426 -- the New_Length of the container, but its last index value cannot be
2427 -- greater than Index_Type'Last.)
2429 New_Capacity := Count_Type'Max (1, Container.Elements.EA'Length);
2430 while New_Capacity < New_Length loop
2431 if New_Capacity > Count_Type'Last / 2 then
2432 New_Capacity := Count_Type'Last;
2436 New_Capacity := 2 * New_Capacity;
2439 if New_Capacity > Max_Length then
2441 -- We have reached the limit of capacity, so no further expansion
2442 -- will occur. (This is not a problem, as there is never a need to
2443 -- have more capacity than the maximum container length.)
2445 New_Capacity := Max_Length;
2448 -- We have computed the length of the new internal array (and this is
2449 -- what "vector capacity" means), so use that to compute its last index.
2451 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2452 Dst_Last := No_Index + Index_Type'Base (New_Capacity);
2456 Index_Type'Base (Count_Type'Base (No_Index) + New_Capacity);
2459 -- Now we allocate the new, longer internal array. If the allocation
2460 -- fails, we have not changed any container state, so no side-effect
2461 -- will occur as a result of propagating the exception.
2463 Dst := new Elements_Type (Dst_Last);
2465 -- We have our new internal array. All that needs to be done now is to
2466 -- copy the existing items (if any) from the old array (the "source"
2467 -- array) to the new array (the "destination" array), and then
2468 -- deallocate the old array.
2471 Src : Elements_Access := Container.Elements;
2474 Dst.EA (Index_Type'First .. Before - 1) :=
2475 Src.EA (Index_Type'First .. Before - 1);
2477 if Before <= Container.Last then
2479 -- The new items are being inserted before some existing elements,
2480 -- so we must slide the existing elements up to their new home.
2482 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2483 Index := Before + Index_Type'Base (Count);
2486 Index := Index_Type'Base (Count_Type'Base (Before) + Count);
2489 Dst.EA (Index .. New_Last) := Src.EA (Before .. Container.Last);
2492 -- We have copied the elements from to the old, source array to the
2493 -- new, destination array, so we can now restore invariants, and
2494 -- deallocate the old array.
2496 Container.Elements := Dst;
2497 Container.Last := New_Last;
2502 procedure Insert_Space
2503 (Container : in out Vector;
2505 Position : out Cursor;
2506 Count : Count_Type := 1)
2508 Index : Index_Type'Base;
2511 if Before.Container /= null
2512 and then Before.Container /= Container'Unrestricted_Access
2514 raise Program_Error with "Before cursor denotes wrong container";
2518 if Before.Container = null
2519 or else Before.Index > Container.Last
2521 Position := No_Element;
2523 Position := (Container'Unrestricted_Access, Before.Index);
2529 if Before.Container = null
2530 or else Before.Index > Container.Last
2532 if Container.Last = Index_Type'Last then
2533 raise Constraint_Error with
2534 "vector is already at its maximum length";
2537 Index := Container.Last + 1;
2540 Index := Before.Index;
2543 Insert_Space (Container, Index, Count);
2545 Position := Cursor'(Container'Unrestricted_Access, Index);
2552 function Is_Empty (Container : Vector) return Boolean is
2554 return Container.Last < Index_Type'First;
2562 (Container : Vector;
2563 Process : not null access procedure (Position : Cursor))
2565 B : Natural renames Container'Unrestricted_Access.all.Busy;
2571 for Indx in Index_Type'First .. Container.Last loop
2572 Process (Cursor'(Container'Unrestricted_Access, Indx));
2583 function Iterate (Container : Vector)
2584 return Vector_Iterator_Interfaces.Reversible_Iterator'Class
2586 V : constant Vector_Access := Container'Unrestricted_Access;
2587 B : Natural renames V.Busy;
2590 -- The value of its Index component influences the behavior of the First
2591 -- and Last selector functions of the iterator object. When the Index
2592 -- component is No_Index (as is the case here), this means the iterator
2593 -- object was constructed without a start expression. This is a complete
2594 -- iterator, meaning that the iteration starts from the (logical)
2595 -- beginning of the sequence of items.
2597 -- Note: For a forward iterator, Container.First is the beginning, and
2598 -- for a reverse iterator, Container.Last is the beginning.
2600 return It : constant Iterator :=
2601 (Limited_Controlled with
2610 (Container : Vector;
2612 return Vector_Iterator_Interfaces.Reversible_Iterator'Class
2614 V : constant Vector_Access := Container'Unrestricted_Access;
2615 B : Natural renames V.Busy;
2618 -- It was formerly the case that when Start = No_Element, the partial
2619 -- iterator was defined to behave the same as for a complete iterator,
2620 -- and iterate over the entire sequence of items. However, those
2621 -- semantics were unintuitive and arguably error-prone (it is too easy
2622 -- to accidentally create an endless loop), and so they were changed,
2623 -- per the ARG meeting in Denver on 2011/11. However, there was no
2624 -- consensus about what positive meaning this corner case should have,
2625 -- and so it was decided to simply raise an exception. This does imply,
2626 -- however, that it is not possible to use a partial iterator to specify
2627 -- an empty sequence of items.
2629 if Start.Container = null then
2630 raise Constraint_Error with
2631 "Start position for iterator equals No_Element";
2634 if Start.Container /= V then
2635 raise Program_Error with
2636 "Start cursor of Iterate designates wrong vector";
2639 if Start.Index > V.Last then
2640 raise Constraint_Error with
2641 "Start position for iterator equals No_Element";
2644 -- The value of its Index component influences the behavior of the First
2645 -- and Last selector functions of the iterator object. When the Index
2646 -- component is not No_Index (as is the case here), it means that this
2647 -- is a partial iteration, over a subset of the complete sequence of
2648 -- items. The iterator object was constructed with a start expression,
2649 -- indicating the position from which the iteration begins. Note that
2650 -- the start position has the same value irrespective of whether this
2651 -- is a forward or reverse iteration.
2653 return It : constant Iterator :=
2654 (Limited_Controlled with
2656 Index => Start.Index)
2666 function Last (Container : Vector) return Cursor is
2668 if Is_Empty (Container) then
2672 return (Container'Unrestricted_Access, Container.Last);
2675 function Last (Object : Iterator) return Cursor is
2677 -- The value of the iterator object's Index component influences the
2678 -- behavior of the Last (and First) selector function.
2680 -- When the Index component is No_Index, this means the iterator
2681 -- object was constructed without a start expression, in which case the
2682 -- (reverse) iteration starts from the (logical) beginning of the entire
2683 -- sequence (corresponding to Container.Last, for a reverse iterator).
2685 -- Otherwise, this is iteration over a partial sequence of items.
2686 -- When the Index component is not No_Index, the iterator object was
2687 -- constructed with a start expression, that specifies the position
2688 -- from which the (reverse) partial iteration begins.
2690 if Object.Index = No_Index then
2691 return Last (Object.Container.all);
2693 return Cursor'(Object.Container, Object.Index);
2701 function Last_Element (Container : Vector) return Element_Type is
2703 if Container.Last = No_Index then
2704 raise Constraint_Error with "Container is empty";
2708 EA : constant Element_Access :=
2709 Container.Elements.EA (Container.Last);
2713 raise Constraint_Error with "last element is empty";
2724 function Last_Index (Container : Vector) return Extended_Index is
2726 return Container.Last;
2733 function Length (Container : Vector) return Count_Type is
2734 L : constant Index_Type'Base := Container.Last;
2735 F : constant Index_Type := Index_Type'First;
2738 -- The base range of the index type (Index_Type'Base) might not include
2739 -- all values for length (Count_Type). Contrariwise, the index type
2740 -- might include values outside the range of length. Hence we use
2741 -- whatever type is wider for intermediate values when calculating
2742 -- length. Note that no matter what the index type is, the maximum
2743 -- length to which a vector is allowed to grow is always the minimum
2744 -- of Count_Type'Last and (IT'Last - IT'First + 1).
2746 -- For example, an Index_Type with range -127 .. 127 is only guaranteed
2747 -- to have a base range of -128 .. 127, but the corresponding vector
2748 -- would have lengths in the range 0 .. 255. In this case we would need
2749 -- to use Count_Type'Base for intermediate values.
2751 -- Another case would be the index range -2**63 + 1 .. -2**63 + 10. The
2752 -- vector would have a maximum length of 10, but the index values lie
2753 -- outside the range of Count_Type (which is only 32 bits). In this
2754 -- case we would need to use Index_Type'Base for intermediate values.
2756 if Count_Type'Base'Last >= Index_Type'Pos (Index_Type'Base'Last) then
2757 return Count_Type'Base (L) - Count_Type'Base (F) + 1;
2759 return Count_Type (L - F + 1);
2768 (Target : in out Vector;
2769 Source : in out Vector)
2772 if Target'Address = Source'Address then
2776 if Source.Busy > 0 then
2777 raise Program_Error with
2778 "attempt to tamper with cursors (Source is busy)";
2781 Clear (Target); -- Checks busy-bit
2784 Target_Elements : constant Elements_Access := Target.Elements;
2786 Target.Elements := Source.Elements;
2787 Source.Elements := Target_Elements;
2790 Target.Last := Source.Last;
2791 Source.Last := No_Index;
2798 function Next (Position : Cursor) return Cursor is
2800 if Position.Container = null then
2804 if Position.Index < Position.Container.Last then
2805 return (Position.Container, Position.Index + 1);
2811 function Next (Object : Iterator; Position : Cursor) return Cursor is
2813 if Position.Container = null then
2817 if Position.Container /= Object.Container then
2818 raise Program_Error with
2819 "Position cursor of Next designates wrong vector";
2822 return Next (Position);
2825 procedure Next (Position : in out Cursor) is
2827 if Position.Container = null then
2831 if Position.Index < Position.Container.Last then
2832 Position.Index := Position.Index + 1;
2834 Position := No_Element;
2842 procedure Prepend (Container : in out Vector; New_Item : Vector) is
2844 Insert (Container, Index_Type'First, New_Item);
2848 (Container : in out Vector;
2849 New_Item : Element_Type;
2850 Count : Count_Type := 1)
2863 procedure Previous (Position : in out Cursor) is
2865 if Position.Container = null then
2869 if Position.Index > Index_Type'First then
2870 Position.Index := Position.Index - 1;
2872 Position := No_Element;
2876 function Previous (Position : Cursor) return Cursor is
2878 if Position.Container = null then
2882 if Position.Index > Index_Type'First then
2883 return (Position.Container, Position.Index - 1);
2889 function Previous (Object : Iterator; Position : Cursor) return Cursor is
2891 if Position.Container = null then
2895 if Position.Container /= Object.Container then
2896 raise Program_Error with
2897 "Position cursor of Previous designates wrong vector";
2900 return Previous (Position);
2907 procedure Query_Element
2908 (Container : Vector;
2910 Process : not null access procedure (Element : Element_Type))
2912 V : Vector renames Container'Unrestricted_Access.all;
2913 B : Natural renames V.Busy;
2914 L : Natural renames V.Lock;
2917 if Index > Container.Last then
2918 raise Constraint_Error with "Index is out of range";
2921 if V.Elements.EA (Index) = null then
2922 raise Constraint_Error with "element is null";
2929 Process (V.Elements.EA (Index).all);
2941 procedure Query_Element
2943 Process : not null access procedure (Element : Element_Type))
2946 if Position.Container = null then
2947 raise Constraint_Error with "Position cursor has no element";
2950 Query_Element (Position.Container.all, Position.Index, Process);
2958 (Stream : not null access Root_Stream_Type'Class;
2959 Container : out Vector)
2961 Length : Count_Type'Base;
2962 Last : Index_Type'Base := Index_Type'Pred (Index_Type'First);
2969 Count_Type'Base'Read (Stream, Length);
2971 if Length > Capacity (Container) then
2972 Reserve_Capacity (Container, Capacity => Length);
2975 for J in Count_Type range 1 .. Length loop
2978 Boolean'Read (Stream, B);
2981 Container.Elements.EA (Last) :=
2982 new Element_Type'(Element_Type'Input (Stream));
2985 Container.Last := Last;
2990 (Stream : not null access Root_Stream_Type'Class;
2991 Position : out Cursor)
2994 raise Program_Error with "attempt to stream vector cursor";
2998 (Stream : not null access Root_Stream_Type'Class;
2999 Item : out Reference_Type)
3002 raise Program_Error with "attempt to stream reference";
3006 (Stream : not null access Root_Stream_Type'Class;
3007 Item : out Constant_Reference_Type)
3010 raise Program_Error with "attempt to stream reference";
3018 (Container : aliased in out Vector;
3019 Position : Cursor) return Reference_Type
3024 if Position.Container = null then
3025 raise Constraint_Error with "Position cursor has no element";
3028 if Position.Container /= Container'Unrestricted_Access then
3029 raise Program_Error with "Position cursor denotes wrong container";
3032 if Position.Index > Position.Container.Last then
3033 raise Constraint_Error with "Position cursor is out of range";
3036 E := Container.Elements.EA (Position.Index);
3039 raise Constraint_Error with "element at Position is empty";
3042 return (Element => E.all'Access);
3046 (Container : aliased in out Vector;
3047 Index : Index_Type) return Reference_Type
3052 if Index > Container.Last then
3053 raise Constraint_Error with "Index is out of range";
3056 E := Container.Elements.EA (Index);
3059 raise Constraint_Error with "element at Index is empty";
3062 return (Element => E.all'Access);
3065 ---------------------
3066 -- Replace_Element --
3067 ---------------------
3069 procedure Replace_Element
3070 (Container : in out Vector;
3072 New_Item : Element_Type)
3075 if Index > Container.Last then
3076 raise Constraint_Error with "Index is out of range";
3079 if Container.Lock > 0 then
3080 raise Program_Error with
3081 "attempt to tamper with elements (vector is locked)";
3085 X : Element_Access := Container.Elements.EA (Index);
3087 Container.Elements.EA (Index) := new Element_Type'(New_Item);
3090 end Replace_Element;
3092 procedure Replace_Element
3093 (Container : in out Vector;
3095 New_Item : Element_Type)
3098 if Position.Container = null then
3099 raise Constraint_Error with "Position cursor has no element";
3102 if Position.Container /= Container'Unrestricted_Access then
3103 raise Program_Error with "Position cursor denotes wrong container";
3106 if Position.Index > Container.Last then
3107 raise Constraint_Error with "Position cursor is out of range";
3110 if Container.Lock > 0 then
3111 raise Program_Error with
3112 "attempt to tamper with elements (vector is locked)";
3116 X : Element_Access := Container.Elements.EA (Position.Index);
3118 Container.Elements.EA (Position.Index) := new Element_Type'(New_Item);
3121 end Replace_Element;
3123 ----------------------
3124 -- Reserve_Capacity --
3125 ----------------------
3127 procedure Reserve_Capacity
3128 (Container : in out Vector;
3129 Capacity : Count_Type)
3131 N : constant Count_Type := Length (Container);
3133 Index : Count_Type'Base;
3134 Last : Index_Type'Base;
3137 -- Reserve_Capacity can be used to either expand the storage available
3138 -- for elements (this would be its typical use, in anticipation of
3139 -- future insertion), or to trim back storage. In the latter case,
3140 -- storage can only be trimmed back to the limit of the container
3141 -- length. Note that Reserve_Capacity neither deletes (active) elements
3142 -- nor inserts elements; it only affects container capacity, never
3143 -- container length.
3145 if Capacity = 0 then
3147 -- This is a request to trim back storage, to the minimum amount
3148 -- possible given the current state of the container.
3152 -- The container is empty, so in this unique case we can
3153 -- deallocate the entire internal array. Note that an empty
3154 -- container can never be busy, so there's no need to check the
3158 X : Elements_Access := Container.Elements;
3161 -- First we remove the internal array from the container, to
3162 -- handle the case when the deallocation raises an exception
3163 -- (although that's unlikely, since this is simply an array of
3164 -- access values, all of which are null).
3166 Container.Elements := null;
3168 -- Container invariants have been restored, so it is now safe
3169 -- to attempt to deallocate the internal array.
3174 elsif N < Container.Elements.EA'Length then
3176 -- The container is not empty, and the current length is less than
3177 -- the current capacity, so there's storage available to trim. In
3178 -- this case, we allocate a new internal array having a length
3179 -- that exactly matches the number of items in the
3180 -- container. (Reserve_Capacity does not delete active elements,
3181 -- so this is the best we can do with respect to minimizing
3184 if Container.Busy > 0 then
3185 raise Program_Error with
3186 "attempt to tamper with cursors (vector is busy)";
3190 subtype Array_Index_Subtype is Index_Type'Base range
3191 Index_Type'First .. Container.Last;
3193 Src : Elements_Array renames
3194 Container.Elements.EA (Array_Index_Subtype);
3196 X : Elements_Access := Container.Elements;
3199 -- Although we have isolated the old internal array that we're
3200 -- going to deallocate, we don't deallocate it until we have
3201 -- successfully allocated a new one. If there is an exception
3202 -- during allocation (because there is not enough storage), we
3203 -- let it propagate without causing any side-effect.
3205 Container.Elements := new Elements_Type'(Container.Last, Src);
3207 -- We have successfully allocated a new internal array (with a
3208 -- smaller length than the old one, and containing a copy of
3209 -- just the active elements in the container), so we can
3210 -- deallocate the old array.
3219 -- Reserve_Capacity can be used to expand the storage available for
3220 -- elements, but we do not let the capacity grow beyond the number of
3221 -- values in Index_Type'Range. (Were it otherwise, there would be no way
3222 -- to refer to the elements with index values greater than
3223 -- Index_Type'Last, so that storage would be wasted.) Here we compute
3224 -- the Last index value of the new internal array, in a way that avoids
3225 -- any possibility of overflow.
3227 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
3229 -- We perform a two-part test. First we determine whether the
3230 -- computed Last value lies in the base range of the type, and then
3231 -- determine whether it lies in the range of the index (sub)type.
3233 -- Last must satisfy this relation:
3234 -- First + Length - 1 <= Last
3235 -- We regroup terms:
3236 -- First - 1 <= Last - Length
3237 -- Which can rewrite as:
3238 -- No_Index <= Last - Length
3240 if Index_Type'Base'Last - Index_Type'Base (Capacity) < No_Index then
3241 raise Constraint_Error with "Capacity is out of range";
3244 -- We now know that the computed value of Last is within the base
3245 -- range of the type, so it is safe to compute its value:
3247 Last := No_Index + Index_Type'Base (Capacity);
3249 -- Finally we test whether the value is within the range of the
3250 -- generic actual index subtype:
3252 if Last > Index_Type'Last then
3253 raise Constraint_Error with "Capacity is out of range";
3256 elsif Index_Type'First <= 0 then
3258 -- Here we can compute Last directly, in the normal way. We know that
3259 -- No_Index is less than 0, so there is no danger of overflow when
3260 -- adding the (positive) value of Capacity.
3262 Index := Count_Type'Base (No_Index) + Capacity; -- Last
3264 if Index > Count_Type'Base (Index_Type'Last) then
3265 raise Constraint_Error with "Capacity is out of range";
3268 -- We know that the computed value (having type Count_Type) of Last
3269 -- is within the range of the generic actual index subtype, so it is
3270 -- safe to convert to Index_Type:
3272 Last := Index_Type'Base (Index);
3275 -- Here Index_Type'First (and Index_Type'Last) is positive, so we
3276 -- must test the length indirectly (by working backwards from the
3277 -- largest possible value of Last), in order to prevent overflow.
3279 Index := Count_Type'Base (Index_Type'Last) - Capacity; -- No_Index
3281 if Index < Count_Type'Base (No_Index) then
3282 raise Constraint_Error with "Capacity is out of range";
3285 -- We have determined that the value of Capacity would not create a
3286 -- Last index value outside of the range of Index_Type, so we can now
3287 -- safely compute its value.
3289 Last := Index_Type'Base (Count_Type'Base (No_Index) + Capacity);
3292 -- The requested capacity is non-zero, but we don't know yet whether
3293 -- this is a request for expansion or contraction of storage.
3295 if Container.Elements = null then
3297 -- The container is empty (it doesn't even have an internal array),
3298 -- so this represents a request to allocate storage having the given
3301 Container.Elements := new Elements_Type (Last);
3305 if Capacity <= N then
3307 -- This is a request to trim back storage, but only to the limit of
3308 -- what's already in the container. (Reserve_Capacity never deletes
3309 -- active elements, it only reclaims excess storage.)
3311 if N < Container.Elements.EA'Length then
3313 -- The container is not empty (because the requested capacity is
3314 -- positive, and less than or equal to the container length), and
3315 -- the current length is less than the current capacity, so there
3316 -- is storage available to trim. In this case, we allocate a new
3317 -- internal array having a length that exactly matches the number
3318 -- of items in the container.
3320 if Container.Busy > 0 then
3321 raise Program_Error with
3322 "attempt to tamper with cursors (vector is busy)";
3326 subtype Array_Index_Subtype is Index_Type'Base range
3327 Index_Type'First .. Container.Last;
3329 Src : Elements_Array renames
3330 Container.Elements.EA (Array_Index_Subtype);
3332 X : Elements_Access := Container.Elements;
3335 -- Although we have isolated the old internal array that we're
3336 -- going to deallocate, we don't deallocate it until we have
3337 -- successfully allocated a new one. If there is an exception
3338 -- during allocation (because there is not enough storage), we
3339 -- let it propagate without causing any side-effect.
3341 Container.Elements := new Elements_Type'(Container.Last, Src);
3343 -- We have successfully allocated a new internal array (with a
3344 -- smaller length than the old one, and containing a copy of
3345 -- just the active elements in the container), so it is now
3346 -- safe to deallocate the old array.
3355 -- The requested capacity is larger than the container length (the
3356 -- number of active elements). Whether this represents a request for
3357 -- expansion or contraction of the current capacity depends on what the
3358 -- current capacity is.
3360 if Capacity = Container.Elements.EA'Length then
3362 -- The requested capacity matches the existing capacity, so there's
3363 -- nothing to do here. We treat this case as a no-op, and simply
3364 -- return without checking the busy bit.
3369 -- There is a change in the capacity of a non-empty container, so a new
3370 -- internal array will be allocated. (The length of the new internal
3371 -- array could be less or greater than the old internal array. We know
3372 -- only that the length of the new internal array is greater than the
3373 -- number of active elements in the container.) We must check whether
3374 -- the container is busy before doing anything else.
3376 if Container.Busy > 0 then
3377 raise Program_Error with
3378 "attempt to tamper with cursors (vector is busy)";
3381 -- We now allocate a new internal array, having a length different from
3382 -- its current value.
3385 X : Elements_Access := Container.Elements;
3387 subtype Index_Subtype is Index_Type'Base range
3388 Index_Type'First .. Container.Last;
3391 -- We now allocate a new internal array, having a length different
3392 -- from its current value.
3394 Container.Elements := new Elements_Type (Last);
3396 -- We have successfully allocated the new internal array, so now we
3397 -- move the existing elements from the existing the old internal
3398 -- array onto the new one. Note that we're just copying access
3399 -- values, to this should not raise any exceptions.
3401 Container.Elements.EA (Index_Subtype) := X.EA (Index_Subtype);
3403 -- We have moved the elements from the old internal array, so now we
3404 -- can deallocate it.
3408 end Reserve_Capacity;
3410 ----------------------
3411 -- Reverse_Elements --
3412 ----------------------
3414 procedure Reverse_Elements (Container : in out Vector) is
3416 if Container.Length <= 1 then
3420 if Container.Lock > 0 then
3421 raise Program_Error with
3422 "attempt to tamper with elements (vector is locked)";
3428 E : Elements_Array renames Container.Elements.EA;
3431 I := Index_Type'First;
3432 J := Container.Last;
3435 EI : constant Element_Access := E (I);
3446 end Reverse_Elements;
3452 function Reverse_Find
3453 (Container : Vector;
3454 Item : Element_Type;
3455 Position : Cursor := No_Element) return Cursor
3457 Last : Index_Type'Base;
3460 if Position.Container /= null
3461 and then Position.Container /= Container'Unrestricted_Access
3463 raise Program_Error with "Position cursor denotes wrong container";
3466 if Position.Container = null
3467 or else Position.Index > Container.Last
3469 Last := Container.Last;
3471 Last := Position.Index;
3474 for Indx in reverse Index_Type'First .. Last loop
3475 if Container.Elements.EA (Indx) /= null
3476 and then Container.Elements.EA (Indx).all = Item
3478 return (Container'Unrestricted_Access, Indx);
3485 ------------------------
3486 -- Reverse_Find_Index --
3487 ------------------------
3489 function Reverse_Find_Index
3490 (Container : Vector;
3491 Item : Element_Type;
3492 Index : Index_Type := Index_Type'Last) return Extended_Index
3494 Last : constant Index_Type'Base :=
3495 (if Index > Container.Last then Container.Last else Index);
3497 for Indx in reverse Index_Type'First .. Last loop
3498 if Container.Elements.EA (Indx) /= null
3499 and then Container.Elements.EA (Indx).all = Item
3506 end Reverse_Find_Index;
3508 ---------------------
3509 -- Reverse_Iterate --
3510 ---------------------
3512 procedure Reverse_Iterate
3513 (Container : Vector;
3514 Process : not null access procedure (Position : Cursor))
3516 V : Vector renames Container'Unrestricted_Access.all;
3517 B : Natural renames V.Busy;
3523 for Indx in reverse Index_Type'First .. Container.Last loop
3524 Process (Cursor'(Container'Unrestricted_Access, Indx));
3533 end Reverse_Iterate;
3539 procedure Set_Length
3540 (Container : in out Vector;
3541 Length : Count_Type)
3543 Count : constant Count_Type'Base := Container.Length - Length;
3546 -- Set_Length allows the user to set the length explicitly, instead of
3547 -- implicitly as a side-effect of deletion or insertion. If the
3548 -- requested length is less than the current length, this is equivalent
3549 -- to deleting items from the back end of the vector. If the requested
3550 -- length is greater than the current length, then this is equivalent to
3551 -- inserting "space" (nonce items) at the end.
3554 Container.Delete_Last (Count);
3556 elsif Container.Last >= Index_Type'Last then
3557 raise Constraint_Error with "vector is already at its maximum length";
3560 Container.Insert_Space (Container.Last + 1, -Count);
3569 (Container : in out Vector;
3573 if I > Container.Last then
3574 raise Constraint_Error with "I index is out of range";
3577 if J > Container.Last then
3578 raise Constraint_Error with "J index is out of range";
3585 if Container.Lock > 0 then
3586 raise Program_Error with
3587 "attempt to tamper with elements (vector is locked)";
3591 EI : Element_Access renames Container.Elements.EA (I);
3592 EJ : Element_Access renames Container.Elements.EA (J);
3594 EI_Copy : constant Element_Access := EI;
3603 (Container : in out Vector;
3607 if I.Container = null then
3608 raise Constraint_Error with "I cursor has no element";
3611 if J.Container = null then
3612 raise Constraint_Error with "J cursor has no element";
3615 if I.Container /= Container'Unrestricted_Access then
3616 raise Program_Error with "I cursor denotes wrong container";
3619 if J.Container /= Container'Unrestricted_Access then
3620 raise Program_Error with "J cursor denotes wrong container";
3623 Swap (Container, I.Index, J.Index);
3631 (Container : Vector;
3632 Index : Extended_Index) return Cursor
3635 if Index not in Index_Type'First .. Container.Last then
3639 return Cursor'(Container'Unrestricted_Access, Index);
3646 function To_Index (Position : Cursor) return Extended_Index is
3648 if Position.Container = null then
3652 if Position.Index <= Position.Container.Last then
3653 return Position.Index;
3663 function To_Vector (Length : Count_Type) return Vector is
3664 Index : Count_Type'Base;
3665 Last : Index_Type'Base;
3666 Elements : Elements_Access;
3670 return Empty_Vector;
3673 -- We create a vector object with a capacity that matches the specified
3674 -- Length, but we do not allow the vector capacity (the length of the
3675 -- internal array) to exceed the number of values in Index_Type'Range
3676 -- (otherwise, there would be no way to refer to those components via an
3677 -- index). We must therefore check whether the specified Length would
3678 -- create a Last index value greater than Index_Type'Last.
3680 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
3682 -- We perform a two-part test. First we determine whether the
3683 -- computed Last value lies in the base range of the type, and then
3684 -- determine whether it lies in the range of the index (sub)type.
3686 -- Last must satisfy this relation:
3687 -- First + Length - 1 <= Last
3688 -- We regroup terms:
3689 -- First - 1 <= Last - Length
3690 -- Which can rewrite as:
3691 -- No_Index <= Last - Length
3693 if Index_Type'Base'Last - Index_Type'Base (Length) < No_Index then
3694 raise Constraint_Error with "Length is out of range";
3697 -- We now know that the computed value of Last is within the base
3698 -- range of the type, so it is safe to compute its value:
3700 Last := No_Index + Index_Type'Base (Length);
3702 -- Finally we test whether the value is within the range of the
3703 -- generic actual index subtype:
3705 if Last > Index_Type'Last then
3706 raise Constraint_Error with "Length is out of range";
3709 elsif Index_Type'First <= 0 then
3711 -- Here we can compute Last directly, in the normal way. We know that
3712 -- No_Index is less than 0, so there is no danger of overflow when
3713 -- adding the (positive) value of Length.
3715 Index := Count_Type'Base (No_Index) + Length; -- Last
3717 if Index > Count_Type'Base (Index_Type'Last) then
3718 raise Constraint_Error with "Length is out of range";
3721 -- We know that the computed value (having type Count_Type) of Last
3722 -- is within the range of the generic actual index subtype, so it is
3723 -- safe to convert to Index_Type:
3725 Last := Index_Type'Base (Index);
3728 -- Here Index_Type'First (and Index_Type'Last) is positive, so we
3729 -- must test the length indirectly (by working backwards from the
3730 -- largest possible value of Last), in order to prevent overflow.
3732 Index := Count_Type'Base (Index_Type'Last) - Length; -- No_Index
3734 if Index < Count_Type'Base (No_Index) then
3735 raise Constraint_Error with "Length is out of range";
3738 -- We have determined that the value of Length would not create a
3739 -- Last index value outside of the range of Index_Type, so we can now
3740 -- safely compute its value.
3742 Last := Index_Type'Base (Count_Type'Base (No_Index) + Length);
3745 Elements := new Elements_Type (Last);
3747 return Vector'(Controlled with Elements, Last, 0, 0);
3751 (New_Item : Element_Type;
3752 Length : Count_Type) return Vector
3754 Index : Count_Type'Base;
3755 Last : Index_Type'Base;
3756 Elements : Elements_Access;
3760 return Empty_Vector;
3763 -- We create a vector object with a capacity that matches the specified
3764 -- Length, but we do not allow the vector capacity (the length of the
3765 -- internal array) to exceed the number of values in Index_Type'Range
3766 -- (otherwise, there would be no way to refer to those components via an
3767 -- index). We must therefore check whether the specified Length would
3768 -- create a Last index value greater than Index_Type'Last.
3770 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
3772 -- We perform a two-part test. First we determine whether the
3773 -- computed Last value lies in the base range of the type, and then
3774 -- determine whether it lies in the range of the index (sub)type.
3776 -- Last must satisfy this relation:
3777 -- First + Length - 1 <= Last
3778 -- We regroup terms:
3779 -- First - 1 <= Last - Length
3780 -- Which can rewrite as:
3781 -- No_Index <= Last - Length
3783 if Index_Type'Base'Last - Index_Type'Base (Length) < No_Index then
3784 raise Constraint_Error with "Length is out of range";
3787 -- We now know that the computed value of Last is within the base
3788 -- range of the type, so it is safe to compute its value:
3790 Last := No_Index + Index_Type'Base (Length);
3792 -- Finally we test whether the value is within the range of the
3793 -- generic actual index subtype:
3795 if Last > Index_Type'Last then
3796 raise Constraint_Error with "Length is out of range";
3799 elsif Index_Type'First <= 0 then
3801 -- Here we can compute Last directly, in the normal way. We know that
3802 -- No_Index is less than 0, so there is no danger of overflow when
3803 -- adding the (positive) value of Length.
3805 Index := Count_Type'Base (No_Index) + Length; -- Last
3807 if Index > Count_Type'Base (Index_Type'Last) then
3808 raise Constraint_Error with "Length is out of range";
3811 -- We know that the computed value (having type Count_Type) of Last
3812 -- is within the range of the generic actual index subtype, so it is
3813 -- safe to convert to Index_Type:
3815 Last := Index_Type'Base (Index);
3818 -- Here Index_Type'First (and Index_Type'Last) is positive, so we
3819 -- must test the length indirectly (by working backwards from the
3820 -- largest possible value of Last), in order to prevent overflow.
3822 Index := Count_Type'Base (Index_Type'Last) - Length; -- No_Index
3824 if Index < Count_Type'Base (No_Index) then
3825 raise Constraint_Error with "Length is out of range";
3828 -- We have determined that the value of Length would not create a
3829 -- Last index value outside of the range of Index_Type, so we can now
3830 -- safely compute its value.
3832 Last := Index_Type'Base (Count_Type'Base (No_Index) + Length);
3835 Elements := new Elements_Type (Last);
3837 -- We use Last as the index of the loop used to populate the internal
3838 -- array with items. In general, we prefer to initialize the loop index
3839 -- immediately prior to entering the loop. However, Last is also used in
3840 -- the exception handler (to reclaim elements that have been allocated,
3841 -- before propagating the exception), and the initialization of Last
3842 -- after entering the block containing the handler confuses some static
3843 -- analysis tools, with respect to whether Last has been properly
3844 -- initialized when the handler executes. So here we initialize our loop
3845 -- variable earlier than we prefer, before entering the block, so there
3848 Last := Index_Type'First;
3852 Elements.EA (Last) := new Element_Type'(New_Item);
3853 exit when Last = Elements.Last;
3859 for J in Index_Type'First .. Last - 1 loop
3860 Free (Elements.EA (J));
3867 return (Controlled with Elements, Last, 0, 0);
3870 --------------------
3871 -- Update_Element --
3872 --------------------
3874 procedure Update_Element
3875 (Container : in out Vector;
3877 Process : not null access procedure (Element : in out Element_Type))
3879 B : Natural renames Container.Busy;
3880 L : Natural renames Container.Lock;
3883 if Index > Container.Last then
3884 raise Constraint_Error with "Index is out of range";
3887 if Container.Elements.EA (Index) = null then
3888 raise Constraint_Error with "element is null";
3895 Process (Container.Elements.EA (Index).all);
3907 procedure Update_Element
3908 (Container : in out Vector;
3910 Process : not null access procedure (Element : in out Element_Type))
3913 if Position.Container = null then
3914 raise Constraint_Error with "Position cursor has no element";
3917 if Position.Container /= Container'Unrestricted_Access then
3918 raise Program_Error with "Position cursor denotes wrong container";
3921 Update_Element (Container, Position.Index, Process);
3929 (Stream : not null access Root_Stream_Type'Class;
3932 N : constant Count_Type := Length (Container);
3935 Count_Type'Base'Write (Stream, N);
3942 E : Elements_Array renames Container.Elements.EA;
3945 for Indx in Index_Type'First .. Container.Last loop
3946 if E (Indx) = null then
3947 Boolean'Write (Stream, False);
3949 Boolean'Write (Stream, True);
3950 Element_Type'Output (Stream, E (Indx).all);
3957 (Stream : not null access Root_Stream_Type'Class;
3961 raise Program_Error with "attempt to stream vector cursor";
3965 (Stream : not null access Root_Stream_Type'Class;
3966 Item : Reference_Type)
3969 raise Program_Error with "attempt to stream reference";
3973 (Stream : not null access Root_Stream_Type'Class;
3974 Item : Constant_Reference_Type)
3977 raise Program_Error with "attempt to stream reference";
3980 end Ada.Containers.Indefinite_Vectors;