2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* array.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2018-01-01 21:40:08 +08:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 09:10:30 +08:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "array.h"
|
2017-01-16 15:04:19 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "hashfuncs.h"
|
|
|
|
#include "object.h"
|
2017-03-05 23:44:50 +08:00
|
|
|
#include "variant.h"
|
|
|
|
#include "vector.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-03-10 03:16:08 +08:00
|
|
|
class ArrayPrivate {
|
|
|
|
public:
|
2014-02-10 09:10:30 +08:00
|
|
|
SafeRefCount refcount;
|
|
|
|
Vector<Variant> array;
|
|
|
|
};
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Array::_ref(const Array &p_from) const {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
ArrayPrivate *_fp = p_from._p;
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!_fp); // should NOT happen.
|
|
|
|
|
|
|
|
if (_fp == _p)
|
2017-09-22 11:58:29 +08:00
|
|
|
return; // whatever it is, nothing to do here move along
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
bool success = _fp->refcount.ref();
|
|
|
|
|
2017-09-22 11:58:29 +08:00
|
|
|
ERR_FAIL_COND(!success); // should really not happen either
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_unref();
|
|
|
|
|
2017-01-11 19:53:31 +08:00
|
|
|
_p = p_from._p;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Array::_unref() const {
|
|
|
|
|
|
|
|
if (!_p)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_p->refcount.unref()) {
|
|
|
|
memdelete(_p);
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
_p = NULL;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Variant &Array::operator[](int p_idx) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-07-25 09:11:03 +08:00
|
|
|
return _p->array.write[p_idx];
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
const Variant &Array::operator[](int p_idx) const {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
return _p->array[p_idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
int Array::size() const {
|
|
|
|
|
|
|
|
return _p->array.size();
|
|
|
|
}
|
|
|
|
bool Array::empty() const {
|
|
|
|
|
|
|
|
return _p->array.empty();
|
|
|
|
}
|
|
|
|
void Array::clear() {
|
|
|
|
|
|
|
|
_p->array.clear();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool Array::operator==(const Array &p_array) const {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
return _p == p_array._p;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Array::hash() const {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
uint32_t h = hash_djb2_one_32(0);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < _p->array.size(); i++) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
h = hash_djb2_one_32(_p->array[i].hash(), h);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
void Array::operator=(const Array &p_array) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_ref(p_array);
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
void Array::push_back(const Variant &p_value) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_p->array.push_back(p_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error Array::resize(int p_new_size) {
|
|
|
|
|
|
|
|
return _p->array.resize(p_new_size);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Array::insert(int p_pos, const Variant &p_value) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
_p->array.insert(p_pos, p_value);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Array::erase(const Variant &p_value) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_p->array.erase(p_value);
|
|
|
|
}
|
|
|
|
|
2016-11-19 04:30:16 +08:00
|
|
|
Variant Array::front() const {
|
|
|
|
ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
|
|
|
|
return operator[](0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant Array::back() const {
|
|
|
|
ERR_FAIL_COND_V(_p->array.size() == 0, Variant());
|
|
|
|
return operator[](_p->array.size() - 1);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int Array::find(const Variant &p_value, int p_from) const {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2016-06-11 01:57:56 +08:00
|
|
|
return _p->array.find(p_value, p_from);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int Array::rfind(const Variant &p_value, int p_from) const {
|
2016-06-04 05:10:43 +08:00
|
|
|
|
2016-06-11 04:28:09 +08:00
|
|
|
if (_p->array.size() == 0)
|
2016-06-04 05:10:43 +08:00
|
|
|
return -1;
|
|
|
|
|
2016-06-11 04:28:09 +08:00
|
|
|
if (p_from < 0) {
|
|
|
|
// Relative offset from the end
|
|
|
|
p_from = _p->array.size() + p_from;
|
|
|
|
}
|
|
|
|
if (p_from < 0 || p_from >= _p->array.size()) {
|
|
|
|
// Limit to array boundaries
|
|
|
|
p_from = _p->array.size() - 1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = p_from; i >= 0; i--) {
|
2016-06-04 05:10:43 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (_p->array[i] == p_value) {
|
2016-06-04 05:10:43 +08:00
|
|
|
return i;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int Array::find_last(const Variant &p_value) const {
|
2016-06-11 04:28:09 +08:00
|
|
|
|
|
|
|
return rfind(p_value);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int Array::count(const Variant &p_value) const {
|
2016-06-04 05:10:43 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (_p->array.size() == 0)
|
2016-06-04 05:10:43 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int amount = 0;
|
|
|
|
for (int i = 0; i < _p->array.size(); i++) {
|
2016-06-04 05:10:43 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (_p->array[i] == p_value) {
|
2016-06-04 05:10:43 +08:00
|
|
|
amount++;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool Array::has(const Variant &p_value) const {
|
2016-07-03 01:03:35 +08:00
|
|
|
return _p->array.find(p_value, 0) != -1;
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void Array::remove(int p_pos) {
|
|
|
|
|
|
|
|
_p->array.remove(p_pos);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Array::set(int p_idx, const Variant &p_value) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
operator[](p_idx) = p_value;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
const Variant &Array::get(int p_idx) const {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
return operator[](p_idx);
|
|
|
|
}
|
|
|
|
|
2018-03-10 03:16:08 +08:00
|
|
|
Array Array::duplicate(bool p_deep) const {
|
2017-09-07 05:13:05 +08:00
|
|
|
|
|
|
|
Array new_arr;
|
|
|
|
int element_count = size();
|
|
|
|
new_arr.resize(element_count);
|
|
|
|
for (int i = 0; i < element_count; i++) {
|
2018-03-10 03:16:08 +08:00
|
|
|
new_arr[i] = p_deep ? get(i).duplicate(p_deep) : get(i);
|
2017-09-07 05:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return new_arr;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
struct _ArrayVariantSort {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
_FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
|
|
|
|
bool valid = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
Variant res;
|
2017-03-05 23:44:50 +08:00
|
|
|
Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid);
|
2014-02-10 09:10:30 +08:00
|
|
|
if (!valid)
|
2017-03-05 23:44:50 +08:00
|
|
|
res = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-07 23:49:23 +08:00
|
|
|
Array &Array::sort() {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_p->array.sort_custom<_ArrayVariantSort>();
|
2017-10-07 23:49:23 +08:00
|
|
|
return *this;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct _ArrayVariantSortCustom {
|
|
|
|
|
|
|
|
Object *obj;
|
|
|
|
StringName func;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
_FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
const Variant *args[2] = { &p_l, &p_r };
|
2014-02-10 09:10:30 +08:00
|
|
|
Variant::CallError err;
|
2017-03-05 23:44:50 +08:00
|
|
|
bool res = obj->call(func, args, 2, err);
|
|
|
|
if (err.error != Variant::CallError::CALL_OK)
|
|
|
|
res = false;
|
2014-02-10 09:10:30 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
2017-10-07 23:49:23 +08:00
|
|
|
Array &Array::sort_custom(Object *p_obj, const StringName &p_function) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-10-07 23:49:23 +08:00
|
|
|
ERR_FAIL_NULL_V(p_obj, *this);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
SortArray<Variant, _ArrayVariantSortCustom> avs;
|
|
|
|
avs.compare.obj = p_obj;
|
|
|
|
avs.compare.func = p_function;
|
2017-11-25 11:07:54 +08:00
|
|
|
avs.sort(_p->array.ptrw(), _p->array.size());
|
2017-10-07 23:49:23 +08:00
|
|
|
return *this;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2018-01-11 02:36:53 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 02:04:38 +08:00
|
|
|
template <typename Less>
|
|
|
|
_FORCE_INLINE_ int bisect(const Vector<Variant> &p_array, const Variant &p_value, bool p_before, const Less &p_less) {
|
|
|
|
|
|
|
|
int lo = 0;
|
|
|
|
int hi = p_array.size();
|
|
|
|
if (p_before) {
|
|
|
|
while (lo < hi) {
|
|
|
|
const int mid = (lo + hi) / 2;
|
|
|
|
if (p_less(p_array.get(mid), p_value)) {
|
|
|
|
lo = mid + 1;
|
|
|
|
} else {
|
|
|
|
hi = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (lo < hi) {
|
|
|
|
const int mid = (lo + hi) / 2;
|
|
|
|
if (p_less(p_value, p_array.get(mid))) {
|
|
|
|
hi = mid;
|
|
|
|
} else {
|
|
|
|
lo = mid + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Array::bsearch(const Variant &p_value, bool p_before) {
|
|
|
|
|
|
|
|
return bisect(_p->array, p_value, p_before, _ArrayVariantSort());
|
|
|
|
}
|
|
|
|
|
|
|
|
int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) {
|
|
|
|
|
|
|
|
ERR_FAIL_NULL_V(p_obj, 0);
|
|
|
|
|
|
|
|
_ArrayVariantSortCustom less;
|
|
|
|
less.obj = p_obj;
|
|
|
|
less.func = p_function;
|
|
|
|
|
|
|
|
return bisect(_p->array, p_value, p_before, less);
|
|
|
|
}
|
|
|
|
|
2017-10-07 23:49:23 +08:00
|
|
|
Array &Array::invert() {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
_p->array.invert();
|
2017-10-07 23:49:23 +08:00
|
|
|
return *this;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Array::push_front(const Variant &p_value) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
_p->array.insert(0, p_value);
|
2015-12-12 19:27:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Variant Array::pop_back() {
|
2015-12-12 19:27:30 +08:00
|
|
|
|
2016-08-27 23:33:45 +08:00
|
|
|
if (!_p->array.empty()) {
|
|
|
|
int n = _p->array.size() - 1;
|
|
|
|
Variant ret = _p->array.get(n);
|
|
|
|
_p->array.resize(n);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return Variant();
|
2015-12-12 19:27:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Variant Array::pop_front() {
|
2016-08-27 23:33:45 +08:00
|
|
|
|
|
|
|
if (!_p->array.empty()) {
|
|
|
|
Variant ret = _p->array.get(0);
|
2015-12-12 19:27:30 +08:00
|
|
|
_p->array.remove(0);
|
2016-08-27 23:33:45 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return Variant();
|
2015-12-12 19:27:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Array::Array(const Array &p_from) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
_p = NULL;
|
2014-02-10 09:10:30 +08:00
|
|
|
_ref(p_from);
|
|
|
|
}
|
2017-01-11 19:53:31 +08:00
|
|
|
Array::Array() {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
_p = memnew(ArrayPrivate);
|
2014-02-10 09:10:30 +08:00
|
|
|
_p->refcount.init();
|
|
|
|
}
|
|
|
|
Array::~Array() {
|
|
|
|
|
|
|
|
_unref();
|
|
|
|
}
|