From fa9c9435ff354c1e610e31aca31fb0a7b789ec6e Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 29 Oct 2019 12:56:53 +0100
Subject: [PATCH] add xref for equation tags

---
 src/ModelTree.cc |  7 ++++++-
 src/ModelTree.hh | 20 +++++++++++++++++---
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index bd34e832..9a6af076 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -100,6 +100,7 @@ ModelTree::ModelTree(const ModelTree &m) :
   DataTree {m},
   equations_lineno {m.equations_lineno},
   equation_tags {m.equation_tags},
+  equation_tags_xref {m.equation_tags_xref},
   NNZDerivatives {m.NNZDerivatives},
   equation_reordered {m.equation_reordered},
   variable_reordered {m.variable_reordered},
@@ -125,6 +126,7 @@ ModelTree::operator=(const ModelTree &m)
   equations_lineno = m.equations_lineno;
   aux_equations.clear();
   equation_tags = m.equation_tags;
+  equation_tags_xref = m.equation_tags_xref;
   NNZDerivatives = m.NNZDerivatives;
 
   derivatives.clear();
@@ -1956,7 +1958,10 @@ ModelTree::addEquation(expr_t eq, int lineno, const vector<pair<string, string>>
 {
   int n = equations.size();
   for (const auto & eq_tag : eq_tags)
-    equation_tags.emplace_back(n, eq_tag);
+    {
+      equation_tags.emplace_back(n, eq_tag);
+      equation_tags_xref.emplace(eq_tag, n);
+    }
   addEquation(eq, lineno);
 }
 
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 36e3b7f3..2a15f955 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -65,12 +65,29 @@ class ModelTree : public DataTree
   friend class DynamicModel;
   friend class StaticModel;
 protected:
+  /*
+   * ************** BEGIN **************
+   * The following structures keep track of the model equations and must all be updated
+   * when adding or removing an equation. Hence, if a new parallel structure is added
+   * in the future, it must be maintained whereever these structures are updated
+   * NB: This message added with the introduction of the `exclude_eqs` option, hence
+   *     that's a place to update future structures.
+   */
   //! Stores declared and generated auxiliary equations
   vector<BinaryOpNode *> equations;
 
   //! Stores line numbers of declared equations; -1 means undefined
   vector<int> equations_lineno;
 
+  //! Stores equation tags
+  vector<pair<int, pair<string, string>>> equation_tags;
+
+  //! Stores mapping from equation tags to equation number
+  multimap<pair<string, string>, int> equation_tags_xref;
+  /*
+   * ************** END **************
+   */
+
   //! Only stores generated auxiliary equations, in an order meaningful for evaluation
   /*! These equations only contain the definition of auxiliary variables, and
       may diverge from those in the main model (equations), if other model
@@ -83,9 +100,6 @@ protected:
       tests/optimal_policy/nk_ramsey_expectation.mod */
   deque<BinaryOpNode *> aux_equations;
 
-  //! Stores equation tags
-  vector<pair<int, pair<string, string>>> equation_tags;
-
   //! Stores derivatives
   /*! Index 0 is not used, index 1 contains first derivatives, ...
      For each derivation order, stores a map whose key is a vector of integer: the
-- 
GitLab