2013-04-06 119 views
1

你好我新来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的列表中。我现在唯一的解决方案是启动 类型valid_rotations是map(x,y,z)的数组(1..24),然后它可以工作。但我不想这样做,因为我不知道那个时候尺寸会是多少。

干杯!

+1

令人困惑的是''type structure''有一个名为'structure'的组件!另外,我不知道为什么当你可以访问判别式时,为什么'type structure'将组件'X','Y','Z'初始化为相应的判别式的值? – 2013-04-07 08:13:21

回答

1

好的,问题是,类型map可以是不同的大小 - 编译器因此不能简单地留出适当数量的内存W/O进一步的信息 - 所以解决方案是创建某种间接的可以是一个数组的元素:我们有这种类型,因为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; 

另外,我想删除的判别的_1(X,Y,Z)看起来没什么问题。

+0

现在编译的人:)!并感谢您的描述。大<3! – user2253350 2013-04-06 23:51:15

+0

用户可见访问类型的问题在于用户需要执行分配,并记住在完成时释放(除非他们不关心内存泄漏)。使用[Ada.Containers.Indefinite_Vectors](http://www.adaic.org/resources/add_content/standards/05rm/html/RM-A-18-10.html)可能会更安全。另外,正如所写,用户不能实际创建'data_puzzle.valid_rotations'的实例,因为他们无法看到如何初始化它(对已分配的映射有24个非空访问)。我不会在这里使用'not null';它在私人部分,至少用户不会错! – 2013-04-07 08:32:06

+0

Indefinite_Vectors是一个很好的解决方案,并且非常适用,但是如果我已经提出了它,它不会解决问题的原因。 – Shark8 2013-04-07 14:23:31