问题描述
我使用Boost的实施项目中的哈希映射的,现在,我想实现的按键自定义类型。我有四个无符号整数,我想合并成一个128位的数据类型,其中作为重点来使用。
I'm using Boost's implementation of a hash map in a project right now, and I'm trying to implement a custom type for keys. I have four unsigned integers which I'd like to combine into a single 128-bit datatype to use as a key.
我已经创建了四个元素的32位整数数组,作为我的存储结构。说实话,我不知道Boost的哈希表是如何工作的,所以我不知道我在做什么在这里,但我跟着Boost文档(的)用于扩展的boost ::乱码,我创建了一个散列函数,以及自定义比较运算符。
I've created a struct with a 32-bit integer array of four elements, which serves as my storage. To be honest, I'm not sure how Boost's hash map works, so I'm not sure what I'm doing here, but I followed the Boost documentation (http://www.boost.org/doc/libs/1%5F37%5F0/doc/html/hash/custom.html) for extending boost::hash, and I created a hash function, as well as a custom comparison operator.
我有一个头文件中定义该自定义类型。这是我的code:
I have this custom type defined in a header. This is my code:
#ifndef INT128_H_
#define INT128_H_
// Custom 128-bit datatype used to store and compare the results of a weakened hash operation.
struct int128
{
unsigned int storage[4];
/* Assignment operation that takes a 32-bit integer array of four elements.
This makes assignment of values a shorter and less painful operation. */
void operator=(const unsigned int input[4])
{
for(int i = 0; i < 4; i++)
storage[i] = input[i];
}
};
bool operator==(int128 const &o1, int128 const &o2)
{
if(o1.storage[0] == o2.storage[0] && o1.storage[1] == o2.storage[1] &&
o1.storage[2] == o2.storage[2] && o1.storage[3] == o2.storage[3])
return true;
return false;
}
// Hash function to make int128 work with boost::hash.
std::size_t hash_value(int128 const &input)
{
boost::hash<unsigned long long> hasher;
unsigned long long hashVal = input.storage[0];
for(int i = 1; i < 3; i++)
{
hashVal *= 37;
hashVal += input.storage[1];
}
return hasher(hashVal);
}
#endif
现在,当我实际使用这种类型的Boost的无序地图,我的code编译,但未能联系起来。链接器声称,我有一个符号多个目标文件定义多次。我很想把我的128-bit类型与此映射工作。更好的方式来做到这一点的就是我搞砸了任何提示或?
Now when I actually use this type in Boost's unordered map, my code compiles, but fails to link. The linker claims that I have a symbol defined multiple times in several object files. I'd really like to get my 128-bit type working with this map. Any tips on what I'm screwing up, or a better way to do this?
推荐答案
无序地图的参与几乎附带您遇到的问题。真正的问题是,你定义 HASH_VALUE
和运算符==
在每一个上面包含头文件。
The involvement of unordered-map is almost incidental to the problem you're encountering. The real problem is that you're defining hash_value
and operator==
in every file that includes the header above.
您可以通过治愈这种
- 定义这两个为内联函数
- 刚刚宣布他们在头文件
如果你选择后者(和它的你通常会想什么),你的这些函数的定义移动到的.cpp
文件(或任何扩展你使用C ++源文件)。然后,您将编译文件,并与使用int128类型的其他code链接生成的对象。
If you do the latter (and it's what you'll usually want) you'll move the definitions of those functions into a .cpp
file (or whatever extension you use for C++ source files). You'll then compile that file, and link the resulting object with your other code that uses the int128 type.
编辑:您仍然可以让你比较干净,是这样的:
You can still make your comparison cleaner, something like:
bool operator==(int128 const &o1, int128 const &o2)
{
return o1.storage[0] == o2.storage[0] && o1.storage[1] == o2.storage[1] &&
o1.storage[2] == o2.storage[2] && o1.storage[3] == o2.storage[3]);
}
这篇关于我如何使用按键的自定义类型在一个boost :: unordered_map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!