2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* array.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "array.h"
|
|
|
|
#include "hashfuncs.h"
|
|
|
|
#include "object.h"
|
2017-03-19 00:36:26 +01:00
|
|
|
#include "variant.h"
|
|
|
|
#include "vector.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct ArrayPrivate {
|
|
|
|
|
|
|
|
SafeRefCount refcount;
|
|
|
|
Vector<Variant> array;
|
|
|
|
bool shared;
|
|
|
|
};
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::_ref(const Array &p_from) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ArrayPrivate *_fp = p_from._p;
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!_fp); // should NOT happen.
|
|
|
|
|
|
|
|
if (_fp == _p)
|
|
|
|
return; //wathever it is, nothing to do here move along
|
|
|
|
|
|
|
|
bool success = _fp->refcount.ref();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!success); //should really not happen either
|
|
|
|
|
|
|
|
_unref();
|
|
|
|
|
|
|
|
if (_fp->shared) {
|
|
|
|
|
|
|
|
_p = p_from._p;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
_p = memnew(ArrayPrivate);
|
|
|
|
_p->shared = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
_p->refcount.init();
|
2017-03-19 00:36:26 +01:00
|
|
|
_p->array = _fp->array;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (_fp->refcount.unref())
|
|
|
|
memdelete(_fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Array::_unref() const {
|
|
|
|
|
|
|
|
if (!_p)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_p->refcount.unref()) {
|
|
|
|
memdelete(_p);
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
_p = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Variant &Array::operator[](int p_idx) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return _p->array[p_idx];
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
const Variant &Array::operator[](int p_idx) const {
|
2014-02-10 02:10:30 +01: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();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Array::is_shared() const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return _p->shared;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool Array::operator==(const Array &p_array) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
return _p == p_array._p;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Array::hash() const {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
uint32_t h = hash_djb2_one_32(0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < _p->array.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
h = hash_djb2_one_32(_p->array[i].hash(), h);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::operator=(const Array &p_array) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_ref(p_array);
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::push_back(const Variant &p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_p->array.push_back(p_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error Array::resize(int p_new_size) {
|
|
|
|
|
|
|
|
return _p->array.resize(p_new_size);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::insert(int p_pos, const Variant &p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
_p->array.insert(p_pos, p_value);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::erase(const Variant &p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_p->array.erase(p_value);
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:30:16 +01: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-19 00:36:26 +01:00
|
|
|
int Array::find(const Variant &p_value, int p_from) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-10 19:57:56 +02:00
|
|
|
return _p->array.find(p_value, p_from);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int Array::rfind(const Variant &p_value, int p_from) const {
|
2016-06-03 23:10:43 +02:00
|
|
|
|
2016-06-10 22:28:09 +02:00
|
|
|
if (_p->array.size() == 0)
|
2016-06-03 23:10:43 +02:00
|
|
|
return -1;
|
|
|
|
|
2016-06-10 22:28:09 +02: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-19 00:36:26 +01:00
|
|
|
for (int i = p_from; i >= 0; i--) {
|
2016-06-03 23:10:43 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (_p->array[i] == p_value) {
|
2016-06-03 23:10:43 +02:00
|
|
|
return i;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int Array::find_last(const Variant &p_value) const {
|
2016-06-10 22:28:09 +02:00
|
|
|
|
|
|
|
return rfind(p_value);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int Array::count(const Variant &p_value) const {
|
2016-06-03 23:10:43 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (_p->array.size() == 0)
|
2016-06-03 23:10:43 +02:00
|
|
|
return 0;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int amount = 0;
|
|
|
|
for (int i = 0; i < _p->array.size(); i++) {
|
2016-06-03 23:10:43 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (_p->array[i] == p_value) {
|
2016-06-03 23:10:43 +02:00
|
|
|
amount++;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool Array::has(const Variant &p_value) const {
|
2016-07-02 19:03:35 +02:00
|
|
|
return _p->array.find(p_value, 0) != -1;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Array::remove(int p_pos) {
|
|
|
|
|
|
|
|
_p->array.remove(p_pos);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::set(int p_idx, const Variant &p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
operator[](p_idx) = p_value;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
const Variant &Array::get(int p_idx) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return operator[](p_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct _ArrayVariantSort {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
_FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
|
|
|
|
bool valid = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
Variant res;
|
2017-03-19 00:36:26 +01:00
|
|
|
Variant::evaluate(Variant::OP_LESS, p_l, p_r, res, valid);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!valid)
|
2017-03-19 00:36:26 +01:00
|
|
|
res = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void Array::sort() {
|
|
|
|
|
|
|
|
_p->array.sort_custom<_ArrayVariantSort>();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct _ArrayVariantSortCustom {
|
|
|
|
|
|
|
|
Object *obj;
|
|
|
|
StringName func;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
_FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
const Variant *args[2] = { &p_l, &p_r };
|
2014-02-10 02:10:30 +01:00
|
|
|
Variant::CallError err;
|
2017-03-19 00:36:26 +01:00
|
|
|
bool res = obj->call(func, args, 2, err);
|
|
|
|
if (err.error != Variant::CallError::CALL_OK)
|
|
|
|
res = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::sort_custom(Object *p_obj, const StringName &p_function) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_NULL(p_obj);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
SortArray<Variant, _ArrayVariantSortCustom> avs;
|
|
|
|
avs.compare.obj = p_obj;
|
|
|
|
avs.compare.func = p_function;
|
|
|
|
avs.sort(_p->array.ptr(), _p->array.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::invert() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_p->array.invert();
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::push_front(const Variant &p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
_p->array.insert(0, p_value);
|
2015-12-12 12:27:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::pop_back() {
|
2015-12-12 12:27:30 +01:00
|
|
|
|
|
|
|
if (!_p->array.empty())
|
2017-03-19 00:36:26 +01:00
|
|
|
_p->array.resize(_p->array.size() - 1);
|
2015-12-12 12:27:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void Array::pop_front() {
|
2015-12-12 12:27:30 +01:00
|
|
|
|
|
|
|
if (!_p->array.empty())
|
|
|
|
_p->array.remove(0);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Array::Array(const Array &p_from) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
_p = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
_ref(p_from);
|
|
|
|
}
|
|
|
|
Array::Array(bool p_shared) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
_p = memnew(ArrayPrivate);
|
2014-02-10 02:10:30 +01:00
|
|
|
_p->refcount.init();
|
2017-03-19 00:36:26 +01:00
|
|
|
_p->shared = p_shared;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
Array::~Array() {
|
|
|
|
|
|
|
|
_unref();
|
|
|
|
}
|