From e40d45d3bbeda09cc1f42b4d8abfa4e75c55ade9 Mon Sep 17 00:00:00 2001 From: Chris Dams Date: Thu, 1 Mar 2007 14:43:33 +0000 Subject: [PATCH] Faster archiving by adding a map from strings to idices in the atoms vector. --- ginac/archive.cpp | 19 ++++++++++--------- ginac/archive.h | 5 +++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/ginac/archive.cpp b/ginac/archive.cpp index 8280e429..e08aaafb 100644 --- a/ginac/archive.cpp +++ b/ginac/archive.cpp @@ -272,8 +272,10 @@ std::istream &operator>>(std::istream &is, archive &ar) // Read atoms unsigned num_atoms = read_unsigned(is); ar.atoms.resize(num_atoms); - for (unsigned i=0; i>(std::istream &is, archive &ar) * represents the string). */ archive_atom archive::atomize(const std::string &s) const { - // Search for string in atoms vector - std::vector::const_iterator i = atoms.begin(), iend = atoms.end(); - archive_atom id = 0; - while (i != iend) { - if (*i == s) - return id; - i++; id++; - } + // Search for string in inverse_atoms map. + inv_at_cit i = inverse_atoms.find(s); + if (i!=inverse_atoms.end()) + return i->second; // Not found, add to atoms vector + archive_atom id = atoms.size(); atoms.push_back(s); + inverse_atoms[s] = id; return id; } @@ -540,6 +540,7 @@ ex archive_node::unarchive(lst &sym_lst) const void archive::clear() { atoms.clear(); + inverse_atoms.clear(); exprs.clear(); nodes.clear(); exprtable.clear(); diff --git a/ginac/archive.h b/ginac/archive.h index dafbb38c..1d60f531 100644 --- a/ginac/archive.h +++ b/ginac/archive.h @@ -242,6 +242,11 @@ public: private: /** Vector of atomized strings (using a vector allows faster unarchiving). */ mutable std::vector atoms; + /** The map of from strings to indices of the atoms vectors allows for + * faster archiving. + */ + typedef std::map::const_iterator inv_at_cit; + mutable std::map inverse_atoms; /** Map of stored expressions to nodes for faster archiving */ typedef std::map::iterator mapit; -- 2.44.0