c++ - Hashing function not working properly -
i've been trying week understand how use std::unordered_map , custom hash functions. after lot of research, tried implement own hash function glm::ivec3. problem here have syntax error of sort. don't i'm doing wrong, might omitted. here's header code:
namespace mctest3 { class chunkmap { public: struct keyhasher { std::size_t operator()(const glm::ivec3& key) const { using std::size_t; using std::hash; return ((key.x * 5209) ^ (key.y * 1811)) ^ (key.z * 7297); } }; std::unordered_map<glm::ivec3, chunk, keyhasher> chunks; chunkmap(); chunk* getchunkfrompos(const glm::vec3 &pos) const; glm::ivec3 getchunkposfrompos(const glm::vec3 &pos) const; }; }
and bugged function need retrieve value unordered_map (or create one):
namespace mctest3 { chunkmap::chunkmap() { } chunk* chunkmap::getchunkfrompos(const glm::vec3 &pos) const { glm::ivec3 ipos = glm::ivec3((int)pos.x >> chunk::bit_size, (int)pos.y >> chunk::bit_size, (int)pos.z >> chunk::bit_size); chunk* result = chunks[ipos]; // bug here return result; } }
those 2 errors get:
chunkmap.cpp|12|error: passing 'const std::unordered_map<glm::detail::tvec3<int, (glm::precision)0u>, mctest3::chunk, mctest3::chunkmap::keyhasher>' 'this' argument of 'std::__detail::_map_base<_key, _pair, std::_select1st<_pair>, true, _hashtable>::mapped_type& std::__detail::_map_base<_key, _pair, std::_select1st<_pair>, true, _hashtable>::operator[](const _key&) [with _key = glm::detail::tvec3<int, (glm::precision)0u>; _pair = std::pair<const glm::detail::tvec3<int, (glm::precision)0u>, mctest3::chunk>; _hashtable =|
chunkmap.cpp|12|error: cannot convert 'std::__detail::_map_base<glm::detail::tvec3<int, (glm::precision)0u>, std::pair<const glm::detail::tvec3<int, (glm::precision)0u>, mctest3::chunk>, std::_select1st<std::pair<const glm::detail::tvec3<int, (glm::precision)0u>, mctest3::chunk> >, true, std::_hashtable<glm::detail::tvec3<int, (glm::precision)0u>, std::pair<const glm::detail::tvec3<int, (glm::precision)0u>, mctest3::chunk>, std::allocator<std::pair<const glm::detail::tvec3<int, (glm::precision)0u>, mctest3::chunk> >, std:|
problem 1
chunk* result = chunks[ipos]; // bug here
in above line trying assign chunks[ipos]
, of type chunk
result
of type chunk*
.
problem 2
when calling std::unordered_map<...>::operator[]
, such in chunks[ipos]
, value @ key (ipos
) default-constructed unless there such key present.
in other words such operation might change container on used.
since member-function getchunkfrompos marked being const
not allowed modify members of class, , compilers throws diagnostic in face - telling code ill-formed.
Comments
Post a Comment