]> www.ginac.de Git - ginac.git/blobdiff - ginac/symbol.cpp
Make symbol::name be initialized lazily.
[ginac.git] / ginac / symbol.cpp
index a1c15ed16cbc270879f7490eb60f44644bdf9e89..e7eb9c92317b5d53b55e333b9ec206e4ae4605a3 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's symbolic objects. */
 
 /*
- *  GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <string>
-#include <stdexcept>
-#include <map>
-
 #include "symbol.h"
 #include "lst.h"
 #include "archive.h"
 #include "tostring.h"
 #include "utils.h"
+#include "hash_seed.h"
 #include "inifcns.h"
 
+#include <map>
+#include <stdexcept>
+#include <string>
+
 namespace GiNaC {
 
 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic,
@@ -45,25 +46,18 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic,
 
 // symbol
 
-symbol::symbol()
- :  serial(next_serial++), name(""), TeX_name(""), domain(domain::complex)
+symbol::symbol() : serial(next_serial++), name(""), TeX_name("")
 {
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
 // realsymbol
 
-realsymbol::realsymbol()
-{
-       domain = domain::real;
-}
+realsymbol::realsymbol() : symbol() { }
 
 // possymbol
 
-possymbol::possymbol()
-{
-       domain = domain::positive;
-}
+possymbol::possymbol() : realsymbol() { }
 
 //////////
 // other constructors
@@ -73,66 +67,67 @@ possymbol::possymbol()
 
 // symbol
 
-symbol::symbol(const std::string & initname, unsigned domain) :
-       serial(next_serial++), name(initname), TeX_name(""),
-       domain(domain)
+symbol::symbol(const std::string & initname) : serial(next_serial++),
+       name(initname), TeX_name("")
 {
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
-symbol::symbol(const std::string & initname, const std::string & texname, unsigned domain) :
-       serial(next_serial++), name(initname), TeX_name(texname), domain(domain)
+symbol::symbol(const std::string & initname, const std::string & texname) :
+       serial(next_serial++), name(initname), TeX_name(texname)
 {
        setflag(status_flags::evaluated | status_flags::expanded);
 }
 
 // realsymbol
        
-realsymbol::realsymbol(const std::string & initname, unsigned domain)
- : symbol(initname, domain) { }
+realsymbol::realsymbol(const std::string & initname) : symbol(initname) { }
 
-realsymbol::realsymbol(const std::string & initname, const std::string & texname, unsigned domain)
: symbol(initname, texname, domain) { }
+realsymbol::realsymbol(const std::string & initname, const std::string & texname)
      : symbol(initname, texname) { }
 
 // possymbol
        
-possymbol::possymbol(const std::string & initname, unsigned domain)
- : symbol(initname, domain) { }
+possymbol::possymbol(const std::string & initname) : realsymbol(initname) { }
 
-possymbol::possymbol(const std::string & initname, const std::string & texname, unsigned domain)
: symbol(initname, texname, domain) { }
+possymbol::possymbol(const std::string & initname, const std::string & texname
      : realsymbol(initname, texname) { }
 
 //////////
 // archiving
 //////////
 
-/** Construct object from archive_node. */
-symbol::symbol(const archive_node &n, lst &sym_lst)
- : inherited(n, sym_lst), serial(next_serial++)
+/** Read object from archive_node. */
+void symbol::read_archive(const archive_node &n, lst &sym_lst)
 {
-       if (!n.find_string("name", name))
-               name = std::string("");
-       if (!n.find_string("TeXname", TeX_name))
-               TeX_name = std::string("");
-       if (!n.find_unsigned("domain", domain))
-               domain = domain::complex;
-       setflag(status_flags::evaluated | status_flags::expanded);
-}
-
-/** Unarchive the object. */
-ex symbol::unarchive(const archive_node &n, lst &sym_lst)
-{
-       ex s = (new symbol(n, sym_lst))->setflag(status_flags::dynallocated);
+       inherited::read_archive(n, sym_lst);
+       serial = next_serial++;
+       std::string tmp_name;
+       n.find_string("name", tmp_name);
 
        // If symbol is in sym_lst, return the existing symbol
        for (lst::const_iterator it = sym_lst.begin(); it != sym_lst.end(); ++it) {
-               if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == ex_to<symbol>(s).name))
-                       return *it;
+               if (is_a<symbol>(*it) && (ex_to<symbol>(*it).name == tmp_name)) {
+                       *this = ex_to<symbol>(*it);
+                       // XXX: This method is responsible for reading realsymbol
+                       // and possymbol objects too. But
+                       // basic::operator=(const basic& other)
+                       // resets status_flags::evaluated if other and *this are
+                       // of different types. Usually this is a good idea, but
+                       // doing this for symbols is wrong (for one, nothing is
+                       // going to set status_flags::evaluated, evaluation will
+                       // loop forever). Therefore we need to restore flags.
+                       setflag(status_flags::evaluated | status_flags::expanded);
+                       return;
+               }
        }
+       name = tmp_name;
+       if (!n.find_string("TeXname", TeX_name))
+               TeX_name = std::string("");
+       setflag(status_flags::evaluated | status_flags::expanded);
 
-       // Otherwise add new symbol to list and return it
-       sym_lst.append(s);
-       return s;
+       setflag(status_flags::dynallocated);
+       sym_lst.append(*this);
 }
 
 /** Archive the object. */
@@ -144,8 +139,6 @@ void symbol::archive(archive_node &n) const
                n.add_string("name", name);
        if (!TeX_name.empty())
                n.add_string("TeX_name", TeX_name);
-       if (domain != domain::complex)
-               n.add_unsigned("domain", domain);
 }
 
 //////////
@@ -157,12 +150,21 @@ static const std::string& get_default_TeX_name(const std::string& name);
 
 // public
 
+std::string symbol::get_name() const
+{
+       if (name.empty()) {
+               std::ostringstream s;
+               s << "symbol" << serial;
+               name = s.str();
+       }
+       return name;
+}
+
+// protected
+
 void symbol::do_print(const print_context & c, unsigned level) const
 {
-       if (!name.empty())
-               c.s << name;
-       else
-               c.s << "symbol" << serial;
+       c.s << get_name();
 }
 
 void symbol::do_print_latex(const print_latex & c, unsigned level) const
@@ -180,7 +182,7 @@ void symbol::do_print_tree(const print_tree & c, unsigned level) const
        c.s << std::string(level, ' ') << name << " (" << class_name() << ")" << " @" << this
            << ", serial=" << serial
            << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
-           << ", domain=" << domain
+           << ", domain=" << get_domain()
            << std::endl;
 }
 
@@ -209,10 +211,10 @@ bool symbol::info(unsigned inf) const
                case info_flags::expanded:
                        return true;
                case info_flags::real:
-                       return domain == domain::real || domain == domain::positive;
+                       return get_domain() == domain::real || get_domain() == domain::positive;
                case info_flags::positive:
                case info_flags::nonnegative:
-                       return domain == domain::positive;
+                       return get_domain() == domain::positive;
                case info_flags::has_indices:
                        return false;
        }
@@ -221,24 +223,16 @@ bool symbol::info(unsigned inf) const
 
 ex symbol::conjugate() const
 {
-       if (this->domain == domain::complex) {
-               return conjugate_function(*this).hold();
-       } else {
-               return *this;
-       }
+       return conjugate_function(*this).hold();
 }
 
 ex symbol::real_part() const
 {
-       if (domain==domain::real || domain==domain::positive)
-               return *this;
        return real_part_function(*this).hold();
 }
 
 ex symbol::imag_part() const
 {
-       if (domain==domain::real || domain==domain::positive)
-               return 0;
        return imag_part_function(*this).hold();
 }
 
@@ -278,8 +272,8 @@ bool symbol::is_equal_same_type(const basic & other) const
 
 unsigned symbol::calchash() const
 {
-       const void* this_tinfo = (const void*)typeid(*this).name();
-       hashvalue = golden_ratio_hash((p_int)this_tinfo ^ serial);
+       unsigned seed = make_hash_seed(typeid(*this));
+       hashvalue = golden_ratio_hash(seed ^ serial);
        setflag(status_flags::hash_calculated);
        return hashvalue;
 }
@@ -350,6 +344,10 @@ static const std::string& get_default_TeX_name(const std::string& name)
                return name;
 }
 
+GINAC_BIND_UNARCHIVER(symbol);
+GINAC_BIND_UNARCHIVER(realsymbol);
+GINAC_BIND_UNARCHIVER(possymbol);
+
 //////////
 // static member variables
 //////////