11#include " openmc/tallies/filter_meshmaterial.h"
22
3+ #include < utility> // for move
4+
35#include < fmt/core.h>
46
57#include " openmc/capi.h"
68#include " openmc/constants.h"
9+ #include " openmc/container_util.h"
710#include " openmc/error.h"
811#include " openmc/material.h"
912#include " openmc/mesh.h"
@@ -30,8 +33,9 @@ void MeshMaterialFilter::from_xml(pugi::xml_node node)
3033
3134 // Get pairs of (element index, material)
3235 auto bins = get_node_array<int32_t >(node, " bins" );
33- if (bins.size () % 2 == 0 ) {
34- fatal_error (fmt::format (" Size of mesh material bins is not even: {}" , bins.size ()));
36+ if (bins.size () % 2 != 0 ) {
37+ fatal_error (
38+ fmt::format (" Size of mesh material bins is not even: {}" , bins.size ()));
3539 }
3640
3741 // Convert into vector of ElementMat
@@ -41,35 +45,34 @@ void MeshMaterialFilter::from_xml(pugi::xml_node node)
4145 int32_t mat_id = bins[2 * i + 1 ];
4246 auto search = model::material_map.find (mat_id);
4347 if (search == model::material_map.end ()) {
44- throw std::runtime_error { fmt::format (
45- " Could not find material {} specified on tally filter." , mat_id)} ;
48+ fatal_error ( fmt::format (
49+ " Could not find material {} specified on tally filter." , mat_id)) ;
4650 }
4751 int32_t mat_index = search->second ;
4852 element_mats.push_back ({element, mat_index});
4953 }
5054
51- this ->set_bins (element_mats);
55+ this ->set_bins (std::move ( element_mats) );
5256
5357 if (check_for_node (node, " translation" )) {
5458 set_translation (get_node_array<double >(node, " translation" ));
5559 }
5660}
5761
58- void MeshMaterialFilter::set_bins (span <ElementMat> bins)
62+ void MeshMaterialFilter::set_bins (vector <ElementMat>&& bins)
5963{
60- // Clear existing cells
61- bins_.clear ();
62- bins_.reserve (bins.size ());
64+ // Swap internal bins_ with the provided vector to avoid copying
65+ bins_.swap (bins);
66+
67+ // Clear and update the mapping and vector of materials
6368 materials_.clear ();
6469 map_.clear ();
65-
66- // Update cells and mapping
67- for (auto & x : bins) {
70+ for (std::size_t i = 0 ; i < bins_.size (); ++i) {
71+ const auto & x = bins_[i];
6872 assert (x.index_mat >= 0 );
6973 assert (x.index_mat < model::materials.size ());
70- bins_.push_back (x);
7174 materials_.insert (x.index_mat );
72- map_[x] = bins_. size () - 1 ;
75+ map_[x] = i ;
7376 }
7477
7578 n_bins_ = bins_.size ();
@@ -118,7 +121,7 @@ void MeshMaterialFilter::get_all_bins(
118121 }
119122 } else {
120123 // If current material is not in any bins, don't bother checking
121- if (contains (materials_, p.material ()) {
124+ if (! contains (materials_, p.material () )) {
122125 return ;
123126 }
124127
0 commit comments