user2253350
user2253350

Reputation: 13

Ada unconstrained type

Hello Im new to ada and I am trying to create some kind of unconstrained array and I can't figure out how to do it in ada.

package data_puzzle is
    type rotation is private;
    type map(x_l,y_l,z_l : Natural) is private;
    type valid_rotations is private;
private
    type rotation is array(1..3) of Natural; 
    type map(x_l,y_l,z_l : Natural) is record
        struct : Structure(x_l,y_l,z_l);
        rot : rotation;
    end record;

    type valid_rotations is array(1..24) of map; --how can I make this row work?
end data_puzzle;

Structure looks like this

type structure(x_l,y_l,z_l : Natural) is record
    structure : xyz(1..x_l,1..y_l,1..z_l);
    X : Natural := x_l;
    Y : Natural := y_l;
    Z : Natural := z_l;
end record;

Basically I have a map with rotations and data. Then I want to store all the different rotations in a list of size 24. The only solution I have right now is to initiate type valid_rotations is array(1..24) of map(x,y,z) then it works. But I do not want to initiate it like that since I do not know what the size will be at that moment.

Cheers!

Upvotes: 1

Views: 1636

Answers (1)

Shark8
Shark8

Reputation: 4198

Ok, the problem is that the type map can be of varying size -- the compiler cannot therefore simply set aside the proper amount of memory w/o further information -- so the solution is to create some sort of indirection which can be the element of an array: we have this type since Ada 83 but with Ada 2005 and on we can further constrain access types from being null.

-- I don't know what this is supposed to be; I'm assuming an array.
type xyz is Array(Positive Range <>, Positive Range <>, Positive Range <>)
  of Integer;

type structure(x_l,y_l,z_l : Natural) is record
structure : xyz(1..x_l,1..y_l,1..z_l);  
X : Natural := x_l;
Y : Natural := y_l;
Z : Natural := z_l;
end record;

package data_puzzle is
type rotation is private;
type map(x_l,y_l,z_l : Natural) is private;
type valid_rotations is private;

type map_handle is private;
private
type rotation is array(1..3) of Natural; 
type map(x_l,y_l,z_l : Natural) is record
    struct : Structure(x_l,y_l,z_l);
    rot : rotation;
end record;

-- Because the size of the record "map" can change
-- depending on its discriminants, we cannot simply
-- make some array -- we can however have an array 
-- of accesses (since we know their sizes at compile)
-- and constrain these access-types to be non-null.
type valid_rotations is array(1..24) of map_handle;

-- Here it is: an access which cannot be null.
type map_handle is Not Null Access Map;

end data_puzzle;

Also, I'd delete the _1 from the discriminants (X,Y,Z) look fine to me.

Upvotes: 1

Related Questions