1

您好,我是 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 的列表中。我现在唯一的解决方案是启动类型 valid_rotations 是 map(x,y,z) 的 array(1..24) 然后它可以工作。但我不想那样启动它,因为我不知道那一刻的大小。

干杯!

4

1 回答 1

1

好的,问题是类型map可以有不同的大小——因此编译器不能简单地留出适当数量的内存而没有进一步的信息——所以解决方案是创建某种间接的元素,它可以是一个数组:我们从 Ada 83 开始就有这种类型,但从Ada 2005 开始,我们可以进一步限制访问类型为空。

-- 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,这对我来说很好。

于 2013-04-06T23:16:52.003 回答