diff --git a/MeshLib/MeshSubsets.h b/MeshLib/MeshSubsets.h index d59255528bde04b8b21f1a3791dad67a5b22eeab..f806b1dfb5a32c34e99d9fbf7f3dd360cc196210 100644 --- a/MeshLib/MeshSubsets.h +++ b/MeshLib/MeshSubsets.h @@ -15,6 +15,7 @@ #include <vector> #include <algorithm> +#include <stdexcept> #include "MeshLib/MeshSubset.h" @@ -35,10 +36,15 @@ public: /// Construct MeshSubsets from a range of MeshSubset. InputIterator must /// dereference to MeshSubset*. + /// \pre All meshes of each of the MeshSubset objects must unique, + /// an exception is thrown otherwise. template <typename InputIterator> MeshSubsets(InputIterator const& first, InputIterator const& last) : _mesh_subsets(first, last) { + if (!areMeshSubsetMeshesUnique()) + throw std::logic_error("Mesh ids of input mesh subsets are not unique."); + _n_total_items = std::accumulate(first, last, 0u, [](std::size_t const& sum, MeshSubset const* const mesh_subset) { @@ -64,6 +70,20 @@ public: return *_mesh_subsets[mesh_index]; } +private: + /// returns true if all mesh ids of _mesh_subsets elements are different. + bool areMeshSubsetMeshesUnique() const + { + std::vector<std::size_t> mesh_ids; + std::transform(_mesh_subsets.cbegin(), _mesh_subsets.cend(), + std::back_inserter(mesh_ids), std::mem_fun(&MeshSubset::getMeshID)); + + std::sort(mesh_ids.begin(), mesh_ids.end()); + auto const it = std::adjacent_find(mesh_ids.cbegin(), mesh_ids.cend()); + + return mesh_ids.cend() == it; + } + private: std::vector<const MeshSubset*> _mesh_subsets;