Ada 运行Heapify SiftDown时出现约束错误

Ada 运行Heapify SiftDown时出现约束错误,ada,constraintexception,Ada,Constraintexception,我在Ada中为一个我们必须自学代码的课程编写代码。我理解堆排序,但是Ada语法真的让我困惑。我不明白为什么在这个排序函数中会出现约束错误。 本质上,我们必须将数组“A”传递到这个过程中,它应该组织它。我在siftDown(A(Start…A'Last))得到约束错误 先谢谢你 Procedure sort_3(A : in out array_type) is procedure swap(Left : in out Integer; Right : in out Integer) is

我在Ada中为一个我们必须自学代码的课程编写代码。我理解堆排序,但是Ada语法真的让我困惑。我不明白为什么在这个排序函数中会出现约束错误。 本质上,我们必须将数组“A”传递到这个过程中,它应该组织它。我在siftDown(A(Start…A'Last))得到约束错误

先谢谢你

Procedure sort_3(A : in out array_type) is

  procedure swap(Left : in out Integer; Right : in out Integer) is
     temp : Integer;
  begin
     temp := Left;
     Left := Right;
     Right := Temp;
  end swap;
  
  procedure siftDown(A : in out array_type) is
     Count : Integer := 1;
     root : Integer := Integer'Pos(A'First);
     child : Integer := Integer'Pos(A'Last);
     last : Integer := Integer'Pos(A'Last);
  begin
     while root * 2 + 1 <= last loop
        child := root * 2 + 1;
        if child + 1 <= last and then A(Integer'Val(child)) < A(Integer'Val(child + 1)) then
           child := child + 1;
        end if;
        if A(Integer'Val(root)) < A(Integer'Val(child)) then
           swap(A(Integer'Val(root)), A(Integer'Val(child)));
           root := child;
        else
           exit;
        end if;
     end loop;
  end siftDown;

  procedure heapify(A : in out array_type) is
     Count : Integer := 0;
     First_Pos : Integer;
     Last_Pos  : Integer;
     Start     : Integer;
  begin
     First_Pos := A'First;
     Last_Pos  := A'Last;
     Start     := Integer'Val((Last_Pos - First_Pos + 1) / 2);
      loop
       siftDown(A(Start...A'Last));
        if Start > Integer'First then
           Start := Integer'Pred(Start);
       else
           exit;
        end if;
     end loop;
  end heapify;
  Last_Index : Integer := Integer'Last;
  
   begin
  heapify(A);
  while Last_Index > Integer'First loop
     swap(A(Last_Index), A(A'First));
     Last_Index := Integer'Pred(Last_Index);
     siftDown(A(A'First..Last_Index));
  end loop;
 
end sort_3;
Procedure sort_3(A:in-out数组类型)为
过程交换(左:输入输出整数;右:输入输出整数)为
温度:整数;
开始
温度:=左;
左:=右;
右:=温度;
末端互换;
程序siftDown(A:输入输出数组类型)为
计数:整数:=1;
根:整数:=Integer'Pos(A'First);
子项:整数:=Integer'Pos(A'Last);
最后:整数:=整数位置(A'last);
开始

而root*2+1代码中有一个语法错误-在
a(Start…a'Last)
中有一个额外的点

语法
A(Start..A'Last)
表示从开始到最后一个元素的一个片段,数组的一部分。
Constraint\u Error
表示
Start
不在数组边界内。尝试添加

Ada.Text_IO.Put_Line (Start'Image);

在该行之前,您将看到开始值以及它何时超出A'范围。

代码中有语法错误-在
A(开始…A'Last)
中有一个额外的点

语法
A(Start..A'Last)
表示从开始到最后一个元素的一个片段,数组的一部分。
Constraint\u Error
表示
Start
不在数组边界内。尝试添加

Ada.Text_IO.Put_Line (Start'Image);

在这一行之前,您将看到起始值以及它何时超出A'范围。

您的代码中有一些对Integer'First和Integer'Last的引用,它们是和数组A及其值无关的巨大值。我很确定你应该用“第一个”和“最后一个”来代替


还有一个关于样式的注意事项:当这些数组可能不同时,对本地(内部、嵌套)过程的参数使用与包含(外部)过程的参数“a”相同的标识符“a”,会引起混淆和错误。最好使用不同的标识符。

您的代码对Integer'First和Integer'Last有一些引用,它们是与数组A及其值无关的巨大值。我很确定你应该用“第一个”和“最后一个”来代替


还有一个关于样式的注意事项:当这些数组可能不同时,对本地(内部、嵌套)过程的参数使用与包含(外部)过程的参数“a”相同的标识符“a”,会引起混淆和错误。最好使用不同的标识符。

如何定义类型数组\u类型?我很好奇为什么在初始化局部变量的过程siftDown中使用Integer'Pos表达式。我也很好奇为什么在条件表达式中使用Integer'val表达式。array_type是一种无约束的数组类型吗?我们的教授让我们以rosetta代码中的堆排序为例,他们就是这样做的。它被约束并在另一个文件中声明,即他用来测试每个人提交的“驱动程序”。该实现是一个通用堆,使用任何离散类型作为无约束数组类型的数组索引。如果数组类型不是泛型的(我假设您的代码没有显示任何泛型参数),那么您就不需要所有与索引位置之间的转换。交换过程假设元素类型是整数。当将数组切片传递给shiftDown过程时,heapify过程假设数组类型不受约束。添加数组类型声明将非常有用。如何定义类型array\u type?我很好奇为什么在初始化局部变量的过程siftDown中使用Integer'Pos表达式。我也很好奇为什么在条件表达式中使用Integer'val表达式。array_type是一种无约束的数组类型吗?我们的教授让我们以rosetta代码中的堆排序为例,他们就是这样做的。它被约束并在另一个文件中声明,即他用来测试每个人提交的“驱动程序”。该实现是一个通用堆,使用任何离散类型作为无约束数组类型的数组索引。如果数组类型不是泛型的(我假设您的代码没有显示任何泛型参数),那么您就不需要所有与索引位置之间的转换。交换过程假设元素类型是整数。当向shiftDown过程传递数组切片时,heapify过程假定数组类型不受约束。添加数组类型声明将非常有用。谢谢!这帮了大忙,谢谢!这帮了大忙。