1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . F O R M A L _ V E C T O R S --
9 -- Copyright (C) 2010-2013, 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/>. --
26 ------------------------------------------------------------------------------
28 with Ada
.Containers
.Generic_Array_Sort
;
29 with System
; use type System
.Address
;
31 package body Ada
.Containers
.Formal_Vectors
is
33 type Int
is range System
.Min_Int
.. System
.Max_Int
;
34 type UInt
is mod System
.Max_Binary_Modulus
;
38 Position
: Count_Type
) return Element_Type
;
40 procedure Insert_Space
41 (Container
: in out Vector
;
42 Before
: Extended_Index
;
43 Count
: Count_Type
:= 1);
49 function "&" (Left
, Right
: Vector
) return Vector
is
50 LN
: constant Count_Type
:= Length
(Left
);
51 RN
: constant Count_Type
:= Length
(Right
);
60 E
: constant Elements_Array
(1 .. Length
(Right
)) :=
61 Right
.Elements
(1 .. RN
);
63 return (Length
(Right
), E
, Last
=> Right
.Last
, others => <>);
69 E
: constant Elements_Array
(1 .. Length
(Left
)) :=
70 Left
.Elements
(1 .. LN
);
72 return (Length
(Left
), E
, Last
=> Left
.Last
, others => <>);
77 N
: constant Int
'Base := Int
(LN
) + Int
(RN
);
78 Last_As_Int
: Int
'Base;
81 if Int
(No_Index
) > Int
'Last - N
then
82 raise Constraint_Error
with "new length is out of range";
85 Last_As_Int
:= Int
(No_Index
) + N
;
87 if Last_As_Int
> Int
(Index_Type
'Last) then
88 raise Constraint_Error
with "new length is out of range";
91 -- TODO: should check whether length > max capacity (cnt_t'last) ???
94 Last
: constant Index_Type
:= Index_Type
(Last_As_Int
);
96 LE
: constant Elements_Array
(1 .. LN
) := Left
.Elements
(1 .. LN
);
97 RE
: Elements_Array
renames Right
.Elements
(1 .. RN
);
99 Capacity
: constant Count_Type
:= Length
(Left
) + Length
(Right
);
102 return (Capacity
, LE
& RE
, Last
=> Last
, others => <>);
107 function "&" (Left
: Vector
; Right
: Element_Type
) return Vector
is
108 LN
: constant Count_Type
:= Length
(Left
);
109 Last_As_Int
: Int
'Base;
113 return (1, (1 .. 1 => Right
), Index_Type
'First, others => <>);
116 if Int
(Index_Type
'First) > Int
'Last - Int
(LN
) then
117 raise Constraint_Error
with "new length is out of range";
120 Last_As_Int
:= Int
(Index_Type
'First) + Int
(LN
);
122 if Last_As_Int
> Int
(Index_Type
'Last) then
123 raise Constraint_Error
with "new length is out of range";
127 Last
: constant Index_Type
:= Index_Type
(Last_As_Int
);
128 LE
: constant Elements_Array
(1 .. LN
) := Left
.Elements
(1 .. LN
);
130 Capacity
: constant Count_Type
:= Length
(Left
) + 1;
133 return (Capacity
, LE
& Right
, Last
=> Last
, others => <>);
137 function "&" (Left
: Element_Type
; Right
: Vector
) return Vector
is
138 RN
: constant Count_Type
:= Length
(Right
);
139 Last_As_Int
: Int
'Base;
143 return (1, (1 .. 1 => Left
),
144 Index_Type
'First, others => <>);
147 if Int
(Index_Type
'First) > Int
'Last - Int
(RN
) then
148 raise Constraint_Error
with "new length is out of range";
151 Last_As_Int
:= Int
(Index_Type
'First) + Int
(RN
);
153 if Last_As_Int
> Int
(Index_Type
'Last) then
154 raise Constraint_Error
with "new length is out of range";
158 Last
: constant Index_Type
:= Index_Type
(Last_As_Int
);
159 RE
: Elements_Array
renames Right
.Elements
(1 .. RN
);
160 Capacity
: constant Count_Type
:= 1 + Length
(Right
);
162 return (Capacity
, Left
& RE
, Last
=> Last
, others => <>);
166 function "&" (Left
, Right
: Element_Type
) return Vector
is
168 if Index_Type
'First >= Index_Type
'Last then
169 raise Constraint_Error
with "new length is out of range";
173 Last
: constant Index_Type
:= Index_Type
'First + 1;
175 return (2, (Left
, Right
), Last
=> Last
, others => <>);
183 function "=" (Left
, Right
: Vector
) return Boolean is
185 if Left
'Address = Right
'Address then
189 if Length
(Left
) /= Length
(Right
) then
193 for J
in Count_Type
range 1 .. Length
(Left
) loop
194 if Get_Element
(Left
, J
) /= Get_Element
(Right
, J
) then
206 procedure Append
(Container
: in out Vector
; New_Item
: Vector
) is
208 if Is_Empty
(New_Item
) then
212 if Container
.Last
= Index_Type
'Last then
213 raise Constraint_Error
with "vector is already at its maximum length";
216 Insert
(Container
, Container
.Last
+ 1, New_Item
);
220 (Container
: in out Vector
;
221 New_Item
: Element_Type
;
222 Count
: Count_Type
:= 1)
229 if Container
.Last
= Index_Type
'Last then
230 raise Constraint_Error
with "vector is already at its maximum length";
233 -- TODO: should check whether length > max capacity (cnt_t'last) ???
235 Insert
(Container
, Container
.Last
+ 1, New_Item
, Count
);
242 procedure Assign
(Target
: in out Vector
; Source
: Vector
) is
243 LS
: constant Count_Type
:= Length
(Source
);
246 if Target
'Address = Source
'Address then
250 if Target
.Capacity
< LS
then
251 raise Constraint_Error
;
256 Target
.Elements
(1 .. LS
) := Source
.Elements
(1 .. LS
);
257 Target
.Last
:= Source
.Last
;
264 function Capacity
(Container
: Vector
) return Count_Type
is
266 return Container
.Elements
'Length;
273 procedure Clear
(Container
: in out Vector
) is
275 Container
.Last
:= No_Index
;
284 Item
: Element_Type
) return Boolean
287 return Find_Index
(Container
, Item
) /= No_Index
;
296 Capacity
: Count_Type
:= 0) return Vector
298 LS
: constant Count_Type
:= Length
(Source
);
304 elsif Capacity
>= LS
then
307 raise Constraint_Error
;
310 return Target
: Vector
(C
) do
311 Target
.Elements
(1 .. LS
) := Source
.Elements
(1 .. LS
);
312 Target
.Last
:= Source
.Last
;
321 (Container
: in out Vector
;
322 Index
: Extended_Index
;
323 Count
: Count_Type
:= 1)
326 if Index
< Index_Type
'First then
327 raise Constraint_Error
with "Index is out of range (too small)";
330 if Index
> Container
.Last
then
331 if Index
> Container
.Last
+ 1 then
332 raise Constraint_Error
with "Index is out of range (too large)";
343 I_As_Int
: constant Int
:= Int
(Index
);
344 Old_Last_As_Int
: constant Int
:= Index_Type
'Pos (Container
.Last
);
346 Count1
: constant Int
'Base := Count_Type
'Pos (Count
);
347 Count2
: constant Int
'Base := Old_Last_As_Int
- I_As_Int
+ 1;
348 N
: constant Int
'Base := Int
'Min (Count1
, Count2
);
350 J_As_Int
: constant Int
'Base := I_As_Int
+ N
;
353 if J_As_Int
> Old_Last_As_Int
then
354 Container
.Last
:= Index
- 1;
358 EA
: Elements_Array
renames Container
.Elements
;
360 II
: constant Int
'Base := I_As_Int
- Int
(No_Index
);
361 I
: constant Count_Type
:= Count_Type
(II
);
363 JJ
: constant Int
'Base := J_As_Int
- Int
(No_Index
);
364 J
: constant Count_Type
:= Count_Type
(JJ
);
366 New_Last_As_Int
: constant Int
'Base := Old_Last_As_Int
- N
;
367 New_Last
: constant Index_Type
:=
368 Index_Type
(New_Last_As_Int
);
370 KK
: constant Int
:= New_Last_As_Int
- Int
(No_Index
);
371 K
: constant Count_Type
:= Count_Type
(KK
);
374 EA
(I
.. K
) := EA
(J
.. Length
(Container
));
375 Container
.Last
:= New_Last
;
382 (Container
: in out Vector
;
383 Position
: in out Cursor
;
384 Count
: Count_Type
:= 1)
387 if not Position
.Valid
then
388 raise Constraint_Error
with "Position cursor has no element";
391 if Position
.Index
> Container
.Last
then
392 raise Program_Error
with "Position index is out of range";
395 Delete
(Container
, Position
.Index
, Count
);
396 Position
:= No_Element
;
403 procedure Delete_First
404 (Container
: in out Vector
;
405 Count
: Count_Type
:= 1)
412 if Count
>= Length
(Container
) then
417 Delete
(Container
, Index_Type
'First, Count
);
424 procedure Delete_Last
425 (Container
: in out Vector
;
426 Count
: Count_Type
:= 1)
435 Index
:= Int
'Base (Container
.Last
) - Int
'Base (Count
);
437 if Index
< Index_Type
'Pos (Index_Type
'First) then
438 Container
.Last
:= No_Index
;
440 Container
.Last
:= Index_Type
(Index
);
450 Index
: Index_Type
) return Element_Type
453 if Index
> Container
.Last
then
454 raise Constraint_Error
with "Index is out of range";
458 II
: constant Int
'Base := Int
(Index
) - Int
(No_Index
);
459 I
: constant Count_Type
:= Count_Type
(II
);
461 return Get_Element
(Container
, I
);
467 Position
: Cursor
) return Element_Type
469 Lst
: constant Index_Type
:= Last_Index
(Container
);
472 if not Position
.Valid
then
473 raise Constraint_Error
with "Position cursor has no element";
476 if Position
.Index
> Lst
then
477 raise Constraint_Error
with "Position cursor is out of range";
481 II
: constant Int
'Base := Int
(Position
.Index
) - Int
(No_Index
);
482 I
: constant Count_Type
:= Count_Type
(II
);
484 return Get_Element
(Container
, I
);
495 Position
: Cursor
:= No_Element
) return Cursor
498 Last
: constant Index_Type
:= Last_Index
(Container
);
501 if Position
.Valid
then
502 if Position
.Index
> Last_Index
(Container
) then
503 raise Program_Error
with "Position index is out of range";
507 K
:= Count_Type
(Int
(Position
.Index
) - Int
(No_Index
));
509 for J
in Position
.Index
.. Last
loop
510 if Get_Element
(Container
, K
) = Item
then
511 return Cursor
'(Index => J, others => <>);
527 Index : Index_Type := Index_Type'First) return Extended_Index
530 Last : constant Index_Type := Last_Index (Container);
533 K := Count_Type (Int (Index) - Int (No_Index));
534 for Indx in Index .. Last loop
535 if Get_Element (Container, K) = Item then
549 function First (Container : Vector) return Cursor is
551 if Is_Empty (Container) then
555 return (True, Index_Type'First);
562 function First_Element (Container : Vector) return Element_Type is
564 if Is_Empty (Container) then
565 raise Constraint_Error with "Container is empty";
568 return Get_Element (Container, 1);
575 function First_Index (Container : Vector) return Index_Type is
576 pragma Unreferenced (Container);
578 return Index_Type'First;
581 ---------------------
582 -- Generic_Sorting --
583 ---------------------
585 package body Generic_Sorting is
591 function Is_Sorted (Container : Vector) return Boolean is
592 Last : constant Index_Type := Last_Index (Container);
595 if Container.Last <= Last then
600 L : constant Count_Type := Length (Container);
602 for J in Count_Type range 1 .. L - 1 loop
603 if Get_Element (Container, J + 1) <
604 Get_Element (Container, J)
618 procedure Merge (Target, Source : in out Vector) is
621 TA : Elements_Array renames Target.Elements;
622 SA : Elements_Array renames Source.Elements;
628 -- if Target.Last < Index_Type'First then
629 -- Move (Target => Target, Source => Source);
633 if Target'Address = Source'Address then
637 if Source.Last < Index_Type'First then
641 -- I think we're missing this check in a-convec.adb... ???
643 I := Length (Target);
644 Set_Length (Target, I + Length (Source));
646 J := Length (Target);
647 while not Is_Empty (Source) loop
648 pragma Assert (Length (Source) <= 1
649 or else not (SA (Length (Source)) <
650 SA (Length (Source) - 1)));
653 TA (1 .. J) := SA (1 .. Length (Source));
654 Source.Last := No_Index;
658 pragma Assert (I <= 1 or else not (TA (I) < TA (I - 1)));
660 if SA (Length (Source)) < TA (I) then
665 TA (J) := SA (Length (Source));
666 Source.Last := Source.Last - 1;
678 procedure Sort (Container : in out Vector)
681 new Generic_Array_Sort
682 (Index_Type => Count_Type,
683 Element_Type => Element_Type,
684 Array_Type => Elements_Array,
688 if Container.Last <= Index_Type'First then
692 Sort (Container.Elements (1 .. Length (Container)));
703 Position : Count_Type) return Element_Type
706 return Container.Elements (Position);
715 Position : Cursor) return Boolean
718 if not Position.Valid then
721 return Position.Index <= Last_Index (Container);
730 (Container : in out Vector;
731 Before : Extended_Index;
732 New_Item : Element_Type;
733 Count : Count_Type := 1)
735 N : constant Int := Count_Type'Pos (Count);
737 First : constant Int := Int (Index_Type'First);
738 New_Last_As_Int : Int'Base;
739 New_Last : Index_Type;
741 Max_Length : constant UInt := UInt (Container.Capacity);
744 if Before < Index_Type'First then
745 raise Constraint_Error with
746 "Before index is out of range (too small)";
749 if Before > Container.Last
750 and then Before > Container.Last + 1
752 raise Constraint_Error with
753 "Before index is out of range (too large)";
761 Old_Last_As_Int : constant Int := Int (Container.Last);
764 if Old_Last_As_Int > Int'Last - N then
765 raise Constraint_Error with "new length is out of range";
768 New_Last_As_Int := Old_Last_As_Int + N;
770 if New_Last_As_Int > Int (Index_Type'Last) then
771 raise Constraint_Error with "new length is out of range";
774 New_Length := UInt (New_Last_As_Int - First + Int'(1));
776 if New_Length
> Max_Length
then
777 raise Constraint_Error
with "new length is out of range";
780 New_Last
:= Index_Type
(New_Last_As_Int
);
782 -- Resolve issue of capacity vs. max index ???
786 EA
: Elements_Array
renames Container
.Elements
;
788 BB
: constant Int
'Base := Int
(Before
) - Int
(No_Index
);
789 B
: constant Count_Type
:= Count_Type
(BB
);
791 LL
: constant Int
'Base := New_Last_As_Int
- Int
(No_Index
);
792 L
: constant Count_Type
:= Count_Type
(LL
);
795 if Before
<= Container
.Last
then
797 II
: constant Int
'Base := BB
+ N
;
798 I
: constant Count_Type
:= Count_Type
(II
);
800 EA
(I
.. L
) := EA
(B
.. Length
(Container
));
801 EA
(B
.. I
- 1) := (others => New_Item
);
805 EA
(B
.. L
) := (others => New_Item
);
809 Container
.Last
:= New_Last
;
813 (Container
: in out Vector
;
814 Before
: Extended_Index
;
817 N
: constant Count_Type
:= Length
(New_Item
);
820 if Before
< Index_Type
'First then
821 raise Constraint_Error
with
822 "Before index is out of range (too small)";
825 if Before
> Container
.Last
826 and then Before
> Container
.Last
+ 1
828 raise Constraint_Error
with
829 "Before index is out of range (too large)";
836 Insert_Space
(Container
, Before
, Count
=> N
);
839 Dst_Last_As_Int
: constant Int
'Base :=
840 Int
(Before
) + Int
(N
) - 1 - Int
(No_Index
);
842 Dst_Last
: constant Count_Type
:= Count_Type
(Dst_Last_As_Int
);
844 BB
: constant Int
'Base := Int
(Before
) - Int
(No_Index
);
845 B
: constant Count_Type
:= Count_Type
(BB
);
848 if Container
'Address /= New_Item
'Address then
849 Container
.Elements
(B
.. Dst_Last
) := New_Item
.Elements
(1 .. N
);
854 Src
: Elements_Array
renames Container
.Elements
(1 .. B
- 1);
856 Index_As_Int
: constant Int
'Base := BB
+ Src
'Length - 1;
858 Index
: constant Count_Type
:= Count_Type
(Index_As_Int
);
860 Dst
: Elements_Array
renames Container
.Elements
(B
.. Index
);
866 if Dst_Last
= Length
(Container
) then
871 Src
: Elements_Array
renames
872 Container
.Elements
(Dst_Last
+ 1 .. Length
(Container
));
874 Index_As_Int
: constant Int
'Base :=
875 Dst_Last_As_Int
- Src
'Length + 1;
877 Index
: constant Count_Type
:= Count_Type
(Index_As_Int
);
879 Dst
: Elements_Array
renames
880 Container
.Elements
(Index
.. Dst_Last
);
889 (Container
: in out Vector
;
893 Index
: Index_Type
'Base;
896 if Is_Empty
(New_Item
) then
901 or else Before
.Index
> Container
.Last
903 if Container
.Last
= Index_Type
'Last then
904 raise Constraint_Error
with
905 "vector is already at its maximum length";
908 Index
:= Container
.Last
+ 1;
911 Index
:= Before
.Index
;
914 Insert
(Container
, Index
, New_Item
);
918 (Container
: in out Vector
;
921 Position
: out Cursor
)
923 Index
: Index_Type
'Base;
926 if Is_Empty
(New_Item
) then
928 or else Before
.Index
> Container
.Last
930 Position
:= No_Element
;
932 Position
:= (True, Before
.Index
);
939 or else Before
.Index
> Container
.Last
941 if Container
.Last
= Index_Type
'Last then
942 raise Constraint_Error
with
943 "vector is already at its maximum length";
946 Index
:= Container
.Last
+ 1;
949 Index
:= Before
.Index
;
952 Insert
(Container
, Index
, New_Item
);
954 Position
:= Cursor
'(True, Index);
958 (Container : in out Vector;
960 New_Item : Element_Type;
961 Count : Count_Type := 1)
963 Index : Index_Type'Base;
971 or else Before.Index > Container.Last
973 if Container.Last = Index_Type'Last then
974 raise Constraint_Error with
975 "vector is already at its maximum length";
978 Index := Container.Last + 1;
981 Index := Before.Index;
984 Insert (Container, Index, New_Item, Count);
988 (Container : in out Vector;
990 New_Item : Element_Type;
991 Position : out Cursor;
992 Count : Count_Type := 1)
994 Index : Index_Type'Base;
999 or else Before.Index > Container.Last
1001 Position := No_Element;
1003 Position := (True, Before.Index);
1010 or else Before.Index > Container.Last
1012 if Container.Last = Index_Type'Last then
1013 raise Constraint_Error with
1014 "vector is already at its maximum length";
1017 Index := Container.Last + 1;
1020 Index := Before.Index;
1023 Insert (Container, Index, New_Item, Count);
1025 Position := Cursor'(True, Index
);
1032 procedure Insert_Space
1033 (Container
: in out Vector
;
1034 Before
: Extended_Index
;
1035 Count
: Count_Type
:= 1)
1037 N
: constant Int
:= Count_Type
'Pos (Count
);
1039 First
: constant Int
:= Int
(Index_Type
'First);
1040 New_Last_As_Int
: Int
'Base;
1041 New_Last
: Index_Type
;
1043 Max_Length
: constant UInt
:= UInt
(Count_Type
'Last);
1046 if Before
< Index_Type
'First then
1047 raise Constraint_Error
with
1048 "Before index is out of range (too small)";
1051 if Before
> Container
.Last
1052 and then Before
> Container
.Last
+ 1
1054 raise Constraint_Error
with
1055 "Before index is out of range (too large)";
1063 Old_Last_As_Int
: constant Int
:= Int
(Container
.Last
);
1066 if Old_Last_As_Int
> Int
'Last - N
then
1067 raise Constraint_Error
with "new length is out of range";
1070 New_Last_As_Int
:= Old_Last_As_Int
+ N
;
1072 if New_Last_As_Int
> Int
(Index_Type
'Last) then
1073 raise Constraint_Error
with "new length is out of range";
1076 New_Length
:= UInt
(New_Last_As_Int
- First
+ Int
'(1));
1078 if New_Length > Max_Length then
1079 raise Constraint_Error with "new length is out of range";
1082 New_Last := Index_Type (New_Last_As_Int);
1084 -- Resolve issue of capacity vs. max index ???
1088 EA : Elements_Array renames Container.Elements;
1090 BB : constant Int'Base := Int (Before) - Int (No_Index);
1091 B : constant Count_Type := Count_Type (BB);
1093 LL : constant Int'Base := New_Last_As_Int - Int (No_Index);
1094 L : constant Count_Type := Count_Type (LL);
1097 if Before <= Container.Last then
1099 II : constant Int'Base := BB + N;
1100 I : constant Count_Type := Count_Type (II);
1102 EA (I .. L) := EA (B .. Length (Container));
1107 Container.Last := New_Last;
1114 function Is_Empty (Container : Vector) return Boolean is
1116 return Last_Index (Container) < Index_Type'First;
1123 function Last (Container : Vector) return Cursor is
1125 if Is_Empty (Container) then
1129 return (True, Last_Index (Container));
1136 function Last_Element (Container : Vector) return Element_Type is
1138 if Is_Empty (Container) then
1139 raise Constraint_Error with "Container is empty";
1142 return Get_Element (Container, Length (Container));
1149 function Last_Index (Container : Vector) return Extended_Index is
1151 return Container.Last;
1158 function Length (Container : Vector) return Count_Type is
1159 L : constant Int := Int (Last_Index (Container));
1160 F : constant Int := Int (Index_Type'First);
1161 N : constant Int'Base := L - F + 1;
1164 return Count_Type (N);
1171 function Left (Container : Vector; Position : Cursor) return Vector is
1172 C : Vector (Container.Capacity) := Copy (Container, Container.Capacity);
1175 if Position = No_Element then
1179 if not Has_Element (Container, Position) then
1180 raise Constraint_Error;
1183 while C.Last /= Position.Index - 1 loop
1194 (Target : in out Vector;
1195 Source : in out Vector)
1197 N : constant Count_Type := Length (Source);
1200 if Target'Address = Source'Address then
1204 if N > Target.Capacity then
1205 raise Constraint_Error with -- correct exception here???
1206 "length of Source is greater than capacity of Target";
1209 -- We could also write this as a loop, and incrementally
1210 -- copy elements from source to target.
1212 Target.Last := No_Index; -- in case array assignment files
1213 Target.Elements (1 .. N) := Source.Elements (1 .. N);
1215 Target.Last := Source.Last;
1216 Source.Last := No_Index;
1223 function Next (Container : Vector; Position : Cursor) return Cursor is
1225 if not Position.Valid then
1229 if Position.Index < Last_Index (Container) then
1230 return (True, Position.Index + 1);
1240 procedure Next (Container : Vector; Position : in out Cursor) is
1242 if not Position.Valid then
1246 if Position.Index < Last_Index (Container) then
1247 Position.Index := Position.Index + 1;
1249 Position := No_Element;
1257 procedure Prepend (Container : in out Vector; New_Item : Vector) is
1259 Insert (Container, Index_Type'First, New_Item);
1263 (Container : in out Vector;
1264 New_Item : Element_Type;
1265 Count : Count_Type := 1)
1278 procedure Previous (Container : Vector; Position : in out Cursor) is
1280 if not Position.Valid then
1284 if Position.Index > Index_Type'First and
1285 Position.Index <= Last_Index (Container) then
1286 Position.Index := Position.Index - 1;
1288 Position := No_Element;
1292 function Previous (Container : Vector; Position : Cursor) return Cursor is
1294 if not Position.Valid then
1298 if Position.Index > Index_Type'First and
1299 Position.Index <= Last_Index (Container) then
1300 return (True, Position.Index - 1);
1306 ---------------------
1307 -- Replace_Element --
1308 ---------------------
1310 procedure Replace_Element
1311 (Container : in out Vector;
1313 New_Item : Element_Type)
1316 if Index > Container.Last then
1317 raise Constraint_Error with "Index is out of range";
1321 II : constant Int'Base := Int (Index) - Int (No_Index);
1322 I : constant Count_Type := Count_Type (II);
1325 Container.Elements (I) := New_Item;
1327 end Replace_Element;
1329 procedure Replace_Element
1330 (Container : in out Vector;
1332 New_Item : Element_Type)
1335 if not Position.Valid then
1336 raise Constraint_Error with "Position cursor has no element";
1339 if Position.Index > Container.Last then
1340 raise Constraint_Error with "Position cursor is out of range";
1344 II : constant Int'Base := Int (Position.Index) - Int (No_Index);
1345 I : constant Count_Type := Count_Type (II);
1347 Container.Elements (I) := New_Item;
1349 end Replace_Element;
1351 ----------------------
1352 -- Reserve_Capacity --
1353 ----------------------
1355 procedure Reserve_Capacity
1356 (Container : in out Vector;
1357 Capacity : Count_Type)
1360 if Capacity > Container.Capacity then
1361 raise Constraint_Error with "Capacity is out of range";
1363 end Reserve_Capacity;
1365 ----------------------
1366 -- Reverse_Elements --
1367 ----------------------
1369 procedure Reverse_Elements (Container : in out Vector) is
1371 if Length (Container) <= 1 then
1377 E : Elements_Array renames Container.Elements;
1381 J := Length (Container);
1384 EI : constant Element_Type := E (I);
1394 end Reverse_Elements;
1400 function Reverse_Find
1401 (Container : Vector;
1402 Item : Element_Type;
1403 Position : Cursor := No_Element) return Cursor
1405 Last : Index_Type'Base;
1409 if not Position.Valid
1410 or else Position.Index > Last_Index (Container)
1412 Last := Last_Index (Container);
1414 Last := Position.Index;
1417 K := Count_Type (Int (Last) - Int (No_Index));
1418 for Indx in reverse Index_Type'First .. Last loop
1419 if Get_Element (Container, K) = Item then
1420 return (True, Indx);
1429 ------------------------
1430 -- Reverse_Find_Index --
1431 ------------------------
1433 function Reverse_Find_Index
1434 (Container : Vector;
1435 Item : Element_Type;
1436 Index : Index_Type := Index_Type'Last) return Extended_Index
1438 Last : Index_Type'Base;
1442 if Index > Last_Index (Container) then
1443 Last := Last_Index (Container);
1448 K := Count_Type (Int (Last) - Int (No_Index));
1449 for Indx in reverse Index_Type'First .. Last loop
1450 if Get_Element (Container, K) = Item then
1458 end Reverse_Find_Index;
1464 function Right (Container : Vector; Position : Cursor) return Vector is
1465 C : Vector (Container.Capacity) := Copy (Container, Container.Capacity);
1468 if Position = No_Element then
1473 if not Has_Element (Container, Position) then
1474 raise Constraint_Error;
1477 while C.Last /= Container.Last - Position.Index + 1 loop
1488 procedure Set_Length
1489 (Container : in out Vector;
1490 New_Length : Count_Type)
1493 if New_Length = Formal_Vectors.Length (Container) then
1497 if New_Length > Container.Capacity then
1498 raise Constraint_Error; -- ???
1502 Last_As_Int : constant Int'Base :=
1503 Int (Index_Type'First) + Int (New_Length) - 1;
1505 Container.Last := Index_Type'Base (Last_As_Int);
1513 procedure Swap (Container : in out Vector; I, J : Index_Type) is
1515 if I > Container.Last then
1516 raise Constraint_Error with "I index is out of range";
1519 if J > Container.Last then
1520 raise Constraint_Error with "J index is out of range";
1528 II : constant Int'Base := Int (I) - Int (No_Index);
1529 JJ : constant Int'Base := Int (J) - Int (No_Index);
1531 EI : Element_Type renames Container.Elements (Count_Type (II));
1532 EJ : Element_Type renames Container.Elements (Count_Type (JJ));
1534 EI_Copy : constant Element_Type := EI;
1542 procedure Swap (Container : in out Vector; I, J : Cursor) is
1545 raise Constraint_Error with "I cursor has no element";
1549 raise Constraint_Error with "J cursor has no element";
1552 Swap (Container, I.Index, J.Index);
1560 (Container : Vector;
1561 Index : Extended_Index) return Cursor
1564 if Index not in Index_Type'First .. Last_Index (Container) then
1568 return Cursor'(True, Index
);
1575 function To_Index
(Position
: Cursor
) return Extended_Index
is
1577 if not Position
.Valid
then
1581 return Position
.Index
;
1589 (New_Item
: Element_Type
;
1590 Length
: Count_Type
) return Vector
1594 return Empty_Vector
;
1598 First
: constant Int
:= Int
(Index_Type
'First);
1599 Last_As_Int
: constant Int
'Base := First
+ Int
(Length
) - 1;
1603 if Last_As_Int
> Index_Type
'Pos (Index_Type
'Last) then
1604 raise Constraint_Error
with "Length is out of range"; -- ???
1607 Last
:= Index_Type
(Last_As_Int
);
1609 return (Length
, (others => New_Item
), Last
=> Last
,
1614 end Ada
.Containers
.Formal_Vectors
;