Skip to content

Commit

Permalink
Try using std::hash<std::type_index>, `std::equal_to<std::type_inde…
Browse files Browse the repository at this point in the history
…x>` everywhere.

From PR pybind#4316 we know that types in the unnamed namespace in different translation units do not compare equal, as desired.

But do types in named namespaces compare equal, as desired?
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Nov 6, 2022
1 parent ee2b522 commit a06949a
Showing 1 changed file with 1 addition and 26 deletions.
27 changes: 1 addition & 26 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,35 +114,10 @@ inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) {
// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
// which works. If not under a known-good stl, provide our own name-based hash and equality
// functions that use the type name.
#if defined(__GLIBCXX__)
inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
using type_hash = std::hash<std::type_index>;
using type_equal_to = std::equal_to<std::type_index>;
#else
inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
}

struct type_hash {
size_t operator()(const std::type_index &t) const {
size_t hash = 5381;
const char *ptr = t.name();
while (auto c = static_cast<unsigned char>(*ptr++)) {
hash = (hash * 33) ^ c;
}
return hash;
}
};

struct type_equal_to {
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
}
};
#endif

template <typename value_type>
using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
using type_map = std::unordered_map<std::type_index, value_type>;

struct override_hash {
inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
Expand Down

0 comments on commit a06949a

Please sign in to comment.