2013-04-06 104 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