阿达不受约束的类型

时间:2013-04-06 22:45:25

标签: ada

您好我是ada的新手,我正在尝试创建某种无约束的数组,我无法弄清楚如何在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;

结构看起来像这样

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;

基本上我有一张带旋转和数据的地图。然后我想将所有不同的旋转存储在24号列表中。我现在唯一的解决方案是启动 type valid_rotations是map(x,y,z)的数组(1..24)然后它可以工作。但我不想这样开始,因为我不知道那时的大小。

干杯!

1 个答案:

答案 0 :(得分:1)

好的,问题是类型map可能有不同的大小 - 编译器因此不能简单地留出适当的内存量而无需进一步的信息 - 所以解决方案是创建一些排序可以作为数组元素的间接性:我们有这种类型,因为Ada 83 但是与Ada 2005,我们可以进一步限制访问类型为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;

另外,我会从判别式(X,Y,Z)中删除_1看起来很好。