Created
May 28, 2017 16:18
-
-
Save gicmo/1cb77c8051aace37f04e68eb2d97e813 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename Container, typename E> | |
void replaceEntities(Container &container, const std::vector<E> &newEntities) { | |
auto cmp = [](const E &a, const E &b) { return a.name() < b.name(); }; | |
std::vector<E> new_entities(newEntities); | |
ObjectType ot = nix::objectToType<E>::type; | |
ndsize_t current = container.entityCount(ot); | |
size_t old_count = nix::check::fits_in_size_t(current, "entityCount() failed; count > size_t."); | |
std::vector<E> old_entities(old_count); | |
for (size_t i = 0; i < old_count; i++) { | |
old_entities[i] = container->getEntity(i, ot); | |
} | |
std::sort(new_entities.begin(), new_entities.end(), cmp); | |
std::sort(old_entities.begin(), old_entities.end(), cmp); | |
std::vector<E> add; | |
std::vector<E> rem; | |
std::set_difference(new_entities.begin(), new_entities.end(), | |
old_tags.begin(), old_tags.end(), | |
std::inserter(add, add.begin()), cmp); | |
std::set_difference(old_tags.begin(), old_tags.end(), | |
new_entities.begin(), new_entities.end(), | |
std::inserter(rem, rem.begin()), cmp); | |
for (const auto &e : add) { | |
E entity = container->getEntity<objectToType<E>::backendType>(e); | |
if (!entity) | |
throw std::runtime_error("One or more entities do not exist in this block!"); | |
addEntity(e); | |
} | |
for (const auto &e : rem) { | |
removeEntity(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment