std::unordered_map
에 사용할 수 있도록 6 바이트 필드를 해시하는 효율적인 방법을 찾고 있습니다. 6 바이트 필드에서 해시 계산 하시겠습니까?
struct Hash {
std::size_t operator()(const std::array<uint8_t, 6> & mac) const {
std::size_t key = 0;
boost::hash_combine(key, mac[0]);
boost::hash_combine(key, mac[1]);
boost::hash_combine(key, mac[2]);
boost::hash_combine(key, mac[3]);
boost::hash_combine(key, mac[4]);
boost::hash_combine(key, mac[5]);
return key;
}
};
내가 할 수있는 것이 조금 더 빨리 (~ 20 %)이 트릭을 사용 :
struct Hash {
std::size_t operator()(const std::array<uint8_t, 6> & mac) const {
std::size_t key = 0;
// Possibly UB?
boost::hash_combine(key, reinterpret_cast<const uint32_t&>(mac[0]));
boost::hash_combine(key, reinterpret_cast<const uint16_t&>(mac[4]));
return key;
}
};
을
그리고 이것은 더 빨리이었다
struct Hash {
std::size_t operator()(const std::array<uint8_t, 6> & mac) const {
// Requires size_t to be 64-bit.
static_assert(sizeof(std::size_t) >= 6, "MAC address doesn't fit in std::size_t!");
std::size_t key = 0;
// Likely UB?
boost::hash_combine(key, 0x0000FFFFFFFFFFFF & reinterpret_cast<const uint64_t&>(mac[0]));
return key;
}
};
내 질문은 두 가지이다 :
- 이러한 최적화를 통해 UB가 생성됩니까?
- 첫 번째 해결 방법은 있습니까? 아니면 더 좋은 방법이 있습니까?
UB는 무엇을 나타 냅니까? "UB에서의 결과"? 아마 나는 커피 한잔을 필요로하고 명백한 것을 잊어 버릴 것입니다. –
@MarkWilkins : 정의되지 않은 동작. –
예 : 'boost :: hash_combine (key [0] | (mac [1] << 8) | (max [2] << 16) | (max [3] << 24)) 등등? –