vset.h 4.1 KB

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