vmap.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* vmap.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/templates/cowdata.h"
  33. #include "core/typedefs.h"
  34. template <typename T, typename 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.is_empty()) {
  51. return 0;
  52. }
  53. int low = 0;
  54. int high = _cowdata.size() - 1;
  55. const Pair *a = _cowdata.ptr();
  56. int middle = 0;
  57. #ifdef DEBUG_ENABLED
  58. if (low > high) {
  59. ERR_PRINT("low > high, this may be a bug");
  60. }
  61. #endif
  62. while (low <= high) {
  63. middle = (low + high) / 2;
  64. if (p_val < a[middle].key) {
  65. high = middle - 1; //search low end of array
  66. } else if (a[middle].key < p_val) {
  67. low = middle + 1; //search high end of array
  68. } else {
  69. r_exact = true;
  70. return middle;
  71. }
  72. }
  73. //return the position where this would be inserted
  74. if (a[middle].key < p_val) {
  75. middle++;
  76. }
  77. return middle;
  78. }
  79. _FORCE_INLINE_ int _find_exact(const T &p_val) const {
  80. if (_cowdata.is_empty()) {
  81. return -1;
  82. }
  83. int low = 0;
  84. int high = _cowdata.size() - 1;
  85. int middle;
  86. const Pair *a = _cowdata.ptr();
  87. while (low <= high) {
  88. middle = (low + high) / 2;
  89. if (p_val < a[middle].key) {
  90. high = middle - 1; //search low end of array
  91. } else if (a[middle].key < p_val) {
  92. low = middle + 1; //search high end of array
  93. } else {
  94. return middle;
  95. }
  96. }
  97. return -1;
  98. }
  99. public:
  100. int insert(const T &p_key, const V &p_val) {
  101. bool exact;
  102. int pos = _find(p_key, exact);
  103. if (exact) {
  104. _cowdata.get_m(pos).value = p_val;
  105. return pos;
  106. }
  107. _cowdata.insert(pos, Pair(p_key, p_val));
  108. return pos;
  109. }
  110. bool has(const T &p_val) const {
  111. return _find_exact(p_val) != -1;
  112. }
  113. void erase(const T &p_val) {
  114. int pos = _find_exact(p_val);
  115. if (pos < 0) {
  116. return;
  117. }
  118. _cowdata.remove_at(pos);
  119. }
  120. int find(const T &p_val) const {
  121. return _find_exact(p_val);
  122. }
  123. int find_nearest(const T &p_val) const {
  124. if (_cowdata.is_empty()) {
  125. return -1;
  126. }
  127. bool exact;
  128. return _find(p_val, exact);
  129. }
  130. _FORCE_INLINE_ int size() const { return _cowdata.size(); }
  131. _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); }
  132. const Pair *get_array() const {
  133. return _cowdata.ptr();
  134. }
  135. Pair *get_array() {
  136. return _cowdata.ptrw();
  137. }
  138. const V &getv(int p_index) const {
  139. return _cowdata.get(p_index).value;
  140. }
  141. V &getv(int p_index) {
  142. return _cowdata.get_m(p_index).value;
  143. }
  144. const T &getk(int p_index) const {
  145. return _cowdata.get(p_index).key;
  146. }
  147. T &getk(int p_index) {
  148. return _cowdata.get_m(p_index).key;
  149. }
  150. inline const V &operator[](const T &p_key) const {
  151. int pos = _find_exact(p_key);
  152. CRASH_COND(pos < 0);
  153. return _cowdata.get(pos).value;
  154. }
  155. inline V &operator[](const T &p_key) {
  156. int pos = _find_exact(p_key);
  157. if (pos < 0) {
  158. pos = insert(p_key, V());
  159. }
  160. return _cowdata.get_m(pos).value;
  161. }
  162. _FORCE_INLINE_ VMap() {}
  163. _FORCE_INLINE_ VMap(const VMap &p_from) { _cowdata._ref(p_from._cowdata); }
  164. inline void operator=(const VMap &p_from) {
  165. _cowdata._ref(p_from._cowdata);
  166. }
  167. };
  168. #endif // VMAP_H