From f59b092d10232435b4f88ba9bda00efa55bcecea Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 6 Feb 2023 12:20:33 +0100 Subject: [PATCH] Added new helper methods to MLPPMatrix and MLPPVector. --- mlpp/lin_alg/mlpp_matrix.cpp | 3 ++ mlpp/lin_alg/mlpp_matrix.h | 68 ++++++++++++++++++++++++++++++++++++ mlpp/lin_alg/mlpp_vector.cpp | 1 + mlpp/lin_alg/mlpp_vector.h | 23 ++++++++++++ 4 files changed, 95 insertions(+) diff --git a/mlpp/lin_alg/mlpp_matrix.cpp b/mlpp/lin_alg/mlpp_matrix.cpp index b6e7931..d8ac2d9 100644 --- a/mlpp/lin_alg/mlpp_matrix.cpp +++ b/mlpp/lin_alg/mlpp_matrix.cpp @@ -24,6 +24,9 @@ String MLPPMatrix::to_string() { void MLPPMatrix::_bind_methods() { ClassDB::bind_method(D_METHOD("add_row", "row"), &MLPPMatrix::add_row_pool_vector); + ClassDB::bind_method(D_METHOD("add_row_mlpp_vector", "row"), &MLPPMatrix::add_row_mlpp_vector); + ClassDB::bind_method(D_METHOD("add_rows_mlpp_matrix", "other"), &MLPPMatrix::add_rows_mlpp_matrix); + ClassDB::bind_method(D_METHOD("remove_row", "index"), &MLPPMatrix::remove_row); ClassDB::bind_method(D_METHOD("remove_row_unordered", "index"), &MLPPMatrix::remove_row_unordered); ClassDB::bind_method(D_METHOD("swap_row", "index_1", "index_2"), &MLPPMatrix::swap_row); diff --git a/mlpp/lin_alg/mlpp_matrix.h b/mlpp/lin_alg/mlpp_matrix.h index b75b8ec..0170340 100644 --- a/mlpp/lin_alg/mlpp_matrix.h +++ b/mlpp/lin_alg/mlpp_matrix.h @@ -30,6 +30,10 @@ public: } _FORCE_INLINE_ void add_row(const Vector &p_row) { + if (p_row.size() == 0) { + return; + } + if (_size.x == 0) { _size.x = p_row.size(); } @@ -51,6 +55,10 @@ public: } _FORCE_INLINE_ void add_row_pool_vector(const PoolRealArray &p_row) { + if (p_row.size() == 0) { + return; + } + if (_size.x == 0) { _size.x = p_row.size(); } @@ -72,6 +80,66 @@ public: } } + _FORCE_INLINE_ void add_row_mlpp_vector(const Ref &p_row) { + ERR_FAIL_COND(!p_row.is_valid()); + + int p_row_size = p_row->size(); + + if (p_row_size == 0) { + return; + } + + if (_size.x == 0) { + _size.x = p_row_size; + } + + ERR_FAIL_COND(_size.x != p_row_size); + + int ci = data_size(); + + ++_size.y; + + _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); + + const real_t *row_ptr = p_row->ptr(); + + for (int i = 0; i < p_row_size; ++i) { + _data[ci + i] = row_ptr[i]; + } + } + + _FORCE_INLINE_ void add_rows_mlpp_matrix(const Ref &p_other) { + ERR_FAIL_COND(!p_other.is_valid()); + + int other_data_size = p_other->data_size(); + + if (other_data_size == 0) { + return; + } + + Size2i other_size = p_other->size(); + + if (_size.x == 0) { + _size.x = other_size.x; + } + + ERR_FAIL_COND(other_size.x != _size.x); + + int start_offset = data_size(); + + _size.y += other_size.y; + + _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); + + const real_t *other_ptr = p_other->ptr(); + + for (int i = 0; i < other_data_size; ++i) { + _data[start_offset + i] = other_ptr[i]; + } + } + void remove_row(real_t p_index) { ERR_FAIL_INDEX(p_index, _size.y); diff --git a/mlpp/lin_alg/mlpp_vector.cpp b/mlpp/lin_alg/mlpp_vector.cpp index 3a151e0..0caa64c 100644 --- a/mlpp/lin_alg/mlpp_vector.cpp +++ b/mlpp/lin_alg/mlpp_vector.cpp @@ -18,6 +18,7 @@ String MLPPVector::to_string() { void MLPPVector::_bind_methods() { ClassDB::bind_method(D_METHOD("push_back", "elem"), &MLPPVector::push_back); + ClassDB::bind_method(D_METHOD("add_mlpp_vector", "other"), &MLPPVector::push_back); ClassDB::bind_method(D_METHOD("remove", "index"), &MLPPVector::remove); ClassDB::bind_method(D_METHOD("remove_unordered", "index"), &MLPPVector::remove_unordered); ClassDB::bind_method(D_METHOD("erase", "val"), &MLPPVector::erase); diff --git a/mlpp/lin_alg/mlpp_vector.h b/mlpp/lin_alg/mlpp_vector.h index fcb7e79..c18a487 100644 --- a/mlpp/lin_alg/mlpp_vector.h +++ b/mlpp/lin_alg/mlpp_vector.h @@ -36,6 +36,29 @@ public: _data[_size - 1] = p_elem; } + _FORCE_INLINE_ void add_mlpp_vector(const Ref &p_other) { + ERR_FAIL_COND(!p_other.is_valid()); + + int other_size = p_other->size(); + + if (other_size == 0) { + return; + } + + int start_offset = _size; + + _size += other_size; + + _data = (real_t *)memrealloc(_data, _size * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); + + const real_t *other_ptr = p_other->ptr(); + + for (int i = 0; i < other_size; ++i) { + _data[start_offset + i] = other_ptr[i]; + } + } + void remove(real_t p_index) { ERR_FAIL_INDEX(p_index, _size);