从Ada中的函数返回大小可变的矩阵 [英] Returning a variable-sized matrix from a function in Ada

查看:99
本文介绍了从Ada中的函数返回大小可变的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在大学攻读Ada课程,但是我在解决其中的一些想法时遇到了很多问题。

I'm trying to learn Ada for a course at the University, and I'm having a lot of problems wrapping my head around some of the ideas in it.

我目前的绊脚石:假设我有一个函数,该函数接受一个Matrix(只是一个整数的二维数组),并返回一个新的较小的矩阵(去除第一行和第一列)。

My current stumbling block: Let's say I have a function which takes a Matrix (just a 2-dimensional array of Integers), and returns a new, smaller matrix (strips out the first row and first column).

我这样声明矩阵和函数:

I declare the matrix and function like this:

type MATRIX is array(INTEGER range <>, INTEGER range <>) of INTEGER;
function RemoveFirstRowCol (InMatrix: in MATRIX) return MATRIX is

然后我决定大小要返回的矩阵的值:

Then I decide on the size of the Matrix to return:

     Result_matrix: MATRIX (InMatrix'First(1) .. InMatrix'Length(1) - 1, InMatrix'First(2) .. InMatrix'Length(2) - 1);

然后我进行计算并返回Result_matrix。

Then I do the calculations and return the Result_matrix.

所以这是我的问题:运行此命令时,我发现如果我尝试将此函数的结果返回到不是以正确大小正确声明的Matrix之外的任何值,则在运行时会出现异常。

So here's my problem: when running this, I discovered that if I try to return the result of this function into anything that's not a Matrix declared with the exact proper size, I get an exception at runtime.

我的问题是,我这样做对吗?在我看来,我不必提前知道函数将返回的大小。即使声明的矩阵比我返回的矩阵更大,我仍然会收到错误消息。再说一次,Ada的整个想法是强类型化,所以也许这很有意义(我应该确切地知道返回类型)。

My question is, am I doing this right? It seems to me like I shouldn't have to know ahead of time what the function will return in terms of size. Even with a declared Matrix bigger than the one I get back, I still get an error. Then again, the whole idea of Ada is strong typing, so maybe this makes sense (I should know exactly the return type).

无论如何,我是否正确地做到了,并且真的没有办法在不事先知道返回矩阵的大小的情况下使用此功能吗?

Anyways, am I doing this correctly, and is there really no way to use this function without knowing in advance the size of the returned matrix?

谢谢,
Edan

Thanks, Edan

推荐答案

您不需要事先知道返回矩阵的大小,也不需要使用访问(指针)类型。只需在单元或块的声明部分中调用您的函数,边界就会自动设置:

You don't need to know the size of the returned matrix in advance, nor do you need to use an access (pointer) type. Just invoke your function in the declarative part of a unit or block and the bounds will be set automatically:

procedure Call_The_Matrix_Reduction_Function (Rows, Cols : Integer) is

   Source_Matrix : Matrix(1 .. Rows, 1 .. Cols);

begin
   -- Populate the source matrix

   -- ...

   declare
      Result : Matrix := RemoveFirstRowCol (Source_Matrix)
      -- Result matrix is automatically sized, can also be declared constant
      -- if appropriate.
   begin
      -- Process the result matrix

      -- ...

   end;
end Call_The_Matrix_Reduction_Function;

注意:由于结果矩阵是在堆栈上分配的,因此如果数字行和列的数量很大。

Caveat: Since the result matrix is being allocated on the stack, you could have a problem if the numbers of rows and columns are large.

这篇关于从Ada中的函数返回大小可变的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆