Reputation: 71
I am trying to write an Ada equivalent of the following statement in Python: L = [[] for i in range(n)]
I am solving a dynamic programming problem and my plan is to eventually copy the contents of the jth array inside L into the ith array (j < i) if the ith array has fewer elements than the jth array.
I have found how to create an empty array by defining its range in the reverse order. So, for instance, arr2 would be an empty array created as follows:
arr2: array(2 .. 1) of Integer;
My question is, how do I define the bigger array L to include n of such arr2 arrays?
Please let me know.
Update: I was able to get it correctly working using the answers below. Here is my code.
package Integer_Vectors is new Ada.Containers.Vectors
(Index_Type => Natural,
Element_Type => Integer);
N: Integer;
Array_Of_Vectors : array(1 .. N) of Integer_Vectors.Vector := (others => Integer_Vectors.Empty_Vector);
Input_Sequence: Integer_Vectors.Vector;
Max: Integer_Vectors.Vector;
Input_List : String := Get_Line;
IntCast : Integer;
Last : Positive := 1;
begin
while Last < Input_List'Last loop
Get(Input_List(Last..Input_List'Last),IntCast,Last);
Input_Sequence.Append(IntCast);
Last := Last + 1;
end loop;
N := 0;
for i of Input_Sequence loop
N := N + 1;
end loop;
Upvotes: 4
Views: 1373
Reputation: 1417
In Ada L will be
L : array (1 .. N, 1 .. 0) of Integer;
But it's useless, because you won't be able to extend it latter. @Zerte is right.
You can use a vector on indefinite elements, for instance. Like this
with Ada.Containers.Indefinite_Vectors;
type Integer_Array is array (Positive range <>) of Integer;
Empty : constant Integer_Array := (1 .. 0 => <>);
package Integer_Array_Vectors is new
Ada.Containers.Indefinite_Vectors
(Index_Type => Positive, Element_Type => Integer_Array);
L : Integer_Array_Vectors.Vector;
L.Append (Empty, N);
if L (J)'Length > L (I)'Length then
L.Replace_Element (I, L(J));
end if;
Upvotes: 7