You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ada中如何向嵌套Vector数组添加元素并实现返回[[]]的功能

向嵌套向量添加元素的实现

你已经定义的Indefinite_Vector就是嵌套向量类型,直接调用Ada容器自带的插入方法即可实现需求,示例如下:

declare
   -- 构造要插入的一维向量 [1]
   Insert_Item : Vector := A.Empty_Vector;
   -- 原嵌套向量 [[2],[2,3]]
   Target_AA   : Indefinite_Vector := AA.Empty_Vector;
begin
   Insert_Item.Append(1);
   -- 初始化原嵌套向量
   declare
      Tmp1 : Vector := A.Empty_Vector;
      Tmp2 : Vector := A.Empty_Vector;
   begin
      Tmp1.Append(2);
      Target_AA.Append(Tmp1);
      Tmp2.Append(2);
      Tmp2.Append(3);
      Target_AA.Append(Tmp2);
   end;
   -- 插入到嵌套向量头部,得到[[1],[2],[2,3]]
   Target_AA.Prepend(Insert_Item);
end;

返回[[]]的实现

[[]]本质是包含1个空一维向量的嵌套向量,构造逻辑如下:

function Get_Empty_Nested return Indefinite_Vector is
   Res : Indefinite_Vector := AA.Empty_Vector;
begin
   -- 往空嵌套向量中添加一个空的一维向量即可
   Res.Append(A.Empty_Vector);
   return Res;
end Get_Empty_Nested;

Python代码的完整Ada翻译

注意Python中列表是引用传递,所以Ada中需要将c参数设为in out类型匹配该逻辑,完整可运行代码如下:

with Ada.Containers.Vectors;
with Ada.Text_IO;

procedure Translate_Test is
   -- 沿用你给出的类型定义
   package A is new Ada.Containers.Vectors(Natural, Integer);
   subtype Vector is A.Vector;
   use all type Vector;
   
   package AA is new Ada.Containers.Indefinite_Vectors (Natural, Vector);
   subtype Indefinite_Vector is AA.Vector;
   use all type Indefinite_Vector;

   -- 辅助打印一维向量
   procedure Print_Single_Vec (V : Vector) is
   begin
      Ada.Text_IO.Put("[");
      for I in V.Iterate loop
         if A.First_Index(V) /= A.To_Index(I) then
            Ada.Text_IO.Put(",");
         end if;
         Ada.Text_IO.Put(Integer'Image(A.Element(I)));
      end loop;
      Ada.Text_IO.Put("]");
   end Print_Single_Vec;

   -- 辅助打印嵌套向量
   procedure Print_Nested_Vec (NV : Indefinite_Vector) is
   begin
      Ada.Text_IO.Put("[");
      for I in NV.Iterate loop
         if AA.First_Index(NV) /= AA.To_Index(I) then
            Ada.Text_IO.Put(",");
         end if;
         Print_Single_Vec(AA.Element(I));
      end loop;
      Ada.Text_IO.Put("]");
   end Print_Nested_Vec;

   -- 翻译的test函数
   function Test (B : Vector; C : in out Indefinite_Vector) return Indefinite_Vector is
   begin
      if B.Is_Empty then
         -- 返回 [[]]
         declare
            Res : Indefinite_Vector := AA.Empty_Vector;
         begin
            Res.Append(A.Empty_Vector);
            return Res;
         end;
      else
         -- 构造[b[0]]添加到c中
         declare
            Tmp : Vector := A.Empty_Vector;
         begin
            Tmp.Append(B.First_Element);
            C.Append(Tmp);
            return C;
         end;
      end if;
   end Test;

   B : Vector := A.Empty_Vector;
   C : Indefinite_Vector := AA.Empty_Vector;
   D : Indefinite_Vector;
begin
   -- 初始化b = [1,2,3]
   B.Append(1);
   B.Append(2);
   B.Append(3);
   -- 调用test函数
   D := Test(B, C);
   -- 输出结果和Python一致:[[ 1]],[[ 1]]
   Print_Nested_Vec(C);
   Ada.Text_IO.Put(",");
   Print_Nested_Vec(D);
   Ada.Text_IO.New_Line;
end Translate_Test;

内容的提问来源于stack exchange,提问作者wang kom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 05:57:04