X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?a=blobdiff_plain;f=ginac%2Fsymbol.cpp;h=76eb79ee75cf3605279daa7d6d2a8d7a2d587a09;hb=96be7a17790700998e2d071e00ad5c1ffb4a2d3e;hp=9a729919fcd6ed4d732752cbcd281666f7a6e452;hpb=8fdf123fe6dac43ad7b8020d7bb7446c51cf75e7;p=ginac.git diff --git a/ginac/symbol.cpp b/ginac/symbol.cpp index 9a729919..76eb79ee 100644 --- a/ginac/symbol.cpp +++ b/ginac/symbol.cpp @@ -3,7 +3,7 @@ * Implementation of GiNaC's symbolic objects. */ /* - * GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2018 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 @@ -20,17 +20,17 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include -#include - #include "symbol.h" #include "lst.h" #include "archive.h" -#include "tostring.h" #include "utils.h" +#include "hash_seed.h" #include "inifcns.h" +#include +#include +#include + namespace GiNaC { GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(symbol, basic, @@ -96,31 +96,37 @@ possymbol::possymbol(const std::string & initname, const std::string & 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(""); - 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(*it) && (ex_to(*it).name == ex_to(s).name)) - return *it; + for (auto & s : sym_lst) { + if (is_a(s) && (ex_to(s).name == tmp_name)) { + *this = ex_to(s); + // 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. */ @@ -143,12 +149,27 @@ static const std::string& get_default_TeX_name(const std::string& name); // public +std::string symbol::get_name() const +{ + if (name.empty()) { + name = "symbol" + std::to_string(serial); + } + return name; +} + +std::string symbol::get_TeX_name() const +{ + if (TeX_name.empty()) { + return get_default_TeX_name(get_name()); + } + return TeX_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 @@ -256,8 +277,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; } @@ -328,6 +349,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 //////////