StringMap.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * Copyright (c) 2006-2017 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_STRING_MAP_H
  21. #define LOVE_STRING_MAP_H
  22. #include "Exception.h"
  23. #include <string>
  24. #include <vector>
  25. // As StringMap instantiates std::vector<std::string> for instances that use
  26. // getNames(), we end up with multiple copies in the object files. This
  27. // declaration means we only emit it once (in StringMap.cpp).
  28. extern template class std::vector<std::string>;
  29. extern template decltype(std::vector<std::string>().emplace_back("")) std::vector<std::string>::emplace_back<const char *const&>(const char *const&);
  30. namespace love
  31. {
  32. template<typename T, unsigned int SIZE>
  33. class StringMap
  34. {
  35. public:
  36. struct Entry
  37. {
  38. const char *key;
  39. T value;
  40. };
  41. StringMap(const Entry *entries, unsigned int num)
  42. {
  43. for (unsigned int i = 0; i < SIZE; ++i)
  44. reverse[i] = nullptr;
  45. unsigned int n = num / sizeof(Entry);
  46. for (unsigned int i = 0; i < n; ++i)
  47. add(entries[i].key, entries[i].value);
  48. }
  49. bool streq(const char *a, const char *b)
  50. {
  51. while (*a != 0 && *b != 0)
  52. {
  53. if (*a != *b)
  54. return false;
  55. ++a;
  56. ++b;
  57. }
  58. return (*a == 0 && *b == 0);
  59. }
  60. bool find(const char *key, T &t)
  61. {
  62. unsigned int str_hash = djb2(key);
  63. for (unsigned int i = 0; i < MAX; ++i)
  64. {
  65. unsigned int str_i = (str_hash + i) % MAX;
  66. if (!records[str_i].set)
  67. return false;
  68. if (streq(records[str_i].key, key))
  69. {
  70. t = records[str_i].value;
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. bool find(T key, const char *&str)
  77. {
  78. unsigned int index = (unsigned int) key;
  79. if (index >= SIZE)
  80. return false;
  81. if (reverse[index] != nullptr)
  82. {
  83. str = reverse[index];
  84. return true;
  85. }
  86. else
  87. {
  88. return false;
  89. }
  90. }
  91. bool add(const char *key, T value)
  92. {
  93. unsigned int str_hash = djb2(key);
  94. bool inserted = false;
  95. for (unsigned int i = 0; i < MAX; ++i)
  96. {
  97. unsigned int str_i = (str_hash + i) % MAX;
  98. if (!records[str_i].set)
  99. {
  100. inserted = true;
  101. records[str_i].set = true;
  102. records[str_i].key = key;
  103. records[str_i].value = value;
  104. break;
  105. }
  106. }
  107. unsigned int index = (unsigned int) value;
  108. if (index >= SIZE)
  109. {
  110. printf("Constant %s out of bounds with %u!\n", key, index);
  111. return false;
  112. }
  113. reverse[index] = key;
  114. return inserted;
  115. }
  116. unsigned int djb2(const char *key)
  117. {
  118. unsigned int hash = 5381;
  119. int c;
  120. while ((c = *key++))
  121. hash = ((hash << 5) + hash) + c;
  122. return hash;
  123. }
  124. std::vector<std::string> getNames() const
  125. {
  126. std::vector<std::string> names;
  127. names.reserve(SIZE);
  128. for (unsigned int i = 0; i < SIZE; ++i)
  129. if (reverse[i] != nullptr)
  130. names.emplace_back(reverse[i]);
  131. return names;
  132. }
  133. private:
  134. struct Record
  135. {
  136. const char *key;
  137. T value;
  138. bool set;
  139. Record() : set(false) {}
  140. };
  141. static const unsigned int MAX = SIZE * 2;
  142. Record records[MAX];
  143. const char *reverse[SIZE];
  144. }; // StringMap
  145. } // love
  146. #endif // LOVE_STRING_MAP_H