| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/vector.h" |
| 34 |
#include "core/typedefs.h" |
| 35 |
|
| 36 |
template <typename T> |
| 37 |
class VSet { |
| 38 |
Vector<T> _data; |
| 39 |
|
| 40 |
protected: |
| 41 |
_FORCE_INLINE_ int _find(const T &p_val, bool &r_exact) const { |
| 42 |
r_exact = false; |
| 43 |
if (_data.is_empty()) { |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
int64_t pos = _data.span().bisect(p_val, true); |
| 48 |
|
| 49 |
if (pos < _data.size() && !(p_val < _data[pos]) && !(_data[pos] < p_val)) { |
| 50 |
r_exact = true; |
| 51 |
} |
| 52 |
return pos; |
| 53 |
} |
| 54 |
|
| 55 |
_FORCE_INLINE_ int _find_exact(const T &p_val) const { |
| 56 |
if (_data.is_empty()) { |
| 57 |
return -1; |
| 58 |
} |
| 59 |
|
| 60 |
int64_t pos = _data.span().bisect(p_val, true); |
| 61 |
|
| 62 |
if (pos < _data.size() && !(p_val < _data[pos]) && !(_data[pos] < p_val)) { |
| 63 |
return pos; |
| 64 |
} |
| 65 |
return -1; |
| 66 |
} |
| 67 |
|
| 68 |
public: |
| 69 |
void insert(const T &p_val) { |
| 70 |
bool exact; |
| 71 |
int pos = _find(p_val, exact); |
| 72 |
if (exact) { |
| 73 |
return; |
| 74 |
} |
| 75 |
_data.insert(pos, p_val); |
| 76 |
} |
| 77 |
|
| 78 |
bool has(const T &p_val) const { |
| 79 |
return _find_exact(p_val) != -1; |
| 80 |
} |
| 81 |
|
| 82 |
void erase(const T &p_val) { |
| 83 |
int pos = _find_exact(p_val); |
| 84 |
if (pos < 0) { |
| 85 |
return; |
| 86 |
} |
| 87 |
_data.remove_at(pos); |
| 88 |
} |
| 89 |
|
| 90 |
int find(const T &p_val) const { |
| 91 |
return _find_exact(p_val); |
| 92 |
} |
| 93 |
|
| 94 |
_FORCE_INLINE_ bool is_empty() const { return _data.is_empty(); } |
| 95 |
|
| 96 |
_FORCE_INLINE_ int size() const { return _data.size(); } |
| 97 |
|
| 98 |
inline T &operator[](int p_index) { |
| 99 |
return _data.write[p_index]; |
| 100 |
} |
| 101 |
|
| 102 |
inline const T &operator[](int p_index) const { |
| 103 |
return _data[p_index]; |
| 104 |
} |
| 105 |
|
| 106 |
_FORCE_INLINE_ VSet() {} |
| 107 |
_FORCE_INLINE_ VSet(std::initializer_list<T> p_init) : |
| 108 |
_data(p_init) {} |
| 109 |
}; |