如何创建整数到向量的哈希图?

时间:2019-01-28 13:32:14

标签: ada

我正在通过尝试https://adventofcode.com/2018/问题来学习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,
   Index_Type => Natural);

我想将它们放在键为Integer的地图中。我有以下内容:

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

似乎正在期待为向量定义的相等运算符? 我可以定义一个,但首先我要检查一下:

  • 我的想法是正确的
  • 如果有更简单的方法来实现此目的

1 个答案:

答案 0 :(得分:5)

  

似乎期望为向量定义一个相等运算符

是的

  

我可以定义一个

请勿这样做,只需使用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;