vset.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**************************************************************************/
  2. /* vset.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/templates/vector.h"
  33. #include "core/typedefs.h"
  34. template <typename 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.is_empty()) {
  40. return 0;
  41. }
  42. int low = 0;
  43. int high = _data.size() - 1;
  44. const T *a = &_data[0];
  45. int middle = 0;
  46. #ifdef DEBUG_ENABLED
  47. if (low > high) {
  48. ERR_PRINT("low > high, this may be a bug");
  49. }
  50. #endif
  51. while (low <= high) {
  52. middle = (low + high) / 2;
  53. if (p_val < a[middle]) {
  54. high = middle - 1; //search low end of array
  55. } else if (a[middle] < p_val) {
  56. low = middle + 1; //search high end of array
  57. } else {
  58. r_exact = true;
  59. return middle;
  60. }
  61. }
  62. //return the position where this would be inserted
  63. if (a[middle] < p_val) {
  64. middle++;
  65. }
  66. return middle;
  67. }
  68. _FORCE_INLINE_ int _find_exact(const T &p_val) const {
  69. if (_data.is_empty()) {
  70. return -1;
  71. }
  72. int low = 0;
  73. int high = _data.size() - 1;
  74. int middle;
  75. const T *a = &_data[0];
  76. while (low <= high) {
  77. middle = (low + high) / 2;
  78. if (p_val < a[middle]) {
  79. high = middle - 1; //search low end of array
  80. } else if (a[middle] < p_val) {
  81. low = middle + 1; //search high end of array
  82. } else {
  83. return middle;
  84. }
  85. }
  86. return -1;
  87. }
  88. public:
  89. void insert(const T &p_val) {
  90. bool exact;
  91. int pos = _find(p_val, exact);
  92. if (exact) {
  93. return;
  94. }
  95. _data.insert(pos, p_val);
  96. }
  97. bool has(const T &p_val) const {
  98. return _find_exact(p_val) != -1;
  99. }
  100. void erase(const T &p_val) {
  101. int pos = _find_exact(p_val);
  102. if (pos < 0) {
  103. return;
  104. }
  105. _data.remove_at(pos);
  106. }
  107. int find(const T &p_val) const {
  108. return _find_exact(p_val);
  109. }
  110. _FORCE_INLINE_ bool is_empty() const { return _data.is_empty(); }
  111. _FORCE_INLINE_ int size() const { return _data.size(); }
  112. inline T &operator[](int p_index) {
  113. return _data.write[p_index];
  114. }
  115. inline const T &operator[](int p_index) const {
  116. return _data[p_index];
  117. }
  118. };
  119. #endif // VSET_H