2020-02-20 15:29:10 +01:00
|
|
|
/*
|
2022-01-06 14:54:57 +01:00
|
|
|
* Copyright © 2020-2022 Dynare Team
|
2020-02-20 15:29:10 +01:00
|
|
|
*
|
|
|
|
* This file is part of Dynare.
|
|
|
|
*
|
|
|
|
* Dynare is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Dynare is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-06-09 16:52:20 +02:00
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2020-02-20 15:29:10 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _EQUATION_TAGS_HH
|
|
|
|
#define _EQUATION_TAGS_HH
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2020-05-28 14:21:58 +02:00
|
|
|
#include <string>
|
2020-02-20 15:29:10 +01:00
|
|
|
|
2022-01-06 14:54:57 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2020-02-20 15:29:10 +01:00
|
|
|
class EquationTags
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
map<int, map<string, string>> eqn_tags;
|
|
|
|
public:
|
2021-07-01 16:16:04 +02:00
|
|
|
class TagNotFoundException
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const string key, value;
|
|
|
|
explicit TagNotFoundException(string key_arg, string value_arg)
|
|
|
|
: key{move(key_arg)}, value{move(value_arg)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-04 16:19:34 +02:00
|
|
|
// Add multiple equation tags for the given equation
|
2022-06-24 15:08:49 +02:00
|
|
|
void
|
|
|
|
add(int eqn, map<string, string> tags)
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
2022-05-04 16:01:34 +02:00
|
|
|
if (eqn_tags.contains(eqn))
|
2022-06-13 15:06:58 +02:00
|
|
|
eqn_tags[eqn].insert(move_iterator{tags.begin()}, move_iterator{tags.end()});
|
2022-05-04 16:01:34 +02:00
|
|
|
else
|
|
|
|
eqn_tags[eqn] = move(tags);
|
2020-02-20 15:29:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Add a single equation tag for the given equation
|
2022-06-24 15:08:49 +02:00
|
|
|
void
|
|
|
|
add(int eqn, string key, string value)
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
|
|
|
eqn_tags[eqn][move(key)] = move(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Clear all equation tag information
|
2022-06-24 15:08:49 +02:00
|
|
|
void
|
|
|
|
clear()
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
|
|
|
eqn_tags.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Erase tags for given equations, using old_eqn_num_2_new as the mapping
|
|
|
|
//! to use for the remaining equation numbers
|
|
|
|
void erase(const set<int> &eqns, const map<int, int> &old_eqn_num_2_new);
|
|
|
|
|
|
|
|
//! Various functions to get info from equation tags
|
|
|
|
//! Get equation tags for a given equation
|
2022-06-24 15:08:49 +02:00
|
|
|
map<string, string>
|
|
|
|
getTagsByEqn(const int eqn) const
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
2022-05-04 14:42:03 +02:00
|
|
|
if (auto it = eqn_tags.find(eqn); it != eqn_tags.end())
|
|
|
|
return it->second;
|
2020-02-20 15:29:10 +01:00
|
|
|
return map<string, string>{};
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get equations that have the given key
|
|
|
|
set<int> getEqnsByKey(const string &key) const;
|
|
|
|
|
|
|
|
//! Get equations that have the given key and value
|
|
|
|
set<int> getEqnsByTag(const string &key, const string &value) const;
|
|
|
|
|
|
|
|
//! Get the first equation that has the given key and value
|
|
|
|
int getEqnByTag(const string &key, const string &value) const;
|
|
|
|
|
|
|
|
//! Get the tag value given the equation number and key
|
2022-06-24 15:08:49 +02:00
|
|
|
string
|
|
|
|
getTagValueByEqnAndKey(int eqn, const string &key) const
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
|
|
|
return exists(eqn, key) ? eqn_tags.at(eqn).at(key) : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get the equations marked dynamic
|
2022-06-24 15:08:49 +02:00
|
|
|
set<int>
|
|
|
|
getDynamicEqns() const
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
|
|
|
return getEqnsByTag("dynamic", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Returns true if equation tag with key and value exists
|
2022-06-24 15:08:49 +02:00
|
|
|
bool
|
|
|
|
exists(const string &key, const string &value) const
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
2021-07-01 16:16:04 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
getEqnByTag(key, value);
|
|
|
|
}
|
|
|
|
catch (TagNotFoundException &e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-02-20 15:29:10 +01:00
|
|
|
}
|
|
|
|
|
2022-06-24 15:08:49 +02:00
|
|
|
bool
|
|
|
|
exists(const int eqn) const
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
2022-05-04 16:01:34 +02:00
|
|
|
return eqn_tags.contains(eqn);
|
2020-02-20 15:29:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Returns true if equation tag with key exists for a given equation
|
2022-06-24 15:08:49 +02:00
|
|
|
bool
|
|
|
|
exists(const int eqn, const string &key) const
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
2022-05-04 16:01:34 +02:00
|
|
|
return exists(eqn) && eqn_tags.at(eqn).contains(key);
|
2020-02-20 15:29:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Returns true if equation tag with key and value exists for a given equation
|
2022-06-24 15:08:49 +02:00
|
|
|
bool
|
|
|
|
exists(const int eqn, const string &key, const string &value) const
|
2020-02-20 15:29:10 +01:00
|
|
|
{
|
2022-05-04 14:42:03 +02:00
|
|
|
return exists(eqn, key) && eqn_tags.at(eqn).at(key) == value;
|
2020-02-20 15:29:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Various functions to write equation tags
|
|
|
|
void writeCheckSumInfo(ostream &output) const;
|
2021-04-19 14:51:34 +02:00
|
|
|
void writeOutput(ostream &output) const;
|
2020-02-20 15:29:10 +01:00
|
|
|
void writeLatexOutput(ostream &output, int eqn) const;
|
|
|
|
void writeJsonAST(ostream &output, const int eq) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|