Dynare++: replace obsolete hash_set and hash_map by unordered_set and unordered_map

- use the implementation from boost
- since these classes will probably be included in the next C++ standard, they are also available natively from g++ with option -std=c++0x, but boost is probably better for the moment
time-shift
Sébastien Villemot 2011-01-14 16:40:59 +01:00
parent 1ca828c134
commit bf13ddcb74
7 changed files with 47 additions and 61 deletions

View File

@ -1,6 +1,4 @@
// Copyright (C) 2005, Ondra Kamenik
// $Id: formula_parser.h 1760 2008-03-31 14:26:35Z kamenik $
// Copyright (C) 2005-2011, Ondra Kamenik
#ifndef OGP_FORMULA_PARSER_H
#define OGP_FORMULA_PARSER_H
@ -193,7 +191,7 @@ namespace ogp {
{otree.nularify(t);}
/** Returns a set of nulary terms of the given term. Just
* calls OperationTree::nulary_of_term. */
const hash_set<int>& nulary_of_term(int t) const
const unordered_set<int>& nulary_of_term(int t) const
{return otree.nulary_of_term(t);}
/** Parse a given string containing one or more formulas. The

View File

@ -1,6 +1,4 @@
// Copyright (C) 2005, Ondra Kamenik
// $Id: tree.cpp 1762 2008-03-31 14:28:54Z kamenik $
// Copyright (C) 2005-2011, Ondra Kamenik
#include "utils/cc/exception.h"
@ -376,14 +374,14 @@ void OperationTree::register_derivative(int t, int v, int tder)
derivatives[t].insert(_Tderivmap::value_type(v, tder));
}
hash_set<int> OperationTree::select_terms(int t, const opselector& sel) const
unordered_set<int> OperationTree::select_terms(int t, const opselector& sel) const
{
hash_set<int> subterms;
unordered_set<int> subterms;
select_terms(t, sel, subterms);
return subterms;
}
void OperationTree::select_terms(int t, const opselector& sel, hash_set<int>& subterms) const
void OperationTree::select_terms(int t, const opselector& sel, unordered_set<int>& subterms) const
{
const Operation& op = terms[t];
@ -398,14 +396,14 @@ void OperationTree::select_terms(int t, const opselector& sel, hash_set<int>& su
}
}
hash_set<int> OperationTree::select_terms_inv(int t, const opselector& sel) const
unordered_set<int> OperationTree::select_terms_inv(int t, const opselector& sel) const
{
hash_set<int> subterms;
unordered_set<int> subterms;
select_terms_inv(t, sel, subterms);
return subterms;
}
bool OperationTree::select_terms_inv(int t, const opselector& sel, hash_set<int>& subterms) const
bool OperationTree::select_terms_inv(int t, const opselector& sel, unordered_set<int>& subterms) const
{
const Operation& op = terms[t];
@ -452,7 +450,7 @@ void OperationTree::print_operation(int t) const
void OperationTree::update_nul_incidence_after_nularify(int t)
{
hash_set<int> updated;
unordered_set<int> updated;
for (int tnode = num_constants; tnode < (int)terms.size(); tnode++) {
const Operation& op = terms[tnode];
if (op.nary() == 2) {

View File

@ -1,6 +1,4 @@
// Copyright (C) 2005, Ondra Kamenik
// $Id: tree.h 1762 2008-03-31 14:28:54Z kamenik $
// Copyright (C) 2005-2011, Ondra Kamenik
#ifndef OGP_TREE_H
#define OGP_TREE_H
@ -8,14 +6,14 @@
#include <vector>
#include <set>
#include <map>
#include <ext/hash_map>
#include <ext/hash_set>
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <cstdio>
namespace ogp {
using __gnu_cxx::hash_set;
using __gnu_cxx::hash_map;
using __gnu_cxx::hash;
using boost::unordered_set;
using boost::unordered_map;
using std::vector;
using std::set;
using std::map;
@ -128,7 +126,7 @@ namespace ogp {
* the caller, since at this level of Operation abstraction, one
* cannot discriminate between different nulary operations
* (constants, variables). The uniqueness is enforced by the
* hash_map whose keys are operations and values are integers
* unordered_map whose keys are operations and values are integers
* (indices of the terms).
* This class can also make derivatives of a given term with
@ -136,7 +134,7 @@ namespace ogp {
* recognize zero derivativates, we maintain a list of nulary
* terms contained in the term. A possible zero derivative is then quickly
* recognized by looking at the list. The list is implemented as a
* hash_set of integers.
* unordered_set of integers.
*
* In addition, many term can be differentiated multiple times wrt
* one variable since they can be referenced multiple times. To
@ -155,7 +153,7 @@ namespace ogp {
/** This defines a type for a map mapping the unary and binary
* operations to their indices. */
typedef hash_map<Operation, int, ophash> _Topmap;
typedef unordered_map<Operation, int, ophash> _Topmap;
typedef _Topmap::value_type _Topval;
/** This is the map mapping the unary and binary operations to
@ -163,14 +161,14 @@ namespace ogp {
_Topmap opmap;
/** This is a type for a set of integers. */
typedef hash_set<int> _Tintset;
typedef unordered_set<int> _Tintset;
/** This is a vector of integer sets corresponding to the
* nulary terms contained in the term. */
vector<_Tintset> nul_incidence;
/** This is a type of the map from variables (nulary terms) to
* the terms. */
typedef hash_map<int, int> _Tderivmap;
typedef unordered_map<int, int> _Tderivmap;
/** This is a vector of derivative mappings. For each term, it
* maps variables to the derivatives of the term with respect
* to the variables. */
@ -258,7 +256,7 @@ namespace ogp {
void nularify(int t);
/** Return the set of nulary terms of the given term. */
const hash_set<int>& nulary_of_term(int t) const
const unordered_set<int>& nulary_of_term(int t) const
{return nul_incidence[t];}
/** Select subterms of the given term according a given
@ -267,7 +265,7 @@ namespace ogp {
* a compound function of the returned subterms and the
* function consists only from operations which yield false in
* the selector. */
hash_set<int> select_terms(int t, const opselector& sel) const;
unordered_set<int> select_terms(int t, const opselector& sel) const;
/** Select subterms of the given term according a given
* operation selector and return the set of terms that
@ -275,7 +273,7 @@ namespace ogp {
* a compound function of the returned subterms and the
* subterms are maximal subterms consisting from operations
* yielding true in the selector. */
hash_set<int> select_terms_inv(int t, const opselector& sel) const;
unordered_set<int> select_terms_inv(int t, const opselector& sel) const;
/** This forgets all the derivative mappings. It is used after
* a term has been nularified, and then the derivative
@ -316,12 +314,12 @@ namespace ogp {
/** This does the same job as select_terms with the only
* difference, that it adds the terms to the given set and
* hence can be used recursivelly. */
void select_terms(int t, const opselector& sel, hash_set<int>& subterms) const;
void select_terms(int t, const opselector& sel, unordered_set<int>& subterms) const;
/** This does the same job as select_terms_inv with the only
* difference, that it adds the terms to the given set and
* hence can be used recursivelly and returns true if the term
* was selected. */
bool select_terms_inv(int t, const opselector& sel, hash_set<int>& subterms) const;
bool select_terms_inv(int t, const opselector& sel, unordered_set<int>& subterms) const;
/** This updates nul_incidence information after the term t
* was turned to a nulary term in all terms. It goes through
* the tree from simplest terms to teh more complex ones and

View File

@ -1,6 +1,4 @@
// Copyright (C) 2006, Ondra Kamenik
// $Id: dynare_model.cpp 2269 2008-11-23 14:33:22Z michel $
// Copyright (C) 2006-2011, Ondra Kamenik
#include "parser/cc/parser_exception.h"
#include "parser/cc/location.h"
@ -196,8 +194,8 @@ void DynareModel::check_model() const
// either constant or assigned to a name
for (int i = 0; i < eqs.nformulas(); i++) {
int ft = eqs.formula(i);
const hash_set<int>& nuls = eqs.nulary_of_term(ft);
for (hash_set<int>::const_iterator it = nuls.begin();
const unordered_set<int>& nuls = eqs.nulary_of_term(ft);
for (unordered_set<int>::const_iterator it = nuls.begin();
it != nuls.end(); ++it)
if (! atoms.is_constant(*it) && ! atoms.is_named_atom(*it))
throw DynareException(__FILE__,__LINE__,
@ -242,11 +240,11 @@ int DynareModel::variable_shift(int t, int tshift)
return res;
}
void DynareModel::variable_shift_map(const hash_set<int>& a_set, int tshift,
void DynareModel::variable_shift_map(const unordered_set<int>& a_set, int tshift,
map<int,int>& s_map)
{
s_map.clear();
for (hash_set<int>::const_iterator it = a_set.begin();
for (unordered_set<int>::const_iterator it = a_set.begin();
it != a_set.end(); ++it) {
int t = *it;
// make shift map only for non-constants and non-parameters
@ -265,8 +263,8 @@ void DynareModel::termspan(int t, int& mlead, int& mlag) const
{
mlead = INT_MIN;
mlag = INT_MAX;
const hash_set<int>& nul_terms = eqs.nulary_of_term(t);
for (hash_set<int>::const_iterator ni = nul_terms.begin();
const unordered_set<int>& nul_terms = eqs.nulary_of_term(t);
for (unordered_set<int>::const_iterator ni = nul_terms.begin();
ni != nul_terms.end(); ++ni) {
if (!atoms.is_constant(*ni) &&
(atoms.is_type(atoms.name(*ni), DynareDynamicAtoms::endovar) ||
@ -282,8 +280,8 @@ void DynareModel::termspan(int t, int& mlead, int& mlag) const
bool DynareModel::is_constant_term(int t) const
{
const hash_set<int>& nul_terms = eqs.nulary_of_term(t);
for (hash_set<int>::const_iterator ni = nul_terms.begin();
const unordered_set<int>& nul_terms = eqs.nulary_of_term(t);
for (unordered_set<int>::const_iterator ni = nul_terms.begin();
ni != nul_terms.end(); ++ni)
if (! atoms.is_constant(*ni) &&
! atoms.is_type(atoms.name(*ni), DynareDynamicAtoms::param))
@ -291,7 +289,7 @@ bool DynareModel::is_constant_term(int t) const
return true;
}
hash_set<int> DynareModel::get_nonlinear_subterms(int t) const
unordered_set<int> DynareModel::get_nonlinear_subterms(int t) const
{
NLSelector nls(*this);
return eqs.getTree().select_terms(t, nls);

View File

@ -1,6 +1,4 @@
// Copyright (C) 2005, Ondra Kamenik
// $Id: dynare_model.h 1766 2008-03-31 14:33:02Z kamenik $
// Copyright (C) 2005-2011, Ondra Kamenik
#ifndef OGDYN_DYNARE_MODEL
#define OGDYN_DYNARE_MODEL
@ -15,10 +13,10 @@
#include "GeneralMatrix.h"
#include <map>
#include <ext/hash_set>
#include <boost/unordered_set.hpp>
namespace ogdyn {
using __gnu_cxx::hash_set;
using boost::unordered_set;
using std::map;
/** This represents an interval in a string by the pair of
@ -172,7 +170,7 @@ namespace ogdyn {
* variable in the given set to its time shifted variable. The
* map is passed through the reference and is cleared in the
* beginning. */
void variable_shift_map(const hash_set<int>& a_set, int tshift,
void variable_shift_map(const unordered_set<int>& a_set, int tshift,
map<int,int>& s_map);
/** This returns maximum lead and minimum lag of an endogenous
* or exogenous variable in the given term. If there are no
@ -182,7 +180,7 @@ namespace ogdyn {
/** This function returns a set of non-linear subterms of the
* given term, these are terms whose linear combination
* constitutes the given term. */
hash_set<int> get_nonlinear_subterms(int t) const;
unordered_set<int> get_nonlinear_subterms(int t) const;
/** This method assigns already used tree index of some term
* to the not-yet used atom name with the given lead/lag. In
* this way, all occurrences of term t are substituted with

View File

@ -1,6 +1,4 @@
// Copyright (C) 2006, Ondra Kamenik
// $Id$
// Copyright (C) 2006-2011, Ondra Kamenik
#include "forw_subst_builder.h"
@ -21,10 +19,10 @@ ForwSubstBuilder::ForwSubstBuilder(DynareModel& m)
if (mlead > 1) {
info.num_affected_equations++;
// break it to non-linear terms
hash_set<int> nlt = model.get_nonlinear_subterms(ft);
unordered_set<int> nlt = model.get_nonlinear_subterms(ft);
int j = 0; // indexes subterms
// and make substitutions for all these non-linear subterms
for (hash_set<int>::const_iterator it = nlt.begin();
for (unordered_set<int>::const_iterator it = nlt.begin();
it != nlt.end(); ++it, ++j)
substitute_for_term(*it, i, j);
}

View File

@ -1,6 +1,4 @@
// Copyright (C) 2006, Ondra Kamenik
// $Id$
// Copyright (C) 2006-2011, Ondra Kamenik
#ifndef PLANNER_BUILDER_H
#define PLANNER_BUILDER_H
@ -9,7 +7,7 @@
namespace ogdyn {
using __gnu_cxx::hash_set;
using boost::unordered_set;
using std::map;
using std::vector;
@ -108,7 +106,7 @@ namespace ogdyn {
friend class MultInitSS;
public:
/** Type for a set of variable names. */
typedef hash_set<const char*> Tvarset;
typedef unordered_set<const char*> Tvarset;
/** Type for a set of equations. An equation is identified by
* an index to an equation in the equation vector given by
* DynareModel::eqs. The tree index of the i-th formula is