From 28b30ff302ed535b1e81902f06d24c9e9fe61c19 Mon Sep 17 00:00:00 2001 From: An-Cheng Huang Date: Wed, 25 Aug 2010 16:41:44 -0700 Subject: switch to unordered_map --- perl_dmod/Cstore/Cstore.xs | 7 +++---- perl_dmod/Cstore/typemap | 2 +- src/cstore/cstore-varref.cpp | 2 +- src/cstore/cstore.cpp | 31 +++++++++++++++++-------------- src/cstore/cstore.hpp | 32 +++++++++++++++++++------------- src/cstore/unionfs/cstore-unionfs.cpp | 19 ++++++++++--------- src/cstore/unionfs/cstore-unionfs.hpp | 4 ++-- 7 files changed, 53 insertions(+), 44 deletions(-) diff --git a/perl_dmod/Cstore/Cstore.xs b/perl_dmod/Cstore/Cstore.xs index 4a726e7..ebef08f 100644 --- a/perl_dmod/Cstore/Cstore.xs +++ b/perl_dmod/Cstore/Cstore.xs @@ -25,7 +25,6 @@ #include #include #include -#include /* currently use the UnionfsCstore implementation */ #include @@ -205,7 +204,7 @@ Cstore::cfgPathGetChildNodesStatus(STRVEC *vref) PREINIT: vector arg_strvec; CODE: - map ret_strstrmap; + Cstore::MapT ret_strstrmap; THIS->cfgPathGetChildNodesStatus(arg_strvec, ret_strstrmap); OUTPUT: RETVAL @@ -263,7 +262,7 @@ Cstore::cfgPathGetChildNodesStatusDA(STRVEC *vref) PREINIT: vector arg_strvec; CODE: - map ret_strstrmap; + Cstore::MapT ret_strstrmap; THIS->cfgPathGetChildNodesStatusDA(arg_strvec, ret_strstrmap); OUTPUT: RETVAL @@ -295,7 +294,7 @@ Cstore::getParsedTmpl(STRVEC *vref, bool allow_val) PREINIT: vector arg_strvec; CODE: - map ret_strstrmap; + Cstore::MapT ret_strstrmap; if (!THIS->getParsedTmpl(arg_strvec, ret_strstrmap, allow_val)) { XSRETURN_UNDEF; } diff --git a/perl_dmod/Cstore/typemap b/perl_dmod/Cstore/typemap index f1f6a82..6df545b 100644 --- a/perl_dmod/Cstore/typemap +++ b/perl_dmod/Cstore/typemap @@ -32,7 +32,7 @@ T_STRVEC_REF T_STRSTRMAP_REF HV *href = (HV *) sv_2mortal((SV *) newHV()); - map::iterator it = ret_strstrmap.begin(); + Cstore::MapT::iterator it = ret_strstrmap.begin(); for (; it != ret_strstrmap.end(); ++it) { const char *key = (*it).first.c_str(); const char *val = (*it).second.c_str(); diff --git a/src/cstore/cstore-varref.cpp b/src/cstore/cstore-varref.cpp index f00ed38..02bdb97 100644 --- a/src/cstore/cstore-varref.cpp +++ b/src/cstore/cstore-varref.cpp @@ -225,7 +225,7 @@ bool Cstore::VarRef::getValue(string& value, vtw_type_e& def_type) { vector result; - map added; + Cstore::MapT added; def_type = ERROR_TYPE; for (size_t i = 0; i < _paths.size(); i++) { if (_paths[i].first.size() == 0) { diff --git a/src/cstore/cstore.cpp b/src/cstore/cstore.cpp index 2c30207..25eaae7 100644 --- a/src/cstore/cstore.cpp +++ b/src/cstore/cstore.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -62,10 +61,14 @@ const string Cstore::C_ENV_SHAPI_HELP_STRS = "_cli_shell_api_hstrs"; const string Cstore::C_ENUM_SCRIPT_DIR = "/opt/vyatta/share/enumeration"; const string Cstore::C_LOGFILE_STDOUT = "/tmp/cfg-stdout.log"; +//// sorting +const unsigned int Cstore::SORT_DEFAULT = 0; +const unsigned int Cstore::SORT_DEB_VERSION = 0; +const unsigned int Cstore::SORT_NONE = 1; ////// static bool Cstore::_init = false; -map Cstore::_sort_func_map; +Cstore::MapT Cstore::_sort_func_map; ////// constructors/destructors @@ -121,7 +124,7 @@ Cstore::validateTmplPath(const vector& path_comps, bool validate_vals, */ bool Cstore::getParsedTmpl(const vector& path_comps, - map& tmap, bool allow_val) + Cstore::MapT& tmap, bool allow_val) { vtw_def def; /* currently this function is used outside actual CLI operations, mainly @@ -1099,7 +1102,7 @@ Cstore::cfgPathGetDeletedChildNodesDA(const vector& path_comps, cfgPathGetChildNodesDA(path_comps, acnodes, true, include_deactivated); vector wcnodes; cfgPathGetChildNodesDA(path_comps, wcnodes, false, include_deactivated); - map cmap; + MapT cmap; for (size_t i = 0; i < wcnodes.size(); i++) { cmap[wcnodes[i]] = true; } @@ -1138,7 +1141,7 @@ Cstore::cfgPathGetDeletedValuesDA(const vector& path_comps, || !cfgPathGetValuesDA(path_comps, nvals, false, include_deactivated)) { return; } - map dmap; + MapT dmap; for (size_t i = 0; i < nvals.size(); i++) { dmap[nvals[i]] = true; } @@ -1159,11 +1162,11 @@ Cstore::cfgPathGetDeletedValuesDA(const vector& path_comps, */ void Cstore::cfgPathGetChildNodesStatus(const vector& path_comps, - map& cmap, + Cstore::MapT& cmap, vector& sorted_keys) { // get a union of active and working - map umap; + MapT umap; vector acnodes; vector wcnodes; cfgPathGetChildNodes(path_comps, acnodes, true); @@ -1177,7 +1180,7 @@ Cstore::cfgPathGetChildNodesStatus(const vector& path_comps, // get the status of each one vector ppath = path_comps; - map::iterator it = umap.begin(); + MapT::iterator it = umap.begin(); for (; it != umap.end(); ++it) { string c = (*it).first; ppath.push_back(c); @@ -1204,7 +1207,7 @@ Cstore::cfgPathGetChildNodesStatus(const vector& path_comps, */ void Cstore::cfgPathGetChildNodesStatusDA(const vector& path_comps, - map& cmap, + Cstore::MapT& cmap, vector& sorted_keys) { // process deleted nodes first @@ -1574,7 +1577,7 @@ Cstore::cfgPathGetEffectiveChildNodes(const vector& path_comps, } // get a union of active and working - map cmap; + MapT cmap; vector acnodes; vector wcnodes; cfgPathGetChildNodes(path_comps, acnodes, true); @@ -1588,7 +1591,7 @@ Cstore::cfgPathGetEffectiveChildNodes(const vector& path_comps, // get only the effective ones from the union vector ppath = path_comps; - map::iterator it = cmap.begin(); + MapT::iterator it = cmap.begin(); for (; it != cmap.end(); ++it) { string c = (*it).first; ppath.push_back(c); @@ -1660,7 +1663,7 @@ Cstore::cfgPathGetEffectiveValues(const vector& path_comps, } // get a union of active and working - map vmap; + MapT vmap; vector ovals; vector nvals; cfgPathGetValues(path_comps, ovals, true); @@ -1674,7 +1677,7 @@ Cstore::cfgPathGetEffectiveValues(const vector& path_comps, // get only the effective ones from the union vector ppath = path_comps; - map::iterator it = vmap.begin(); + MapT::iterator it = vmap.begin(); for (; it != vmap.end(); ++it) { string c = (*it).first; ppath.push_back(c); @@ -1907,7 +1910,7 @@ Cstore::sort_func_deb_version(string a, string b) } void -Cstore::sort_nodes(vector& nvec, Cstore::SortAlgT sort_alg) +Cstore::sort_nodes(vector& nvec, unsigned int sort_alg) { if (_sort_func_map.find(sort_alg) == _sort_func_map.end()) { return; diff --git a/src/cstore/cstore.hpp b/src/cstore/cstore.hpp index 8cb3fd1..5da07fd 100644 --- a/src/cstore/cstore.hpp +++ b/src/cstore/cstore.hpp @@ -18,7 +18,7 @@ #define _CSTORE_H_ #include #include -#include +#include #include @@ -42,6 +42,10 @@ public: Cstore(string& env); virtual ~Cstore() {}; + // types + template + class MapT : public tr1::unordered_map {}; + // constants static const string C_NODE_STATUS_DELETED; static const string C_NODE_STATUS_ADDED; @@ -74,7 +78,7 @@ public: bool validateTmplPath(const vector& path_comps, bool validate_vals, vtw_def& def); bool getParsedTmpl(const vector& path_comps, - map& tmap, bool allow_val = true); + MapT& tmap, bool allow_val = true); void tmplGetChildNodes(const vector& path_comps, vector& cnodes); @@ -187,12 +191,12 @@ public: void cfgPathGetDeletedValues(const vector& path_comps, vector& dvals); void cfgPathGetChildNodesStatus(const vector& path_comps, - map& cmap) { + MapT& cmap) { vector dummy; cfgPathGetChildNodesStatus(path_comps, cmap, dummy); }; void cfgPathGetChildNodesStatus(const vector& path_comps, - map& cmap, + MapT& cmap, vector& sorted_keys); /* observers for "effective config". can be used both during a config @@ -251,12 +255,12 @@ public: vector& dvals, bool include_deactivated = true); void cfgPathGetChildNodesStatusDA(const vector& path_comps, - map& cmap) { + MapT& cmap) { vector dummy; cfgPathGetChildNodesStatusDA(path_comps, cmap, dummy); }; void cfgPathGetChildNodesStatusDA(const vector& path_comps, - map& cmap, + MapT& cmap, vector& sorted_keys); @@ -363,16 +367,18 @@ private: ////// implemented // for sorting - typedef enum { - SORT_DEFAULT = 0, - SORT_DEB_VERSION = 0, - SORT_NONE - } SortAlgT; + /* apparently unordered_map template does not work with "enum" type, so + * change this to simply unsigned ints to allow unifying all map types, + * i.e., "Cstore::MapT". + */ + static const unsigned int SORT_DEFAULT; + static const unsigned int SORT_DEB_VERSION; + static const unsigned int SORT_NONE; typedef bool (*SortFuncT)(std::string, std::string); - static map _sort_func_map; + static MapT _sort_func_map; static bool sort_func_deb_version(string a, string b); - void sort_nodes(vector& nvec, SortAlgT sort_alg = SORT_DEFAULT); + void sort_nodes(vector& nvec, unsigned int sort_alg = SORT_DEFAULT); // init static bool _init; diff --git a/src/cstore/unionfs/cstore-unionfs.cpp b/src/cstore/unionfs/cstore-unionfs.cpp index 5cfd8ee..849d659 100644 --- a/src/cstore/unionfs/cstore-unionfs.cpp +++ b/src/cstore/unionfs/cstore-unionfs.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include @@ -63,8 +62,8 @@ const string UnionfsCstore::C_DEF_NAME = "node.def"; ////// static -static map _fs_escape_chars; -static map _fs_unescape_chars; +static Cstore::MapT _fs_escape_chars; +static Cstore::MapT _fs_unescape_chars; static void _init_fs_escape_chars() { @@ -80,7 +79,7 @@ _init_fs_escape_chars() static string _escape_char(char c) { - map::iterator p = _fs_escape_chars.find(c); + Cstore::MapT::iterator p = _fs_escape_chars.find(c); if (p != _fs_escape_chars.end()) { return _fs_escape_chars[c]; } else { @@ -88,12 +87,13 @@ _escape_char(char c) } } -static map _escape_path_name_cache; +static Cstore::MapT _escape_path_name_cache; static string _escape_path_name(const string& path) { - map::iterator p = _escape_path_name_cache.find(path); + Cstore::MapT::iterator p + = _escape_path_name_cache.find(path); if (p != _escape_path_name_cache.end()) { // found escaped string in cache. just return it. return _escape_path_name_cache[path]; @@ -110,12 +110,13 @@ _escape_path_name(const string& path) return npath; } -static map _unescape_path_name_cache; +static Cstore::MapT _unescape_path_name_cache; static string _unescape_path_name(const string& path) { - map::iterator p = _unescape_path_name_cache.find(path); + Cstore::MapT::iterator p + = _unescape_path_name_cache.find(path); if (p != _unescape_path_name_cache.end()) { // found unescaped string in cache. just return it. return _unescape_path_name_cache[path]; @@ -129,7 +130,7 @@ _unescape_path_name(const string& path) break; } string s = path.substr(i, 3); - map::iterator p = _fs_unescape_chars.find(s); + Cstore::MapT::iterator p = _fs_unescape_chars.find(s); if (p != _fs_unescape_chars.end()) { char c = _fs_unescape_chars[s]; if (path.size() == 3 && c == -1) { diff --git a/src/cstore/unionfs/cstore-unionfs.hpp b/src/cstore/unionfs/cstore-unionfs.hpp index 90f28c5..3942e01 100644 --- a/src/cstore/unionfs/cstore-unionfs.hpp +++ b/src/cstore/unionfs/cstore-unionfs.hpp @@ -83,7 +83,7 @@ private: // path buffers b_fs::path mutable_cfg_path; // mutable part of config path b_fs::path tmpl_path; // whole template path - map > saved_paths; + Cstore::MapT > saved_paths; // saved mutable part of cfg path and whole template path ////// virtual functions defined in base class @@ -122,7 +122,7 @@ private: saved_paths[handle] = p; }; void restore_paths(const void *handle = NULL) { - map >::iterator it + Cstore::MapT >::iterator it = saved_paths.find(handle); if (it == saved_paths.end()) { exit_internal("restore_paths: handle not found\n"); -- cgit v1.2.3