vset.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*************************************************************************/
  2. /* vset.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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 VSET_H
  30. #define VSET_H
  31. #include "typedefs.h"
  32. #include "vector.h"
  33. template<class T>
  34. class VSet {
  35. Vector<T> _data;
  36. _FORCE_INLINE_ int _find(const T& p_val,bool &r_exact) const {
  37. r_exact=false;
  38. if (_data.empty())
  39. return 0;
  40. int low = 0;
  41. int high = _data.size() -1;
  42. int middle;
  43. const T *a=&_data[0];
  44. while( low <= high )
  45. {
  46. middle = ( low + high ) / 2;
  47. if( p_val < a[ middle] ) {
  48. high = middle - 1; //search low end of array
  49. } else if ( a[middle] < p_val) {
  50. low = middle + 1; //search high end of array
  51. } else {
  52. r_exact=true;
  53. return middle;
  54. }
  55. }
  56. //return the position where this would be inserted
  57. if (a[middle]<p_val)
  58. middle++;
  59. return middle;
  60. }
  61. _FORCE_INLINE_ int _find_exact(const T& p_val) const {
  62. if (_data.empty())
  63. return -1;
  64. int low = 0;
  65. int high = _data.size() -1;
  66. int middle;
  67. const T *a=&_data[0];
  68. while( low <= high )
  69. {
  70. middle = ( low + high ) / 2;
  71. if( p_val < a[ middle] ) {
  72. high = middle - 1; //search low end of array
  73. } else if ( a[middle] < p_val) {
  74. low = middle + 1; //search high end of array
  75. } else {
  76. return middle;
  77. }
  78. }
  79. return -1;
  80. }
  81. public:
  82. void insert(const T& p_val) {
  83. bool exact;
  84. int pos = _find(p_val,exact);
  85. if (exact)
  86. return;
  87. _data.insert(pos,p_val);
  88. }
  89. bool has(const T& p_val) const {
  90. return _find_exact(p_val)!=-1;
  91. }
  92. void erase(const T& p_val) {
  93. int pos = _find_exact(p_val);
  94. if (pos<0)
  95. return;
  96. _data.remove(pos);
  97. }
  98. int find(const T& p_val) const {
  99. return _find_exact(p_val);
  100. }
  101. _FORCE_INLINE_ bool empty() const { return _data.empty(); }
  102. _FORCE_INLINE_ int size() const { return _data.size(); }
  103. inline T& operator[](int p_index) {
  104. return _data[p_index];
  105. }
  106. inline const T& operator[](int p_index) const {
  107. return _data[p_index];
  108. }
  109. };
  110. #endif // VSET_H