vset.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "typedefs.h"
  33. #include "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. int middle;
  44. const T *a = &_data[0];
  45. while (low <= high) {
  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. middle = (low + high) / 2;
  70. if (p_val < a[middle]) {
  71. high = middle - 1; //search low end of array
  72. } else if (a[middle] < p_val) {
  73. low = middle + 1; //search high end of array
  74. } else {
  75. return middle;
  76. }
  77. }
  78. return -1;
  79. }
  80. public:
  81. void insert(const T &p_val) {
  82. bool exact;
  83. int pos = _find(p_val, exact);
  84. if (exact)
  85. return;
  86. _data.insert(pos, p_val);
  87. }
  88. bool has(const T &p_val) const {
  89. return _find_exact(p_val) != -1;
  90. }
  91. void erase(const T &p_val) {
  92. int pos = _find_exact(p_val);
  93. if (pos < 0)
  94. return;
  95. _data.remove(pos);
  96. }
  97. int find(const T &p_val) const {
  98. return _find_exact(p_val);
  99. }
  100. _FORCE_INLINE_ bool empty() const { return _data.empty(); }
  101. _FORCE_INLINE_ int size() const { return _data.size(); }
  102. inline T &operator[](int p_index) {
  103. return _data[p_index];
  104. }
  105. inline const T &operator[](int p_index) const {
  106. return _data[p_index];
  107. }
  108. };
  109. #endif // VSET_H