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-2008, 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- This unit was originally developed by Matthew J Heaney. --
30 ------------------------------------------------------------------------------
32 with Ada.Containers.Generic_Array_Sort;
33 with Ada.Unchecked_Deallocation;
35 with System; use type System.Address;
37 package body Ada.Containers.Vectors is
39 type Int is range System.Min_Int .. System.Max_Int;
40 type UInt is mod System.Max_Binary_Modulus;
43 new Ada.Unchecked_Deallocation (Elements_Type, Elements_Access);
49 function "&" (Left, Right : Vector) return Vector is
50 LN : constant Count_Type := Length (Left);
51 RN : constant Count_Type := Length (Right);
60 RE : Elements_Array renames
61 Right.Elements.EA (Index_Type'First .. Right.Last);
63 Elements : constant Elements_Access :=
64 new Elements_Type'(Right.Last, RE);
67 return (Controlled with Elements, Right.Last, 0, 0);
73 LE : Elements_Array renames
74 Left.Elements.EA (Index_Type'First .. Left.Last);
76 Elements : constant Elements_Access :=
77 new Elements_Type'(Left.Last, LE);
80 return (Controlled with Elements, Left.Last, 0, 0);
86 N : constant Int'Base := Int (LN) + Int (RN);
87 Last_As_Int : Int'Base;
90 if Int (No_Index) > Int'Last - N then
91 raise Constraint_Error with "new length is out of range";
94 Last_As_Int := Int (No_Index) + N;
96 if Last_As_Int > Int (Index_Type'Last) then
97 raise Constraint_Error with "new length is out of range";
101 Last : constant Index_Type := Index_Type (Last_As_Int);
103 LE : Elements_Array renames
104 Left.Elements.EA (Index_Type'First .. Left.Last);
106 RE : Elements_Array renames
107 Right.Elements.EA (Index_Type'First .. Right.Last);
109 Elements : constant Elements_Access :=
110 new Elements_Type'(Last, LE & RE);
113 return (Controlled with Elements, Last, 0, 0);
118 function "&" (Left : Vector; Right : Element_Type) return Vector is
119 LN : constant Count_Type := Length (Left);
124 Elements : constant Elements_Access :=
126 (Last => Index_Type'First,
127 EA => (others => Right));
130 return (Controlled with Elements, Index_Type'First, 0, 0);
135 Last_As_Int : Int'Base;
138 if Int (Index_Type'First) > Int'Last - Int (LN) then
139 raise Constraint_Error with "new length is out of range";
142 Last_As_Int := Int (Index_Type'First) + Int (LN);
144 if Last_As_Int > Int (Index_Type'Last) then
145 raise Constraint_Error with "new length is out of range";
149 Last : constant Index_Type := Index_Type (Last_As_Int);
151 LE : Elements_Array renames
152 Left.Elements.EA (Index_Type'First .. Left.Last);
154 Elements : constant Elements_Access :=
160 return (Controlled with Elements, Last, 0, 0);
165 function "&" (Left : Element_Type; Right : Vector) return Vector is
166 RN : constant Count_Type := Length (Right);
171 Elements : constant Elements_Access :=
173 (Last => Index_Type'First,
174 EA => (others => Left));
177 return (Controlled with Elements, Index_Type'First, 0, 0);
182 Last_As_Int : Int'Base;
185 if Int (Index_Type'First) > Int'Last - Int (RN) then
186 raise Constraint_Error with "new length is out of range";
189 Last_As_Int := Int (Index_Type'First) + Int (RN);
191 if Last_As_Int > Int (Index_Type'Last) then
192 raise Constraint_Error with "new length is out of range";
196 Last : constant Index_Type := Index_Type (Last_As_Int);
198 RE : Elements_Array renames
199 Right.Elements.EA (Index_Type'First .. Right.Last);
201 Elements : constant Elements_Access :=
207 return (Controlled with Elements, Last, 0, 0);
212 function "&" (Left, Right : Element_Type) return Vector is
214 if Index_Type'First >= Index_Type'Last then
215 raise Constraint_Error with "new length is out of range";
219 Last : constant Index_Type := Index_Type'First + 1;
221 Elements : constant Elements_Access :=
224 EA => (Left, Right));
227 return (Controlled with Elements, Last, 0, 0);
235 function "=" (Left, Right : Vector) return Boolean is
237 if Left'Address = Right'Address then
241 if Left.Last /= Right.Last then
245 for J in Index_Type range Index_Type'First .. Left.Last loop
246 if Left.Elements.EA (J) /= Right.Elements.EA (J) then
258 procedure Adjust (Container : in out Vector) is
260 if Container.Last = No_Index then
261 Container.Elements := null;
266 L : constant Index_Type := Container.Last;
267 EA : Elements_Array renames
268 Container.Elements.EA (Index_Type'First .. L);
271 Container.Elements := null;
272 Container.Last := No_Index;
276 Container.Elements := new Elements_Type'(L, EA);
285 procedure Append (Container : in out Vector; New_Item : Vector) is
287 if Is_Empty (New_Item) then
291 if Container.Last = Index_Type'Last then
292 raise Constraint_Error with "vector is already at its maximum length";
302 (Container : in out Vector;
303 New_Item : Element_Type;
304 Count : Count_Type := 1)
311 if Container.Last = Index_Type'Last then
312 raise Constraint_Error with "vector is already at its maximum length";
326 function Capacity (Container : Vector) return Count_Type is
328 if Container.Elements = null then
332 return Container.Elements.EA'Length;
339 procedure Clear (Container : in out Vector) is
341 if Container.Busy > 0 then
342 raise Program_Error with
343 "attempt to tamper with elements (vector is busy)";
346 Container.Last := No_Index;
355 Item : Element_Type) return Boolean
358 return Find_Index (Container, Item) /= No_Index;
366 (Container : in out Vector;
367 Index : Extended_Index;
368 Count : Count_Type := 1)
371 if Index < Index_Type'First then
372 raise Constraint_Error with "Index is out of range (too small)";
375 if Index > Container.Last then
376 if Index > Container.Last + 1 then
377 raise Constraint_Error with "Index is out of range (too large)";
387 if Container.Busy > 0 then
388 raise Program_Error with
389 "attempt to tamper with elements (vector is busy)";
393 I_As_Int : constant Int := Int (Index);
394 Old_Last_As_Int : constant Int := Index_Type'Pos (Container.Last);
396 Count1 : constant Int'Base := Count_Type'Pos (Count);
397 Count2 : constant Int'Base := Old_Last_As_Int - I_As_Int + 1;
398 N : constant Int'Base := Int'Min (Count1, Count2);
400 J_As_Int : constant Int'Base := I_As_Int + N;
403 if J_As_Int > Old_Last_As_Int then
404 Container.Last := Index - 1;
408 J : constant Index_Type := Index_Type (J_As_Int);
409 EA : Elements_Array renames Container.Elements.EA;
411 New_Last_As_Int : constant Int'Base := Old_Last_As_Int - N;
412 New_Last : constant Index_Type :=
413 Index_Type (New_Last_As_Int);
416 EA (Index .. New_Last) := EA (J .. Container.Last);
417 Container.Last := New_Last;
424 (Container : in out Vector;
425 Position : in out Cursor;
426 Count : Count_Type := 1)
428 pragma Warnings (Off, Position);
431 if Position.Container = null then
432 raise Constraint_Error with "Position cursor has no element";
435 if Position.Container /= Container'Unrestricted_Access then
436 raise Program_Error with "Position cursor denotes wrong container";
439 if Position.Index > Container.Last then
440 raise Program_Error with "Position index is out of range";
443 Delete (Container, Position.Index, Count);
444 Position := No_Element;
451 procedure Delete_First
452 (Container : in out Vector;
453 Count : Count_Type := 1)
460 if Count >= Length (Container) then
465 Delete (Container, Index_Type'First, Count);
472 procedure Delete_Last
473 (Container : in out Vector;
474 Count : Count_Type := 1)
483 if Container.Busy > 0 then
484 raise Program_Error with
485 "attempt to tamper with elements (vector is busy)";
488 Index := Int'Base (Container.Last) - Int'Base (Count);
490 if Index < Index_Type'Pos (Index_Type'First) then
491 Container.Last := No_Index;
493 Container.Last := Index_Type (Index);
503 Index : Index_Type) return Element_Type
506 if Index > Container.Last then
507 raise Constraint_Error with "Index is out of range";
510 return Container.Elements.EA (Index);
513 function Element (Position : Cursor) return Element_Type is
515 if Position.Container = null then
516 raise Constraint_Error with "Position cursor has no element";
519 if Position.Index > Position.Container.Last then
520 raise Constraint_Error with "Position cursor is out of range";
523 return Position.Container.Elements.EA (Position.Index);
530 procedure Finalize (Container : in out Vector) is
531 X : Elements_Access := Container.Elements;
534 if Container.Busy > 0 then
535 raise Program_Error with
536 "attempt to tamper with elements (vector is busy)";
539 Container.Elements := null;
540 Container.Last := No_Index;
551 Position : Cursor := No_Element) return Cursor
554 if Position.Container /= null then
555 if Position.Container /= Container'Unrestricted_Access then
556 raise Program_Error with "Position cursor denotes wrong container";
559 if Position.Index > Container.Last then
560 raise Program_Error with "Position index is out of range";
564 for J in Position.Index .. Container.Last loop
565 if Container.Elements.EA (J) = Item then
566 return (Container'Unchecked_Access, J);
580 Index : Index_Type := Index_Type'First) return Extended_Index
583 for Indx in Index .. Container.Last loop
584 if Container.Elements.EA (Indx) = Item then
596 function First (Container : Vector) return Cursor is
598 if Is_Empty (Container) then
602 return (Container'Unchecked_Access, Index_Type'First);
609 function First_Element (Container : Vector) return Element_Type is
611 if Container.Last = No_Index then
612 raise Constraint_Error with "Container is empty";
615 return Container.Elements.EA (Index_Type'First);
622 function First_Index (Container : Vector) return Index_Type is
623 pragma Unreferenced (Container);
625 return Index_Type'First;
628 ---------------------
629 -- Generic_Sorting --
630 ---------------------
632 package body Generic_Sorting is
638 function Is_Sorted (Container : Vector) return Boolean is
640 if Container.Last <= Index_Type'First then
645 EA : Elements_Array renames Container.Elements.EA;
647 for I in Index_Type'First .. Container.Last - 1 loop
648 if EA (I + 1) < EA (I) then
661 procedure Merge (Target, Source : in out Vector) is
662 I : Index_Type'Base := Target.Last;
666 if Target.Last < Index_Type'First then
667 Move (Target => Target, Source => Source);
671 if Target'Address = Source'Address then
675 if Source.Last < Index_Type'First then
679 if Source.Busy > 0 then
680 raise Program_Error with
681 "attempt to tamper with elements (vector is busy)";
684 Target.Set_Length (Length (Target) + Length (Source));
687 TA : Elements_Array renames Target.Elements.EA;
688 SA : Elements_Array renames Source.Elements.EA;
692 while Source.Last >= Index_Type'First loop
693 pragma Assert (Source.Last <= Index_Type'First
694 or else not (SA (Source.Last) <
695 SA (Source.Last - 1)));
697 if I < Index_Type'First then
698 TA (Index_Type'First .. J) :=
699 SA (Index_Type'First .. Source.Last);
701 Source.Last := No_Index;
705 pragma Assert (I <= Index_Type'First
706 or else not (TA (I) < TA (I - 1)));
708 if SA (Source.Last) < TA (I) then
713 TA (J) := SA (Source.Last);
714 Source.Last := Source.Last - 1;
726 procedure Sort (Container : in out Vector)
729 new Generic_Array_Sort
730 (Index_Type => Index_Type,
731 Element_Type => Element_Type,
732 Array_Type => Elements_Array,
736 if Container.Last <= Index_Type'First then
740 if Container.Lock > 0 then
741 raise Program_Error with
742 "attempt to tamper with cursors (vector is locked)";
745 Sort (Container.Elements.EA (Index_Type'First .. Container.Last));
754 function Has_Element (Position : Cursor) return Boolean is
756 if Position.Container = null then
760 return Position.Index <= Position.Container.Last;
768 (Container : in out Vector;
769 Before : Extended_Index;
770 New_Item : Element_Type;
771 Count : Count_Type := 1)
773 N : constant Int := Count_Type'Pos (Count);
775 First : constant Int := Int (Index_Type'First);
776 New_Last_As_Int : Int'Base;
777 New_Last : Index_Type;
779 Max_Length : constant UInt := UInt (Count_Type'Last);
781 Dst : Elements_Access;
784 if Before < Index_Type'First then
785 raise Constraint_Error with
786 "Before index is out of range (too small)";
789 if Before > Container.Last
790 and then Before > Container.Last + 1
792 raise Constraint_Error with
793 "Before index is out of range (too large)";
801 Old_Last_As_Int : constant Int := Int (Container.Last);
804 if Old_Last_As_Int > Int'Last - N then
805 raise Constraint_Error with "new length is out of range";
808 New_Last_As_Int := Old_Last_As_Int + N;
810 if New_Last_As_Int > Int (Index_Type'Last) then
811 raise Constraint_Error with "new length is out of range";
814 New_Length := UInt (New_Last_As_Int - First + Int'(1));
816 if New_Length > Max_Length then
817 raise Constraint_Error with "new length is out of range";
820 New_Last := Index_Type (New_Last_As_Int);
823 if Container.Busy > 0 then
824 raise Program_Error with
825 "attempt to tamper with elements (vector is busy)";
828 if Container.Elements = null then
829 Container.Elements := new Elements_Type'
831 EA => (others => New_Item));
832 Container.Last := New_Last;
836 if New_Last <= Container.Elements.Last then
838 EA : Elements_Array renames Container.Elements.EA;
841 if Before <= Container.Last then
843 Index_As_Int : constant Int'Base :=
844 Index_Type'Pos (Before) + N;
846 Index : constant Index_Type := Index_Type (Index_As_Int);
849 EA (Index .. New_Last) := EA (Before .. Container.Last);
851 EA (Before .. Index_Type'Pred (Index)) :=
852 (others => New_Item);
856 EA (Before .. New_Last) := (others => New_Item);
860 Container.Last := New_Last;
868 C := UInt'Max (1, Container.Elements.EA'Length); -- ???
869 while C < New_Length loop
870 if C > UInt'Last / 2 then
878 if C > Max_Length then
882 if Index_Type'First <= 0
883 and then Index_Type'Last >= 0
885 CC := UInt (Index_Type'Last) + UInt (-Index_Type'First) + 1;
888 CC := UInt (Int (Index_Type'Last) - First + 1);
896 Dst_Last : constant Index_Type :=
897 Index_Type (First + UInt'Pos (C) - 1);
900 Dst := new Elements_Type (Dst_Last);
905 SA : Elements_Array renames Container.Elements.EA;
906 DA : Elements_Array renames Dst.EA;
909 DA (Index_Type'First .. Index_Type'Pred (Before)) :=
910 SA (Index_Type'First .. Index_Type'Pred (Before));
912 if Before <= Container.Last then
914 Index_As_Int : constant Int'Base :=
915 Index_Type'Pos (Before) + N;
917 Index : constant Index_Type := Index_Type (Index_As_Int);
920 DA (Before .. Index_Type'Pred (Index)) := (others => New_Item);
921 DA (Index .. New_Last) := SA (Before .. Container.Last);
925 DA (Before .. New_Last) := (others => New_Item);
934 X : Elements_Access := Container.Elements;
936 Container.Elements := Dst;
937 Container.Last := New_Last;
943 (Container : in out Vector;
944 Before : Extended_Index;
947 N : constant Count_Type := Length (New_Item);
950 if Before < Index_Type'First then
951 raise Constraint_Error with
952 "Before index is out of range (too small)";
955 if Before > Container.Last
956 and then Before > Container.Last + 1
958 raise Constraint_Error with
959 "Before index is out of range (too large)";
966 Insert_Space (Container, Before, Count => N);
969 Dst_Last_As_Int : constant Int'Base :=
970 Int'Base (Before) + Int'Base (N) - 1;
972 Dst_Last : constant Index_Type := Index_Type (Dst_Last_As_Int);
975 if Container'Address /= New_Item'Address then
976 Container.Elements.EA (Before .. Dst_Last) :=
977 New_Item.Elements.EA (Index_Type'First .. New_Item.Last);
983 subtype Src_Index_Subtype is Index_Type'Base range
984 Index_Type'First .. Before - 1;
986 Src : Elements_Array renames
987 Container.Elements.EA (Src_Index_Subtype);
989 Index_As_Int : constant Int'Base :=
990 Int (Before) + Src'Length - 1;
992 Index : constant Index_Type'Base :=
993 Index_Type'Base (Index_As_Int);
995 Dst : Elements_Array renames
996 Container.Elements.EA (Before .. Index);
1002 if Dst_Last = Container.Last then
1007 subtype Src_Index_Subtype is Index_Type'Base range
1008 Dst_Last + 1 .. Container.Last;
1010 Src : Elements_Array renames
1011 Container.Elements.EA (Src_Index_Subtype);
1013 Index_As_Int : constant Int'Base :=
1014 Dst_Last_As_Int - Src'Length + 1;
1016 Index : constant Index_Type :=
1017 Index_Type (Index_As_Int);
1019 Dst : Elements_Array renames
1020 Container.Elements.EA (Index .. Dst_Last);
1029 (Container : in out Vector;
1033 Index : Index_Type'Base;
1036 if Before.Container /= null
1037 and then Before.Container /= Container'Unchecked_Access
1039 raise Program_Error with "Before cursor denotes wrong container";
1042 if Is_Empty (New_Item) then
1046 if Before.Container = null
1047 or else Before.Index > Container.Last
1049 if Container.Last = Index_Type'Last then
1050 raise Constraint_Error with
1051 "vector is already at its maximum length";
1054 Index := Container.Last + 1;
1057 Index := Before.Index;
1060 Insert (Container, Index, New_Item);
1064 (Container : in out Vector;
1067 Position : out Cursor)
1069 Index : Index_Type'Base;
1072 if Before.Container /= null
1073 and then Before.Container /= Container'Unchecked_Access
1075 raise Program_Error with "Before cursor denotes wrong container";
1078 if Is_Empty (New_Item) then
1079 if Before.Container = null
1080 or else Before.Index > Container.Last
1082 Position := No_Element;
1084 Position := (Container'Unchecked_Access, Before.Index);
1090 if Before.Container = null
1091 or else Before.Index > Container.Last
1093 if Container.Last = Index_Type'Last then
1094 raise Constraint_Error with
1095 "vector is already at its maximum length";
1098 Index := Container.Last + 1;
1101 Index := Before.Index;
1104 Insert (Container, Index, New_Item);
1106 Position := Cursor'(Container'Unchecked_Access, Index);
1110 (Container : in out Vector;
1112 New_Item : Element_Type;
1113 Count : Count_Type := 1)
1115 Index : Index_Type'Base;
1118 if Before.Container /= null
1119 and then Before.Container /= Container'Unchecked_Access
1121 raise Program_Error with "Before cursor denotes wrong container";
1128 if Before.Container = null
1129 or else Before.Index > Container.Last
1131 if Container.Last = Index_Type'Last then
1132 raise Constraint_Error with
1133 "vector is already at its maximum length";
1136 Index := Container.Last + 1;
1139 Index := Before.Index;
1142 Insert (Container, Index, New_Item, Count);
1146 (Container : in out Vector;
1148 New_Item : Element_Type;
1149 Position : out Cursor;
1150 Count : Count_Type := 1)
1152 Index : Index_Type'Base;
1155 if Before.Container /= null
1156 and then Before.Container /= Container'Unchecked_Access
1158 raise Program_Error with "Before cursor denotes wrong container";
1162 if Before.Container = null
1163 or else Before.Index > Container.Last
1165 Position := No_Element;
1167 Position := (Container'Unchecked_Access, Before.Index);
1173 if Before.Container = null
1174 or else Before.Index > Container.Last
1176 if Container.Last = Index_Type'Last then
1177 raise Constraint_Error with
1178 "vector is already at its maximum length";
1181 Index := Container.Last + 1;
1184 Index := Before.Index;
1187 Insert (Container, Index, New_Item, Count);
1189 Position := Cursor'(Container'Unchecked_Access, Index);
1193 (Container : in out Vector;
1194 Before : Extended_Index;
1195 Count : Count_Type := 1)
1197 New_Item : Element_Type; -- Default-initialized value
1198 pragma Warnings (Off, New_Item);
1201 Insert (Container, Before, New_Item, Count);
1205 (Container : in out Vector;
1207 Position : out Cursor;
1208 Count : Count_Type := 1)
1210 New_Item : Element_Type; -- Default-initialized value
1211 pragma Warnings (Off, New_Item);
1214 Insert (Container, Before, New_Item, Position, Count);
1221 procedure Insert_Space
1222 (Container : in out Vector;
1223 Before : Extended_Index;
1224 Count : Count_Type := 1)
1226 N : constant Int := Count_Type'Pos (Count);
1228 First : constant Int := Int (Index_Type'First);
1229 New_Last_As_Int : Int'Base;
1230 New_Last : Index_Type;
1232 Max_Length : constant UInt := UInt (Count_Type'Last);
1234 Dst : Elements_Access;
1237 if Before < Index_Type'First then
1238 raise Constraint_Error with
1239 "Before index is out of range (too small)";
1242 if Before > Container.Last
1243 and then Before > Container.Last + 1
1245 raise Constraint_Error with
1246 "Before index is out of range (too large)";
1254 Old_Last_As_Int : constant Int := Int (Container.Last);
1257 if Old_Last_As_Int > Int'Last - N then
1258 raise Constraint_Error with "new length is out of range";
1261 New_Last_As_Int := Old_Last_As_Int + N;
1263 if New_Last_As_Int > Int (Index_Type'Last) then
1264 raise Constraint_Error with "new length is out of range";
1267 New_Length := UInt (New_Last_As_Int - First + Int'(1));
1269 if New_Length > Max_Length then
1270 raise Constraint_Error with "new length is out of range";
1273 New_Last := Index_Type (New_Last_As_Int);
1276 if Container.Busy > 0 then
1277 raise Program_Error with
1278 "attempt to tamper with elements (vector is busy)";
1281 if Container.Elements = null then
1282 Container.Elements := new Elements_Type (New_Last);
1283 Container.Last := New_Last;
1287 if New_Last <= Container.Elements.Last then
1289 EA : Elements_Array renames Container.Elements.EA;
1291 if Before <= Container.Last then
1293 Index_As_Int : constant Int'Base :=
1294 Index_Type'Pos (Before) + N;
1296 Index : constant Index_Type := Index_Type (Index_As_Int);
1299 EA (Index .. New_Last) := EA (Before .. Container.Last);
1304 Container.Last := New_Last;
1312 C := UInt'Max (1, Container.Elements.EA'Length); -- ???
1313 while C < New_Length loop
1314 if C > UInt'Last / 2 then
1322 if C > Max_Length then
1326 if Index_Type'First <= 0
1327 and then Index_Type'Last >= 0
1329 CC := UInt (Index_Type'Last) + UInt (-Index_Type'First) + 1;
1332 CC := UInt (Int (Index_Type'Last) - First + 1);
1340 Dst_Last : constant Index_Type :=
1341 Index_Type (First + UInt'Pos (C) - 1);
1344 Dst := new Elements_Type (Dst_Last);
1349 SA : Elements_Array renames Container.Elements.EA;
1350 DA : Elements_Array renames Dst.EA;
1353 DA (Index_Type'First .. Index_Type'Pred (Before)) :=
1354 SA (Index_Type'First .. Index_Type'Pred (Before));
1356 if Before <= Container.Last then
1358 Index_As_Int : constant Int'Base :=
1359 Index_Type'Pos (Before) + N;
1361 Index : constant Index_Type := Index_Type (Index_As_Int);
1364 DA (Index .. New_Last) := SA (Before .. Container.Last);
1374 X : Elements_Access := Container.Elements;
1376 Container.Elements := Dst;
1377 Container.Last := New_Last;
1382 procedure Insert_Space
1383 (Container : in out Vector;
1385 Position : out Cursor;
1386 Count : Count_Type := 1)
1388 Index : Index_Type'Base;
1391 if Before.Container /= null
1392 and then Before.Container /= Container'Unchecked_Access
1394 raise Program_Error with "Before cursor denotes wrong container";
1398 if Before.Container = null
1399 or else Before.Index > Container.Last
1401 Position := No_Element;
1403 Position := (Container'Unchecked_Access, Before.Index);
1409 if Before.Container = null
1410 or else Before.Index > Container.Last
1412 if Container.Last = Index_Type'Last then
1413 raise Constraint_Error with
1414 "vector is already at its maximum length";
1417 Index := Container.Last + 1;
1420 Index := Before.Index;
1423 Insert_Space (Container, Index, Count => Count);
1425 Position := Cursor'(Container'Unchecked_Access, Index);
1432 function Is_Empty (Container : Vector) return Boolean is
1434 return Container.Last < Index_Type'First;
1442 (Container : Vector;
1443 Process : not null access procedure (Position : Cursor))
1445 V : Vector renames Container'Unrestricted_Access.all;
1446 B : Natural renames V.Busy;
1452 for Indx in Index_Type'First .. Container.Last loop
1453 Process (Cursor'(Container'Unchecked_Access, Indx));
1468 function Last (Container : Vector) return Cursor is
1470 if Is_Empty (Container) then
1474 return (Container'Unchecked_Access, Container.Last);
1481 function Last_Element (Container : Vector) return Element_Type is
1483 if Container.Last = No_Index then
1484 raise Constraint_Error with "Container is empty";
1487 return Container.Elements.EA (Container.Last);
1494 function Last_Index (Container : Vector) return Extended_Index is
1496 return Container.Last;
1503 function Length (Container : Vector) return Count_Type is
1504 L : constant Int := Int (Container.Last);
1505 F : constant Int := Int (Index_Type'First);
1506 N : constant Int'Base := L - F + 1;
1509 return Count_Type (N);
1517 (Target : in out Vector;
1518 Source : in out Vector)
1521 if Target'Address = Source'Address then
1525 if Target.Busy > 0 then
1526 raise Program_Error with
1527 "attempt to tamper with elements (Target is busy)";
1530 if Source.Busy > 0 then
1531 raise Program_Error with
1532 "attempt to tamper with elements (Source is busy)";
1536 Target_Elements : constant Elements_Access := Target.Elements;
1538 Target.Elements := Source.Elements;
1539 Source.Elements := Target_Elements;
1542 Target.Last := Source.Last;
1543 Source.Last := No_Index;
1550 function Next (Position : Cursor) return Cursor is
1552 if Position.Container = null then
1556 if Position.Index < Position.Container.Last then
1557 return (Position.Container, Position.Index + 1);
1567 procedure Next (Position : in out Cursor) is
1569 if Position.Container = null then
1573 if Position.Index < Position.Container.Last then
1574 Position.Index := Position.Index + 1;
1576 Position := No_Element;
1584 procedure Prepend (Container : in out Vector; New_Item : Vector) is
1586 Insert (Container, Index_Type'First, New_Item);
1590 (Container : in out Vector;
1591 New_Item : Element_Type;
1592 Count : Count_Type := 1)
1605 procedure Previous (Position : in out Cursor) is
1607 if Position.Container = null then
1611 if Position.Index > Index_Type'First then
1612 Position.Index := Position.Index - 1;
1614 Position := No_Element;
1618 function Previous (Position : Cursor) return Cursor is
1620 if Position.Container = null then
1624 if Position.Index > Index_Type'First then
1625 return (Position.Container, Position.Index - 1);
1635 procedure Query_Element
1636 (Container : Vector;
1638 Process : not null access procedure (Element : Element_Type))
1640 V : Vector renames Container'Unrestricted_Access.all;
1641 B : Natural renames V.Busy;
1642 L : Natural renames V.Lock;
1645 if Index > Container.Last then
1646 raise Constraint_Error with "Index is out of range";
1653 Process (V.Elements.EA (Index));
1665 procedure Query_Element
1667 Process : not null access procedure (Element : Element_Type))
1670 if Position.Container = null then
1671 raise Constraint_Error with "Position cursor has no element";
1674 Query_Element (Position.Container.all, Position.Index, Process);
1682 (Stream : not null access Root_Stream_Type'Class;
1683 Container : out Vector)
1685 Length : Count_Type'Base;
1686 Last : Index_Type'Base := No_Index;
1691 Count_Type'Base'Read (Stream, Length);
1693 if Length > Capacity (Container) then
1694 Reserve_Capacity (Container, Capacity => Length);
1697 for J in Count_Type range 1 .. Length loop
1699 Element_Type'Read (Stream, Container.Elements.EA (Last));
1700 Container.Last := Last;
1705 (Stream : not null access Root_Stream_Type'Class;
1706 Position : out Cursor)
1709 raise Program_Error with "attempt to stream vector cursor";
1712 ---------------------
1713 -- Replace_Element --
1714 ---------------------
1716 procedure Replace_Element
1717 (Container : in out Vector;
1719 New_Item : Element_Type)
1722 if Index > Container.Last then
1723 raise Constraint_Error with "Index is out of range";
1726 if Container.Lock > 0 then
1727 raise Program_Error with
1728 "attempt to tamper with cursors (vector is locked)";
1731 Container.Elements.EA (Index) := New_Item;
1732 end Replace_Element;
1734 procedure Replace_Element
1735 (Container : in out Vector;
1737 New_Item : Element_Type)
1740 if Position.Container = null then
1741 raise Constraint_Error with "Position cursor has no element";
1744 if Position.Container /= Container'Unrestricted_Access then
1745 raise Program_Error with "Position cursor denotes wrong container";
1748 if Position.Index > Container.Last then
1749 raise Constraint_Error with "Position cursor is out of range";
1752 if Container.Lock > 0 then
1753 raise Program_Error with
1754 "attempt to tamper with cursors (vector is locked)";
1757 Container.Elements.EA (Position.Index) := New_Item;
1758 end Replace_Element;
1760 ----------------------
1761 -- Reserve_Capacity --
1762 ----------------------
1764 procedure Reserve_Capacity
1765 (Container : in out Vector;
1766 Capacity : Count_Type)
1768 N : constant Count_Type := Length (Container);
1771 if Capacity = 0 then
1774 X : Elements_Access := Container.Elements;
1776 Container.Elements := null;
1780 elsif N < Container.Elements.EA'Length then
1781 if Container.Busy > 0 then
1782 raise Program_Error with
1783 "attempt to tamper with elements (vector is busy)";
1787 subtype Src_Index_Subtype is Index_Type'Base range
1788 Index_Type'First .. Container.Last;
1790 Src : Elements_Array renames
1791 Container.Elements.EA (Src_Index_Subtype);
1793 X : Elements_Access := Container.Elements;
1796 Container.Elements := new Elements_Type'(Container.Last, Src);
1804 if Container.Elements = null then
1806 Last_As_Int : constant Int'Base :=
1807 Int (Index_Type'First) + Int (Capacity) - 1;
1810 if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
1811 raise Constraint_Error with "new length is out of range";
1815 Last : constant Index_Type := Index_Type (Last_As_Int);
1818 Container.Elements := new Elements_Type (Last);
1825 if Capacity <= N then
1826 if N < Container.Elements.EA'Length then
1827 if Container.Busy > 0 then
1828 raise Program_Error with
1829 "attempt to tamper with elements (vector is busy)";
1833 subtype Src_Index_Subtype is Index_Type'Base range
1834 Index_Type'First .. Container.Last;
1836 Src : Elements_Array renames
1837 Container.Elements.EA (Src_Index_Subtype);
1839 X : Elements_Access := Container.Elements;
1842 Container.Elements := new Elements_Type'(Container.Last, Src);
1851 if Capacity = Container.Elements.EA'Length then
1855 if Container.Busy > 0 then
1856 raise Program_Error with
1857 "attempt to tamper with elements (vector is busy)";
1861 Last_As_Int : constant Int'Base :=
1862 Int (Index_Type'First) + Int (Capacity) - 1;
1865 if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
1866 raise Constraint_Error with "new length is out of range";
1870 Last : constant Index_Type := Index_Type (Last_As_Int);
1872 E : Elements_Access := new Elements_Type (Last);
1876 subtype Index_Subtype is Index_Type'Base range
1877 Index_Type'First .. Container.Last;
1879 Src : Elements_Array renames
1880 Container.Elements.EA (Index_Subtype);
1882 Tgt : Elements_Array renames E.EA (Index_Subtype);
1894 X : Elements_Access := Container.Elements;
1896 Container.Elements := E;
1901 end Reserve_Capacity;
1903 ----------------------
1904 -- Reverse_Elements --
1905 ----------------------
1907 procedure Reverse_Elements (Container : in out Vector) is
1909 if Container.Length <= 1 then
1913 if Container.Lock > 0 then
1914 raise Program_Error with
1915 "attempt to tamper with cursors (vector is locked)";
1920 E : Elements_Type renames Container.Elements.all;
1923 I := Index_Type'First;
1924 J := Container.Last;
1927 EI : constant Element_Type := E.EA (I);
1930 E.EA (I) := E.EA (J);
1938 end Reverse_Elements;
1944 function Reverse_Find
1945 (Container : Vector;
1946 Item : Element_Type;
1947 Position : Cursor := No_Element) return Cursor
1949 Last : Index_Type'Base;
1952 if Position.Container /= null
1953 and then Position.Container /= Container'Unchecked_Access
1955 raise Program_Error with "Position cursor denotes wrong container";
1958 if Position.Container = null
1959 or else Position.Index > Container.Last
1961 Last := Container.Last;
1963 Last := Position.Index;
1966 for Indx in reverse Index_Type'First .. Last loop
1967 if Container.Elements.EA (Indx) = Item then
1968 return (Container'Unchecked_Access, Indx);
1975 ------------------------
1976 -- Reverse_Find_Index --
1977 ------------------------
1979 function Reverse_Find_Index
1980 (Container : Vector;
1981 Item : Element_Type;
1982 Index : Index_Type := Index_Type'Last) return Extended_Index
1984 Last : Index_Type'Base;
1987 if Index > Container.Last then
1988 Last := Container.Last;
1993 for Indx in reverse Index_Type'First .. Last loop
1994 if Container.Elements.EA (Indx) = Item then
2000 end Reverse_Find_Index;
2002 ---------------------
2003 -- Reverse_Iterate --
2004 ---------------------
2006 procedure Reverse_Iterate
2007 (Container : Vector;
2008 Process : not null access procedure (Position : Cursor))
2010 V : Vector renames Container'Unrestricted_Access.all;
2011 B : Natural renames V.Busy;
2017 for Indx in reverse Index_Type'First .. Container.Last loop
2018 Process (Cursor'(Container'Unchecked_Access, Indx));
2027 end Reverse_Iterate;
2033 procedure Set_Length (Container : in out Vector; Length : Count_Type) is
2035 if Length = Vectors.Length (Container) then
2039 if Container.Busy > 0 then
2040 raise Program_Error with
2041 "attempt to tamper with elements (vector is busy)";
2044 if Length > Capacity (Container) then
2045 Reserve_Capacity (Container, Capacity => Length);
2049 Last_As_Int : constant Int'Base :=
2050 Int (Index_Type'First) + Int (Length) - 1;
2052 Container.Last := Index_Type'Base (Last_As_Int);
2060 procedure Swap (Container : in out Vector; I, J : Index_Type) is
2062 if I > Container.Last then
2063 raise Constraint_Error with "I index is out of range";
2066 if J > Container.Last then
2067 raise Constraint_Error with "J index is out of range";
2074 if Container.Lock > 0 then
2075 raise Program_Error with
2076 "attempt to tamper with cursors (vector is locked)";
2080 EI_Copy : constant Element_Type := Container.Elements.EA (I);
2082 Container.Elements.EA (I) := Container.Elements.EA (J);
2083 Container.Elements.EA (J) := EI_Copy;
2087 procedure Swap (Container : in out Vector; I, J : Cursor) is
2089 if I.Container = null then
2090 raise Constraint_Error with "I cursor has no element";
2093 if J.Container = null then
2094 raise Constraint_Error with "J cursor has no element";
2097 if I.Container /= Container'Unrestricted_Access then
2098 raise Program_Error with "I cursor denotes wrong container";
2101 if J.Container /= Container'Unrestricted_Access then
2102 raise Program_Error with "J cursor denotes wrong container";
2105 Swap (Container, I.Index, J.Index);
2113 (Container : Vector;
2114 Index : Extended_Index) return Cursor
2117 if Index not in Index_Type'First .. Container.Last then
2121 return Cursor'(Container'Unchecked_Access, Index);
2128 function To_Index (Position : Cursor) return Extended_Index is
2130 if Position.Container = null then
2134 if Position.Index <= Position.Container.Last then
2135 return Position.Index;
2145 function To_Vector (Length : Count_Type) return Vector is
2148 return Empty_Vector;
2152 First : constant Int := Int (Index_Type'First);
2153 Last_As_Int : constant Int'Base := First + Int (Length) - 1;
2155 Elements : Elements_Access;
2158 if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
2159 raise Constraint_Error with "Length is out of range";
2162 Last := Index_Type (Last_As_Int);
2163 Elements := new Elements_Type (Last);
2165 return Vector'(Controlled with Elements, Last, 0, 0);
2170 (New_Item : Element_Type;
2171 Length : Count_Type) return Vector
2175 return Empty_Vector;
2179 First : constant Int := Int (Index_Type'First);
2180 Last_As_Int : constant Int'Base := First + Int (Length) - 1;
2182 Elements : Elements_Access;
2185 if Last_As_Int > Index_Type'Pos (Index_Type'Last) then
2186 raise Constraint_Error with "Length is out of range";
2189 Last := Index_Type (Last_As_Int);
2190 Elements := new Elements_Type'(Last, EA => (others => New_Item));
2192 return Vector'(Controlled with Elements, Last, 0, 0);
2196 --------------------
2197 -- Update_Element --
2198 --------------------
2200 procedure Update_Element
2201 (Container : in out Vector;
2203 Process : not null access procedure (Element : in out Element_Type))
2205 B : Natural renames Container.Busy;
2206 L : Natural renames Container.Lock;
2209 if Index > Container.Last then
2210 raise Constraint_Error with "Index is out of range";
2217 Process (Container.Elements.EA (Index));
2229 procedure Update_Element
2230 (Container : in out Vector;
2232 Process : not null access procedure (Element : in out Element_Type))
2235 if Position.Container = null then
2236 raise Constraint_Error with "Position cursor has no element";
2239 if Position.Container /= Container'Unrestricted_Access then
2240 raise Program_Error with "Position cursor denotes wrong container";
2243 Update_Element (Container, Position.Index, Process);
2251 (Stream : not null access Root_Stream_Type'Class;
2255 Count_Type'Base'Write (Stream, Length (Container));
2257 for J in Index_Type'First .. Container.Last loop
2258 Element_Type'Write (Stream, Container.Elements.EA (J));
2263 (Stream : not null access Root_Stream_Type'Class;
2267 raise Program_Error with "attempt to stream vector cursor";
2270 end Ada.Containers.Vectors;