vmap.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************************************/
  2. /* vmap.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "typedefs.h"
  33. #include "vector.h"
  34. template <class T, class V>
  35. class VMap {
  36. struct _Pair {
  37. T key;
  38. V value;
  39. _FORCE_INLINE_ _Pair() {}
  40. _FORCE_INLINE_ _Pair(const T &p_key, const V &p_value) {
  41. key = p_key;
  42. value = p_value;
  43. }
  44. };
  45. Vector<_Pair> _data;
  46. _FORCE_INLINE_ int _find(const T &p_val, bool &r_exact) const {
  47. r_exact = false;
  48. if (_data.empty())
  49. return 0;
  50. int low = 0;
  51. int high = _data.size() - 1;
  52. const _Pair *a = &_data[0];
  53. int middle = 0;
  54. #if DEBUG_ENABLED
  55. if (low > high)
  56. ERR_PRINT("low > high, this may be a bug");
  57. #endif
  58. while (low <= high) {
  59. middle = (low + high) / 2;
  60. if (p_val < a[middle].key) {
  61. high = middle - 1; //search low end of array
  62. } else if (a[middle].key < p_val) {
  63. low = middle + 1; //search high end of array
  64. } else {
  65. r_exact = true;
  66. return middle;
  67. }
  68. }
  69. //return the position where this would be inserted
  70. if (a[middle].key < p_val)
  71. middle++;
  72. return middle;
  73. }
  74. _FORCE_INLINE_ int _find_exact(const T &p_val) const {
  75. if (_data.empty())
  76. return -1;
  77. int low = 0;
  78. int high = _data.size() - 1;
  79. int middle;
  80. const _Pair *a = &_data[0];
  81. while (low <= high) {
  82. middle = (low + high) / 2;
  83. if (p_val < a[middle].key) {
  84. high = middle - 1; //search low end of array
  85. } else if (a[middle].key < p_val) {
  86. low = middle + 1; //search high end of array
  87. } else {
  88. return middle;
  89. }
  90. }
  91. return -1;
  92. }
  93. public:
  94. int insert(const T &p_key, const V &p_val) {
  95. bool exact;
  96. int pos = _find(p_key, exact);
  97. if (exact) {
  98. _data[pos].value = p_val;
  99. return pos;
  100. }
  101. _data.insert(pos, _Pair(p_key, p_val));
  102. return pos;
  103. }
  104. bool has(const T &p_val) const {
  105. return _find_exact(p_val) != -1;
  106. }
  107. void erase(const T &p_val) {
  108. int pos = _find_exact(p_val);
  109. if (pos < 0)
  110. return;
  111. _data.remove(pos);
  112. }
  113. int find(const T &p_val) const {
  114. return _find_exact(p_val);
  115. }
  116. int find_nearest(const T &p_val) const {
  117. bool exact;
  118. return _find(p_val, exact);
  119. }
  120. _FORCE_INLINE_ int size() const { return _data.size(); }
  121. _FORCE_INLINE_ bool empty() const { return _data.empty(); }
  122. const _Pair *get_array() const {
  123. return _data.ptr();
  124. }
  125. _Pair *get_array() {
  126. return _data.ptr();
  127. }
  128. const V &getv(int p_index) const {
  129. return _data[p_index].value;
  130. }
  131. V &getv(int p_index) {
  132. return _data[p_index].value;
  133. }
  134. const T &getk(int p_index) const {
  135. return _data[p_index].key;
  136. }
  137. T &getk(int p_index) {
  138. return _data[p_index].key;
  139. }
  140. inline const V &operator[](const T &p_key) const {
  141. int pos = _find_exact(p_key);
  142. CRASH_COND(pos < 0);
  143. return _data[pos].value;
  144. }
  145. inline V &operator[](const T &p_key) {
  146. int pos = _find_exact(p_key);
  147. if (pos < 0) {
  148. V val;
  149. pos = insert(p_key, val);
  150. }
  151. return _data[pos].value;
  152. }
  153. };
  154. #endif // VMAP_H