]> www.ginac.de Git - ginac.git/commitdiff
Faster archiving by adding a map from strings to idices in the atoms vector.
authorChris Dams <Chris.Dams@mi.infn.it>
Thu, 1 Mar 2007 14:43:33 +0000 (14:43 +0000)
committerChris Dams <Chris.Dams@mi.infn.it>
Thu, 1 Mar 2007 14:43:33 +0000 (14:43 +0000)
ginac/archive.cpp
ginac/archive.h

index 8280e429f105cf4ef05c922e0776f57f49a6eae2..e08aaafb0713cf634106aab0acd48acf5e45d7a3 100644 (file)
@@ -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);
        // Read atoms
        unsigned num_atoms = read_unsigned(is);
        ar.atoms.resize(num_atoms);
-       for (unsigned i=0; i<num_atoms; i++)
+       for (unsigned i=0; i<num_atoms; i++) {
                getline(is, ar.atoms[i], '\0');
                getline(is, ar.atoms[i], '\0');
+               ar.inverse_atoms[ar.atoms[i]] = i;
+       }
 
        // Read expressions
        unsigned num_exprs = read_unsigned(is);
 
        // Read expressions
        unsigned num_exprs = read_unsigned(is);
@@ -297,17 +299,15 @@ std::istream &operator>>(std::istream &is, archive &ar)
  *  represents the string). */
 archive_atom archive::atomize(const std::string &s) const
 {
  *  represents the string). */
 archive_atom archive::atomize(const std::string &s) const
 {
-       // Search for string in atoms vector
-       std::vector<std::string>::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
 
        // Not found, add to atoms vector
+       archive_atom id = atoms.size();
        atoms.push_back(s);
        atoms.push_back(s);
+       inverse_atoms[s] = id;
        return id;
 }
 
        return id;
 }
 
@@ -540,6 +540,7 @@ ex archive_node::unarchive(lst &sym_lst) const
 void archive::clear()
 {
        atoms.clear();
 void archive::clear()
 {
        atoms.clear();
+       inverse_atoms.clear();
        exprs.clear();
        nodes.clear();
        exprtable.clear();
        exprs.clear();
        nodes.clear();
        exprtable.clear();
index dafbb38cc5dfac050179860aca91e66136b96718..1d60f5317748f08d41e818b111acf90ccb56da57 100644 (file)
@@ -242,6 +242,11 @@ public:
 private:
        /** Vector of atomized strings (using a vector allows faster unarchiving). */
        mutable std::vector<std::string> atoms;
 private:
        /** Vector of atomized strings (using a vector allows faster unarchiving). */
        mutable std::vector<std::string> atoms;
+       /** The map of from strings to indices of the atoms vectors allows for
+        *  faster archiving.
+        */
+       typedef std::map<std::string, archive_atom>::const_iterator inv_at_cit;
+       mutable std::map<std::string, archive_atom> inverse_atoms;
 
        /** Map of stored expressions to nodes for faster archiving */
        typedef std::map<ex, archive_node_id, ex_is_less>::iterator mapit;
 
        /** Map of stored expressions to nodes for faster archiving */
        typedef std::map<ex, archive_node_id, ex_is_less>::iterator mapit;