Dictionary.h 1012 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef ANKI_UTIL_DICTIONARY_H
  2. #define ANKI_UTIL_DICTIONARY_H
  3. #include "anki/util/Allocator.h"
  4. #include <unordered_map>
  5. #include <cstring>
  6. namespace anki {
  7. /// @addtogroup util
  8. /// @{
  9. /// @addtogroup containers
  10. /// @{
  11. /// The hash function
  12. struct DictionaryHasher
  13. {
  14. size_t operator()(const char* str) const
  15. {
  16. size_t h = 0;
  17. for (; *str != '\0'; ++str)
  18. {
  19. h += *str;
  20. }
  21. return h;
  22. }
  23. };
  24. /// The collision evaluation function
  25. struct DictionaryEqual
  26. {
  27. bool operator()(const char* a, const char* b) const
  28. {
  29. return strcmp(a, b) == 0;
  30. }
  31. };
  32. /// The hash map that has as key an old school C string. When inserting the
  33. /// char MUST NOT point to a temporary or the evaluation function will fail.
  34. /// Its template struct because C++ does not offer template typedefs
  35. template<typename T, typename Alloc = Allocator<std::pair<const char*, T>>>
  36. using Dictionary =
  37. std::unordered_map<
  38. const char*,
  39. T,
  40. DictionaryHasher,
  41. DictionaryEqual,
  42. Alloc>;
  43. /// @}
  44. /// @}
  45. } // end namespace anki
  46. #endif