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-2012, 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);
581 procedure Adjust (Control : in out Reference_Control_Type) is
583 if Control.Container /= null then
585 C : Vector renames Control.Container.all;
586 B : Natural renames C.Busy;
587 L : Natural renames C.Lock;
599 procedure Append (Container : in out Vector; New_Item : Vector) is
601 if Is_Empty (New_Item) then
605 if Container.Last = Index_Type'Last then
606 raise Constraint_Error with "vector is already at its maximum length";
616 (Container : in out Vector;
617 New_Item : Element_Type;
618 Count : Count_Type := 1)
625 if Container.Last = Index_Type'Last then
626 raise Constraint_Error with "vector is already at its maximum length";
640 procedure Assign (Target : in out Vector; Source : Vector) is
642 if Target'Address = Source'Address then
647 Target.Append (Source);
654 function Capacity (Container : Vector) return Count_Type is
656 if Container.Elements = null then
660 return Container.Elements.EA'Length;
667 procedure Clear (Container : in out Vector) is
669 if Container.Busy > 0 then
670 raise Program_Error with
671 "attempt to tamper with cursors (vector is busy)";
674 while Container.Last >= Index_Type'First loop
676 X : Element_Access := Container.Elements.EA (Container.Last);
678 Container.Elements.EA (Container.Last) := null;
679 Container.Last := Container.Last - 1;
685 ------------------------
686 -- Constant_Reference --
687 ------------------------
689 function Constant_Reference
690 (Container : aliased Vector;
691 Position : Cursor) return Constant_Reference_Type
696 if Position.Container = null then
697 raise Constraint_Error with "Position cursor has no element";
700 if Position.Container /= Container'Unrestricted_Access then
701 raise Program_Error with "Position cursor denotes wrong container";
704 if Position.Index > Position.Container.Last then
705 raise Constraint_Error with "Position cursor is out of range";
708 E := Container.Elements.EA (Position.Index);
711 raise Constraint_Error with "element at Position is empty";
715 C : Vector renames Container'Unrestricted_Access.all;
716 B : Natural renames C.Busy;
717 L : Natural renames C.Lock;
719 return R : constant Constant_Reference_Type :=
720 (Element => E.all'Access,
722 (Controlled with Container'Unrestricted_Access))
728 end Constant_Reference;
730 function Constant_Reference
731 (Container : aliased Vector;
732 Index : Index_Type) return Constant_Reference_Type
737 if Index > Container.Last then
738 raise Constraint_Error with "Index is out of range";
741 E := Container.Elements.EA (Index);
744 raise Constraint_Error with "element at Index is empty";
748 C : Vector renames Container'Unrestricted_Access.all;
749 B : Natural renames C.Busy;
750 L : Natural renames C.Lock;
752 return R : constant Constant_Reference_Type :=
753 (Element => E.all'Access,
755 (Controlled with Container'Unrestricted_Access))
761 end Constant_Reference;
769 Item : Element_Type) return Boolean
772 return Find_Index (Container, Item) /= No_Index;
781 Capacity : Count_Type := 0) return Vector
789 elsif Capacity >= Source.Length then
794 with "Requested capacity is less than Source length";
797 return Target : Vector do
798 Target.Reserve_Capacity (C);
799 Target.Assign (Source);
808 (Container : in out Vector;
809 Index : Extended_Index;
810 Count : Count_Type := 1)
812 Old_Last : constant Index_Type'Base := Container.Last;
813 New_Last : Index_Type'Base;
814 Count2 : Count_Type'Base; -- count of items from Index to Old_Last
815 J : Index_Type'Base; -- first index of items that slide down
818 -- Delete removes items from the vector, the number of which is the
819 -- minimum of the specified Count and the items (if any) that exist from
820 -- Index to Container.Last. There are no constraints on the specified
821 -- value of Count (it can be larger than what's available at this
822 -- position in the vector, for example), but there are constraints on
823 -- the allowed values of the Index.
825 -- As a precondition on the generic actual Index_Type, the base type
826 -- must include Index_Type'Pred (Index_Type'First); this is the value
827 -- that Container.Last assumes when the vector is empty. However, we do
828 -- not allow that as the value for Index when specifying which items
829 -- should be deleted, so we must manually check. (That the user is
830 -- allowed to specify the value at all here is a consequence of the
831 -- declaration of the Extended_Index subtype, which includes the values
832 -- in the base range that immediately precede and immediately follow the
833 -- values in the Index_Type.)
835 if Index < Index_Type'First then
836 raise Constraint_Error with "Index is out of range (too small)";
839 -- We do allow a value greater than Container.Last to be specified as
840 -- the Index, but only if it's immediately greater. This allows the
841 -- corner case of deleting no items from the back end of the vector to
842 -- be treated as a no-op. (It is assumed that specifying an index value
843 -- greater than Last + 1 indicates some deeper flaw in the caller's
844 -- algorithm, so that case is treated as a proper error.)
846 if Index > Old_Last then
847 if Index > Old_Last + 1 then
848 raise Constraint_Error with "Index is out of range (too large)";
854 -- Here and elsewhere we treat deleting 0 items from the container as a
855 -- no-op, even when the container is busy, so we simply return.
861 -- The internal elements array isn't guaranteed to exist unless we have
862 -- elements, so we handle that case here in order to avoid having to
863 -- check it later. (Note that an empty vector can never be busy, so
864 -- there's no semantic harm in returning early.)
866 if Container.Is_Empty then
870 -- The tampering bits exist to prevent an item from being deleted (or
871 -- otherwise harmfully manipulated) while it is being visited. Query,
872 -- Update, and Iterate increment the busy count on entry, and decrement
873 -- the count on exit. Delete checks the count to determine whether it is
874 -- being called while the associated callback procedure is executing.
876 if Container.Busy > 0 then
877 raise Program_Error with
878 "attempt to tamper with cursors (vector is busy)";
881 -- We first calculate what's available for deletion starting at
882 -- Index. Here and elsewhere we use the wider of Index_Type'Base and
883 -- Count_Type'Base as the type for intermediate values. (See function
884 -- Length for more information.)
886 if Count_Type'Base'Last >= Index_Type'Pos (Index_Type'Base'Last) then
887 Count2 := Count_Type'Base (Old_Last) - Count_Type'Base (Index) + 1;
890 Count2 := Count_Type'Base (Old_Last - Index + 1);
893 -- If the number of elements requested (Count) for deletion is equal to
894 -- (or greater than) the number of elements available (Count2) for
895 -- deletion beginning at Index, then everything from Index to
896 -- Container.Last is deleted (this is equivalent to Delete_Last).
898 if Count >= Count2 then
899 -- Elements in an indefinite vector are allocated, so we must iterate
900 -- over the loop and deallocate elements one-at-a-time. We work from
901 -- back to front, deleting the last element during each pass, in
902 -- order to gracefully handle deallocation failures.
905 EA : Elements_Array renames Container.Elements.EA;
908 while Container.Last >= Index loop
910 K : constant Index_Type := Container.Last;
911 X : Element_Access := EA (K);
914 -- We first isolate the element we're deleting, removing it
915 -- from the vector before we attempt to deallocate it, in
916 -- case the deallocation fails.
919 Container.Last := K - 1;
921 -- Container invariants have been restored, so it is now
922 -- safe to attempt to deallocate the element.
932 -- There are some elements that aren't being deleted (the requested
933 -- count was less than the available count), so we must slide them down
934 -- to Index. We first calculate the index values of the respective array
935 -- slices, using the wider of Index_Type'Base and Count_Type'Base as the
936 -- type for intermediate calculations. For the elements that slide down,
937 -- index value New_Last is the last index value of their new home, and
938 -- index value J is the first index of their old home.
940 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
941 New_Last := Old_Last - Index_Type'Base (Count);
942 J := Index + Index_Type'Base (Count);
945 New_Last := Index_Type'Base (Count_Type'Base (Old_Last) - Count);
946 J := Index_Type'Base (Count_Type'Base (Index) + Count);
949 -- The internal elements array isn't guaranteed to exist unless we have
950 -- elements, but we have that guarantee here because we know we have
951 -- elements to slide. The array index values for each slice have
952 -- already been determined, so what remains to be done is to first
953 -- deallocate the elements that are being deleted, and then slide down
954 -- to Index the elements that aren't being deleted.
957 EA : Elements_Array renames Container.Elements.EA;
960 -- Before we can slide down the elements that aren't being deleted,
961 -- we need to deallocate the elements that are being deleted.
963 for K in Index .. J - 1 loop
965 X : Element_Access := EA (K);
968 -- First we remove the element we're about to deallocate from
969 -- the vector, in case the deallocation fails, in order to
970 -- preserve representation invariants.
974 -- The element has been removed from the vector, so it is now
975 -- safe to attempt to deallocate it.
981 EA (Index .. New_Last) := EA (J .. Old_Last);
982 Container.Last := New_Last;
987 (Container : in out Vector;
988 Position : in out Cursor;
989 Count : Count_Type := 1)
991 pragma Warnings (Off, Position);
994 if Position.Container = null then
995 raise Constraint_Error with "Position cursor has no element";
998 if Position.Container /= Container'Unrestricted_Access then
999 raise Program_Error with "Position cursor denotes wrong container";
1002 if Position.Index > Container.Last then
1003 raise Program_Error with "Position index is out of range";
1006 Delete (Container, Position.Index, Count);
1008 Position := No_Element;
1015 procedure Delete_First
1016 (Container : in out Vector;
1017 Count : Count_Type := 1)
1024 if Count >= Length (Container) then
1029 Delete (Container, Index_Type'First, Count);
1036 procedure Delete_Last
1037 (Container : in out Vector;
1038 Count : Count_Type := 1)
1041 -- It is not permitted to delete items while the container is busy (for
1042 -- example, we're in the middle of a passive iteration). However, we
1043 -- always treat deleting 0 items as a no-op, even when we're busy, so we
1044 -- simply return without checking.
1050 -- We cannot simply subsume the empty case into the loop below (the loop
1051 -- would iterate 0 times), because we rename the internal array object
1052 -- (which is allocated), but an empty vector isn't guaranteed to have
1053 -- actually allocated an array. (Note that an empty vector can never be
1054 -- busy, so there's no semantic harm in returning early here.)
1056 if Container.Is_Empty then
1060 -- The tampering bits exist to prevent an item from being deleted (or
1061 -- otherwise harmfully manipulated) while it is being visited. Query,
1062 -- Update, and Iterate increment the busy count on entry, and decrement
1063 -- the count on exit. Delete_Last checks the count to determine whether
1064 -- it is being called while the associated callback procedure is
1067 if Container.Busy > 0 then
1068 raise Program_Error with
1069 "attempt to tamper with cursors (vector is busy)";
1072 -- Elements in an indefinite vector are allocated, so we must iterate
1073 -- over the loop and deallocate elements one-at-a-time. We work from
1074 -- back to front, deleting the last element during each pass, in order
1075 -- to gracefully handle deallocation failures.
1078 E : Elements_Array renames Container.Elements.EA;
1081 for Indx in 1 .. Count_Type'Min (Count, Container.Length) loop
1083 J : constant Index_Type := Container.Last;
1084 X : Element_Access := E (J);
1087 -- Note that we first isolate the element we're deleting,
1088 -- removing it from the vector, before we actually deallocate
1089 -- it, in order to preserve representation invariants even if
1090 -- the deallocation fails.
1093 Container.Last := J - 1;
1095 -- Container invariants have been restored, so it is now safe
1096 -- to deallocate the element.
1109 (Container : Vector;
1110 Index : Index_Type) return Element_Type
1113 if Index > Container.Last then
1114 raise Constraint_Error with "Index is out of range";
1118 EA : constant Element_Access := Container.Elements.EA (Index);
1122 raise Constraint_Error with "element is empty";
1129 function Element (Position : Cursor) return Element_Type is
1131 if Position.Container = null then
1132 raise Constraint_Error with "Position cursor has no element";
1135 if Position.Index > Position.Container.Last then
1136 raise Constraint_Error with "Position cursor is out of range";
1140 EA : constant Element_Access :=
1141 Position.Container.Elements.EA (Position.Index);
1145 raise Constraint_Error with "element is empty";
1156 procedure Finalize (Container : in out Vector) is
1158 Clear (Container); -- Checks busy-bit
1161 X : Elements_Access := Container.Elements;
1163 Container.Elements := null;
1168 procedure Finalize (Object : in out Iterator) is
1169 B : Natural renames Object.Container.Busy;
1174 procedure Finalize (Control : in out Reference_Control_Type) is
1176 if Control.Container /= null then
1178 C : Vector renames Control.Container.all;
1179 B : Natural renames C.Busy;
1180 L : Natural renames C.Lock;
1186 Control.Container := null;
1195 (Container : Vector;
1196 Item : Element_Type;
1197 Position : Cursor := No_Element) return Cursor
1200 if Position.Container /= null then
1201 if Position.Container /= Container'Unrestricted_Access then
1202 raise Program_Error with "Position cursor denotes wrong container";
1205 if Position.Index > Container.Last then
1206 raise Program_Error with "Position index is out of range";
1210 for J in Position.Index .. Container.Last loop
1211 if Container.Elements.EA (J) /= null
1212 and then Container.Elements.EA (J).all = Item
1214 return (Container'Unrestricted_Access, J);
1226 (Container : Vector;
1227 Item : Element_Type;
1228 Index : Index_Type := Index_Type'First) return Extended_Index
1231 for Indx in Index .. Container.Last loop
1232 if Container.Elements.EA (Indx) /= null
1233 and then Container.Elements.EA (Indx).all = Item
1246 function First (Container : Vector) return Cursor is
1248 if Is_Empty (Container) then
1252 return (Container'Unrestricted_Access, Index_Type'First);
1255 function First (Object : Iterator) return Cursor is
1257 -- The value of the iterator object's Index component influences the
1258 -- behavior of the First (and Last) selector function.
1260 -- When the Index component is No_Index, this means the iterator
1261 -- object was constructed without a start expression, in which case the
1262 -- (forward) iteration starts from the (logical) beginning of the entire
1263 -- sequence of items (corresponding to Container.First, for a forward
1266 -- Otherwise, this is iteration over a partial sequence of items.
1267 -- When the Index component isn't No_Index, the iterator object was
1268 -- constructed with a start expression, that specifies the position
1269 -- from which the (forward) partial iteration begins.
1271 if Object.Index = No_Index then
1272 return First (Object.Container.all);
1274 return Cursor'(Object.Container, Object.Index);
1282 function First_Element (Container : Vector) return Element_Type is
1284 if Container.Last = No_Index then
1285 raise Constraint_Error with "Container is empty";
1289 EA : constant Element_Access :=
1290 Container.Elements.EA (Index_Type'First);
1294 raise Constraint_Error with "first element is empty";
1305 function First_Index (Container : Vector) return Index_Type is
1306 pragma Unreferenced (Container);
1308 return Index_Type'First;
1311 ---------------------
1312 -- Generic_Sorting --
1313 ---------------------
1315 package body Generic_Sorting is
1317 -----------------------
1318 -- Local Subprograms --
1319 -----------------------
1321 function Is_Less (L, R : Element_Access) return Boolean;
1322 pragma Inline (Is_Less);
1328 function Is_Less (L, R : Element_Access) return Boolean is
1335 return L.all < R.all;
1343 function Is_Sorted (Container : Vector) return Boolean is
1345 if Container.Last <= Index_Type'First then
1350 E : Elements_Array renames Container.Elements.EA;
1352 for I in Index_Type'First .. Container.Last - 1 loop
1353 if Is_Less (E (I + 1), E (I)) then
1366 procedure Merge (Target, Source : in out Vector) is
1367 I, J : Index_Type'Base;
1371 -- The semantics of Merge changed slightly per AI05-0021. It was
1372 -- originally the case that if Target and Source denoted the same
1373 -- container object, then the GNAT implementation of Merge did
1374 -- nothing. However, it was argued that RM05 did not precisely
1375 -- specify the semantics for this corner case. The decision of the
1376 -- ARG was that if Target and Source denote the same non-empty
1377 -- container object, then Program_Error is raised.
1379 if Source.Last < Index_Type'First then -- Source is empty
1383 if Target'Address = Source'Address then
1384 raise Program_Error with
1385 "Target and Source denote same non-empty container";
1388 if Target.Last < Index_Type'First then -- Target is empty
1389 Move (Target => Target, Source => Source);
1393 if Source.Busy > 0 then
1394 raise Program_Error with
1395 "attempt to tamper with cursors (vector is busy)";
1398 I := Target.Last; -- original value (before Set_Length)
1399 Target.Set_Length (Length (Target) + Length (Source));
1401 J := Target.Last; -- new value (after Set_Length)
1402 while Source.Last >= Index_Type'First loop
1404 (Source.Last <= Index_Type'First
1405 or else not (Is_Less
1406 (Source.Elements.EA (Source.Last),
1407 Source.Elements.EA (Source.Last - 1))));
1409 if I < Index_Type'First then
1411 Src : Elements_Array renames
1412 Source.Elements.EA (Index_Type'First .. Source.Last);
1415 Target.Elements.EA (Index_Type'First .. J) := Src;
1416 Src := (others => null);
1419 Source.Last := No_Index;
1424 (I <= Index_Type'First
1425 or else not (Is_Less
1426 (Target.Elements.EA (I),
1427 Target.Elements.EA (I - 1))));
1430 Src : Element_Access renames Source.Elements.EA (Source.Last);
1431 Tgt : Element_Access renames Target.Elements.EA (I);
1434 if Is_Less (Src, Tgt) then
1435 Target.Elements.EA (J) := Tgt;
1440 Target.Elements.EA (J) := Src;
1442 Source.Last := Source.Last - 1;
1454 procedure Sort (Container : in out Vector) is
1455 procedure Sort is new Generic_Array_Sort
1456 (Index_Type => Index_Type,
1457 Element_Type => Element_Access,
1458 Array_Type => Elements_Array,
1461 -- Start of processing for Sort
1464 if Container.Last <= Index_Type'First then
1468 -- The exception behavior for the vector container must match that
1469 -- for the list container, so we check for cursor tampering here
1470 -- (which will catch more things) instead of for element tampering
1471 -- (which will catch fewer things). It's true that the elements of
1472 -- this vector container could be safely moved around while (say) an
1473 -- iteration is taking place (iteration only increments the busy
1474 -- counter), and so technically all we would need here is a test for
1475 -- element tampering (indicated by the lock counter), that's simply
1476 -- an artifact of our array-based implementation. Logically Sort
1477 -- requires a check for cursor tampering.
1479 if Container.Busy > 0 then
1480 raise Program_Error with
1481 "attempt to tamper with cursors (vector is busy)";
1484 Sort (Container.Elements.EA (Index_Type'First .. Container.Last));
1487 end Generic_Sorting;
1493 function Has_Element (Position : Cursor) return Boolean is
1495 if Position.Container = null then
1499 return Position.Index <= Position.Container.Last;
1507 (Container : in out Vector;
1508 Before : Extended_Index;
1509 New_Item : Element_Type;
1510 Count : Count_Type := 1)
1512 Old_Length : constant Count_Type := Container.Length;
1514 Max_Length : Count_Type'Base; -- determined from range of Index_Type
1515 New_Length : Count_Type'Base; -- sum of current length and Count
1516 New_Last : Index_Type'Base; -- last index of vector after insertion
1518 Index : Index_Type'Base; -- scratch for intermediate values
1519 J : Count_Type'Base; -- scratch
1521 New_Capacity : Count_Type'Base; -- length of new, expanded array
1522 Dst_Last : Index_Type'Base; -- last index of new, expanded array
1523 Dst : Elements_Access; -- new, expanded internal array
1526 -- As a precondition on the generic actual Index_Type, the base type
1527 -- must include Index_Type'Pred (Index_Type'First); this is the value
1528 -- that Container.Last assumes when the vector is empty. However, we do
1529 -- not allow that as the value for Index when specifying where the new
1530 -- items should be inserted, so we must manually check. (That the user
1531 -- is allowed to specify the value at all here is a consequence of the
1532 -- declaration of the Extended_Index subtype, which includes the values
1533 -- in the base range that immediately precede and immediately follow the
1534 -- values in the Index_Type.)
1536 if Before < Index_Type'First then
1537 raise Constraint_Error with
1538 "Before index is out of range (too small)";
1541 -- We do allow a value greater than Container.Last to be specified as
1542 -- the Index, but only if it's immediately greater. This allows for the
1543 -- case of appending items to the back end of the vector. (It is assumed
1544 -- that specifying an index value greater than Last + 1 indicates some
1545 -- deeper flaw in the caller's algorithm, so that case is treated as a
1548 if Before > Container.Last
1549 and then Before > Container.Last + 1
1551 raise Constraint_Error with
1552 "Before index is out of range (too large)";
1555 -- We treat inserting 0 items into the container as a no-op, even when
1556 -- the container is busy, so we simply return.
1562 -- There are two constraints we need to satisfy. The first constraint is
1563 -- that a container cannot have more than Count_Type'Last elements, so
1564 -- we must check the sum of the current length and the insertion count.
1565 -- Note that we cannot simply add these values, because of the
1566 -- possibility of overflow.
1568 if Old_Length > Count_Type'Last - Count then
1569 raise Constraint_Error with "Count is out of range";
1572 -- It is now safe compute the length of the new vector, without fear of
1575 New_Length := Old_Length + Count;
1577 -- The second constraint is that the new Last index value cannot exceed
1578 -- Index_Type'Last. In each branch below, we calculate the maximum
1579 -- length (computed from the range of values in Index_Type), and then
1580 -- compare the new length to the maximum length. If the new length is
1581 -- acceptable, then we compute the new last index from that.
1583 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1585 -- We have to handle the case when there might be more values in the
1586 -- range of Index_Type than in the range of Count_Type.
1588 if Index_Type'First <= 0 then
1590 -- We know that No_Index (the same as Index_Type'First - 1) is
1591 -- less than 0, so it is safe to compute the following sum without
1592 -- fear of overflow.
1594 Index := No_Index + Index_Type'Base (Count_Type'Last);
1596 if Index <= Index_Type'Last then
1598 -- We have determined that range of Index_Type has at least as
1599 -- many values as in Count_Type, so Count_Type'Last is the
1600 -- maximum number of items that are allowed.
1602 Max_Length := Count_Type'Last;
1605 -- The range of Index_Type has fewer values than in Count_Type,
1606 -- so the maximum number of items is computed from the range of
1609 Max_Length := Count_Type'Base (Index_Type'Last - No_Index);
1613 -- No_Index is equal or greater than 0, so we can safely compute
1614 -- the difference without fear of overflow (which we would have to
1615 -- worry about if No_Index were less than 0, but that case is
1618 Max_Length := Count_Type'Base (Index_Type'Last - No_Index);
1621 elsif Index_Type'First <= 0 then
1623 -- We know that No_Index (the same as Index_Type'First - 1) is less
1624 -- than 0, so it is safe to compute the following sum without fear of
1627 J := Count_Type'Base (No_Index) + Count_Type'Last;
1629 if J <= Count_Type'Base (Index_Type'Last) then
1631 -- We have determined that range of Index_Type has at least as
1632 -- many values as in Count_Type, so Count_Type'Last is the maximum
1633 -- number of items that are allowed.
1635 Max_Length := Count_Type'Last;
1638 -- The range of Index_Type has fewer values than Count_Type does,
1639 -- so the maximum number of items is computed from the range of
1643 Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index);
1647 -- No_Index is equal or greater than 0, so we can safely compute the
1648 -- difference without fear of overflow (which we would have to worry
1649 -- about if No_Index were less than 0, but that case is handled
1653 Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index);
1656 -- We have just computed the maximum length (number of items). We must
1657 -- now compare the requested length to the maximum length, as we do not
1658 -- allow a vector expand beyond the maximum (because that would create
1659 -- an internal array with a last index value greater than
1660 -- Index_Type'Last, with no way to index those elements).
1662 if New_Length > Max_Length then
1663 raise Constraint_Error with "Count is out of range";
1666 -- New_Last is the last index value of the items in the container after
1667 -- insertion. Use the wider of Index_Type'Base and Count_Type'Base to
1668 -- compute its value from the New_Length.
1670 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1671 New_Last := No_Index + Index_Type'Base (New_Length);
1674 New_Last := Index_Type'Base (Count_Type'Base (No_Index) + New_Length);
1677 if Container.Elements = null then
1678 pragma Assert (Container.Last = No_Index);
1680 -- This is the simplest case, with which we must always begin: we're
1681 -- inserting items into an empty vector that hasn't allocated an
1682 -- internal array yet. Note that we don't need to check the busy bit
1683 -- here, because an empty container cannot be busy.
1685 -- In an indefinite vector, elements are allocated individually, and
1686 -- stored as access values on the internal array (the length of which
1687 -- represents the vector "capacity"), which is separately allocated.
1689 Container.Elements := new Elements_Type (New_Last);
1691 -- The element backbone has been successfully allocated, so now we
1692 -- allocate the elements.
1694 for Idx in Container.Elements.EA'Range loop
1696 -- In order to preserve container invariants, we always attempt
1697 -- the element allocation first, before setting the Last index
1698 -- value, in case the allocation fails (either because there is no
1699 -- storage available, or because element initialization fails).
1701 Container.Elements.EA (Idx) := new Element_Type'(New_Item);
1703 -- The allocation of the element succeeded, so it is now safe to
1704 -- update the Last index, restoring container invariants.
1706 Container.Last := Idx;
1712 -- The tampering bits exist to prevent an item from being harmfully
1713 -- manipulated while it is being visited. Query, Update, and Iterate
1714 -- increment the busy count on entry, and decrement the count on
1715 -- exit. Insert checks the count to determine whether it is being called
1716 -- while the associated callback procedure is executing.
1718 if Container.Busy > 0 then
1719 raise Program_Error with
1720 "attempt to tamper with cursors (vector is busy)";
1723 if New_Length <= Container.Elements.EA'Length then
1725 -- In this case, we're inserting elements into a vector that has
1726 -- already allocated an internal array, and the existing array has
1727 -- enough unused storage for the new items.
1730 E : Elements_Array renames Container.Elements.EA;
1731 K : Index_Type'Base;
1734 if Before > Container.Last then
1736 -- The new items are being appended to the vector, so no
1737 -- sliding of existing elements is required.
1739 for Idx in Before .. New_Last loop
1741 -- In order to preserve container invariants, we always
1742 -- attempt the element allocation first, before setting the
1743 -- Last index value, in case the allocation fails (either
1744 -- because there is no storage available, or because element
1745 -- initialization fails).
1747 E (Idx) := new Element_Type'(New_Item);
1749 -- The allocation of the element succeeded, so it is now
1750 -- safe to update the Last index, restoring container
1753 Container.Last := Idx;
1757 -- The new items are being inserted before some existing
1758 -- elements, so we must slide the existing elements up to their
1759 -- new home. We use the wider of Index_Type'Base and
1760 -- Count_Type'Base as the type for intermediate index values.
1762 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1763 Index := Before + Index_Type'Base (Count);
1765 Index := Index_Type'Base (Count_Type'Base (Before) + Count);
1768 -- The new items are being inserted in the middle of the array,
1769 -- in the range [Before, Index). Copy the existing elements to
1770 -- the end of the array, to make room for the new items.
1772 E (Index .. New_Last) := E (Before .. Container.Last);
1773 Container.Last := New_Last;
1775 -- We have copied the existing items up to the end of the
1776 -- array, to make room for the new items in the middle of
1777 -- the array. Now we actually allocate the new items.
1779 -- Note: initialize K outside loop to make it clear that
1780 -- K always has a value if the exception handler triggers.
1784 while K < Index loop
1785 E (K) := new Element_Type'(New_Item);
1792 -- Values in the range [Before, K) were successfully
1793 -- allocated, but values in the range [K, Index) are
1794 -- stale (these array positions contain copies of the
1795 -- old items, that did not get assigned a new item,
1796 -- because the allocation failed). We must finish what
1797 -- we started by clearing out all of the stale values,
1798 -- leaving a "hole" in the middle of the array.
1800 E (K .. Index - 1) := (others => null);
1809 -- In this case, we're inserting elements into a vector that has already
1810 -- allocated an internal array, but the existing array does not have
1811 -- enough storage, so we must allocate a new, longer array. In order to
1812 -- guarantee that the amortized insertion cost is O(1), we always
1813 -- allocate an array whose length is some power-of-two factor of the
1814 -- current array length. (The new array cannot have a length less than
1815 -- the New_Length of the container, but its last index value cannot be
1816 -- greater than Index_Type'Last.)
1818 New_Capacity := Count_Type'Max (1, Container.Elements.EA'Length);
1819 while New_Capacity < New_Length loop
1820 if New_Capacity > Count_Type'Last / 2 then
1821 New_Capacity := Count_Type'Last;
1825 New_Capacity := 2 * New_Capacity;
1828 if New_Capacity > Max_Length then
1830 -- We have reached the limit of capacity, so no further expansion
1831 -- will occur. (This is not a problem, as there is never a need to
1832 -- have more capacity than the maximum container length.)
1834 New_Capacity := Max_Length;
1837 -- We have computed the length of the new internal array (and this is
1838 -- what "vector capacity" means), so use that to compute its last index.
1840 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1841 Dst_Last := No_Index + Index_Type'Base (New_Capacity);
1845 Index_Type'Base (Count_Type'Base (No_Index) + New_Capacity);
1848 -- Now we allocate the new, longer internal array. If the allocation
1849 -- fails, we have not changed any container state, so no side-effect
1850 -- will occur as a result of propagating the exception.
1852 Dst := new Elements_Type (Dst_Last);
1854 -- We have our new internal array. All that needs to be done now is to
1855 -- copy the existing items (if any) from the old array (the "source"
1856 -- array) to the new array (the "destination" array), and then
1857 -- deallocate the old array.
1860 Src : Elements_Access := Container.Elements;
1863 Dst.EA (Index_Type'First .. Before - 1) :=
1864 Src.EA (Index_Type'First .. Before - 1);
1866 if Before > Container.Last then
1868 -- The new items are being appended to the vector, so no
1869 -- sliding of existing elements is required.
1871 -- We have copied the elements from to the old, source array to
1872 -- the new, destination array, so we can now deallocate the old
1875 Container.Elements := Dst;
1878 -- Now we append the new items.
1880 for Idx in Before .. New_Last loop
1882 -- In order to preserve container invariants, we always
1883 -- attempt the element allocation first, before setting the
1884 -- Last index value, in case the allocation fails (either
1885 -- because there is no storage available, or because element
1886 -- initialization fails).
1888 Dst.EA (Idx) := new Element_Type'(New_Item);
1890 -- The allocation of the element succeeded, so it is now safe
1891 -- to update the Last index, restoring container invariants.
1893 Container.Last := Idx;
1897 -- The new items are being inserted before some existing elements,
1898 -- so we must slide the existing elements up to their new home.
1900 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
1901 Index := Before + Index_Type'Base (Count);
1904 Index := Index_Type'Base (Count_Type'Base (Before) + Count);
1907 Dst.EA (Index .. New_Last) := Src.EA (Before .. Container.Last);
1909 -- We have copied the elements from to the old, source array to
1910 -- the new, destination array, so we can now deallocate the old
1913 Container.Elements := Dst;
1914 Container.Last := New_Last;
1917 -- The new array has a range in the middle containing null access
1918 -- values. We now fill in that partition of the array with the new
1921 for Idx in Before .. Index - 1 loop
1923 -- Note that container invariants have already been satisfied
1924 -- (in particular, the Last index value of the vector has
1925 -- already been updated), so if this allocation fails we simply
1926 -- let it propagate.
1928 Dst.EA (Idx) := new Element_Type'(New_Item);
1935 (Container : in out Vector;
1936 Before : Extended_Index;
1939 N : constant Count_Type := Length (New_Item);
1940 J : Index_Type'Base;
1943 -- Use Insert_Space to create the "hole" (the destination slice) into
1944 -- which we copy the source items.
1946 Insert_Space (Container, Before, Count => N);
1950 -- There's nothing else to do here (vetting of parameters was
1951 -- performed already in Insert_Space), so we simply return.
1956 if Container'Address /= New_Item'Address then
1958 -- This is the simple case. New_Item denotes an object different
1959 -- from Container, so there's nothing special we need to do to copy
1960 -- the source items to their destination, because all of the source
1961 -- items are contiguous.
1964 subtype Src_Index_Subtype is Index_Type'Base range
1965 Index_Type'First .. New_Item.Last;
1967 Src : Elements_Array renames
1968 New_Item.Elements.EA (Src_Index_Subtype);
1970 Dst : Elements_Array renames Container.Elements.EA;
1972 Dst_Index : Index_Type'Base;
1975 Dst_Index := Before - 1;
1976 for Src_Index in Src'Range loop
1977 Dst_Index := Dst_Index + 1;
1979 if Src (Src_Index) /= null then
1980 Dst (Dst_Index) := new Element_Type'(Src (Src_Index).all);
1988 -- New_Item denotes the same object as Container, so an insertion has
1989 -- potentially split the source items. The first source slice is
1990 -- [Index_Type'First, Before), and the second source slice is
1991 -- [J, Container.Last], where index value J is the first index of the
1992 -- second slice. (J gets computed below, but only after we have
1993 -- determined that the second source slice is non-empty.) The
1994 -- destination slice is always the range [Before, J). We perform the
1995 -- copy in two steps, using each of the two slices of the source items.
1998 L : constant Index_Type'Base := Before - 1;
2000 subtype Src_Index_Subtype is Index_Type'Base range
2001 Index_Type'First .. L;
2003 Src : Elements_Array renames
2004 Container.Elements.EA (Src_Index_Subtype);
2006 Dst : Elements_Array renames Container.Elements.EA;
2008 Dst_Index : Index_Type'Base;
2011 -- We first copy the source items that precede the space we
2012 -- inserted. (If Before equals Index_Type'First, then this first
2013 -- source slice will be empty, which is harmless.)
2015 Dst_Index := Before - 1;
2016 for Src_Index in Src'Range loop
2017 Dst_Index := Dst_Index + 1;
2019 if Src (Src_Index) /= null then
2020 Dst (Dst_Index) := new Element_Type'(Src (Src_Index).all);
2024 if Src'Length = N then
2026 -- The new items were effectively appended to the container, so we
2027 -- have already copied all of the items that need to be copied.
2028 -- We return early here, even though the source slice below is
2029 -- empty (so the assignment would be harmless), because we want to
2030 -- avoid computing J, which will overflow if J is greater than
2031 -- Index_Type'Base'Last.
2037 -- Index value J is the first index of the second source slice. (It is
2038 -- also 1 greater than the last index of the destination slice.) Note:
2039 -- avoid computing J if J is greater than Index_Type'Base'Last, in order
2040 -- to avoid overflow. Prevent that by returning early above, immediately
2041 -- after copying the first slice of the source, and determining that
2042 -- this second slice of the source is empty.
2044 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2045 J := Before + Index_Type'Base (N);
2048 J := Index_Type'Base (Count_Type'Base (Before) + N);
2052 subtype Src_Index_Subtype is Index_Type'Base range
2053 J .. Container.Last;
2055 Src : Elements_Array renames
2056 Container.Elements.EA (Src_Index_Subtype);
2058 Dst : Elements_Array renames Container.Elements.EA;
2060 Dst_Index : Index_Type'Base;
2063 -- We next copy the source items that follow the space we inserted.
2064 -- Index value Dst_Index is the first index of that portion of the
2065 -- destination that receives this slice of the source. (For the
2066 -- reasons given above, this slice is guaranteed to be non-empty.)
2068 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2069 Dst_Index := J - Index_Type'Base (Src'Length);
2072 Dst_Index := Index_Type'Base (Count_Type'Base (J) - Src'Length);
2075 for Src_Index in Src'Range loop
2076 if Src (Src_Index) /= null then
2077 Dst (Dst_Index) := new Element_Type'(Src (Src_Index).all);
2080 Dst_Index := Dst_Index + 1;
2086 (Container : in out Vector;
2090 Index : Index_Type'Base;
2093 if Before.Container /= null
2094 and then Before.Container /= Container'Unrestricted_Access
2096 raise Program_Error with "Before cursor denotes wrong container";
2099 if Is_Empty (New_Item) then
2103 if Before.Container = null
2104 or else Before.Index > Container.Last
2106 if Container.Last = Index_Type'Last then
2107 raise Constraint_Error with
2108 "vector is already at its maximum length";
2111 Index := Container.Last + 1;
2114 Index := Before.Index;
2117 Insert (Container, Index, New_Item);
2121 (Container : in out Vector;
2124 Position : out Cursor)
2126 Index : Index_Type'Base;
2129 if Before.Container /= null
2130 and then Before.Container /=
2131 Vector_Access'(Container'Unrestricted_Access)
2133 raise Program_Error with "Before cursor denotes wrong container";
2136 if Is_Empty (New_Item) then
2137 if Before.Container = null
2138 or else Before.Index > Container.Last
2140 Position := No_Element;
2142 Position := (Container'Unrestricted_Access, Before.Index);
2148 if Before.Container = null
2149 or else Before.Index > Container.Last
2151 if Container.Last = Index_Type'Last then
2152 raise Constraint_Error with
2153 "vector is already at its maximum length";
2156 Index := Container.Last + 1;
2159 Index := Before.Index;
2162 Insert (Container, Index, New_Item);
2164 Position := Cursor'(Container'Unrestricted_Access, Index);
2168 (Container : in out Vector;
2170 New_Item : Element_Type;
2171 Count : Count_Type := 1)
2173 Index : Index_Type'Base;
2176 if Before.Container /= null
2177 and then Before.Container /= Container'Unrestricted_Access
2179 raise Program_Error with "Before cursor denotes wrong container";
2186 if Before.Container = null
2187 or else Before.Index > Container.Last
2189 if Container.Last = Index_Type'Last then
2190 raise Constraint_Error with
2191 "vector is already at its maximum length";
2194 Index := Container.Last + 1;
2197 Index := Before.Index;
2200 Insert (Container, Index, New_Item, Count);
2204 (Container : in out Vector;
2206 New_Item : Element_Type;
2207 Position : out Cursor;
2208 Count : Count_Type := 1)
2210 Index : Index_Type'Base;
2213 if Before.Container /= null
2214 and then Before.Container /= Container'Unrestricted_Access
2216 raise Program_Error with "Before cursor denotes wrong container";
2220 if Before.Container = null
2221 or else Before.Index > Container.Last
2223 Position := No_Element;
2225 Position := (Container'Unrestricted_Access, Before.Index);
2231 if Before.Container = null
2232 or else Before.Index > Container.Last
2234 if Container.Last = Index_Type'Last then
2235 raise Constraint_Error with
2236 "vector is already at its maximum length";
2239 Index := Container.Last + 1;
2242 Index := Before.Index;
2245 Insert (Container, Index, New_Item, Count);
2247 Position := (Container'Unrestricted_Access, Index);
2254 procedure Insert_Space
2255 (Container : in out Vector;
2256 Before : Extended_Index;
2257 Count : Count_Type := 1)
2259 Old_Length : constant Count_Type := Container.Length;
2261 Max_Length : Count_Type'Base; -- determined from range of Index_Type
2262 New_Length : Count_Type'Base; -- sum of current length and Count
2263 New_Last : Index_Type'Base; -- last index of vector after insertion
2265 Index : Index_Type'Base; -- scratch for intermediate values
2266 J : Count_Type'Base; -- scratch
2268 New_Capacity : Count_Type'Base; -- length of new, expanded array
2269 Dst_Last : Index_Type'Base; -- last index of new, expanded array
2270 Dst : Elements_Access; -- new, expanded internal array
2273 -- As a precondition on the generic actual Index_Type, the base type
2274 -- must include Index_Type'Pred (Index_Type'First); this is the value
2275 -- that Container.Last assumes when the vector is empty. However, we do
2276 -- not allow that as the value for Index when specifying where the new
2277 -- items should be inserted, so we must manually check. (That the user
2278 -- is allowed to specify the value at all here is a consequence of the
2279 -- declaration of the Extended_Index subtype, which includes the values
2280 -- in the base range that immediately precede and immediately follow the
2281 -- values in the Index_Type.)
2283 if Before < Index_Type'First then
2284 raise Constraint_Error with
2285 "Before index is out of range (too small)";
2288 -- We do allow a value greater than Container.Last to be specified as
2289 -- the Index, but only if it's immediately greater. This allows for the
2290 -- case of appending items to the back end of the vector. (It is assumed
2291 -- that specifying an index value greater than Last + 1 indicates some
2292 -- deeper flaw in the caller's algorithm, so that case is treated as a
2295 if Before > Container.Last
2296 and then Before > Container.Last + 1
2298 raise Constraint_Error with
2299 "Before index is out of range (too large)";
2302 -- We treat inserting 0 items into the container as a no-op, even when
2303 -- the container is busy, so we simply return.
2309 -- There are two constraints we need to satisfy. The first constraint is
2310 -- that a container cannot have more than Count_Type'Last elements, so
2311 -- we must check the sum of the current length and the insertion
2312 -- count. Note that we cannot simply add these values, because of the
2313 -- possibility of overflow.
2315 if Old_Length > Count_Type'Last - Count then
2316 raise Constraint_Error with "Count is out of range";
2319 -- It is now safe compute the length of the new vector, without fear of
2322 New_Length := Old_Length + Count;
2324 -- The second constraint is that the new Last index value cannot exceed
2325 -- Index_Type'Last. In each branch below, we calculate the maximum
2326 -- length (computed from the range of values in Index_Type), and then
2327 -- compare the new length to the maximum length. If the new length is
2328 -- acceptable, then we compute the new last index from that.
2330 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2331 -- We have to handle the case when there might be more values in the
2332 -- range of Index_Type than in the range of Count_Type.
2334 if Index_Type'First <= 0 then
2336 -- We know that No_Index (the same as Index_Type'First - 1) is
2337 -- less than 0, so it is safe to compute the following sum without
2338 -- fear of overflow.
2340 Index := No_Index + Index_Type'Base (Count_Type'Last);
2342 if Index <= Index_Type'Last then
2344 -- We have determined that range of Index_Type has at least as
2345 -- many values as in Count_Type, so Count_Type'Last is the
2346 -- maximum number of items that are allowed.
2348 Max_Length := Count_Type'Last;
2351 -- The range of Index_Type has fewer values than in Count_Type,
2352 -- so the maximum number of items is computed from the range of
2355 Max_Length := Count_Type'Base (Index_Type'Last - No_Index);
2359 -- No_Index is equal or greater than 0, so we can safely compute
2360 -- the difference without fear of overflow (which we would have to
2361 -- worry about if No_Index were less than 0, but that case is
2364 Max_Length := Count_Type'Base (Index_Type'Last - No_Index);
2367 elsif Index_Type'First <= 0 then
2369 -- We know that No_Index (the same as Index_Type'First - 1) is less
2370 -- than 0, so it is safe to compute the following sum without fear of
2373 J := Count_Type'Base (No_Index) + Count_Type'Last;
2375 if J <= Count_Type'Base (Index_Type'Last) then
2377 -- We have determined that range of Index_Type has at least as
2378 -- many values as in Count_Type, so Count_Type'Last is the maximum
2379 -- number of items that are allowed.
2381 Max_Length := Count_Type'Last;
2384 -- The range of Index_Type has fewer values than Count_Type does,
2385 -- so the maximum number of items is computed from the range of
2389 Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index);
2393 -- No_Index is equal or greater than 0, so we can safely compute the
2394 -- difference without fear of overflow (which we would have to worry
2395 -- about if No_Index were less than 0, but that case is handled
2399 Count_Type'Base (Index_Type'Last) - Count_Type'Base (No_Index);
2402 -- We have just computed the maximum length (number of items). We must
2403 -- now compare the requested length to the maximum length, as we do not
2404 -- allow a vector expand beyond the maximum (because that would create
2405 -- an internal array with a last index value greater than
2406 -- Index_Type'Last, with no way to index those elements).
2408 if New_Length > Max_Length then
2409 raise Constraint_Error with "Count is out of range";
2412 -- New_Last is the last index value of the items in the container after
2413 -- insertion. Use the wider of Index_Type'Base and Count_Type'Base to
2414 -- compute its value from the New_Length.
2416 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2417 New_Last := No_Index + Index_Type'Base (New_Length);
2420 New_Last := Index_Type'Base (Count_Type'Base (No_Index) + New_Length);
2423 if Container.Elements = null then
2424 pragma Assert (Container.Last = No_Index);
2426 -- This is the simplest case, with which we must always begin: we're
2427 -- inserting items into an empty vector that hasn't allocated an
2428 -- internal array yet. Note that we don't need to check the busy bit
2429 -- here, because an empty container cannot be busy.
2431 -- In an indefinite vector, elements are allocated individually, and
2432 -- stored as access values on the internal array (the length of which
2433 -- represents the vector "capacity"), which is separately allocated.
2434 -- We have no elements here (because we're inserting "space"), so all
2435 -- we need to do is allocate the backbone.
2437 Container.Elements := new Elements_Type (New_Last);
2438 Container.Last := New_Last;
2443 -- The tampering bits exist to prevent an item from being harmfully
2444 -- manipulated while it is being visited. Query, Update, and Iterate
2445 -- increment the busy count on entry, and decrement the count on exit.
2446 -- Insert checks the count to determine whether it is being called while
2447 -- the associated callback procedure is executing.
2449 if Container.Busy > 0 then
2450 raise Program_Error with
2451 "attempt to tamper with cursors (vector is busy)";
2454 if New_Length <= Container.Elements.EA'Length then
2455 -- In this case, we're inserting elements into a vector that has
2456 -- already allocated an internal array, and the existing array has
2457 -- enough unused storage for the new items.
2460 E : Elements_Array renames Container.Elements.EA;
2463 if Before <= Container.Last then
2465 -- The new space is being inserted before some existing
2466 -- elements, so we must slide the existing elements up to their
2467 -- new home. We use the wider of Index_Type'Base and
2468 -- Count_Type'Base as the type for intermediate index values.
2470 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2471 Index := Before + Index_Type'Base (Count);
2474 Index := Index_Type'Base (Count_Type'Base (Before) + Count);
2477 E (Index .. New_Last) := E (Before .. Container.Last);
2478 E (Before .. Index - 1) := (others => null);
2482 Container.Last := New_Last;
2486 -- In this case, we're inserting elements into a vector that has already
2487 -- allocated an internal array, but the existing array does not have
2488 -- enough storage, so we must allocate a new, longer array. In order to
2489 -- guarantee that the amortized insertion cost is O(1), we always
2490 -- allocate an array whose length is some power-of-two factor of the
2491 -- current array length. (The new array cannot have a length less than
2492 -- the New_Length of the container, but its last index value cannot be
2493 -- greater than Index_Type'Last.)
2495 New_Capacity := Count_Type'Max (1, Container.Elements.EA'Length);
2496 while New_Capacity < New_Length loop
2497 if New_Capacity > Count_Type'Last / 2 then
2498 New_Capacity := Count_Type'Last;
2502 New_Capacity := 2 * New_Capacity;
2505 if New_Capacity > Max_Length then
2507 -- We have reached the limit of capacity, so no further expansion
2508 -- will occur. (This is not a problem, as there is never a need to
2509 -- have more capacity than the maximum container length.)
2511 New_Capacity := Max_Length;
2514 -- We have computed the length of the new internal array (and this is
2515 -- what "vector capacity" means), so use that to compute its last index.
2517 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2518 Dst_Last := No_Index + Index_Type'Base (New_Capacity);
2522 Index_Type'Base (Count_Type'Base (No_Index) + New_Capacity);
2525 -- Now we allocate the new, longer internal array. If the allocation
2526 -- fails, we have not changed any container state, so no side-effect
2527 -- will occur as a result of propagating the exception.
2529 Dst := new Elements_Type (Dst_Last);
2531 -- We have our new internal array. All that needs to be done now is to
2532 -- copy the existing items (if any) from the old array (the "source"
2533 -- array) to the new array (the "destination" array), and then
2534 -- deallocate the old array.
2537 Src : Elements_Access := Container.Elements;
2540 Dst.EA (Index_Type'First .. Before - 1) :=
2541 Src.EA (Index_Type'First .. Before - 1);
2543 if Before <= Container.Last then
2545 -- The new items are being inserted before some existing elements,
2546 -- so we must slide the existing elements up to their new home.
2548 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
2549 Index := Before + Index_Type'Base (Count);
2552 Index := Index_Type'Base (Count_Type'Base (Before) + Count);
2555 Dst.EA (Index .. New_Last) := Src.EA (Before .. Container.Last);
2558 -- We have copied the elements from to the old, source array to the
2559 -- new, destination array, so we can now restore invariants, and
2560 -- deallocate the old array.
2562 Container.Elements := Dst;
2563 Container.Last := New_Last;
2568 procedure Insert_Space
2569 (Container : in out Vector;
2571 Position : out Cursor;
2572 Count : Count_Type := 1)
2574 Index : Index_Type'Base;
2577 if Before.Container /= null
2578 and then Before.Container /= Container'Unrestricted_Access
2580 raise Program_Error with "Before cursor denotes wrong container";
2584 if Before.Container = null
2585 or else Before.Index > Container.Last
2587 Position := No_Element;
2589 Position := (Container'Unrestricted_Access, Before.Index);
2595 if Before.Container = null
2596 or else Before.Index > Container.Last
2598 if Container.Last = Index_Type'Last then
2599 raise Constraint_Error with
2600 "vector is already at its maximum length";
2603 Index := Container.Last + 1;
2606 Index := Before.Index;
2609 Insert_Space (Container, Index, Count);
2611 Position := Cursor'(Container'Unrestricted_Access, Index);
2618 function Is_Empty (Container : Vector) return Boolean is
2620 return Container.Last < Index_Type'First;
2628 (Container : Vector;
2629 Process : not null access procedure (Position : Cursor))
2631 B : Natural renames Container'Unrestricted_Access.all.Busy;
2637 for Indx in Index_Type'First .. Container.Last loop
2638 Process (Cursor'(Container'Unrestricted_Access, Indx));
2649 function Iterate (Container : Vector)
2650 return Vector_Iterator_Interfaces.Reversible_Iterator'Class
2652 V : constant Vector_Access := Container'Unrestricted_Access;
2653 B : Natural renames V.Busy;
2656 -- The value of its Index component influences the behavior of the First
2657 -- and Last selector functions of the iterator object. When the Index
2658 -- component is No_Index (as is the case here), this means the iterator
2659 -- object was constructed without a start expression. This is a complete
2660 -- iterator, meaning that the iteration starts from the (logical)
2661 -- beginning of the sequence of items.
2663 -- Note: For a forward iterator, Container.First is the beginning, and
2664 -- for a reverse iterator, Container.Last is the beginning.
2666 return It : constant Iterator :=
2667 (Limited_Controlled with
2676 (Container : Vector;
2678 return Vector_Iterator_Interfaces.Reversible_Iterator'Class
2680 V : constant Vector_Access := Container'Unrestricted_Access;
2681 B : Natural renames V.Busy;
2684 -- It was formerly the case that when Start = No_Element, the partial
2685 -- iterator was defined to behave the same as for a complete iterator,
2686 -- and iterate over the entire sequence of items. However, those
2687 -- semantics were unintuitive and arguably error-prone (it is too easy
2688 -- to accidentally create an endless loop), and so they were changed,
2689 -- per the ARG meeting in Denver on 2011/11. However, there was no
2690 -- consensus about what positive meaning this corner case should have,
2691 -- and so it was decided to simply raise an exception. This does imply,
2692 -- however, that it is not possible to use a partial iterator to specify
2693 -- an empty sequence of items.
2695 if Start.Container = null then
2696 raise Constraint_Error with
2697 "Start position for iterator equals No_Element";
2700 if Start.Container /= V then
2701 raise Program_Error with
2702 "Start cursor of Iterate designates wrong vector";
2705 if Start.Index > V.Last then
2706 raise Constraint_Error with
2707 "Start position for iterator equals No_Element";
2710 -- The value of its Index component influences the behavior of the First
2711 -- and Last selector functions of the iterator object. When the Index
2712 -- component is not No_Index (as is the case here), it means that this
2713 -- is a partial iteration, over a subset of the complete sequence of
2714 -- items. The iterator object was constructed with a start expression,
2715 -- indicating the position from which the iteration begins. Note that
2716 -- the start position has the same value irrespective of whether this
2717 -- is a forward or reverse iteration.
2719 return It : constant Iterator :=
2720 (Limited_Controlled with
2722 Index => Start.Index)
2732 function Last (Container : Vector) return Cursor is
2734 if Is_Empty (Container) then
2738 return (Container'Unrestricted_Access, Container.Last);
2741 function Last (Object : Iterator) return Cursor is
2743 -- The value of the iterator object's Index component influences the
2744 -- behavior of the Last (and First) selector function.
2746 -- When the Index component is No_Index, this means the iterator
2747 -- object was constructed without a start expression, in which case the
2748 -- (reverse) iteration starts from the (logical) beginning of the entire
2749 -- sequence (corresponding to Container.Last, for a reverse iterator).
2751 -- Otherwise, this is iteration over a partial sequence of items.
2752 -- When the Index component is not No_Index, the iterator object was
2753 -- constructed with a start expression, that specifies the position
2754 -- from which the (reverse) partial iteration begins.
2756 if Object.Index = No_Index then
2757 return Last (Object.Container.all);
2759 return Cursor'(Object.Container, Object.Index);
2767 function Last_Element (Container : Vector) return Element_Type is
2769 if Container.Last = No_Index then
2770 raise Constraint_Error with "Container is empty";
2774 EA : constant Element_Access :=
2775 Container.Elements.EA (Container.Last);
2779 raise Constraint_Error with "last element is empty";
2790 function Last_Index (Container : Vector) return Extended_Index is
2792 return Container.Last;
2799 function Length (Container : Vector) return Count_Type is
2800 L : constant Index_Type'Base := Container.Last;
2801 F : constant Index_Type := Index_Type'First;
2804 -- The base range of the index type (Index_Type'Base) might not include
2805 -- all values for length (Count_Type). Contrariwise, the index type
2806 -- might include values outside the range of length. Hence we use
2807 -- whatever type is wider for intermediate values when calculating
2808 -- length. Note that no matter what the index type is, the maximum
2809 -- length to which a vector is allowed to grow is always the minimum
2810 -- of Count_Type'Last and (IT'Last - IT'First + 1).
2812 -- For example, an Index_Type with range -127 .. 127 is only guaranteed
2813 -- to have a base range of -128 .. 127, but the corresponding vector
2814 -- would have lengths in the range 0 .. 255. In this case we would need
2815 -- to use Count_Type'Base for intermediate values.
2817 -- Another case would be the index range -2**63 + 1 .. -2**63 + 10. The
2818 -- vector would have a maximum length of 10, but the index values lie
2819 -- outside the range of Count_Type (which is only 32 bits). In this
2820 -- case we would need to use Index_Type'Base for intermediate values.
2822 if Count_Type'Base'Last >= Index_Type'Pos (Index_Type'Base'Last) then
2823 return Count_Type'Base (L) - Count_Type'Base (F) + 1;
2825 return Count_Type (L - F + 1);
2834 (Target : in out Vector;
2835 Source : in out Vector)
2838 if Target'Address = Source'Address then
2842 if Source.Busy > 0 then
2843 raise Program_Error with
2844 "attempt to tamper with cursors (Source is busy)";
2847 Clear (Target); -- Checks busy-bit
2850 Target_Elements : constant Elements_Access := Target.Elements;
2852 Target.Elements := Source.Elements;
2853 Source.Elements := Target_Elements;
2856 Target.Last := Source.Last;
2857 Source.Last := No_Index;
2864 function Next (Position : Cursor) return Cursor is
2866 if Position.Container = null then
2870 if Position.Index < Position.Container.Last then
2871 return (Position.Container, Position.Index + 1);
2877 function Next (Object : Iterator; Position : Cursor) return Cursor is
2879 if Position.Container = null then
2883 if Position.Container /= Object.Container then
2884 raise Program_Error with
2885 "Position cursor of Next designates wrong vector";
2888 return Next (Position);
2891 procedure Next (Position : in out Cursor) is
2893 if Position.Container = null then
2897 if Position.Index < Position.Container.Last then
2898 Position.Index := Position.Index + 1;
2900 Position := No_Element;
2908 procedure Prepend (Container : in out Vector; New_Item : Vector) is
2910 Insert (Container, Index_Type'First, New_Item);
2914 (Container : in out Vector;
2915 New_Item : Element_Type;
2916 Count : Count_Type := 1)
2929 procedure Previous (Position : in out Cursor) is
2931 if Position.Container = null then
2935 if Position.Index > Index_Type'First then
2936 Position.Index := Position.Index - 1;
2938 Position := No_Element;
2942 function Previous (Position : Cursor) return Cursor is
2944 if Position.Container = null then
2948 if Position.Index > Index_Type'First then
2949 return (Position.Container, Position.Index - 1);
2955 function Previous (Object : Iterator; Position : Cursor) return Cursor is
2957 if Position.Container = null then
2961 if Position.Container /= Object.Container then
2962 raise Program_Error with
2963 "Position cursor of Previous designates wrong vector";
2966 return Previous (Position);
2973 procedure Query_Element
2974 (Container : Vector;
2976 Process : not null access procedure (Element : Element_Type))
2978 V : Vector renames Container'Unrestricted_Access.all;
2979 B : Natural renames V.Busy;
2980 L : Natural renames V.Lock;
2983 if Index > Container.Last then
2984 raise Constraint_Error with "Index is out of range";
2987 if V.Elements.EA (Index) = null then
2988 raise Constraint_Error with "element is null";
2995 Process (V.Elements.EA (Index).all);
3007 procedure Query_Element
3009 Process : not null access procedure (Element : Element_Type))
3012 if Position.Container = null then
3013 raise Constraint_Error with "Position cursor has no element";
3016 Query_Element (Position.Container.all, Position.Index, Process);
3024 (Stream : not null access Root_Stream_Type'Class;
3025 Container : out Vector)
3027 Length : Count_Type'Base;
3028 Last : Index_Type'Base := Index_Type'Pred (Index_Type'First);
3035 Count_Type'Base'Read (Stream, Length);
3037 if Length > Capacity (Container) then
3038 Reserve_Capacity (Container, Capacity => Length);
3041 for J in Count_Type range 1 .. Length loop
3044 Boolean'Read (Stream, B);
3047 Container.Elements.EA (Last) :=
3048 new Element_Type'(Element_Type'Input (Stream));
3051 Container.Last := Last;
3056 (Stream : not null access Root_Stream_Type'Class;
3057 Position : out Cursor)
3060 raise Program_Error with "attempt to stream vector cursor";
3064 (Stream : not null access Root_Stream_Type'Class;
3065 Item : out Reference_Type)
3068 raise Program_Error with "attempt to stream reference";
3072 (Stream : not null access Root_Stream_Type'Class;
3073 Item : out Constant_Reference_Type)
3076 raise Program_Error with "attempt to stream reference";
3084 (Container : aliased in out Vector;
3085 Position : Cursor) return Reference_Type
3090 if Position.Container = null then
3091 raise Constraint_Error with "Position cursor has no element";
3094 if Position.Container /= Container'Unrestricted_Access then
3095 raise Program_Error with "Position cursor denotes wrong container";
3098 if Position.Index > Position.Container.Last then
3099 raise Constraint_Error with "Position cursor is out of range";
3102 E := Container.Elements.EA (Position.Index);
3105 raise Constraint_Error with "element at Position is empty";
3109 C : Vector renames Container'Unrestricted_Access.all;
3110 B : Natural renames C.Busy;
3111 L : Natural renames C.Lock;
3113 return R : constant Reference_Type :=
3114 (Element => E.all'Access,
3115 Control => (Controlled with Position.Container))
3124 (Container : aliased in out Vector;
3125 Index : Index_Type) return Reference_Type
3130 if Index > Container.Last then
3131 raise Constraint_Error with "Index is out of range";
3134 E := Container.Elements.EA (Index);
3137 raise Constraint_Error with "element at Index is empty";
3141 C : Vector renames Container'Unrestricted_Access.all;
3142 B : Natural renames C.Busy;
3143 L : Natural renames C.Lock;
3145 return R : constant Reference_Type :=
3146 (Element => E.all'Access,
3148 (Controlled with Container'Unrestricted_Access))
3156 ---------------------
3157 -- Replace_Element --
3158 ---------------------
3160 procedure Replace_Element
3161 (Container : in out Vector;
3163 New_Item : Element_Type)
3166 if Index > Container.Last then
3167 raise Constraint_Error with "Index is out of range";
3170 if Container.Lock > 0 then
3171 raise Program_Error with
3172 "attempt to tamper with elements (vector is locked)";
3176 X : Element_Access := Container.Elements.EA (Index);
3178 Container.Elements.EA (Index) := new Element_Type'(New_Item);
3181 end Replace_Element;
3183 procedure Replace_Element
3184 (Container : in out Vector;
3186 New_Item : Element_Type)
3189 if Position.Container = null then
3190 raise Constraint_Error with "Position cursor has no element";
3193 if Position.Container /= Container'Unrestricted_Access then
3194 raise Program_Error with "Position cursor denotes wrong container";
3197 if Position.Index > Container.Last then
3198 raise Constraint_Error with "Position cursor is out of range";
3201 if Container.Lock > 0 then
3202 raise Program_Error with
3203 "attempt to tamper with elements (vector is locked)";
3207 X : Element_Access := Container.Elements.EA (Position.Index);
3209 Container.Elements.EA (Position.Index) := new Element_Type'(New_Item);
3212 end Replace_Element;
3214 ----------------------
3215 -- Reserve_Capacity --
3216 ----------------------
3218 procedure Reserve_Capacity
3219 (Container : in out Vector;
3220 Capacity : Count_Type)
3222 N : constant Count_Type := Length (Container);
3224 Index : Count_Type'Base;
3225 Last : Index_Type'Base;
3228 -- Reserve_Capacity can be used to either expand the storage available
3229 -- for elements (this would be its typical use, in anticipation of
3230 -- future insertion), or to trim back storage. In the latter case,
3231 -- storage can only be trimmed back to the limit of the container
3232 -- length. Note that Reserve_Capacity neither deletes (active) elements
3233 -- nor inserts elements; it only affects container capacity, never
3234 -- container length.
3236 if Capacity = 0 then
3238 -- This is a request to trim back storage, to the minimum amount
3239 -- possible given the current state of the container.
3243 -- The container is empty, so in this unique case we can
3244 -- deallocate the entire internal array. Note that an empty
3245 -- container can never be busy, so there's no need to check the
3249 X : Elements_Access := Container.Elements;
3252 -- First we remove the internal array from the container, to
3253 -- handle the case when the deallocation raises an exception
3254 -- (although that's unlikely, since this is simply an array of
3255 -- access values, all of which are null).
3257 Container.Elements := null;
3259 -- Container invariants have been restored, so it is now safe
3260 -- to attempt to deallocate the internal array.
3265 elsif N < Container.Elements.EA'Length then
3267 -- The container is not empty, and the current length is less than
3268 -- the current capacity, so there's storage available to trim. In
3269 -- this case, we allocate a new internal array having a length
3270 -- that exactly matches the number of items in the
3271 -- container. (Reserve_Capacity does not delete active elements,
3272 -- so this is the best we can do with respect to minimizing
3275 if Container.Busy > 0 then
3276 raise Program_Error with
3277 "attempt to tamper with cursors (vector is busy)";
3281 subtype Array_Index_Subtype is Index_Type'Base range
3282 Index_Type'First .. Container.Last;
3284 Src : Elements_Array renames
3285 Container.Elements.EA (Array_Index_Subtype);
3287 X : Elements_Access := Container.Elements;
3290 -- Although we have isolated the old internal array that we're
3291 -- going to deallocate, we don't deallocate it until we have
3292 -- successfully allocated a new one. If there is an exception
3293 -- during allocation (because there is not enough storage), we
3294 -- let it propagate without causing any side-effect.
3296 Container.Elements := new Elements_Type'(Container.Last, Src);
3298 -- We have successfully allocated a new internal array (with a
3299 -- smaller length than the old one, and containing a copy of
3300 -- just the active elements in the container), so we can
3301 -- deallocate the old array.
3310 -- Reserve_Capacity can be used to expand the storage available for
3311 -- elements, but we do not let the capacity grow beyond the number of
3312 -- values in Index_Type'Range. (Were it otherwise, there would be no way
3313 -- to refer to the elements with index values greater than
3314 -- Index_Type'Last, so that storage would be wasted.) Here we compute
3315 -- the Last index value of the new internal array, in a way that avoids
3316 -- any possibility of overflow.
3318 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
3320 -- We perform a two-part test. First we determine whether the
3321 -- computed Last value lies in the base range of the type, and then
3322 -- determine whether it lies in the range of the index (sub)type.
3324 -- Last must satisfy this relation:
3325 -- First + Length - 1 <= Last
3326 -- We regroup terms:
3327 -- First - 1 <= Last - Length
3328 -- Which can rewrite as:
3329 -- No_Index <= Last - Length
3331 if Index_Type'Base'Last - Index_Type'Base (Capacity) < No_Index then
3332 raise Constraint_Error with "Capacity is out of range";
3335 -- We now know that the computed value of Last is within the base
3336 -- range of the type, so it is safe to compute its value:
3338 Last := No_Index + Index_Type'Base (Capacity);
3340 -- Finally we test whether the value is within the range of the
3341 -- generic actual index subtype:
3343 if Last > Index_Type'Last then
3344 raise Constraint_Error with "Capacity is out of range";
3347 elsif Index_Type'First <= 0 then
3349 -- Here we can compute Last directly, in the normal way. We know that
3350 -- No_Index is less than 0, so there is no danger of overflow when
3351 -- adding the (positive) value of Capacity.
3353 Index := Count_Type'Base (No_Index) + Capacity; -- Last
3355 if Index > Count_Type'Base (Index_Type'Last) then
3356 raise Constraint_Error with "Capacity is out of range";
3359 -- We know that the computed value (having type Count_Type) of Last
3360 -- is within the range of the generic actual index subtype, so it is
3361 -- safe to convert to Index_Type:
3363 Last := Index_Type'Base (Index);
3366 -- Here Index_Type'First (and Index_Type'Last) is positive, so we
3367 -- must test the length indirectly (by working backwards from the
3368 -- largest possible value of Last), in order to prevent overflow.
3370 Index := Count_Type'Base (Index_Type'Last) - Capacity; -- No_Index
3372 if Index < Count_Type'Base (No_Index) then
3373 raise Constraint_Error with "Capacity is out of range";
3376 -- We have determined that the value of Capacity would not create a
3377 -- Last index value outside of the range of Index_Type, so we can now
3378 -- safely compute its value.
3380 Last := Index_Type'Base (Count_Type'Base (No_Index) + Capacity);
3383 -- The requested capacity is non-zero, but we don't know yet whether
3384 -- this is a request for expansion or contraction of storage.
3386 if Container.Elements = null then
3388 -- The container is empty (it doesn't even have an internal array),
3389 -- so this represents a request to allocate storage having the given
3392 Container.Elements := new Elements_Type (Last);
3396 if Capacity <= N then
3398 -- This is a request to trim back storage, but only to the limit of
3399 -- what's already in the container. (Reserve_Capacity never deletes
3400 -- active elements, it only reclaims excess storage.)
3402 if N < Container.Elements.EA'Length then
3404 -- The container is not empty (because the requested capacity is
3405 -- positive, and less than or equal to the container length), and
3406 -- the current length is less than the current capacity, so there
3407 -- is storage available to trim. In this case, we allocate a new
3408 -- internal array having a length that exactly matches the number
3409 -- of items in the container.
3411 if Container.Busy > 0 then
3412 raise Program_Error with
3413 "attempt to tamper with cursors (vector is busy)";
3417 subtype Array_Index_Subtype is Index_Type'Base range
3418 Index_Type'First .. Container.Last;
3420 Src : Elements_Array renames
3421 Container.Elements.EA (Array_Index_Subtype);
3423 X : Elements_Access := Container.Elements;
3426 -- Although we have isolated the old internal array that we're
3427 -- going to deallocate, we don't deallocate it until we have
3428 -- successfully allocated a new one. If there is an exception
3429 -- during allocation (because there is not enough storage), we
3430 -- let it propagate without causing any side-effect.
3432 Container.Elements := new Elements_Type'(Container.Last, Src);
3434 -- We have successfully allocated a new internal array (with a
3435 -- smaller length than the old one, and containing a copy of
3436 -- just the active elements in the container), so it is now
3437 -- safe to deallocate the old array.
3446 -- The requested capacity is larger than the container length (the
3447 -- number of active elements). Whether this represents a request for
3448 -- expansion or contraction of the current capacity depends on what the
3449 -- current capacity is.
3451 if Capacity = Container.Elements.EA'Length then
3453 -- The requested capacity matches the existing capacity, so there's
3454 -- nothing to do here. We treat this case as a no-op, and simply
3455 -- return without checking the busy bit.
3460 -- There is a change in the capacity of a non-empty container, so a new
3461 -- internal array will be allocated. (The length of the new internal
3462 -- array could be less or greater than the old internal array. We know
3463 -- only that the length of the new internal array is greater than the
3464 -- number of active elements in the container.) We must check whether
3465 -- the container is busy before doing anything else.
3467 if Container.Busy > 0 then
3468 raise Program_Error with
3469 "attempt to tamper with cursors (vector is busy)";
3472 -- We now allocate a new internal array, having a length different from
3473 -- its current value.
3476 X : Elements_Access := Container.Elements;
3478 subtype Index_Subtype is Index_Type'Base range
3479 Index_Type'First .. Container.Last;
3482 -- We now allocate a new internal array, having a length different
3483 -- from its current value.
3485 Container.Elements := new Elements_Type (Last);
3487 -- We have successfully allocated the new internal array, so now we
3488 -- move the existing elements from the existing the old internal
3489 -- array onto the new one. Note that we're just copying access
3490 -- values, to this should not raise any exceptions.
3492 Container.Elements.EA (Index_Subtype) := X.EA (Index_Subtype);
3494 -- We have moved the elements from the old internal array, so now we
3495 -- can deallocate it.
3499 end Reserve_Capacity;
3501 ----------------------
3502 -- Reverse_Elements --
3503 ----------------------
3505 procedure Reverse_Elements (Container : in out Vector) is
3507 if Container.Length <= 1 then
3511 -- The exception behavior for the vector container must match that for
3512 -- the list container, so we check for cursor tampering here (which will
3513 -- catch more things) instead of for element tampering (which will catch
3514 -- fewer things). It's true that the elements of this vector container
3515 -- could be safely moved around while (say) an iteration is taking place
3516 -- (iteration only increments the busy counter), and so technically all
3517 -- we would need here is a test for element tampering (indicated by the
3518 -- lock counter), that's simply an artifact of our array-based
3519 -- implementation. Logically Reverse_Elements requires a check for
3520 -- cursor tampering.
3522 if Container.Busy > 0 then
3523 raise Program_Error with
3524 "attempt to tamper with cursors (vector is busy)";
3530 E : Elements_Array renames Container.Elements.EA;
3533 I := Index_Type'First;
3534 J := Container.Last;
3537 EI : constant Element_Access := E (I);
3548 end Reverse_Elements;
3554 function Reverse_Find
3555 (Container : Vector;
3556 Item : Element_Type;
3557 Position : Cursor := No_Element) return Cursor
3559 Last : Index_Type'Base;
3562 if Position.Container /= null
3563 and then Position.Container /= Container'Unrestricted_Access
3565 raise Program_Error with "Position cursor denotes wrong container";
3568 if Position.Container = null
3569 or else Position.Index > Container.Last
3571 Last := Container.Last;
3573 Last := Position.Index;
3576 for Indx in reverse Index_Type'First .. Last loop
3577 if Container.Elements.EA (Indx) /= null
3578 and then Container.Elements.EA (Indx).all = Item
3580 return (Container'Unrestricted_Access, Indx);
3587 ------------------------
3588 -- Reverse_Find_Index --
3589 ------------------------
3591 function Reverse_Find_Index
3592 (Container : Vector;
3593 Item : Element_Type;
3594 Index : Index_Type := Index_Type'Last) return Extended_Index
3596 Last : constant Index_Type'Base :=
3597 (if Index > Container.Last then Container.Last else Index);
3599 for Indx in reverse Index_Type'First .. Last loop
3600 if Container.Elements.EA (Indx) /= null
3601 and then Container.Elements.EA (Indx).all = Item
3608 end Reverse_Find_Index;
3610 ---------------------
3611 -- Reverse_Iterate --
3612 ---------------------
3614 procedure Reverse_Iterate
3615 (Container : Vector;
3616 Process : not null access procedure (Position : Cursor))
3618 V : Vector renames Container'Unrestricted_Access.all;
3619 B : Natural renames V.Busy;
3625 for Indx in reverse Index_Type'First .. Container.Last loop
3626 Process (Cursor'(Container'Unrestricted_Access, Indx));
3635 end Reverse_Iterate;
3641 procedure Set_Length
3642 (Container : in out Vector;
3643 Length : Count_Type)
3645 Count : constant Count_Type'Base := Container.Length - Length;
3648 -- Set_Length allows the user to set the length explicitly, instead of
3649 -- implicitly as a side-effect of deletion or insertion. If the
3650 -- requested length is less than the current length, this is equivalent
3651 -- to deleting items from the back end of the vector. If the requested
3652 -- length is greater than the current length, then this is equivalent to
3653 -- inserting "space" (nonce items) at the end.
3656 Container.Delete_Last (Count);
3658 elsif Container.Last >= Index_Type'Last then
3659 raise Constraint_Error with "vector is already at its maximum length";
3662 Container.Insert_Space (Container.Last + 1, -Count);
3671 (Container : in out Vector;
3675 if I > Container.Last then
3676 raise Constraint_Error with "I index is out of range";
3679 if J > Container.Last then
3680 raise Constraint_Error with "J index is out of range";
3687 if Container.Lock > 0 then
3688 raise Program_Error with
3689 "attempt to tamper with elements (vector is locked)";
3693 EI : Element_Access renames Container.Elements.EA (I);
3694 EJ : Element_Access renames Container.Elements.EA (J);
3696 EI_Copy : constant Element_Access := EI;
3705 (Container : in out Vector;
3709 if I.Container = null then
3710 raise Constraint_Error with "I cursor has no element";
3713 if J.Container = null then
3714 raise Constraint_Error with "J cursor has no element";
3717 if I.Container /= Container'Unrestricted_Access then
3718 raise Program_Error with "I cursor denotes wrong container";
3721 if J.Container /= Container'Unrestricted_Access then
3722 raise Program_Error with "J cursor denotes wrong container";
3725 Swap (Container, I.Index, J.Index);
3733 (Container : Vector;
3734 Index : Extended_Index) return Cursor
3737 if Index not in Index_Type'First .. Container.Last then
3741 return Cursor'(Container'Unrestricted_Access, Index);
3748 function To_Index (Position : Cursor) return Extended_Index is
3750 if Position.Container = null then
3754 if Position.Index <= Position.Container.Last then
3755 return Position.Index;
3765 function To_Vector (Length : Count_Type) return Vector is
3766 Index : Count_Type'Base;
3767 Last : Index_Type'Base;
3768 Elements : Elements_Access;
3772 return Empty_Vector;
3775 -- We create a vector object with a capacity that matches the specified
3776 -- Length, but we do not allow the vector capacity (the length of the
3777 -- internal array) to exceed the number of values in Index_Type'Range
3778 -- (otherwise, there would be no way to refer to those components via an
3779 -- index). We must therefore check whether the specified Length would
3780 -- create a Last index value greater than Index_Type'Last.
3782 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
3784 -- We perform a two-part test. First we determine whether the
3785 -- computed Last value lies in the base range of the type, and then
3786 -- determine whether it lies in the range of the index (sub)type.
3788 -- Last must satisfy this relation:
3789 -- First + Length - 1 <= Last
3790 -- We regroup terms:
3791 -- First - 1 <= Last - Length
3792 -- Which can rewrite as:
3793 -- No_Index <= Last - Length
3795 if Index_Type'Base'Last - Index_Type'Base (Length) < No_Index then
3796 raise Constraint_Error with "Length is out of range";
3799 -- We now know that the computed value of Last is within the base
3800 -- range of the type, so it is safe to compute its value:
3802 Last := No_Index + Index_Type'Base (Length);
3804 -- Finally we test whether the value is within the range of the
3805 -- generic actual index subtype:
3807 if Last > Index_Type'Last then
3808 raise Constraint_Error with "Length is out of range";
3811 elsif Index_Type'First <= 0 then
3813 -- Here we can compute Last directly, in the normal way. We know that
3814 -- No_Index is less than 0, so there is no danger of overflow when
3815 -- adding the (positive) value of Length.
3817 Index := Count_Type'Base (No_Index) + Length; -- Last
3819 if Index > Count_Type'Base (Index_Type'Last) then
3820 raise Constraint_Error with "Length is out of range";
3823 -- We know that the computed value (having type Count_Type) of Last
3824 -- is within the range of the generic actual index subtype, so it is
3825 -- safe to convert to Index_Type:
3827 Last := Index_Type'Base (Index);
3830 -- Here Index_Type'First (and Index_Type'Last) is positive, so we
3831 -- must test the length indirectly (by working backwards from the
3832 -- largest possible value of Last), in order to prevent overflow.
3834 Index := Count_Type'Base (Index_Type'Last) - Length; -- No_Index
3836 if Index < Count_Type'Base (No_Index) then
3837 raise Constraint_Error with "Length is out of range";
3840 -- We have determined that the value of Length would not create a
3841 -- Last index value outside of the range of Index_Type, so we can now
3842 -- safely compute its value.
3844 Last := Index_Type'Base (Count_Type'Base (No_Index) + Length);
3847 Elements := new Elements_Type (Last);
3849 return Vector'(Controlled with Elements, Last, 0, 0);
3853 (New_Item : Element_Type;
3854 Length : Count_Type) return Vector
3856 Index : Count_Type'Base;
3857 Last : Index_Type'Base;
3858 Elements : Elements_Access;
3862 return Empty_Vector;
3865 -- We create a vector object with a capacity that matches the specified
3866 -- Length, but we do not allow the vector capacity (the length of the
3867 -- internal array) to exceed the number of values in Index_Type'Range
3868 -- (otherwise, there would be no way to refer to those components via an
3869 -- index). We must therefore check whether the specified Length would
3870 -- create a Last index value greater than Index_Type'Last.
3872 if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
3874 -- We perform a two-part test. First we determine whether the
3875 -- computed Last value lies in the base range of the type, and then
3876 -- determine whether it lies in the range of the index (sub)type.
3878 -- Last must satisfy this relation:
3879 -- First + Length - 1 <= Last
3880 -- We regroup terms:
3881 -- First - 1 <= Last - Length
3882 -- Which can rewrite as:
3883 -- No_Index <= Last - Length
3885 if Index_Type'Base'Last - Index_Type'Base (Length) < No_Index then
3886 raise Constraint_Error with "Length is out of range";
3889 -- We now know that the computed value of Last is within the base
3890 -- range of the type, so it is safe to compute its value:
3892 Last := No_Index + Index_Type'Base (Length);
3894 -- Finally we test whether the value is within the range of the
3895 -- generic actual index subtype:
3897 if Last > Index_Type'Last then
3898 raise Constraint_Error with "Length is out of range";
3901 elsif Index_Type'First <= 0 then
3903 -- Here we can compute Last directly, in the normal way. We know that
3904 -- No_Index is less than 0, so there is no danger of overflow when
3905 -- adding the (positive) value of Length.
3907 Index := Count_Type'Base (No_Index) + Length; -- Last
3909 if Index > Count_Type'Base (Index_Type'Last) then
3910 raise Constraint_Error with "Length is out of range";
3913 -- We know that the computed value (having type Count_Type) of Last
3914 -- is within the range of the generic actual index subtype, so it is
3915 -- safe to convert to Index_Type:
3917 Last := Index_Type'Base (Index);
3920 -- Here Index_Type'First (and Index_Type'Last) is positive, so we
3921 -- must test the length indirectly (by working backwards from the
3922 -- largest possible value of Last), in order to prevent overflow.
3924 Index := Count_Type'Base (Index_Type'Last) - Length; -- No_Index
3926 if Index < Count_Type'Base (No_Index) then
3927 raise Constraint_Error with "Length is out of range";
3930 -- We have determined that the value of Length would not create a
3931 -- Last index value outside of the range of Index_Type, so we can now
3932 -- safely compute its value.
3934 Last := Index_Type'Base (Count_Type'Base (No_Index) + Length);
3937 Elements := new Elements_Type (Last);
3939 -- We use Last as the index of the loop used to populate the internal
3940 -- array with items. In general, we prefer to initialize the loop index
3941 -- immediately prior to entering the loop. However, Last is also used in
3942 -- the exception handler (to reclaim elements that have been allocated,
3943 -- before propagating the exception), and the initialization of Last
3944 -- after entering the block containing the handler confuses some static
3945 -- analysis tools, with respect to whether Last has been properly
3946 -- initialized when the handler executes. So here we initialize our loop
3947 -- variable earlier than we prefer, before entering the block, so there
3950 Last := Index_Type'First;
3954 Elements.EA (Last) := new Element_Type'(New_Item);
3955 exit when Last = Elements.Last;
3961 for J in Index_Type'First .. Last - 1 loop
3962 Free (Elements.EA (J));
3969 return (Controlled with Elements, Last, 0, 0);
3972 --------------------
3973 -- Update_Element --
3974 --------------------
3976 procedure Update_Element
3977 (Container : in out Vector;
3979 Process : not null access procedure (Element : in out Element_Type))
3981 B : Natural renames Container.Busy;
3982 L : Natural renames Container.Lock;
3985 if Index > Container.Last then
3986 raise Constraint_Error with "Index is out of range";
3989 if Container.Elements.EA (Index) = null then
3990 raise Constraint_Error with "element is null";
3997 Process (Container.Elements.EA (Index).all);
4009 procedure Update_Element
4010 (Container : in out Vector;
4012 Process : not null access procedure (Element : in out Element_Type))
4015 if Position.Container = null then
4016 raise Constraint_Error with "Position cursor has no element";
4019 if Position.Container /= Container'Unrestricted_Access then
4020 raise Program_Error with "Position cursor denotes wrong container";
4023 Update_Element (Container, Position.Index, Process);
4031 (Stream : not null access Root_Stream_Type'Class;
4034 N : constant Count_Type := Length (Container);
4037 Count_Type'Base'Write (Stream, N);
4044 E : Elements_Array renames Container.Elements.EA;
4047 for Indx in Index_Type'First .. Container.Last loop
4048 if E (Indx) = null then
4049 Boolean'Write (Stream, False);
4051 Boolean'Write (Stream, True);
4052 Element_Type'Output (Stream, E (Indx).all);
4059 (Stream : not null access Root_Stream_Type'Class;
4063 raise Program_Error with "attempt to stream vector cursor";
4067 (Stream : not null access Root_Stream_Type'Class;
4068 Item : Reference_Type)
4071 raise Program_Error with "attempt to stream reference";
4075 (Stream : not null access Root_Stream_Type'Class;
4076 Item : Constant_Reference_Type)
4079 raise Program_Error with "attempt to stream reference";
4082 end Ada.Containers.Indefinite_Vectors;