gpt4 book ai didi

generics - 返回映射的通用函数

转载 作者:行者123 更新时间:2023-12-02 20:15:25 24 4
gpt4 key购买 nike

在以下示例中,函数创建并返回有序映射:

with Ada.Containers.Ordered_Maps;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure Main is
package My_Map is new Ada.Containers.Ordered_Maps
(Key_Type => Natural,
Element_Type => Unbounded_String);

function Create_Map return My_Map.Map is
Map_Instance : My_Map.Map;
begin
Map_Instance.Insert (1, To_Unbounded_String ("Foo"));
Map_Instance.Insert (2, To_Unbounded_String ("Bar"));

return Map_Instance;
end Create_Map;
begin
null;
end Main;

我想知道是否可以将函数 Create_Map 转换为更通用的函数?下面的代码显示了我的想法,但不幸的是我真的不知道正确的形式类型 ???:

   generic
type Key_Type is ???;
type Element_Type is ???;
type Map_Type is ???;
function Create_Map_Generic return Map_Type is
Map_Instance : Map_Type;
begin
-- Inserts calls would be based on parsing data ...

return Map_Instance;
end;

package My_Map_One is new Ada.Containers.Ordered_Maps
(Key_Type => Natural,
Element_Type => Unbounded_String);

function Create_Map_One is new Create_Map_Generic
(Key_Type => Natural,
Element_Type => Unbounded_String,
Map_Type => My_Map_One.Map);

package My_Map_Two is new Ada.Containers.Ordered_Maps
(Key_Type => Unbounded_String,
Element_Type => Positive);

function Create_Map_Two is new Create_Map_Generic
(Key_Type => Unbounded_String,
Element_Type => Positive,
Map_Type => My_Map_Two.Map);

形式类型Key_TypeElement_Type被指定两次(包声明和泛型函数的实例化)。这些可以从包本身的声明中提取吗?

最佳答案

西蒙给出了更好、更完整的答案。我只是提供一种替代方案,其中 map 包作为形式参数传入。为此,您可以将包作为形式参数传递,只要它的参数也作为形式参数发送即可:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Ordered_Maps;

procedure Hello is

generic
type Key_Type is private;
type Element_Type is private;
with function "<"(L,R : Key_Type) return Boolean is <>;
with function "="(L,R : Key_Type) return Boolean is <>;

with package Map_Package is new Ada.Containers.Ordered_Maps
(Key_Type => Key_Type,
Element_Type => Element_Type,
"<" => "<",
"=" => "=");
function Create_Map_Generic return Map_Package.Map;

function Create_Map_Generic return Map_Package.Map is
begin
return Result : Map_Package.Map do
null; -- replace with your input based creation
end return;
end Create_Map_Generic;

-- Just a nonsense type to test out compilation
type My_Key is null record;
function "<"(L,R : My_Key) return Boolean is (True);
function "="(L,R : My_Key) return Boolean is (True);

package Maps is new Ada.Containers.Ordered_Maps
(Key_Type => My_Key,
Element_Type => Integer);

function Create is new Create_Map_Generic
(Key_Type => My_Key,
Element_Type => Integer,
Map_Package => Maps);

begin
Put_Line("Hello, world!");
end Hello;

作为额外的花絮,如果您不关心函数内的键和元素类型是什么(我认为您关心,但以防万一),那么您不必指定它们,只需执行以下操作:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Ordered_Maps;

procedure Hello is

generic
with package Map_Package is new Ada.Containers.Ordered_Maps
(others => <>);
function Create_Map_Generic return Map_Package.Map;

function Create_Map_Generic return Map_Package.Map is
begin
return Result : Map_Package.Map do
null; -- replace with your input based creation
end return;
end Create_Map_Generic;

-- Just a nonsense type to test out compilation
type My_Key is null record;
function "<"(L,R : My_Key) return Boolean is (True);
function "="(L,R : My_Key) return Boolean is (True);

package Maps is new Ada.Containers.Ordered_Maps
(Key_Type => My_Key,
Element_Type => Integer);

function Create is new Create_Map_Generic
(Map_Package => Maps);

begin
Put_Line("Hello, world!");
end Hello;

您也可以根据您想要在创建函数中使用的内容进行折中:

generic
type Key_Type is private;
type Element_Type is private;
with package Map_Package is new Ada.Containers.Ordered_Maps
(Key_Type => Key_Type,
Element_Type => Element_Type,
others => <>);
function Create_Map_Generic return Map_Package.Map;

关于generics - 返回映射的通用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59704901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com