diff --git a/src/core/particle_data.cpp b/src/core/particle_data.cpp index a185fa71eff..bf1d9127394 100644 --- a/src/core/particle_data.cpp +++ b/src/core/particle_data.cpp @@ -1142,39 +1142,49 @@ void init_type_map(int type) { if (type < 0) throw std::runtime_error("Types may not be negative"); - // fill particle map - if (particle_type_map.count(type) == 0) - particle_type_map[type] = std::unordered_set(); - + auto &map_for_type = particle_type_map[type]; + map_for_type.clear(); for (auto const &p : partCfg()) { if (p.p.type == type) - particle_type_map.at(type).insert(p.p.identity); + map_for_type.insert(p.p.identity); } } void remove_id_from_map(int part_id, int type) { - if (particle_type_map.find(type) != particle_type_map.end()) - particle_type_map.at(type).erase(part_id); + auto it = particle_type_map.find(type); + if (it != particle_type_map.end()) + it->second.erase(part_id); } int get_random_p_id(int type, int random_index_in_type_map) { - if (random_index_in_type_map + 1 > particle_type_map.at(type).size()) + auto it = particle_type_map.find(type); + if (it == particle_type_map.end()) { + throw std::runtime_error("The provided particle type " + + std::to_string(type) + + " is currently not tracked by the system."); + } + + if (random_index_in_type_map + 1 > it->second.size()) throw std::runtime_error("The provided index exceeds the number of " "particle types listed in the particle_type_map"); - return *std::next(particle_type_map[type].begin(), random_index_in_type_map); + return *std::next(it->second.begin(), random_index_in_type_map); } void add_id_to_type_map(int part_id, int type) { - if (particle_type_map.find(type) != particle_type_map.end()) - particle_type_map.at(type).insert(part_id); + auto it = particle_type_map.find(type); + if (it != particle_type_map.end()) + it->second.insert(part_id); } int number_of_particles_with_type(int type) { - if (particle_type_map.count(type) == 0) + auto it = particle_type_map.find(type); + if (it == particle_type_map.end()) { throw std::runtime_error("The provided particle type " + std::to_string(type) + " is currently not tracked by the system."); - return static_cast(particle_type_map.at(type).size()); + } + + return static_cast(it->second.size()); } // The following functions are used by the python interface to obtain