Dictionary.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_UTIL_DICTIONARY_H
  6. #define ANKI_UTIL_DICTIONARY_H
  7. #include "anki/util/Allocator.h"
  8. #include "anki/util/String.h"
  9. #include <unordered_map>
  10. namespace anki {
  11. /// @addtogroup util_containers
  12. /// @{
  13. /// The hash function
  14. class DictionaryHasher
  15. {
  16. public:
  17. PtrSize operator()(const CString& cstr) const
  18. {
  19. PtrSize h = 0;
  20. auto str = cstr.get();
  21. for (; *str != '\0'; ++str)
  22. {
  23. h += *str;
  24. }
  25. return h;
  26. }
  27. };
  28. /// The collision evaluation function
  29. class DictionaryEqual
  30. {
  31. public:
  32. Bool operator()(const CString& a, const CString& b) const
  33. {
  34. return a == b;
  35. }
  36. };
  37. /// The hash map that has as key an old school C string. When inserting the
  38. /// char MUST NOT point to a temporary or the evaluation function will fail.
  39. /// Its template struct because C++ does not offer template typedefs
  40. template<
  41. typename T,
  42. typename TAlloc = HeapAllocator<std::pair<CString, T>>>
  43. using Dictionary =
  44. std::unordered_map<
  45. CString,
  46. T,
  47. DictionaryHasher,
  48. DictionaryEqual,
  49. TAlloc>;
  50. /// @}
  51. } // end namespace anki
  52. #endif