7.3. Unconstrained arrays - JulTob/Ada GitHub Wiki
Unconstrained arrays
To have arrays of undefined length we use these types. Bounds have to be determined for specific objects, but not for the general type.
The symbol <>
is called box. “Black Box”
It means... undefined.
In Ada, when you define a type as an array with range <>, you are declaring an unconstrained array type. This means that the array's size is not fixed at the type declaration but will be specified when objects of that type are created. This approach provides the flexibility to handle arrays of varying lengths using the same type definition.
type Data is array (Positive range <>) of Float;
- Type Name: Data
- Index Type: Positive (an Ada predefined subtype of Integer that only includes positive values)
- Element Type: Float
- Unconstrained: Indicated by
range <>
, meaning the range of the array is not specified here and will be provided during the creation of Data objects.
with Ada.Text_IO; use Ada.Text_IO;
with Statistics; use Statistics;
procedure Test_Statistics is
My_Data : Data(1 .. 5) := (1.0, 2.0, 3.0, 4.0, 5.0);
Average : Float;
Maximum : Float;
begin
Average := Mean(My_Data);
Maximum := Max(My_Data);
Put_Line("Average: " & Float'Image(Average));
Put_Line("Maximum: " & Float'Image(Maximum));
end Test_Statistics;
- Defines an array
My_Data
of the typeData
with specified indices from 1 to 5. - Computes the average and maximum values using the Mean and Max functions.
- Outputs the results to the standard output.
Thus, the use of <> in the array type definition in Ada allows for creating versatile data structures that can adapt to different sizes, which is particularly useful in contexts like statistical computation where data sets can vary widely in size. This flexibility is one of the strong suits of Ada, especially in systems programming and contexts requiring high reliability.
Type String is array (Positive range <>) of Character;
-- Indefinite length
A_Buffer: String(1..80); --Defined at declaration
Str : constant String
-- Dynamic declaration
A : array (1..N) of Natural
Type Buffer_Type is array (0..1023) of Character;
-- Unconstrained Array (specified in declaration)
type A_t is array (Integer range <>) of Natural
A : A_t(1..25);
Type Buffer_Type is array (Integer range <>) of Character;
...
B1: Buffer_Type(0..31);
B2: Buffer_Type(1..32);
B3: Buffer_Type(0..32);
...
B1 := B2;
-- Surprisingly ok, same length
B3 := B1;
-- Not ok, not the same length
procedure Process_Buffer (Buffer : in Buffer_Type) is
begin
for I in Buffer’Range loop
--Buffer(I);
end loop;
end Process_Buffer ;
...
I := B1’First; --is 0, First Index
I := B2’Last; --is 16, Last Index
procedure ReadBuffer(Buffer: Buffer_Type) is --Size?
for b in Buffer’Range is
...
Buffer’First;
Buffer’Last;
...
Type Buffer_Type is array (Integer range <>) of Character;
...
B1: Buffer_Type ( 0 .. 15);
B2: Buffer_Type ( 1 .. 16);
B3: Buffer_Type ( 16 .. 32);
...
B1 := B2; --Works because it's the same length
...
procedure Process_Buffer (Buffer : in Buffer_Type) is
begin
for I in Buffer’Range loop
--Buffer(I);
end loop;
end Process_Buffer ;
...
I := B1’First; --is 0, First Index
I := B2’Last; --is 16, Last Index
-- Multidimensional array
Type dmatrix is array(integer range <>,integer range <>) of Long_long_float;
Note that the identifier Positive
in declaration limits the indexing to these, therefore making the array starts at 1.
<>
in Non-Generic Contexts
Clarifying <>
simply specifies that the exact bounds of the array will be determined when instances of the type are declared, not at the type's declaration. It is different from its use in generics where it indicates a placeholder for a type or operation to be specified later.
How to Use This Type
Here's how you might use this Data
type and the functions in a practical example:
with Ada.Text_IO; use Ada.Text_IO;
with Statistics; use Statistics;
procedure Test_Statistics is
My_Data : Data(1 .. 5) := (1.0, 2.0, 3.0, 4.0, 5.0);
Average : Float;
Maximum : Float;
begin
Average := Mean(My_Data);
Maximum := Max(My_Data);
Put_Line("Average: " & Float'Image(Average));
Put_Line("Maximum: " & Float'Image(Maximum));
end Test_Statistics;
Thus, the use of <>
in the array type definition in Ada allows for creating versatile data structures that can adapt to different sizes, which is particularly useful in contexts like statistical computation where data sets can vary widely in size. This flexibility is one of the strong suits of Ada, especially in systems programming and contexts requiring high reliability.