From 4920a07d3845a97e6a13c3b3e0f1079ed2759e7d Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <dmitri.naumov@ufz.de>
Date: Tue, 13 Aug 2013 11:30:46 +0200
Subject: [PATCH] Add MeshSubsets meshes uniqueness precondition in ctor.

---
 MeshLib/MeshSubsets.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/MeshLib/MeshSubsets.h b/MeshLib/MeshSubsets.h
index d59255528bd..f806b1dfb5a 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;
 
-- 
GitLab