【问题标题】:Ada unconstrained typeAda 无约束类型
【发布时间】:2013-04-06 22:45:25
【问题描述】:

您好,我是 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) 然后它可以工作。但我不想那样启动它,因为我不知道那一刻的大小。

干杯!

【问题讨论】:

  • 令人困惑的是type structure 有一个名为structure 的组件!另外,我想知道为什么type structure 有组件XYZ 初始化为相应的判别式的值,而您只能访问判别式?

标签: ada


【解决方案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,这对我来说很好。

【讨论】:

  • 现在可以编译了 :)!并感谢您的描述。大
  • 用户可见访问类型的问题在于用户可以进行分配,并记住在完成后解除分配(除非他们不关心内存泄漏)。使用Ada.Containers.Indefinite_Vectors 可能更安全。此外,正如所写,用户实际上无法创建 data_puzzle.valid_rotations 的实例,因为他们看不到如何初始化它(对已分配的映射进行 24 次非空访问)。我不会在这里使用not null;在私密部分,至少用户不会弄错!
  • Indefinite_Vectors 是一个很好的解决方案,并且非常适用,但如果我立即提出建议,它就不会解决问题的why
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多