summaryrefslogtreecommitdiffstats
path: root/include/godot_cpp/templates/vector.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/godot_cpp/templates/vector.hpp')
-rw-r--r--include/godot_cpp/templates/vector.hpp91
1 files changed, 53 insertions, 38 deletions
diff --git a/include/godot_cpp/templates/vector.hpp b/include/godot_cpp/templates/vector.hpp
index f5626af..05b7184 100644
--- a/include/godot_cpp/templates/vector.hpp
+++ b/include/godot_cpp/templates/vector.hpp
@@ -50,7 +50,7 @@ namespace godot {
template <class T>
class VectorWriteProxy {
public:
- _FORCE_INLINE_ T &operator[](int p_index) {
+ _FORCE_INLINE_ T &operator[](typename CowData<T>::Size p_index) {
CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size());
return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index];
@@ -63,22 +63,26 @@ class Vector {
public:
VectorWriteProxy<T> write;
+ typedef typename CowData<T>::Size Size;
private:
CowData<T> _cowdata;
public:
bool push_back(T p_elem);
- _FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } // alias
+ _FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias
void fill(T p_elem);
- void remove_at(int p_index) { _cowdata.remove_at(p_index); }
- void erase(const T &p_val) {
- int idx = find(p_val);
+ void remove_at(Size p_index) { _cowdata.remove_at(p_index); }
+ _FORCE_INLINE_ bool erase(const T &p_val) {
+ Size idx = find(p_val);
if (idx >= 0) {
remove_at(idx);
+ return true;
}
+ return false;
}
+
void reverse();
_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
@@ -86,37 +90,45 @@ public:
_FORCE_INLINE_ void clear() { resize(0); }
_FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); }
- _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); }
- _FORCE_INLINE_ const T &get(int p_index) const { return _cowdata.get(p_index); }
- _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
- _FORCE_INLINE_ int size() const { return _cowdata.size(); }
- Error resize(int p_size) { return _cowdata.resize(p_size); }
- _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); }
- Error insert(int p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
- int find(const T &p_val, int p_from = 0) const { return _cowdata.find(p_val, p_from); }
+ _FORCE_INLINE_ T get(Size p_index) { return _cowdata.get(p_index); }
+ _FORCE_INLINE_ const T &get(Size p_index) const { return _cowdata.get(p_index); }
+ _FORCE_INLINE_ void set(Size p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
+ _FORCE_INLINE_ Size size() const { return _cowdata.size(); }
+ Error resize(Size p_size) { return _cowdata.resize(p_size); }
+ Error resize_zeroed(Size p_size) { return _cowdata.template resize<true>(p_size); }
+ _FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
+ Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
+ Size find(const T &p_val, Size p_from = 0) const { return _cowdata.find(p_val, p_from); }
+ Size rfind(const T &p_val, Size p_from = -1) const { return _cowdata.rfind(p_val, p_from); }
+ Size count(const T &p_val) const { return _cowdata.count(p_val); }
void append_array(Vector<T> p_other);
_FORCE_INLINE_ bool has(const T &p_val) const { return find(p_val) != -1; }
- template <class C>
- void sort_custom() {
- int len = _cowdata.size();
+ void sort() {
+ sort_custom<_DefaultComparator<T>>();
+ }
+
+ template <class Comparator, bool Validate = SORT_ARRAY_VALIDATE_ENABLED, class... Args>
+ void sort_custom(Args &&...args) {
+ Size len = _cowdata.size();
if (len == 0) {
return;
}
T *data = ptrw();
- SortArray<T, C> sorter;
+ SortArray<T, Comparator, Validate> sorter{ args... };
sorter.sort(data, len);
}
- void sort() {
- sort_custom<_DefaultComparator<T>>();
+ Size bsearch(const T &p_value, bool p_before) {
+ return bsearch_custom<_DefaultComparator<T>>(p_value, p_before);
}
- int bsearch(const T &p_value, bool p_before) {
- SearchArray<T> search;
+ template <class Comparator, class Value, class... Args>
+ Size bsearch_custom(const Value &p_value, bool p_before, Args &&...args) {
+ SearchArray<T, Comparator> search{ args... };
return search.bisect(ptrw(), size(), p_value, p_before);
}
@@ -125,7 +137,7 @@ public:
}
void ordered_insert(const T &p_val) {
- int i;
+ Size i;
for (i = 0; i < _cowdata.size(); i++) {
if (p_val < operator[](i)) {
break;
@@ -140,33 +152,36 @@ public:
Vector<uint8_t> to_byte_array() const {
Vector<uint8_t> ret;
+ if (is_empty()) {
+ return ret;
+ }
ret.resize(size() * sizeof(T));
memcpy(ret.ptrw(), ptr(), sizeof(T) * size());
return ret;
}
- Vector<T> slice(int p_begin, int p_end = INT_MAX) const {
+ Vector<T> slice(Size p_begin, Size p_end = CowData<T>::MAX_INT) const {
Vector<T> result;
- const int s = size();
+ const Size s = size();
- int begin = Math::clamp(p_begin, -s, s);
+ Size begin = CLAMP(p_begin, -s, s);
if (begin < 0) {
begin += s;
}
- int end = Math::clamp(p_end, -s, s);
+ Size end = CLAMP(p_end, -s, s);
if (end < 0) {
end += s;
}
ERR_FAIL_COND_V(begin > end, result);
- int result_size = end - begin;
+ Size result_size = end - begin;
result.resize(result_size);
const T *const r = ptr();
T *const w = result.ptrw();
- for (int i = 0; i < result_size; ++i) {
+ for (Size i = 0; i < result_size; ++i) {
w[i] = r[begin + i];
}
@@ -174,11 +189,11 @@ public:
}
bool operator==(const Vector<T> &p_arr) const {
- int s = size();
+ Size s = size();
if (s != p_arr.size()) {
return false;
}
- for (int i = 0; i < s; i++) {
+ for (Size i = 0; i < s; i++) {
if (operator[](i) != p_arr[i]) {
return false;
}
@@ -187,11 +202,11 @@ public:
}
bool operator!=(const Vector<T> &p_arr) const {
- int s = size();
+ Size s = size();
if (s != p_arr.size()) {
return true;
}
- for (int i = 0; i < s; i++) {
+ for (Size i = 0; i < s; i++) {
if (operator[](i) != p_arr[i]) {
return true;
}
@@ -268,7 +283,7 @@ public:
Error err = _cowdata.resize(p_init.size());
ERR_FAIL_COND(err);
- int i = 0;
+ Size i = 0;
for (const T &element : p_init) {
_cowdata.set(i++, element);
}
@@ -280,7 +295,7 @@ public:
template <class T>
void Vector<T>::reverse() {
- for (int i = 0; i < size() / 2; i++) {
+ for (Size i = 0; i < size() / 2; i++) {
T *p = ptrw();
SWAP(p[i], p[size() - i - 1]);
}
@@ -288,13 +303,13 @@ void Vector<T>::reverse() {
template <class T>
void Vector<T>::append_array(Vector<T> p_other) {
- const int ds = p_other.size();
+ const Size ds = p_other.size();
if (ds == 0) {
return;
}
- const int bs = size();
+ const Size bs = size();
resize(bs + ds);
- for (int i = 0; i < ds; ++i) {
+ for (Size i = 0; i < ds; ++i) {
ptrw()[bs + i] = p_other[i];
}
}
@@ -311,7 +326,7 @@ bool Vector<T>::push_back(T p_elem) {
template <class T>
void Vector<T>::fill(T p_elem) {
T *p = ptrw();
- for (int i = 0; i < size(); i++) {
+ for (Size i = 0; i < size(); i++) {
p[i] = p_elem;
}
}