Rename _size to _data_size in MLPPMatrix.

This commit is contained in:
Relintai 2023-01-26 17:16:16 +01:00
parent 5029a6e8ea
commit 1718f33c84

View File

@ -6,6 +6,7 @@
#include "core/containers/vector.h" #include "core/containers/vector.h"
#include "core/error/error_macros.h" #include "core/error/error_macros.h"
#include "core/os/memory.h" #include "core/os/memory.h"
#include "core/math/vector2i.h"
#include "core/object/reference.h" #include "core/object/reference.h"
@ -24,38 +25,38 @@ public:
} }
_FORCE_INLINE_ void push_back(double p_elem) { _FORCE_INLINE_ void push_back(double p_elem) {
++_size; ++_data_size;
_data = (double *)memrealloc(_data, _size * sizeof(double)); _data = (double *)memrealloc(_data, _data_size * sizeof(double));
CRASH_COND_MSG(!_data, "Out of memory"); CRASH_COND_MSG(!_data, "Out of memory");
_data[_size - 1] = p_elem; _data[_data_size - 1] = p_elem;
} }
void remove(double p_index) { void remove(double p_index) {
ERR_FAIL_INDEX(p_index, _size); ERR_FAIL_INDEX(p_index, _data_size);
--_size; --_data_size;
for (int i = p_index; i < _size; i++) { for (int i = p_index; i < _data_size; i++) {
_data[i] = _data[i + 1]; _data[i] = _data[i + 1];
} }
_data = (double *)memrealloc(_data, _size * sizeof(double)); _data = (double *)memrealloc(_data, _data_size * sizeof(double));
CRASH_COND_MSG(!_data, "Out of memory"); CRASH_COND_MSG(!_data, "Out of memory");
} }
// Removes the item copying the last value into the position of the one to // Removes the item copying the last value into the position of the one to
// remove. It's generally faster than `remove`. // remove. It's generally faster than `remove`.
void remove_unordered(int p_index) { void remove_unordered(int p_index) {
ERR_FAIL_INDEX(p_index, _size); ERR_FAIL_INDEX(p_index, _data_size);
_size--; _data_size--;
if (_size > p_index) { if (_data_size > p_index) {
_data[p_index] = _data[_size]; _data[p_index] = _data[_data_size];
} }
_data = (double *)memrealloc(_data, _size * sizeof(double)); _data = (double *)memrealloc(_data, _data_size * sizeof(double));
CRASH_COND_MSG(!_data, "Out of memory"); CRASH_COND_MSG(!_data, "Out of memory");
} }
@ -83,8 +84,8 @@ public:
} }
void invert() { void invert() {
for (int i = 0; i < _size / 2; i++) { for (int i = 0; i < _data_size / 2; i++) {
SWAP(_data[i], _data[_size - i - 1]); SWAP(_data[i], _data[_data_size - i - 1]);
} }
} }
@ -94,66 +95,66 @@ public:
if (_data) { if (_data) {
memfree(_data); memfree(_data);
_data = NULL; _data = NULL;
_size = 0; _data_size = 0;
} }
} }
_FORCE_INLINE_ bool empty() const { return _size == 0; } _FORCE_INLINE_ bool empty() const { return _data_size == 0; }
_FORCE_INLINE_ int size() const { return _size; } _FORCE_INLINE_ int size() const { return _data_size; }
void resize(int p_size) { void resize(int p_size) {
_size = p_size; _data_size = p_size;
_data = (double *)memrealloc(_data, _size * sizeof(double)); _data = (double *)memrealloc(_data, _data_size * sizeof(double));
CRASH_COND_MSG(!_data, "Out of memory"); CRASH_COND_MSG(!_data, "Out of memory");
} }
_FORCE_INLINE_ const double &operator[](int p_index) const { _FORCE_INLINE_ const double &operator[](int p_index) const {
CRASH_BAD_UNSIGNED_INDEX(p_index, _size); CRASH_BAD_UNSIGNED_INDEX(p_index, _data_size);
return _data[p_index]; return _data[p_index];
} }
_FORCE_INLINE_ double &operator[](int p_index) { _FORCE_INLINE_ double &operator[](int p_index) {
CRASH_BAD_UNSIGNED_INDEX(p_index, _size); CRASH_BAD_UNSIGNED_INDEX(p_index, _data_size);
return _data[p_index]; return _data[p_index];
} }
_FORCE_INLINE_ double get_element(int p_index) const { _FORCE_INLINE_ double get_element(int p_index) const {
CRASH_BAD_UNSIGNED_INDEX(p_index, _size); CRASH_BAD_UNSIGNED_INDEX(p_index, _data_size);
return _data[p_index]; return _data[p_index];
} }
_FORCE_INLINE_ double get_element(int p_index) { _FORCE_INLINE_ double get_element(int p_index) {
CRASH_BAD_UNSIGNED_INDEX(p_index, _size); CRASH_BAD_UNSIGNED_INDEX(p_index, _data_size);
return _data[p_index]; return _data[p_index];
} }
_FORCE_INLINE_ real_t get_element_bind(int p_index) const { _FORCE_INLINE_ real_t get_element_bind(int p_index) const {
CRASH_BAD_UNSIGNED_INDEX(p_index, _size); CRASH_BAD_UNSIGNED_INDEX(p_index, _data_size);
return static_cast<real_t>(_data[p_index]); return static_cast<real_t>(_data[p_index]);
} }
_FORCE_INLINE_ void set_element(int p_index, double p_val) { _FORCE_INLINE_ void set_element(int p_index, double p_val) {
CRASH_BAD_UNSIGNED_INDEX(p_index, _size); CRASH_BAD_UNSIGNED_INDEX(p_index, _data_size);
_data[p_index] = p_val; _data[p_index] = p_val;
} }
_FORCE_INLINE_ void set_element_bind(int p_index, real_t p_val) { _FORCE_INLINE_ void set_element_bind(int p_index, real_t p_val) {
CRASH_BAD_UNSIGNED_INDEX(p_index, _size); CRASH_BAD_UNSIGNED_INDEX(p_index, _data_size);
_data[p_index] = p_val; _data[p_index] = p_val;
} }
void fill(double p_val) { void fill(double p_val) {
for (int i = 0; i < _size; i++) { for (int i = 0; i < _data_size; i++) {
_data[i] = p_val; _data[i] = p_val;
} }
} }
void insert(int p_pos, double p_val) { void insert(int p_pos, double p_val) {
ERR_FAIL_INDEX(p_pos, _size + 1); ERR_FAIL_INDEX(p_pos, _data_size + 1);
if (p_pos == _size) { if (p_pos == _data_size) {
push_back(p_val); push_back(p_val);
} else { } else {
resize(_size + 1); resize(_data_size + 1);
for (int i = _size - 1; i > p_pos; i--) { for (int i = _data_size - 1; i > p_pos; i--) {
_data[i] = _data[i - 1]; _data[i] = _data[i - 1];
} }
_data[p_pos] = p_val; _data[p_pos] = p_val;
@ -161,7 +162,7 @@ public:
} }
int find(const double &p_val, int p_from = 0) const { int find(const double &p_val, int p_from = 0) const {
for (int i = p_from; i < _size; i++) { for (int i = p_from; i < _data_size; i++) {
if (_data[i] == p_val) { if (_data[i] == p_val) {
return i; return i;
} }
@ -171,7 +172,7 @@ public:
template <class C> template <class C>
void sort_custom() { void sort_custom() {
int len = _size; int len = _data_size;
if (len == 0) { if (len == 0) {
return; return;
} }
@ -186,7 +187,7 @@ public:
void ordered_insert(double p_val) { void ordered_insert(double p_val) {
int i; int i;
for (i = 0; i < _size; i++) { for (i = 0; i < _data_size; i++) {
if (p_val < _data[i]) { if (p_val < _data[i]) {
break; break;
} }
@ -198,7 +199,7 @@ public:
Vector<double> ret; Vector<double> ret;
ret.resize(size()); ret.resize(size());
double *w = ret.ptrw(); double *w = ret.ptrw();
memcpy(w, _data, sizeof(double) * _size); memcpy(w, _data, sizeof(double) * _data_size);
return ret; return ret;
} }
@ -218,9 +219,9 @@ public:
Vector<uint8_t> to_byte_array() const { Vector<uint8_t> to_byte_array() const {
Vector<uint8_t> ret; Vector<uint8_t> ret;
ret.resize(_size * sizeof(double)); ret.resize(_data_size * sizeof(double));
uint8_t *w = ret.ptrw(); uint8_t *w = ret.ptrw();
memcpy(w, _data, sizeof(double) * _size); memcpy(w, _data, sizeof(double) * _data_size);
return ret; return ret;
} }
@ -235,7 +236,7 @@ public:
_FORCE_INLINE_ void set_from_mlpp_matrixr(const MLPPMatrix &p_from) { _FORCE_INLINE_ void set_from_mlpp_matrixr(const MLPPMatrix &p_from) {
resize(p_from.size()); resize(p_from.size());
for (int i = 0; i < p_from._size; i++) { for (int i = 0; i < p_from._data_size; i++) {
_data[i] = p_from._data[i]; _data[i] = p_from._data[i];
} }
} }
@ -259,7 +260,7 @@ public:
_FORCE_INLINE_ void set_from_vector(const Vector<double> &p_from) { _FORCE_INLINE_ void set_from_vector(const Vector<double> &p_from) {
resize(p_from.size()); resize(p_from.size());
for (int i = 0; i < _size; i++) { for (int i = 0; i < _data_size; i++) {
_data[i] = p_from[i]; _data[i] = p_from[i];
} }
} }
@ -267,42 +268,42 @@ public:
_FORCE_INLINE_ void set_from_pool_vector(const PoolRealArray &p_from) { _FORCE_INLINE_ void set_from_pool_vector(const PoolRealArray &p_from) {
resize(p_from.size()); resize(p_from.size());
typename PoolRealArray::Read r = p_from.read(); typename PoolRealArray::Read r = p_from.read();
for (int i = 0; i < _size; i++) { for (int i = 0; i < _data_size; i++) {
_data[i] = r[i]; _data[i] = r[i];
} }
} }
_FORCE_INLINE_ MLPPMatrix() { _FORCE_INLINE_ MLPPMatrix() {
_size = 0; _data_size = 0;
_data = NULL; _data = NULL;
} }
_FORCE_INLINE_ MLPPMatrix(const MLPPMatrix &p_from) { _FORCE_INLINE_ MLPPMatrix(const MLPPMatrix &p_from) {
_size = 0; _data_size = 0;
_data = NULL; _data = NULL;
resize(p_from.size()); resize(p_from.size());
for (int i = 0; i < p_from._size; i++) { for (int i = 0; i < p_from._data_size; i++) {
_data[i] = p_from._data[i]; _data[i] = p_from._data[i];
} }
} }
MLPPMatrix(const Vector<double> &p_from) { MLPPMatrix(const Vector<double> &p_from) {
_size = 0; _data_size = 0;
_data = NULL; _data = NULL;
resize(p_from.size()); resize(p_from.size());
for (int i = 0; i < _size; i++) { for (int i = 0; i < _data_size; i++) {
_data[i] = p_from[i]; _data[i] = p_from[i];
} }
} }
MLPPMatrix(const PoolRealArray &p_from) { MLPPMatrix(const PoolRealArray &p_from) {
_size = 0; _data_size = 0;
_data = NULL; _data = NULL;
resize(p_from.size()); resize(p_from.size());
typename PoolRealArray::Read r = p_from.read(); typename PoolRealArray::Read r = p_from.read();
for (int i = 0; i < _size; i++) { for (int i = 0; i < _data_size; i++) {
_data[i] = r[i]; _data[i] = r[i];
} }
} }
@ -318,23 +319,23 @@ public:
std::vector<double> ret; std::vector<double> ret;
ret.resize(size()); ret.resize(size());
double *w = &ret[0]; double *w = &ret[0];
memcpy(w, _data, sizeof(double) * _size); memcpy(w, _data, sizeof(double) * _data_size);
return ret; return ret;
} }
_FORCE_INLINE_ void set_from_std_vector(const std::vector<double> &p_from) { _FORCE_INLINE_ void set_from_std_vector(const std::vector<double> &p_from) {
resize(p_from.size()); resize(p_from.size());
for (int i = 0; i < _size; i++) { for (int i = 0; i < _data_size; i++) {
_data[i] = p_from[i]; _data[i] = p_from[i];
} }
} }
MLPPMatrix(const std::vector<double> &p_from) { MLPPMatrix(const std::vector<double> &p_from) {
_size = 0; _data_size = 0;
_data = NULL; _data = NULL;
resize(p_from.size()); resize(p_from.size());
for (int i = 0; i < _size; i++) { for (int i = 0; i < _data_size; i++) {
_data[i] = p_from[i]; _data[i] = p_from[i];
} }
} }
@ -343,7 +344,7 @@ protected:
static void _bind_methods(); static void _bind_methods();
protected: protected:
int _size; int _data_size;
double *_data; double *_data;
}; };