From 907fc524c6f210659b374c21dc2629c5ae176843 Mon Sep 17 00:00:00 2001 From: Robert Chisholm Date: Tue, 10 Dec 2024 23:02:11 +0000 Subject: [PATCH] Fix: AgentVector::internal_resize() would reduce size without updating size() Noticed this whilst resolving it's copy-paste comment, no evidence it was used in this manner. Full test suite still passes. --- include/flamegpu/simulation/AgentVector.h | 7 ++++--- src/flamegpu/simulation/AgentVector.cpp | 9 ++------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/include/flamegpu/simulation/AgentVector.h b/include/flamegpu/simulation/AgentVector.h index e840c944a..9d61a2974 100644 --- a/include/flamegpu/simulation/AgentVector.h +++ b/include/flamegpu/simulation/AgentVector.h @@ -600,9 +600,10 @@ class AgentVector { */ virtual void _requireLength() const { } /** - * Notify any subclasses that all variables are about to be accessed - * Should be called by operations which move agents (e.g. insert/erase) - * @note This is not called in conjunction with _insert() or _erase() + * Resize the capacity of the AgentVector + * @param count The new capacity of the agent vector + * @param init Whether any new agents should be default init + * @note If count < size() agent data will be lost */ void internal_resize(size_type count, bool init); /** diff --git a/src/flamegpu/simulation/AgentVector.cpp b/src/flamegpu/simulation/AgentVector.cpp index 6b73d3aaa..426a983fa 100644 --- a/src/flamegpu/simulation/AgentVector.cpp +++ b/src/flamegpu/simulation/AgentVector.cpp @@ -543,13 +543,6 @@ void AgentVector::internal_resize(size_type count, bool init) { // Need to create the variable's vector auto t = std::unique_ptr(v.second.memory_vector->clone()); t->resize(count); - // Default init all new elements - if (init) { - char* t_data = static_cast(t->getDataPtr()); - for (unsigned int i = 0; i < count; ++i) { - memcpy(t_data + i * variable_size, v.second.default_value, variable_size); - } - } _data->emplace(v.first, std::move(t)); } else { // Need to resize the variables vector @@ -561,6 +554,8 @@ void AgentVector::internal_resize(size_type count, bool init) { // Default init all new elements if (init && count > old_capacity) { this->init(old_capacity, _capacity); + } else if (count < old_capacity) { + _size = count; } } void AgentVector::swap(AgentVector& other) noexcept {