Ada 如何创建整数到向量的哈希映射?
我正在学习Ada(通过尝试问题) 我有一个向量Ada 如何创建整数到向量的哈希映射?,ada,Ada,我正在学习Ada(通过尝试问题) 我有一个向量ActivityVector的ActivityRecord记录: type ActivityRecord is record dt: Ada.Calendar.Time; str: UStr.Unbounded_String; end record; package ActivityVector is new Ada.Containers.Vectors (Element_Type => ActivityRecord,
ActivityVector
的ActivityRecord
记录:
type ActivityRecord is
record
dt: Ada.Calendar.Time;
str: UStr.Unbounded_String;
end record;
package ActivityVector is new Ada.Containers.Vectors
(Element_Type => ActivityRecord,
Index_Type => Natural);
我想把它们放在一张地图上,键是Integer
s。我有以下资料:
function IntegerHash(i: Integer) return Ada.Containers.Hash_Type;
package ActivityMap is new Ada.Containers.Indefinite_Hashed_Maps(
Key_Type => Integer,
Element_Type => Activity.ActivityVector.Vector,
Hash => IntegerHash,
Equivalent_Keys => "="
);
当我尝试编译此文件时,我得到:
act_map.ads:9:04: instantiation error at a-cihama.ads:46
act_map.ads:9:04: no visible subprogram matches the specification for "="
act_map.ads:9:04: instantiation error at a-cihama.ads:46
act_map.ads:9:04: default "=" on "Vector" is not directly visible
看起来它需要为向量定义一个相等运算符?
我可以定义一个,但首先我想检查一下:
- 我的想法是正确的
- 如果有更简单的方法来实现这一点
Ada.Containers.Vectors的实例化中定义的现有函数即可:
package ActivityMap is new Ada.Containers.Indefinite_Hashed_Maps(
Key_Type => Integer,
Element_Type => Activity.ActivityVector.Vector,
Hash => IntegerHash,
Equivalent_Keys => "=",
"=" => Activity.ActivityVector."="
);
或者,通过执行以下操作使Activity.ActivityVector。“=”
函数直接可见
use type Activity.ActivityVector.Vector;
谢谢-需要相等运算符的原因是什么?如果值已经匹配,我猜是为了避免不必要的写入?在映射类型上实现=
需要元素类型上的=
。我认为实现通常不会使用它进行重复数据消除(这是您建议的),而且我认为在不违反指定API的情况下实际上是不可能的,至少因为Reference\u Type
是在Ada 2012中添加的。啊,这是有道理的。再次感谢。