1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . V E C T O R S --
9 -- Copyright (C) 2004-2006 Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 2, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
21 -- for more details. You should have received a copy of the GNU General --
22 -- Public License distributed with GNAT; see file COPYING. If not, write --
23 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
24 -- Boston, MA 02110-1301, USA. --
26 -- As a special exception, if other files instantiate generics from this --
27 -- unit, or you link this unit with other files to produce an executable, --
28 -- this unit does not by itself cause the resulting executable to be --
29 -- covered by the GNU General Public License. This exception does not --
30 -- however invalidate any other reasons why the executable file might be --
31 -- covered by the GNU Public License. --
33 -- This unit was originally developed by Matthew J Heaney. --
34 ------------------------------------------------------------------------------
36 with Ada.Containers.Generic_Array_Sort;
37 with Ada.Unchecked_Deallocation;
39 with System; use type System.Address;
41 package body Ada.Containers.Vectors is
43 type Int is range System.Min_Int .. System.Max_Int;
44 type UInt is mod System.Max_Binary_Modulus;
47 new Ada.Unchecked_Deallocation (Elements_Type, Elements_Access);
53 function "&" (Left, Right : Vector) return Vector is
54 LN : constant Count_Type := Length (Left);
55 RN : constant Count_Type := Length (Right);
64 RE : Elements_Type renames
65 Right.Elements (Index_Type'First .. Right.Last);
67 Elements : constant Elements_Access :=
68 new Elements_Type'(RE);
71 return (Controlled with Elements, Right.Last, 0, 0);
77 LE : Elements_Type renames
78 Left.Elements (Index_Type'First .. Left.Last);
80 Elements : constant Elements_Access :=
81 new Elements_Type'(LE);
84 return (Controlled with Elements, Left.Last, 0, 0);
90 N : constant Int'Base := Int (LN) + Int (RN);
91 Last_As_Int : Int'Base;
94 if Int (No_Index) > Int'Last - N then
95 raise Constraint_Error with "new length is out of range";
98 Last_As_Int := Int (No_Index) + N;
100 if Last_As_Int > Int (Index_Type'Last) then
101 raise Constraint_Error with "new length is out of range";
105 Last : constant Index_Type := Index_Type (Last_As_Int);
107 LE : Elements_Type renames
108 Left.Elements (Index_Type'First .. Left.Last);
110 RE : Elements_Type renames
111 Right.Elements (Index_Type'First .. Right.Last);
113 Elements : constant Elements_Access :=
114 new Elements_Type'(LE & RE);
117 return (Controlled with Elements, Last, 0, 0);
122 function "&" (Left : Vector; Right : Element_Type) return Vector is
123 LN : constant Count_Type := Length (Left);
128 subtype Elements_Subtype is
129 Elements_Type (Index_Type'First .. Index_Type'First);
131 Elements : constant Elements_Access :=
132 new Elements_Subtype'(others => Right);
135 return (Controlled with Elements, Index_Type'First, 0, 0);
140 Last_As_Int : Int'Base;
143 if Int (Index_Type'First) > Int'Last - Int (LN) then
144 raise Constraint_Error with "new length is out of range";
147 Last_As_Int := Int (Index_Type'First) + Int (LN);
149 if Last_As_Int > Int (Index_Type'Last) then
150 raise Constraint_Error with "new length is out of range";
154 Last : constant Index_Type := Index_Type (Last_As_Int);
156 LE : Elements_Type renames
157 Left.Elements (Index_Type'First .. Left.Last);
159 subtype ET is Elements_Type (Index_Type'First .. Last);
161 Elements : constant Elements_Access := new ET'(LE & Right);
164 return (Controlled with Elements, Last, 0, 0);
169 function "&" (Left : Element_Type; Right : Vector) return Vector is
170 RN : constant Count_Type := Length (Right);
175 subtype Elements_Subtype is
176 Elements_Type (Index_Type'First .. Index_Type'First);
178 Elements : constant Elements_Access :=
179 new Elements_Subtype'(others => Left);
182 return (Controlled with Elements, Index_Type'First, 0, 0);
187 Last_As_Int : Int'Base;
190 if Int (Index_Type'First) > Int'Last - Int (RN) then
191 raise Constraint_Error with "new length is out of range";
194 Last_As_Int := Int (Index_Type'First) + Int (RN);
196 if Last_As_Int > Int (Index_Type'Last) then
197 raise Constraint_Error with "new length is out of range";
201 Last : constant Index_Type := Index_Type (Last_As_Int);
203 RE : Elements_Type renames
204 Right.Elements (Index_Type'First .. Right.Last);
206 subtype ET is Elements_Type (Index_Type'First .. Last);
208 Elements : constant Elements_Access := new ET'(Left & RE);
211 return (Controlled with Elements, Last, 0, 0);
216 function "&" (Left, Right : Element_Type) return Vector is
218 if Index_Type'First >= Index_Type'Last then
219 raise Constraint_Error with "new length is out of range";
223 Last : constant Index_Type := Index_Type'First + 1;
225 subtype ET is Elements_Type (Index_Type'First .. Last);
227 Elements : constant Elements_Access := new ET'(Left, Right);
230 return (Controlled with Elements, Last, 0, 0);
238 function "=" (Left, Right : Vector) return Boolean is
240 if Left'Address = Right'Address then
244 if Left.Last /= Right.Last then
248 for J in Index_Type range Index_Type'First .. Left.Last loop
249 if Left.Elements (J) /= Right.Elements (J) then
261 procedure Adjust (Container : in out Vector) is
263 if Container.Last = No_Index then
264 Container.Elements := null;
269 E : constant Elements_Access := Container.Elements;
270 L : constant Index_Type := Container.Last;
273 Container.Elements := null;
274 Container.Last := No_Index;
277 Container.Elements := new Elements_Type'(E (Index_Type'First .. L));
286 procedure Append (Container : in out Vector; New_Item : Vector) is
288 if Is_Empty (New_Item) then
292 if Container.Last = Index_Type'Last then
293 raise Constraint_Error with "vector is already at its maximum length";
303 (Container : in out Vector;
304 New_Item : Element_Type;
305 Count : Count_Type := 1)
312 if Container.Last = Index_Type'Last then
313 raise Constraint_Error with "vector is already at its maximum length";
327 function Capacity (Container : Vector) return Count_Type is
329 if Container.Elements = null then
333 return Container.Elements'Length;
340 procedure Clear (Container : in out Vector) is
342 if Container.Busy > 0 then
343 raise Program_Error with
344 "attempt to tamper with elements (vector is busy)";
347 Container.Last := No_Index;
356 Item : Element_Type) return Boolean
359 return Find_Index (Container, Item) /= No_Index;
367 (Container : in out Vector;
368 Index : Extended_Index;
369 Count : Count_Type := 1)
372 if Index < Index_Type'First then
373 raise Constraint_Error with "Index is out of range (too small)";
376 if Index > Container.Last then
377 if Index > Container.Last + 1 then
378 raise Constraint_Error with "Index is out of range (too large)";
388 if Container.Busy > 0 then
389 raise Program_Error with
390 "attempt to tamper with elements (vector is busy)";
394 I_As_Int : constant Int := Int (Index);
395 Old_Last_As_Int : constant Int := Index_Type'Pos (Container.Last);
397 Count1 : constant Int'Base := Count_Type'Pos (Count);
398 Count2 : constant Int'Base := Old_Last_As_Int - I_As_Int + 1;
399 N : constant Int'Base := Int'Min (Count1, Count2);
401 J_As_Int : constant Int'Base := I_As_Int + N;
404 if J_As_Int > Old_Last_As_Int then
405 Container.Last := Index - 1;
409 J : constant Index_Type := Index_Type (J_As_Int);
410 E : Elements_Type renames Container.Elements.all;
412 New_Last_As_Int : constant Int'Base := Old_Last_As_Int - N;
413 New_Last : constant Index_Type :=
414 Index_Type (New_Last_As_Int);
417 E (Index .. New_Last) := E (J .. Container.Last);
418 Container.Last := New_Last;
425 (Container : in out Vector;
426 Position : in out Cursor;
427 Count : Count_Type := 1)
430 if Position.Container = null then
431 raise Constraint_Error with "Position cursor has no element";
434 if Position.Container /= Container'Unrestricted_Access then
435 raise Program_Error with "Position cursor denotes wrong container";
438 if Position.Index > Container.Last then
439 raise Program_Error with "Position index is out of range";
442 Delete (Container, Position.Index, Count);
444 -- This is the old behavior, prior to the York API (2005/06):
446 -- if Position.Index <= Container.Last then
447 -- Position := (Container'Unchecked_Access, Position.Index);
449 -- Position := No_Element;
452 -- This is the behavior specified by the York API:
454 Position := No_Element;
461 procedure Delete_First
462 (Container : in out Vector;
463 Count : Count_Type := 1)
470 if Count >= Length (Container) then
475 Delete (Container, Index_Type'First, Count);
482 procedure Delete_Last
483 (Container : in out Vector;
484 Count : Count_Type := 1)
493 if Container.Busy > 0 then
494 raise Program_Error with
495 "attempt to tamper with elements (vector is busy)";
498 Index := Int'Base (Container.Last) - Int'Base (Count);
500 if Index < Index_Type'Pos (Index_Type'First) then
501 Container.Last := No_Index;
503 Container.Last := Index_Type (Index);
513 Index : Index_Type) return Element_Type
516 if Index > Container.Last then
517 raise Constraint_Error with "Index is out of range";
520 return Container.Elements (Index);
523 function Element (Position : Cursor) return Element_Type is
525 if Position.Container = null then
526 raise Constraint_Error with "Position cursor has no element";
529 return Element (Position.Container.all, Position.Index);
536 procedure Finalize (Container : in out Vector) is
537 X : Elements_Access := Container.Elements;
540 if Container.Busy > 0 then
541 raise Program_Error with
542 "attempt to tamper with elements (vector is busy)";
545 Container.Elements := null;
546 Container.Last := No_Index;
557 Position : Cursor := No_Element) return Cursor
560 if Position.Container /= null then
561 if Position.Container /= Container'Unrestricted_Access then
562 raise Program_Error with "Position cursor denotes wrong container";
565 if Position.Index > Container.Last then
566 raise Program_Error with "Position index is out of range";
570 for J in Position.Index .. Container.Last loop
571 if Container.Elements (J) = Item then
572 return (Container'Unchecked_Access, J);
586 Index : Index_Type := Index_Type'First) return Extended_Index
589 for Indx in Index .. Container.Last loop
590 if Container.Elements (Indx) = Item then
602 function First (Container : Vector) return Cursor is
604 if Is_Empty (Container) then
608 return (Container'Unchecked_Access, Index_Type'First);
615 function First_Element (Container : Vector) return Element_Type is
617 return Element (Container, Index_Type'First);
624 function First_Index (Container : Vector) return Index_Type is
625 pragma Unreferenced (Container);
627 return Index_Type'First;
630 ---------------------
631 -- Generic_Sorting --
632 ---------------------
634 package body Generic_Sorting is
640 function Is_Sorted (Container : Vector) return Boolean is
642 if Container.Last <= Index_Type'First then
647 E : Elements_Type renames Container.Elements.all;
649 for I in Index_Type'First .. Container.Last - 1 loop
650 if E (I + 1) < E (I) then
663 procedure Merge (Target, Source : in out Vector) is
664 I : Index_Type'Base := Target.Last;
668 if Target.Last < Index_Type'First then
669 Move (Target => Target, Source => Source);
673 if Target'Address = Source'Address then
677 if Source.Last < Index_Type'First then
681 if Source.Busy > 0 then
682 raise Program_Error with
683 "attempt to tamper with elements (vector is busy)";
686 Target.Set_Length (Length (Target) + Length (Source));
689 while Source.Last >= Index_Type'First loop
690 pragma Assert (Source.Last <= Index_Type'First
691 or else not (Source.Elements (Source.Last) <
692 Source.Elements (Source.Last - 1)));
694 if I < Index_Type'First then
695 Target.Elements (Index_Type'First .. J) :=
696 Source.Elements (Index_Type'First .. Source.Last);
698 Source.Last := No_Index;
702 pragma Assert (I <= Index_Type'First
703 or else not (Target.Elements (I) <
704 Target.Elements (I - 1)));
706 if Source.Elements (Source.Last) < Target.Elements (I) then
707 Target.Elements (J) := Target.Elements (I);
711 Target.Elements (J) := Source.Elements (Source.Last);
712 Source.Last := Source.Last - 1;
723 procedure Sort (Container : in out Vector)
726 new Generic_Array_Sort
727 (Index_Type => Index_Type,
728 Element_Type => Element_Type,
729 Array_Type => Elements_Type,
733 if Container.Last <= Index_Type'First then
737 if Container.Lock > 0 then
738 raise Program_Error with
739 "attempt to tamper with cursors (vector is locked)";
742 Sort (Container.Elements (Index_Type'First .. Container.Last));
751 function Has_Element (Position : Cursor) return Boolean is
753 if Position.Container = null then
757 return Position.Index <= Position.Container.Last;
765 (Container : in out Vector;
766 Before : Extended_Index;
767 New_Item : Element_Type;
768 Count : Count_Type := 1)
770 N : constant Int := Count_Type'Pos (Count);
772 First : constant Int := Int (Index_Type'First);
773 New_Last_As_Int : Int'Base;
774 New_Last : Index_Type;
776 Max_Length : constant UInt := UInt (Count_Type'Last);
778 Dst : Elements_Access;
781 if Before < Index_Type'First then
782 raise Constraint_Error with
783 "Before index is out of range (too small)";
786 if Before > Container.Last
787 and then Before > Container.Last + 1
789 raise Constraint_Error with
790 "Before index is out of range (too large)";
798 Old_Last_As_Int : constant Int := Int (Container.Last);
801 if Old_Last_As_Int > Int'Last - N then
805 -- The purpose of this test is to ensure that the calculation of
806 -- New_Last_As_Int (see below) doesn't overflow.
808 -- This isn't quite right, since the only requirements are:
809 -- V.Last <= Index_Type'Last
810 -- V.Length <= Count_Type'Last
812 -- To be strictly correct there's no (explicit) requirement that
813 -- Old_Last + N <= Int'Last
815 -- However, there might indeed be an implied requirement, since
816 -- machine constraints dictate that
817 -- Index_Type'Last <= Int'Last
818 -- and so this check is perhaps proper after all.
820 -- This shouldn't be an issue in practice, since it can only
821 -- happen when N is very large, or V.Last is near Int'Last.
823 -- N isn't likely to be large, since there's probably not enough
826 -- V.Last would only be large if IT'First is very large (and
827 -- V.Length has some "normal" size). But typically IT'First is
830 raise Constraint_Error with "new length is out of range";
833 New_Last_As_Int := Old_Last_As_Int + N;
835 if New_Last_As_Int > Int (Index_Type'Last) then
836 raise Constraint_Error with "new length is out of range";
839 New_Length := UInt (New_Last_As_Int - First + Int'(1));
841 if New_Length > Max_Length then
842 raise Constraint_Error with "new length is out of range";
845 New_Last := Index_Type (New_Last_As_Int);
848 if Container.Busy > 0 then
849 raise Program_Error with
850 "attempt to tamper with elements (vector is busy)";
853 if Container.Elements = null then
855 subtype Elements_Subtype is
856 Elements_Type (Index_Type'First .. New_Last);
858 Container.Elements := new Elements_Subtype'(others => New_Item);
861 Container.Last := New_Last;
865 if New_Last <= Container.Elements'Last then
867 E : Elements_Type renames Container.Elements.all;
870 if Before <= Container.Last then
872 Index_As_Int : constant Int'Base :=
873 Index_Type'Pos (Before) + N;
875 Index : constant Index_Type := Index_Type (Index_As_Int);
878 E (Index .. New_Last) := E (Before .. Container.Last);
880 E (Before .. Index_Type'Pred (Index)) :=
881 (others => New_Item);
885 E (Before .. New_Last) := (others => New_Item);
889 Container.Last := New_Last;
897 C := UInt'Max (1, Container.Elements'Length);
898 while C < New_Length loop
899 if C > UInt'Last / 2 then
907 if C > Max_Length then
911 if Index_Type'First <= 0
912 and then Index_Type'Last >= 0
914 CC := UInt (Index_Type'Last) + UInt (-Index_Type'First) + 1;
917 CC := UInt (Int (Index_Type'Last) - First + 1);
925 Dst_Last : constant Index_Type :=
926 Index_Type (First + UInt'Pos (C) - 1);
929 Dst := new Elements_Type (Index_Type'First .. Dst_Last);
934 Src : Elements_Type renames Container.Elements.all;
937 Dst (Index_Type'First .. Index_Type'Pred (Before)) :=
938 Src (Index_Type'First .. Index_Type'Pred (Before));
940 if Before <= Container.Last then
942 Index_As_Int : constant Int'Base :=
943 Index_Type'Pos (Before) + N;
945 Index : constant Index_Type := Index_Type (Index_As_Int);
948 Dst (Before .. Index_Type'Pred (Index)) := (others => New_Item);
949 Dst (Index .. New_Last) := Src (Before .. Container.Last);
953 Dst (Before .. New_Last) := (others => New_Item);
962 X : Elements_Access := Container.Elements;
964 Container.Elements := Dst;
965 Container.Last := New_Last;
971 (Container : in out Vector;
972 Before : Extended_Index;
975 N : constant Count_Type := Length (New_Item);
978 if Before < Index_Type'First then
979 raise Constraint_Error with
980 "Before index is out of range (too small)";
983 if Before > Container.Last
984 and then Before > Container.Last + 1
986 raise Constraint_Error with
987 "Before index is out of range (too large)";
994 Insert_Space (Container, Before, Count => N);
997 Dst_Last_As_Int : constant Int'Base :=
998 Int'Base (Before) + Int'Base (N) - 1;
1000 Dst_Last : constant Index_Type := Index_Type (Dst_Last_As_Int);
1003 if Container'Address /= New_Item'Address then
1004 Container.Elements (Before .. Dst_Last) :=
1005 New_Item.Elements (Index_Type'First .. New_Item.Last);
1011 subtype Src_Index_Subtype is Index_Type'Base range
1012 Index_Type'First .. Before - 1;
1014 Src : Elements_Type renames
1015 Container.Elements (Src_Index_Subtype);
1017 Index_As_Int : constant Int'Base :=
1018 Int (Before) + Src'Length - 1;
1020 Index : constant Index_Type'Base :=
1021 Index_Type'Base (Index_As_Int);
1023 Dst : Elements_Type renames
1024 Container.Elements (Before .. Index);
1030 if Dst_Last = Container.Last then
1035 subtype Src_Index_Subtype is Index_Type'Base range
1036 Dst_Last + 1 .. Container.Last;
1038 Src : Elements_Type renames
1039 Container.Elements (Src_Index_Subtype);
1041 Index_As_Int : constant Int'Base :=
1042 Dst_Last_As_Int - Src'Length + 1;
1044 Index : constant Index_Type :=
1045 Index_Type (Index_As_Int);
1047 Dst : Elements_Type renames
1048 Container.Elements (Index .. Dst_Last);
1057 (Container : in out Vector;
1061 Index : Index_Type'Base;
1064 if Before.Container /= null
1065 and then Before.Container /= Container'Unchecked_Access
1067 raise Program_Error with "Before cursor denotes wrong container";
1070 if Is_Empty (New_Item) then
1074 if Before.Container = null
1075 or else Before.Index > Container.Last
1077 if Container.Last = Index_Type'Last then
1078 raise Constraint_Error with
1079 "vector is already at its maximum length";
1082 Index := Container.Last + 1;
1085 Index := Before.Index;
1088 Insert (Container, Index, New_Item);
1092 (Container : in out Vector;
1095 Position : out Cursor)
1097 Index : Index_Type'Base;
1100 if Before.Container /= null
1101 and then Before.Container /= Container'Unchecked_Access
1103 raise Program_Error with "Before cursor denotes wrong container";
1106 if Is_Empty (New_Item) then
1107 if Before.Container = null
1108 or else Before.Index > Container.Last
1110 Position := No_Element;
1112 Position := (Container'Unchecked_Access, Before.Index);
1118 if Before.Container = null
1119 or else Before.Index > Container.Last
1121 if Container.Last = Index_Type'Last then
1122 raise Constraint_Error with
1123 "vector is already at its maximum length";
1126 Index := Container.Last + 1;
1129 Index := Before.Index;
1132 Insert (Container, Index, New_Item);
1134 Position := Cursor'(Container'Unchecked_Access, Index);
1138 (Container : in out Vector;
1140 New_Item : Element_Type;
1141 Count : Count_Type := 1)
1143 Index : Index_Type'Base;
1146 if Before.Container /= null
1147 and then Before.Container /= Container'Unchecked_Access
1149 raise Program_Error with "Before cursor denotes wrong container";
1156 if Before.Container = null
1157 or else Before.Index > Container.Last
1159 if Container.Last = Index_Type'Last then
1160 raise Constraint_Error with
1161 "vector is already at its maximum length";
1164 Index := Container.Last + 1;
1167 Index := Before.Index;
1170 Insert (Container, Index, New_Item, Count);
1174 (Container : in out Vector;
1176 New_Item : Element_Type;
1177 Position : out Cursor;
1178 Count : Count_Type := 1)
1180 Index : Index_Type'Base;
1183 if Before.Container /= null
1184 and then Before.Container /= Container'Unchecked_Access
1186 raise Program_Error with "Before cursor denotes wrong container";
1190 if Before.Container = null
1191 or else Before.Index > Container.Last
1193 Position := No_Element;
1195 Position := (Container'Unchecked_Access, Before.Index);
1201 if Before.Container = null
1202 or else Before.Index > Container.Last
1204 if Container.Last = Index_Type'Last then
1205 raise Constraint_Error with
1206 "vector is already at its maximum length";
1209 Index := Container.Last + 1;
1212 Index := Before.Index;
1215 Insert (Container, Index, New_Item, Count);
1217 Position := Cursor'(Container'Unchecked_Access, Index);
1221 (Container : in out Vector;
1222 Before : Extended_Index;
1223 Count : Count_Type := 1)
1225 New_Item : Element_Type; -- Default-initialized value
1226 pragma Warnings (Off, New_Item);
1229 Insert (Container, Before, New_Item, Count);
1233 (Container : in out Vector;
1235 Position : out Cursor;
1236 Count : Count_Type := 1)
1238 New_Item : Element_Type; -- Default-initialized value
1239 pragma Warnings (Off, New_Item);
1242 Insert (Container, Before, New_Item, Position, Count);
1249 procedure Insert_Space
1250 (Container : in out Vector;
1251 Before : Extended_Index;
1252 Count : Count_Type := 1)
1254 N : constant Int := Count_Type'Pos (Count);
1256 First : constant Int := Int (Index_Type'First);
1257 New_Last_As_Int : Int'Base;
1258 New_Last : Index_Type;
1260 Max_Length : constant UInt := UInt (Count_Type'Last);
1262 Dst : Elements_Access;
1265 if Before < Index_Type'First then
1266 raise Constraint_Error with
1267 "Before index is out of range (too small)";
1270 if Before > Container.Last
1271 and then Before > Container.Last + 1
1273 raise Constraint_Error with
1274 "Before index is out of range (too large)";
1282 Old_Last_As_Int : constant Int := Int (Container.Last);
1285 if Old_Last_As_Int > Int'Last - N then -- see Insert ???
1286 raise Constraint_Error with "new length is out of range";
1289 New_Last_As_Int := Old_Last_As_Int + N;
1291 if New_Last_As_Int > Int (Index_Type'Last) then
1292 raise Constraint_Error with "new length is out of range";
1295 New_Length := UInt (New_Last_As_Int - First + Int'(1));
1297 if New_Length > Max_Length then
1298 raise Constraint_Error with "new length is out of range";
1301 New_Last := Index_Type (New_Last_As_Int);
1304 if Container.Busy > 0 then
1305 raise Program_Error with
1306 "attempt to tamper with elements (vector is busy)";
1309 if Container.Elements = null then
1310 Container.Elements :=
1311 new Elements_Type (Index_Type'First .. New_Last);
1313 Container.Last := New_Last;
1317 if New_Last <= Container.Elements'Last then
1319 E : Elements_Type renames Container.Elements.all;
1321 if Before <= Container.Last then
1323 Index_As_Int : constant Int'Base :=
1324 Index_Type'Pos (Before) + N;
1326 Index : constant Index_Type := Index_Type (Index_As_Int);
1329 E (Index .. New_Last) := E (Before .. Container.Last);
1334 Container.Last := New_Last;
1342 C := UInt'Max (1, Container.Elements'Length);
1343 while C < New_Length loop
1344 if C > UInt'Last / 2 then
1352 if C > Max_Length then
1356 if Index_Type'First <= 0
1357 and then Index_Type'Last >= 0
1359 CC := UInt (Index_Type'Last) + UInt (-Index_Type'First) + 1;
1362 CC := UInt (Int (Index_Type'Last) - First + 1);
1370 Dst_Last : constant Index_Type :=
1371 Index_Type (First + UInt'Pos (C) - 1);
1374 Dst := new Elements_Type (Index_Type'First .. Dst_Last);
1379 Src : Elements_Type renames Container.Elements.all;
1382 Dst (Index_Type'First .. Index_Type'Pred (Before)) :=
1383 Src (Index_Type'First .. Index_Type'Pred (Before));
1385 if Before <= Container.Last then
1387 Index_As_Int : constant Int'Base :=
1388 Index_Type'Pos (Before) + N;
1390 Index : constant Index_Type := Index_Type (Index_As_Int);
1393 Dst (Index .. New_Last) := Src (Before .. Container.Last);
1403 X : Elements_Access := Container.Elements;
1405 Container.Elements := Dst;
1406 Container.Last := New_Last;
1411 procedure Insert_Space
1412 (Container : in out Vector;
1414 Position : out Cursor;
1415 Count : Count_Type := 1)
1417 Index : Index_Type'Base;
1420 if Before.Container /= null
1421 and then Before.Container /= Container'Unchecked_Access
1423 raise Program_Error with "Before cursor denotes wrong container";
1427 if Before.Container = null
1428 or else Before.Index > Container.Last
1430 Position := No_Element;
1432 Position := (Container'Unchecked_Access, Before.Index);
1438 if Before.Container = null
1439 or else Before.Index > Container.Last
1441 if Container.Last = Index_Type'Last then
1442 raise Constraint_Error with
1443 "vector is already at its maximum length";
1446 Index := Container.Last + 1;
1449 Index := Before.Index;
1452 Insert_Space (Container, Index, Count => Count);
1454 Position := Cursor'(Container'Unchecked_Access, Index);
1461 function Is_Empty (Container : Vector) return Boolean is
1463 return Container.Last < Index_Type'First;
1471 (Container : Vector;
1472 Process : not null access procedure (Position : Cursor))
1474 V : Vector renames Container'Unrestricted_Access.all;
1475 B : Natural renames V.Busy;
1481 for Indx in Index_Type'First .. Container.Last loop
1482 Process (Cursor'(Container'Unchecked_Access, Indx));
1497 function Last (Container : Vector) return Cursor is
1499 if Is_Empty (Container) then
1503 return (Container'Unchecked_Access, Container.Last);
1510 function Last_Element (Container : Vector) return Element_Type is
1512 return Element (Container, Container.Last);
1519 function Last_Index (Container : Vector) return Extended_Index is
1521 return Container.Last;
1528 function Length (Container : Vector) return Count_Type is
1529 L : constant Int := Int (Container.Last);
1530 F : constant Int := Int (Index_Type'First);
1531 N : constant Int'Base := L - F + 1;
1534 return Count_Type (N);
1542 (Target : in out Vector;
1543 Source : in out Vector)
1546 if Target'Address = Source'Address then
1550 if Target.Busy > 0 then
1551 raise Program_Error with
1552 "attempt to tamper with elements (Target is busy)";
1555 if Source.Busy > 0 then
1556 raise Program_Error with
1557 "attempt to tamper with elements (Source is busy)";
1561 Target_Elements : constant Elements_Access := Target.Elements;
1563 Target.Elements := Source.Elements;
1564 Source.Elements := Target_Elements;
1567 Target.Last := Source.Last;
1568 Source.Last := No_Index;
1575 function Next (Position : Cursor) return Cursor is
1577 if Position.Container = null then
1581 if Position.Index < Position.Container.Last then
1582 return (Position.Container, Position.Index + 1);
1592 procedure Next (Position : in out Cursor) is
1594 if Position.Container = null then
1598 if Position.Index < Position.Container.Last then
1599 Position.Index := Position.Index + 1;
1601 Position := No_Element;
1609 procedure Prepend (Container : in out Vector; New_Item : Vector) is
1611 Insert (Container, Index_Type'First, New_Item);
1615 (Container : in out Vector;
1616 New_Item : Element_Type;
1617 Count : Count_Type := 1)
1630 procedure Previous (Position : in out Cursor) is
1632 if Position.Container = null then
1636 if Position.Index > Index_Type'First then
1637 Position.Index := Position.Index - 1;
1639 Position := No_Element;
1643 function Previous (Position : Cursor) return Cursor is
1645 if Position.Container = null then
1649 if Position.Index > Index_Type'First then
1650 return (Position.Container, Position.Index - 1);
1660 procedure Query_Element
1661 (Container : Vector;
1663 Process : not null access procedure (Element : Element_Type))
1665 V : Vector renames Container'Unrestricted_Access.all;
1666 B : Natural renames V.Busy;
1667 L : Natural renames V.Lock;
1670 if Index > Container.Last then
1671 raise Constraint_Error with "Index is out of range";
1678 Process (V.Elements (Index));
1690 procedure Query_Element
1692 Process : not null access procedure (Element : Element_Type))
1695 if Position.Container = null then
1696 raise Constraint_Error with "Position cursor has no element";
1699 Query_Element (Position.Container.all, Position.Index, Process);
1707 (Stream : not null access Root_Stream_Type'Class;
1708 Container : out Vector)
1710 Length : Count_Type'Base;
1711 Last : Index_Type'Base := No_Index;
1716 Count_Type'Base'Read (Stream, Length);
1718 if Length > Capacity (Container) then
1719 Reserve_Capacity (Container, Capacity => Length);
1722 for J in Count_Type range 1 .. Length loop
1724 Element_Type'Read (Stream, Container.Elements (Last));
1725 Container.Last := Last;
1730 (Stream : not null access Root_Stream_Type'Class;
1731 Position : out Cursor)
1734 raise Program_Error with "attempt to stream vector cursor";
1737 ---------------------
1738 -- Replace_Element --
1739 ---------------------
1741 procedure Replace_Element
1742 (Container : in out Vector;
1744 New_Item : Element_Type)
1747 if Index > Container.Last then
1748 raise Constraint_Error with "Index is out of range";
1751 if Container.Lock > 0 then
1752 raise Program_Error with
1753 "attempt to tamper with cursors (vector is locked)";
1756 Container.Elements (Index) := New_Item;
1757 end Replace_Element;
1759 procedure Replace_Element
1760 (Container : in out Vector;
1762 New_Item : Element_Type)
1765 if Position.Container = null then
1766 raise Constraint_Error with "Position cursor has no element";
1769 if Position.Container /= Container'Unrestricted_Access then
1770 raise Program_Error with "Position cursor denotes wrong container";
1773 Replace_Element (Container, Position.Index, New_Item);
1774 end Replace_Element;
1776 ----------------------
1777 -- Reserve_Capacity --
1778 ----------------------
1780 procedure Reserve_Capacity
1781 (Container : in out Vector;
1782 Capacity : Count_Type)
1784 N : constant Count_Type := Length (Container);
1787 if Capacity = 0 then
1790 X : Elements_Access := Container.Elements;
1792 Container.Elements := null;
1796 elsif N < Container.Elements'Length then
1797 if Container.Busy > 0 then
1798 raise Program_Error with
1799 "attempt to tamper with elements (vector is busy)";
1803 subtype Array_Index_Subtype is Index_Type'Base range
1804 Index_Type'First .. Container.Last;
1806 Src : Elements_Type renames
1807 Container.Elements (Array_Index_Subtype);
1809 subtype Array_Subtype is
1810 Elements_Type (Array_Index_Subtype);
1812 X : Elements_Access := Container.Elements;
1815 Container.Elements := new Array_Subtype'(Src);
1823 if Container.Elements = null then
1825 Last_As_Int : constant Int'Base :=
1826 Int (Index_Type'First) + Int (Capacity) - 1;
1829 if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
1830 raise Constraint_Error with "new length is out of range";
1834 Last : constant Index_Type := Index_Type (Last_As_Int);
1836 subtype Array_Subtype is
1837 Elements_Type (Index_Type'First .. Last);
1840 Container.Elements := new Array_Subtype;
1847 if Capacity <= N then
1848 if N < Container.Elements'Length then
1849 if Container.Busy > 0 then
1850 raise Program_Error with
1851 "attempt to tamper with elements (vector is busy)";
1855 subtype Array_Index_Subtype is Index_Type'Base range
1856 Index_Type'First .. Container.Last;
1858 Src : Elements_Type renames
1859 Container.Elements (Array_Index_Subtype);
1861 subtype Array_Subtype is
1862 Elements_Type (Array_Index_Subtype);
1864 X : Elements_Access := Container.Elements;
1867 Container.Elements := new Array_Subtype'(Src);
1876 if Capacity = Container.Elements'Length then
1880 if Container.Busy > 0 then
1881 raise Program_Error with
1882 "attempt to tamper with elements (vector is busy)";
1886 Last_As_Int : constant Int'Base :=
1887 Int (Index_Type'First) + Int (Capacity) - 1;
1890 if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
1891 raise Constraint_Error with "new length is out of range";
1895 Last : constant Index_Type := Index_Type (Last_As_Int);
1897 subtype Array_Subtype is
1898 Elements_Type (Index_Type'First .. Last);
1900 E : Elements_Access := new Array_Subtype;
1904 Src : Elements_Type renames
1905 Container.Elements (Index_Type'First .. Container.Last);
1907 Tgt : Elements_Type renames
1908 E (Index_Type'First .. Container.Last);
1920 X : Elements_Access := Container.Elements;
1922 Container.Elements := E;
1927 end Reserve_Capacity;
1929 ----------------------
1930 -- Reverse_Elements --
1931 ----------------------
1933 procedure Reverse_Elements (Container : in out Vector) is
1935 if Container.Length <= 1 then
1939 if Container.Lock > 0 then
1940 raise Program_Error with
1941 "attempt to tamper with cursors (vector is locked)";
1946 E : Elements_Type renames Container.Elements.all;
1949 I := Index_Type'First;
1950 J := Container.Last;
1953 EI : constant Element_Type := E (I);
1964 end Reverse_Elements;
1970 function Reverse_Find
1971 (Container : Vector;
1972 Item : Element_Type;
1973 Position : Cursor := No_Element) return Cursor
1975 Last : Index_Type'Base;
1978 if Position.Container /= null
1979 and then Position.Container /= Container'Unchecked_Access
1981 raise Program_Error with "Position cursor denotes wrong container";
1984 if Position.Container = null
1985 or else Position.Index > Container.Last
1987 Last := Container.Last;
1989 Last := Position.Index;
1992 for Indx in reverse Index_Type'First .. Last loop
1993 if Container.Elements (Indx) = Item then
1994 return (Container'Unchecked_Access, Indx);
2001 ------------------------
2002 -- Reverse_Find_Index --
2003 ------------------------
2005 function Reverse_Find_Index
2006 (Container : Vector;
2007 Item : Element_Type;
2008 Index : Index_Type := Index_Type'Last) return Extended_Index
2010 Last : Index_Type'Base;
2013 if Index > Container.Last then
2014 Last := Container.Last;
2019 for Indx in reverse Index_Type'First .. Last loop
2020 if Container.Elements (Indx) = Item then
2026 end Reverse_Find_Index;
2028 ---------------------
2029 -- Reverse_Iterate --
2030 ---------------------
2032 procedure Reverse_Iterate
2033 (Container : Vector;
2034 Process : not null access procedure (Position : Cursor))
2036 V : Vector renames Container'Unrestricted_Access.all;
2037 B : Natural renames V.Busy;
2043 for Indx in reverse Index_Type'First .. Container.Last loop
2044 Process (Cursor'(Container'Unchecked_Access, Indx));
2053 end Reverse_Iterate;
2059 procedure Set_Length (Container : in out Vector; Length : Count_Type) is
2061 if Length = Vectors.Length (Container) then
2065 if Container.Busy > 0 then
2066 raise Program_Error with
2067 "attempt to tamper with elements (vector is busy)";
2070 if Length > Capacity (Container) then
2071 Reserve_Capacity (Container, Capacity => Length);
2075 Last_As_Int : constant Int'Base :=
2076 Int (Index_Type'First) + Int (Length) - 1;
2078 Container.Last := Index_Type'Base (Last_As_Int);
2086 procedure Swap (Container : in out Vector; I, J : Index_Type) is
2088 if I > Container.Last then
2089 raise Constraint_Error with "I index is out of range";
2092 if J > Container.Last then
2093 raise Constraint_Error with "J index is out of range";
2100 if Container.Lock > 0 then
2101 raise Program_Error with
2102 "attempt to tamper with cursors (vector is locked)";
2106 EI : Element_Type renames Container.Elements (I);
2107 EJ : Element_Type renames Container.Elements (J);
2109 EI_Copy : constant Element_Type := EI;
2117 procedure Swap (Container : in out Vector; I, J : Cursor) is
2119 if I.Container = null then
2120 raise Constraint_Error with "I cursor has no element";
2123 if J.Container = null then
2124 raise Constraint_Error with "J cursor has no element";
2127 if I.Container /= Container'Unrestricted_Access then
2128 raise Program_Error with "I cursor denotes wrong container";
2131 if J.Container /= Container'Unrestricted_Access then
2132 raise Program_Error with "J cursor denotes wrong container";
2135 Swap (Container, I.Index, J.Index);
2143 (Container : Vector;
2144 Index : Extended_Index) return Cursor
2147 if Index not in Index_Type'First .. Container.Last then
2151 return Cursor'(Container'Unchecked_Access, Index);
2158 function To_Index (Position : Cursor) return Extended_Index is
2160 if Position.Container = null then
2164 if Position.Index <= Position.Container.Last then
2165 return Position.Index;
2175 function To_Vector (Length : Count_Type) return Vector is
2178 return Empty_Vector;
2182 First : constant Int := Int (Index_Type'First);
2183 Last_As_Int : constant Int'Base := First + Int (Length) - 1;
2185 Elements : Elements_Access;
2188 if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
2189 raise Constraint_Error with "Length is out of range";
2192 Last := Index_Type (Last_As_Int);
2193 Elements := new Elements_Type (Index_Type'First .. Last);
2195 return Vector'(Controlled with Elements, Last, 0, 0);
2200 (New_Item : Element_Type;
2201 Length : Count_Type) return Vector
2205 return Empty_Vector;
2209 First : constant Int := Int (Index_Type'First);
2210 Last_As_Int : constant Int'Base := First + Int (Length) - 1;
2212 Elements : Elements_Access;
2215 if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
2216 raise Constraint_Error with "Length is out of range";
2219 Last := Index_Type (Last_As_Int);
2220 Elements := new Elements_Type'(Index_Type'First .. Last => New_Item);
2222 return Vector'(Controlled with Elements, Last, 0, 0);
2226 --------------------
2227 -- Update_Element --
2228 --------------------
2230 procedure Update_Element
2231 (Container : in out Vector;
2233 Process : not null access procedure (Element : in out Element_Type))
2235 B : Natural renames Container.Busy;
2236 L : Natural renames Container.Lock;
2239 if Index > Container.Last then
2240 raise Constraint_Error with "Index is out of range";
2247 Process (Container.Elements (Index));
2259 procedure Update_Element
2260 (Container : in out Vector;
2262 Process : not null access procedure (Element : in out Element_Type))
2265 if Position.Container = null then
2266 raise Constraint_Error with "Position cursor has no element";
2269 if Position.Container /= Container'Unrestricted_Access then
2270 raise Program_Error with "Position cursor denotes wrong container";
2273 Update_Element (Container, Position.Index, Process);
2281 (Stream : not null access Root_Stream_Type'Class;
2285 Count_Type'Base'Write (Stream, Length (Container));
2287 for J in Index_Type'First .. Container.Last loop
2288 Element_Type'Write (Stream, Container.Elements (J));
2293 (Stream : not null access Root_Stream_Type'Class;
2297 raise Program_Error with "attempt to stream vector cursor";
2300 end Ada.Containers.Vectors;