Skip to content

Commit

Permalink
Fix type hash map
Browse files Browse the repository at this point in the history
  • Loading branch information
barche committed Sep 2, 2023
1 parent 847c000 commit c28f293
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion include/jlcxx/jlcxx_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#endif

#define JLCXX_VERSION_MAJOR 0
#define JLCXX_VERSION_MINOR 10
#define JLCXX_VERSION_MINOR 11
#define JLCXX_VERSION_PATCH 0

// From https://stackoverflow.com/questions/5459868/concatenate-int-to-string-using-c-preprocessor
Expand Down
35 changes: 31 additions & 4 deletions include/jlcxx/type_conversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <complex>
#include <map>
#include <unordered_map>
#include <memory>
#include <stack>
#include <stdexcept>
Expand Down Expand Up @@ -335,6 +336,27 @@ struct CachedDatatype
// Work around the fact that references aren't part of the typeid result
using type_hash_t = std::pair<std::type_index, std::size_t>;

} // namespace jlcxx

namespace std {

// Hash implementation from https://en.cppreference.com/w/cpp/utility/hash
template<>
struct hash<jlcxx::type_hash_t>
{
std::size_t operator()(const jlcxx::type_hash_t& h) const noexcept
{
std::size_t h1 = std::hash<std::type_index>{}(h.first);
std::size_t h2 = std::hash<std::size_t>{}(h.second);
return h1 ^ (h2 << 1);
}
};

}

namespace jlcxx
{

namespace detail
{

Expand Down Expand Up @@ -373,7 +395,7 @@ inline type_hash_t type_hash()
return detail::TypeHash<T>::value();
}

JLCXX_API std::map<type_hash_t, CachedDatatype>& jlcxx_type_map();
JLCXX_API std::unordered_map<type_hash_t, CachedDatatype>& jlcxx_type_map();

/// Store the Julia datatype linked to SourceT
template<typename SourceT>
Expand All @@ -393,10 +415,15 @@ class JuliaTypeCache

static inline void set_julia_type(jl_datatype_t* dt, bool protect = true)
{
const auto insresult = jlcxx_type_map().insert(std::make_pair(type_hash<SourceT>(), CachedDatatype(dt, protect)));
if(!insresult.second)
type_hash_t new_hash = type_hash<SourceT>();
const auto [inserted_it, insert_success] = jlcxx_type_map().insert(std::make_pair(new_hash, CachedDatatype(dt, protect)));
if(!insert_success)
{
std::cout << "Warning: Type " << typeid(SourceT).name() << " already had a mapped type set as " << julia_type_name(insresult.first->second.get_dt()) << " using hash " << insresult.first->first.first.hash_code() << " and const-ref indicator " << insresult.first->first.second << std::endl;
type_hash_t old_hash = inserted_it->first;
std::cout << "Warning: Type " << new_hash.first.name() << " already had a mapped type set as "
<< julia_type_name(inserted_it->second.get_dt()) << " and const-ref indicator " << old_hash.second << " and C++ type name " << old_hash.first.name()
<< ". Hash comparison: old(" << old_hash.first.hash_code() << "," << old_hash.second << ") == new(" << old_hash.first.hash_code() << "," << old_hash.second << ") == "
<< std::boolalpha << (old_hash == new_hash) << std::endl;
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/jlcxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ namespace detail
};
}

JLCXX_API std::map<type_hash_t, CachedDatatype>& jlcxx_type_map()
JLCXX_API std::unordered_map<type_hash_t, CachedDatatype>& jlcxx_type_map()
{
static std::map<type_hash_t, CachedDatatype> m_map;
static std::unordered_map<type_hash_t, CachedDatatype> m_map;
return m_map;
}

Expand Down

0 comments on commit c28f293

Please sign in to comment.