vmap.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*************************************************************************/
  2. /* vmap.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef VMAP_H
  30. #define VMAP_H
  31. #include "typedefs.h"
  32. #include "vector.h"
  33. template<class T,class V>
  34. class VMap {
  35. struct _Pair {
  36. T key;
  37. V value;
  38. _FORCE_INLINE_ _Pair() {}
  39. _FORCE_INLINE_ _Pair(const T& p_key, const V& p_value) {
  40. key=p_key;
  41. value=p_value;
  42. }
  43. };
  44. Vector<_Pair> _data;
  45. _FORCE_INLINE_ int _find(const T& p_val,bool &r_exact) const {
  46. r_exact=false;
  47. if (_data.empty())
  48. return 0;
  49. int low = 0;
  50. int high = _data.size() -1;
  51. int middle;
  52. const _Pair *a=&_data[0];
  53. while( low <= high )
  54. {
  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. {
  79. middle = ( low + high ) / 2;
  80. if( p_val < a[ middle].key ) {
  81. high = middle - 1; //search low end of array
  82. } else if ( a[middle].key < p_val) {
  83. low = middle + 1; //search high end of array
  84. } else {
  85. return middle;
  86. }
  87. }
  88. return -1;
  89. }
  90. public:
  91. int insert(const T& p_key,const V& p_val) {
  92. bool exact;
  93. int pos = _find(p_key,exact);
  94. if (exact) {
  95. _data[pos].value=p_val;
  96. return pos;
  97. }
  98. _data.insert(pos,_Pair(p_key,p_val));
  99. return pos;
  100. }
  101. bool has(const T& p_val) const {
  102. return _find_exact(p_val)!=-1;
  103. }
  104. void erase(const T& p_val) {
  105. int pos = _find_exact(p_val);
  106. if (pos<0)
  107. return;
  108. _data.remove(pos);
  109. }
  110. int find(const T& p_val) const {
  111. return _find_exact(p_val);
  112. }
  113. _FORCE_INLINE_ int size() const { return _data.size(); }
  114. _FORCE_INLINE_ bool empty() const { return _data.empty(); }
  115. const _Pair *get_array() const {
  116. return _data.ptr();
  117. }
  118. _Pair *get_array() {
  119. return _data.ptr();
  120. }
  121. const V& getv(int p_index) const {
  122. return _data[p_index].value;
  123. }
  124. V& getv(int p_index) {
  125. return _data[p_index].value;
  126. }
  127. const T& getk(int p_index) const {
  128. return _data[p_index].key;
  129. }
  130. T& getk(int p_index) {
  131. return _data[p_index].key;
  132. }
  133. inline const V& operator[](const T& p_key) const {
  134. int pos = _find_exact(p_key);
  135. if (pos<0) {
  136. const T& aux=*((T*)0); //nullreturn
  137. ERR_FAIL_COND_V(pos<1,aux);
  138. }
  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