From e0f30db951fcedd0ec51c680bd60a2157bd355a6 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 16 May 2021 20:07:14 +0900 Subject: [PATCH 1/2] Fix bug using callForMatchingFunction(s) Fixed bug where invalid entity ids were used when using the callForMatchingFunction(s) functions. --- src/EC/Manager.hpp | 93 ++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index 46e7b72..8e4b08c 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -888,7 +888,7 @@ namespace EC std::vector matching, void* context) { - if(threadCount <= 1) + if(threadCount <= 1 || matching.size() < threadCount) { for(auto eid : matching) { @@ -907,24 +907,21 @@ namespace EC { std::size_t begin = s * i; std::size_t end; - if(i == threadCount - 1) - { + if(i == threadCount - 1) { end = matching.size(); - } - else - { + } else { end = s * (i + 1); } threads[i] = std::thread( - [this, &function, &helper, &context] + [this, &function, &helper, &context, &matching] (std::size_t begin, std::size_t end) { - for(std::size_t i = begin; i < end; ++i) + for(std::size_t j = begin; j < end; ++j) { - if(isAlive(i)) + if(isAlive(matching[j])) { helper.callInstancePtr( - i, *this, &function, context); + matching[j], *this, &function, context); } } }, @@ -946,7 +943,7 @@ namespace EC { std::vector > matchingV(bitsets.size()); - if(threadCount <= 1) + if(threadCount <= 1 || currentSize <= threadCount) { for(std::size_t i = 0; i < currentSize; ++i) { @@ -969,44 +966,60 @@ namespace EC std::vector threads(threadCount); std::size_t s = currentSize / threadCount; std::mutex mutex; - for(std::size_t i = 0; i < threadCount; ++i) - { - std::size_t begin = s * i; - std::size_t end; - if(i == threadCount - 1) - { - end = currentSize; - } - else - { - end = s * (i + 1); - } - threads[i] = std::thread( - [this, &matchingV, &bitsets, &mutex] - (std::size_t begin, std::size_t end) - { - for(std::size_t j = begin; j < end; ++j) - { - if(!isAlive(j)) - { - continue; + + if(s == 0) { + for(std::size_t i = 0; i < currentSize; ++i) { + threads[i] = std::thread( + [this, &matchingV, &bitsets, &mutex] (std::size_t idx) { + if(!isAlive(idx)) { + return; } for(std::size_t k = 0; k < bitsets.size(); ++k) { if(((*bitsets[k]) & - std::get(entities[j])) - == (*bitsets[k])) + std::get(entities[idx])) + == (*bitsets[k])) { std::lock_guard guard(mutex); - matchingV[k].push_back(j); + matchingV[k].push_back(idx); } } + }, i); + } + for(std::size_t i = 0; i < currentSize; ++i) { + threads[i].join(); + } + } else { + for (std::size_t i = 0; i < threadCount; ++i) { + std::size_t begin = s * i; + std::size_t end; + if (i == threadCount - 1) { + end = currentSize; + } else { + end = s * (i + 1); } - }, begin, end); - } - for(std::size_t i = 0; i < threadCount; ++i) - { - threads[i].join(); + + threads[i] = std::thread( + [this, &matchingV, &bitsets, &mutex] + (std::size_t begin, std::size_t end) { + for (std::size_t j = begin; j < end; ++j) { + if (!isAlive(j)) { + continue; + } + for (std::size_t k = 0; k < bitsets.size(); ++k) { + if (((*bitsets[k]) & + std::get(entities[j])) + == (*bitsets[k])) { + std::lock_guard guard(mutex); + matchingV[k].push_back(j); + } + } + } + }, begin, end); + } + for (std::size_t i = 0; i < threadCount; ++i) { + threads[i].join(); + } } } From f9c40984c70a768c966a671cc493115907aa07ef Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 16 May 2021 20:08:06 +0900 Subject: [PATCH 2/2] Update LICENSE year --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index c5f8ac2..4673721 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016-2019 Stephen Seo +Copyright (c) 2016-2019,2021 Stephen Seo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal