summaryrefslogtreecommitdiff
path: root/core/array.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/array.cpp')
-rw-r--r--core/array.cpp22
1 files changed, 18 insertions, 4 deletions
diff --git a/core/array.cpp b/core/array.cpp
index c53fea1f28..9e3250fd47 100644
--- a/core/array.cpp
+++ b/core/array.cpp
@@ -35,8 +35,8 @@
#include "variant.h"
#include "vector.h"
-struct ArrayPrivate {
-
+class ArrayPrivate {
+public:
SafeRefCount refcount;
Vector<Variant> array;
};
@@ -211,13 +211,13 @@ const Variant &Array::get(int p_idx) const {
return operator[](p_idx);
}
-Array Array::duplicate() const {
+Array Array::duplicate(bool p_deep) const {
Array new_arr;
int element_count = size();
new_arr.resize(element_count);
for (int i = 0; i < element_count; i++) {
- new_arr[i] = get(i);
+ new_arr[i] = p_deep ? get(i).duplicate(p_deep) : get(i);
}
return new_arr;
@@ -266,6 +266,20 @@ Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
return *this;
}
+void Array::shuffle() {
+
+ const int n = _p->array.size();
+ if (n < 2)
+ return;
+ Variant *data = _p->array.ptrw();
+ for (int i = n - 1; i >= 1; i--) {
+ const int j = Math::rand() % (i + 1);
+ const Variant tmp = data[j];
+ data[j] = data[i];
+ data[i] = tmp;
+ }
+}
+
template <typename Less>
_FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value, bool p_before, const Less &p_less) {