3 * Type-specific hash seed. */
5 #ifndef GINAC_HASH_SEED_H
6 #define GINAC_HASH_SEED_H
11 #if defined(_WIN32) || defined(__APPLE__)
12 #define GINAC_HASH_USE_MANGLED_NAME 1
15 #ifdef GINAC_HASH_USE_MANGLED_NAME
22 * We need a hash function which gives different values for objects of
23 * different types. Hence we need some unique integer for each type.
24 * Fortunately, standard C++ RTTI class `type_info' stores a pointer to
25 * mangled type name. Normally this pointer is the same for all objects
26 * of the same type (although it changes from run to run), so it can be
27 * used for computing hashes. However, on some platforms (such as woe32)
28 * the pointer returned by type_info::name() might be different even for
29 * objects of the same type! Hence we need to resort to comparing string
30 * representation of the (mangled) type names. This is quite expensive,
31 * so we compare crc32 hashes of those strings. We might get more hash
32 * collisions (and slower evaluation as a result), but being a bit slower
33 * is much better than being wrong.
35 #ifndef GINAC_HASH_USE_MANGLED_NAME
36 static inline unsigned make_hash_seed(const std::type_info& tinfo)
38 // This pointer is the same for all objects of the same type.
39 // Hence we can use it.
40 const void* mangled_name_ptr = (const void*)tinfo.name();
41 unsigned v = golden_ratio_hash((uintptr_t)mangled_name_ptr);
45 static unsigned make_hash_seed(const std::type_info& tinfo)
47 const char* mangled_name = tinfo.name();
48 return crc32(mangled_name, std::strlen(mangled_name), 0);
52 #endif /* GINAC_HASH_SEED_H */