如何创建整数到向量的散列映射?
How do I create a hashed map of integers to vectors?
我正在学习 Ada(通过尝试 https://adventofcode.com/2018/ 个问题)。
我有一个包含 ActivityRecord
条记录的向量 ActivityVector
:
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
它看起来像是在期待为向量定义的相等运算符?
我可以定义一个,但首先我想检查一下:
- 我的想法是正确的
- 如果有更简单的方法来实现这个
It looks like it is expecting an equality operator defined for the vector
是的。
I could define one
不要那样做,只需使用 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;
我正在学习 Ada(通过尝试 https://adventofcode.com/2018/ 个问题)。
我有一个包含 ActivityRecord
条记录的向量 ActivityVector
:
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
它看起来像是在期待为向量定义的相等运算符? 我可以定义一个,但首先我想检查一下:
- 我的想法是正确的
- 如果有更简单的方法来实现这个
It looks like it is expecting an equality operator defined for the vector
是的。
I could define one
不要那样做,只需使用 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;