지금 프로젝트에서 Boost의 해시 맵 구현을 사용하고 있으며 키에 대한 사용자 정의 유형을 구현하려고합니다. 필자는 4 개의 부호없는 정수를 하나의 128 비트 데이터 형식으로 결합하여 키로 사용하려고합니다.boost :: unordered_map의 키에 사용자 정의 유형을 사용하려면 어떻게해야합니까?
내 스토리지로 사용되는 4 개 요소의 32 비트 정수 배열로 구조체를 만들었습니다. 솔직히 말해서 Boost의 해시 맵이 어떻게 작동하는지 모르겠다. 그래서 내가 여기서 뭘하고 있는지 모르겠다.하지만 boost :: hash 확장을위한 Boost 문서 (http://www.boost.org/doc/libs/1_37_0/doc/html/hash/custom.html)를 따라 갔고 해시 함수를 만들었다. 뿐만 아니라 사용자 지정 비교 연산자가 포함됩니다.
이 사용자 정의 유형이 헤더에 정의되어 있습니다. 실제로 부스트의 정렬되지 않은지도에서이 형식을 사용할 때 내 코드 컴파일, 이제
#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
하지만, 연결 실패 :이 내 코드입니다. 링커는 여러 객체 파일에 여러 번 정의 된 심볼을 가지고 있다고 주장합니다. 이 맵을 사용하여 128 비트 타입을 사용하고 싶습니다. 내가 망쳐 놓은 것에 대한 조언이나 더 좋은 방법이 있습니까?
로 함수를 선언합니다. == 대신 XOR 연산자를 사용하는 이유는 무엇입니까? 또한 비교를 중첩하는 것에 대한 그 진술은 쓰레기입니다. C + + 논리 AND 연산자는 단락 회로 평가를하므로 절대적으로 더 많은 코드를 입력했습니다. – rlbond
고정, 비판 주셔서 감사. – jakogut