ConstCharPtrHashMap.h 910 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #ifndef ANKI_UTIL_CONST_CHAR_PTR_HASH_MAP_H
  2. #define ANKI_UTIL_CONST_CHAR_PTR_HASH_MAP_H
  3. #include <unordered_map>
  4. #include <cstring>
  5. namespace anki {
  6. /// The hash function
  7. struct CreateCharPtrHashMapKey
  8. {
  9. size_t operator()(const char* str) const
  10. {
  11. size_t h = 0;
  12. for (; *str != '\0'; ++str)
  13. {
  14. h += *str;
  15. }
  16. return h;
  17. }
  18. };
  19. /// The collision evaluation function
  20. struct CompareCharPtrHashMapKeys
  21. {
  22. bool operator()(const char* a, const char* b) const
  23. {
  24. return strcmp(a, b) == 0;
  25. }
  26. };
  27. /// The hash map that has as key an old school C string. When inserting the
  28. /// char MUST NOT point to a temporary or the evaluation function will fail.
  29. /// Its template struct because C++ does not offer template typedefs
  30. template<typename T>
  31. struct ConstCharPtrHashMap
  32. {
  33. typedef std::unordered_map<const char*, T,
  34. CreateCharPtrHashMapKey, CompareCharPtrHashMapKeys> Type;
  35. };
  36. } // end namespace
  37. #endif