vmap.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*************************************************************************/
  2. /* vmap.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef VMAP_H
  31. #define VMAP_H
  32. #include "core/cowdata.h"
  33. #include "core/typedefs.h"
  34. template <class T, class V>
  35. class VMap {
  36. public:
  37. struct Pair {
  38. T key;
  39. V value;
  40. _FORCE_INLINE_ Pair() {}
  41. _FORCE_INLINE_ Pair(const T &p_key, const V &p_value) {
  42. key = p_key;
  43. value = p_value;
  44. }
  45. };
  46. private:
  47. CowData<Pair> _cowdata;
  48. _FORCE_INLINE_ int _find(const T &p_val, bool &r_exact) const {
  49. r_exact = false;
  50. if (_cowdata.empty())
  51. return 0;
  52. int low = 0;
  53. int high = _cowdata.size() - 1;
  54. const Pair *a = _cowdata.ptr();
  55. int middle = 0;
  56. #ifdef DEBUG_ENABLED
  57. if (low > high)
  58. ERR_PRINT("low > high, this may be a bug");
  59. #endif
  60. while (low <= high) {
  61. middle = (low + high) / 2;
  62. if (p_val < a[middle].key) {
  63. high = middle - 1; //search low end of array
  64. } else if (a[middle].key < p_val) {
  65. low = middle + 1; //search high end of array
  66. } else {
  67. r_exact = true;
  68. return middle;
  69. }
  70. }
  71. //return the position where this would be inserted
  72. if (a[middle].key < p_val)
  73. middle++;
  74. return middle;
  75. }
  76. _FORCE_INLINE_ int _find_exact(const T &p_val) const {
  77. if (_cowdata.empty())
  78. return -1;
  79. int low = 0;
  80. int high = _cowdata.size() - 1;
  81. int middle;
  82. const Pair *a = _cowdata.ptr();
  83. while (low <= high) {
  84. middle = (low + high) / 2;
  85. if (p_val < a[middle].key) {
  86. high = middle - 1; //search low end of array
  87. } else if (a[middle].key < p_val) {
  88. low = middle + 1; //search high end of array
  89. } else {
  90. return middle;
  91. }
  92. }
  93. return -1;
  94. }
  95. public:
  96. int insert(const T &p_key, const V &p_val) {
  97. bool exact;
  98. int pos = _find(p_key, exact);
  99. if (exact) {
  100. _cowdata.get_m(pos).value = p_val;
  101. return pos;
  102. }
  103. _cowdata.insert(pos, Pair(p_key, p_val));
  104. return pos;
  105. }
  106. bool has(const T &p_val) const {
  107. return _find_exact(p_val) != -1;
  108. }
  109. void erase(const T &p_val) {
  110. int pos = _find_exact(p_val);
  111. if (pos < 0)
  112. return;
  113. _cowdata.remove(pos);
  114. }
  115. int find(const T &p_val) const {
  116. return _find_exact(p_val);
  117. }
  118. int find_nearest(const T &p_val) const {
  119. bool exact;
  120. return _find(p_val, exact);
  121. }
  122. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  123. _FORCE_INLINE_ bool empty() const { return _cowdata.empty(); }
  124. const Pair *get_array() const {
  125. return _cowdata.ptr();
  126. }
  127. Pair *get_array() {
  128. return _cowdata.ptrw();
  129. }
  130. const V &getv(int p_index) const {
  131. return _cowdata.get(p_index).value;
  132. }
  133. V &getv(int p_index) {
  134. return _cowdata.get_m(p_index).value;
  135. }
  136. const T &getk(int p_index) const {
  137. return _cowdata.get(p_index).key;
  138. }
  139. T &getk(int p_index) {
  140. return _cowdata.get_m(p_index).key;
  141. }
  142. inline const V &operator[](const T &p_key) const {
  143. int pos = _find_exact(p_key);
  144. CRASH_COND(pos < 0);
  145. return _cowdata.get(pos).value;
  146. }
  147. inline V &operator[](const T &p_key) {
  148. int pos = _find_exact(p_key);
  149. if (pos < 0) {
  150. pos = insert(p_key, V());
  151. }
  152. return _cowdata.get_m(pos).value;
  153. }
  154. _FORCE_INLINE_ VMap(){};
  155. _FORCE_INLINE_ VMap(const VMap &p_from) { _cowdata._ref(p_from._cowdata); }
  156. inline VMap &operator=(const VMap &p_from) {
  157. _cowdata._ref(p_from._cowdata);
  158. return *this;
  159. }
  160. };
  161. #endif // VMAP_H