vmap.h 4.9 KB

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