From 7836d40168f98cad2e8d02de11b7cd89bf5a863f Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@unibas.ch>
Date: Sat, 27 Aug 2016 17:20:15 +0200
Subject: [PATCH] make input graph of GenerateMinimalWidthTree const to avoid
 confusion

---
 core/src/tree.cc     |  7 +++++--
 core/src/tree.hh     | 12 +++++++++++-
 modelling/src/mtm.cc |  3 +--
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/core/src/tree.cc b/core/src/tree.cc
index f4f4843b..4bf941d4 100644
--- a/core/src/tree.cc
+++ b/core/src/tree.cc
@@ -7,13 +7,16 @@
 
 namespace promod3{ namespace core{
 
-TreeBag<int>* GenerateMinimalWidthTree(Graph& graph){
+TreeBag<int>* GenerateMinimalWidthTree(const Graph& input_graph){
 
-  if(!graph.IsConnected()){
+  if(!input_graph.IsConnected()){
     String err = "Graph must be connected to generate minimal width tree!";
     throw promod3::Error(err);
   }
 
+  //generate copy of graph since it will be modified
+  Graph graph(input_graph);
+
   //bags, that will be produced in a first stage of the algorithm
   std::stack<TreeBag<int>*> bags;
 
diff --git a/core/src/tree.hh b/core/src/tree.hh
index 40d26931..c8a1628c 100644
--- a/core/src/tree.hh
+++ b/core/src/tree.hh
@@ -10,6 +10,13 @@
 
 namespace promod3 { namespace core {
 
+//If you're brave enough to use the TreeBag class, you can build up any tree
+//structure you like. This is done by starting from a root node and attaching 
+//children to it. IMPORTANT: if you want to delete such a tree you only have
+//to call delete on the root, the destructor will then recuresively be called
+//for all children. This might be dangerous if you want to delete a single bag.
+//Make sure that you call ClearChildren before you delete it to avoid deleting 
+//other connected bags.
 
 template<class ITEM>
 class TreeBag{
@@ -61,6 +68,9 @@ public:
                      member) != members_.end(); 
   }
 
+  ITEM& operator [] (int idx) { return members_[idx]; } 
+  const ITEM& operator [] (int idx) const { return members_[idx]; } 
+
   typedef typename std::vector<TreeBag<ITEM>*>::iterator child_iterator;
   typedef typename std::vector<TreeBag<ITEM>*>::const_iterator const_child_iterator;
 
@@ -136,7 +146,7 @@ std::vector<TreeBag<T>* > PostOrderTraversal(TreeBag<T>* root){
 }
 
 
-TreeBag<int>* GenerateMinimalWidthTree(Graph& graph);
+TreeBag<int>* GenerateMinimalWidthTree(const Graph& graph);
 
 }} //ns
 
diff --git a/modelling/src/mtm.cc b/modelling/src/mtm.cc
index 0b3967f9..07064cf5 100644
--- a/modelling/src/mtm.cc
+++ b/modelling/src/mtm.cc
@@ -470,8 +470,7 @@ std::vector<int> TreeResolve(promod3::modelling::ModellingHandle& mhandle,
 
   //the graph requires a copy because we still need it later on and
   //the tree decomposition alters the graph
-  promod3::core::Graph decomposition_graph = graph;
-  TreeBag* tree = promod3::core::GenerateMinimalWidthTree(decomposition_graph);
+  TreeBag* tree = promod3::core::GenerateMinimalWidthTree(graph);
 
   //generate post order traversal of previously constructed tree
   std::vector<TreeBag*> traversal = promod3::core::PostOrderTraversal(tree);
-- 
GitLab