Skip to content

Commit

Permalink
Fix: AgentVector::internal_resize() would reduce size without updatin…
Browse files Browse the repository at this point in the history
…g size()

Also remove redundant double initialisation.

Noticed this whilst resolving it's copy-paste comment, no evidence it was used in this manner. Full test suite still passes.
  • Loading branch information
Robadob committed Dec 11, 2024
1 parent c3f3574 commit 9b41358
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
7 changes: 4 additions & 3 deletions include/flamegpu/simulation/AgentVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
/**
Expand Down
10 changes: 2 additions & 8 deletions src/flamegpu/simulation/AgentVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,18 +538,10 @@ void AgentVector::internal_resize(size_type count, bool init) {
for (const auto& v : agent->variables) {
// For each variable inside agent, add it to the map or replace it in the map
const auto it = _data->find(v.first);
const size_t variable_size = v.second.type_size * v.second.elements;
if (it == _data->end()) {
// Need to create the variable's vector
auto t = std::unique_ptr<detail::GenericMemoryVector>(v.second.memory_vector->clone());
t->resize(count);
// Default init all new elements
if (init) {
char* t_data = static_cast<char*>(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
Expand All @@ -561,6 +553,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 {
Expand Down

0 comments on commit 9b41358

Please sign in to comment.