From e78622a06f749a124b007aa7b969de02fcc9c3d2 Mon Sep 17 00:00:00 2001 From: Christian Bauer Date: Sat, 19 May 2001 21:37:44 +0000 Subject: [PATCH] replaced "precedence" static member variable by virtual precedence() function --- ginac/add.cpp | 42 +++++++++++++++++------------------------- ginac/add.h | 12 +----------- ginac/basic.cpp | 14 ++++++-------- ginac/basic.h | 2 +- ginac/container.pl | 12 ++---------- ginac/expairseq.cpp | 10 +++------- ginac/expairseq.h | 2 +- ginac/function.pl | 5 +++-- ginac/mul.cpp | 26 +++++++++----------------- ginac/mul.h | 6 +----- ginac/ncmul.cpp | 14 +++----------- ginac/ncmul.h | 6 +----- ginac/numeric.cpp | 17 ++++------------- ginac/numeric.h | 2 +- ginac/power.cpp | 16 ++++------------ ginac/power.h | 2 +- ginac/pseries.cpp | 12 ++---------- ginac/pseries.h | 3 +-- ginac/registrar.h | 1 - ginac/relational.cpp | 16 ++++------------ ginac/relational.h | 2 +- 21 files changed, 66 insertions(+), 156 deletions(-) diff --git a/ginac/add.cpp b/ginac/add.cpp index 412bd5a5..92cb325f 100644 --- a/ginac/add.cpp +++ b/ginac/add.cpp @@ -121,7 +121,7 @@ void add::print(const print_context & c, unsigned level) const } else if (is_of_type(c, print_csrc)) { - if (precedence <= level) + if (precedence() <= level) c.s << "("; // Print arguments, separated by "+" @@ -130,23 +130,23 @@ void add::print(const print_context & c, unsigned level) const // If the coefficient is -1, it is replaced by a single minus sign if (it->coeff.compare(_num1()) == 0) { - it->rest.bp->print(c, precedence); + it->rest.bp->print(c, precedence()); } else if (it->coeff.compare(_num_1()) == 0) { c.s << "-"; - it->rest.bp->print(c, precedence); + it->rest.bp->print(c, precedence()); } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) { - it->rest.bp->print(c, precedence); + it->rest.bp->print(c, precedence()); c.s << "/"; - ex_to_numeric(it->coeff).denom().print(c, precedence); + ex_to_numeric(it->coeff).denom().print(c, precedence()); } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) { c.s << "-"; - it->rest.bp->print(c, precedence); + it->rest.bp->print(c, precedence()); c.s << "/"; - ex_to_numeric(it->coeff).denom().print(c, precedence); + ex_to_numeric(it->coeff).denom().print(c, precedence()); } else { - it->coeff.bp->print(c, precedence); + it->coeff.bp->print(c, precedence()); c.s << "*"; - it->rest.bp->print(c, precedence); + it->rest.bp->print(c, precedence()); } // Separator is "+", except if the following expression would have a leading minus sign @@ -158,15 +158,15 @@ void add::print(const print_context & c, unsigned level) const if (!overall_coeff.is_zero()) { if (overall_coeff.info(info_flags::positive)) c.s << '+'; - overall_coeff.bp->print(c, precedence); + overall_coeff.bp->print(c, precedence()); } - if (precedence <= level) + if (precedence() <= level) c.s << ")"; } else { - if (precedence <= level) { + if (precedence() <= level) { if (is_of_type(c, print_latex)) c.s << "{("; else @@ -181,7 +181,7 @@ void add::print(const print_context & c, unsigned level) const if (!is_of_type(c, print_tree)) overall_coeff.print(c, 0); else - overall_coeff.print(c, precedence); + overall_coeff.print(c, precedence()); first = false; } @@ -204,20 +204,20 @@ void add::print(const print_context & c, unsigned level) const coeff.print(c); } else { if (coeff.csgn() == -1) - (-coeff).print(c, precedence); + (-coeff).print(c, precedence()); else - coeff.print(c, precedence); + coeff.print(c, precedence()); } if (is_of_type(c, print_latex)) c.s << ' '; else c.s << '*'; } - it->rest.print(c, precedence); + it->rest.print(c, precedence()); it++; } - if (precedence <= level) { + if (precedence() <= level) { if (is_of_type(c, print_latex)) c.s << ")}"; else @@ -468,12 +468,4 @@ ex add::expand(unsigned options) const return (new add(vp,overall_coeff))->setflag(status_flags::expanded | status_flags::dynallocated); } -////////// -// static member variables -////////// - -// protected - -unsigned add::precedence = 40; - } // namespace GiNaC diff --git a/ginac/add.h b/ginac/add.h index c6d04d08..1e036627 100644 --- a/ginac/add.h +++ b/ginac/add.h @@ -47,6 +47,7 @@ public: // functions overriding virtual functions from bases classes public: void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 40;} bool info(unsigned inf) const; int degree(const ex & s) const; int ldegree(const ex & s) const; @@ -73,17 +74,6 @@ protected: const ex & c) const; ex recombine_pair_to_ex(const expair & p) const; ex expand(unsigned options=0) const; - - // new virtual functions which can be overridden by derived classes - // none - - // non-virtual functions in this class - // none - -// member variables - -protected: - static unsigned precedence; }; // utility functions diff --git a/ginac/basic.cpp b/ginac/basic.cpp index 5b0b1803..bf5bd6cb 100644 --- a/ginac/basic.cpp +++ b/ginac/basic.cpp @@ -154,6 +154,12 @@ void basic::dbgprinttree(void) const this->print(print_tree(std::cerr)); } +/** Return relative operator precedence (for parenthizing output). */ +unsigned basic::precedence(void) const +{ + return 70; +} + /** Create a new copy of this on the heap. One can think of this as simulating * a virtual copy constructor which is needed for instance by the refcounted * construction of an ex from a basic. */ @@ -635,14 +641,6 @@ void basic::ensure_if_modifiable(void) const throw(std::runtime_error("cannot modify multiply referenced object")); } -////////// -// static member variables -////////// - -// protected - -unsigned basic::precedence = 70; - ////////// // global variables ////////// diff --git a/ginac/basic.h b/ginac/basic.h index 555d35cf..33a9d207 100644 --- a/ginac/basic.h +++ b/ginac/basic.h @@ -103,6 +103,7 @@ public: // only const functions please (may break reference counting) virtual void print(const print_context & c, unsigned level = 0) const; virtual void dbgprint(void) const; virtual void dbgprinttree(void) const; + virtual unsigned precedence(void) const; virtual bool info(unsigned inf) const; virtual unsigned nops() const; virtual ex op(int i) const; @@ -162,7 +163,6 @@ protected: unsigned tinfo_key; ///< typeinfo mutable unsigned flags; ///< of type status_flags mutable unsigned hashvalue; ///< hash value - static unsigned precedence; ///< precedence for printing parens private: unsigned refcount; ///< Number of reference counts }; diff --git a/ginac/container.pl b/ginac/container.pl index af9d5372..8a025033 100755 --- a/ginac/container.pl +++ b/ginac/container.pl @@ -206,6 +206,7 @@ ${constructors_interface} public: void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 10;} bool info(unsigned inf) const; unsigned nops() const; ex & let_op(int i); @@ -241,7 +242,6 @@ protected: protected: ${STLT} seq; - static unsigned precedence; }; // utility functions @@ -418,7 +418,7 @@ void ${CONTAINER}::print(const print_context & c, unsigned level) const } else { // always print brackets around seq, ignore upper_precedence - printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence, precedence+1); + printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence(), precedence()+1); } } @@ -726,14 +726,6 @@ ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const return 0; // nothing has changed } -////////// -// static member variables -////////// - -// protected - -unsigned ${CONTAINER}::precedence = 10; - } // namespace GiNaC END_OF_IMPLEMENTATION diff --git a/ginac/expairseq.cpp b/ginac/expairseq.cpp index b6a0f372..b14ffb30 100644 --- a/ginac/expairseq.cpp +++ b/ginac/expairseq.cpp @@ -267,7 +267,7 @@ void expairseq::print(const print_context & c, unsigned level) const } else { c.s << "[["; - printseq(c, ',', precedence, level); + printseq(c, ',', precedence(), level); c.s << "]]"; } } @@ -540,9 +540,9 @@ ex expairseq::thisexpairseq(epvector *vp, const ex &oc) const void expairseq::printpair(const print_context & c, const expair & p, unsigned upper_precedence) const { c.s << "[["; - p.rest.bp->print(c, precedence); + p.rest.bp->print(c, precedence()); c.s << ","; - p.coeff.bp->print(c, precedence); + p.coeff.bp->print(c, precedence()); c.s << "]]"; } @@ -1631,10 +1631,6 @@ epvector * expairseq::subschildren(const lst &ls, const lst &lr) const // static member variables ////////// -// protected - -unsigned expairseq::precedence = 10; - #if EXPAIRSEQ_USE_HASHTAB unsigned expairseq::maxhashtabsize = 0x4000000U; unsigned expairseq::minhashtabsize = 0x1000U; diff --git a/ginac/expairseq.h b/ginac/expairseq.h index e4e34653..fac36eec 100644 --- a/ginac/expairseq.h +++ b/ginac/expairseq.h @@ -88,6 +88,7 @@ public: public: basic * duplicate() const; void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 10;} bool info(unsigned inf) const; unsigned nops() const; ex op(int i) const; @@ -174,7 +175,6 @@ protected: protected: epvector seq; ex overall_coeff; - static unsigned precedence; #if EXPAIRSEQ_USE_HASHTAB epplistvector hashtab; unsigned hashtabsize; diff --git a/ginac/function.pl b/ginac/function.pl index 9e303d07..a08d35f1 100755 --- a/ginac/function.pl +++ b/ginac/function.pl @@ -335,6 +335,7 @@ $constructors_interface // functions overriding virtual functions from bases classes public: void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 70;} int degree(const ex & s) const; int ldegree(const ex & s) const; ex coeff(const ex & s, int n = 1) const; @@ -678,10 +679,10 @@ void function::print(const print_context & c, unsigned level) const } else if is_of_type(c, print_latex) { c.s << registered_functions()[serial].TeX_name; - printseq(c, '(', ',', ')', exprseq::precedence, function::precedence); + printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence()); } else { c.s << registered_functions()[serial].name; - printseq(c, '(', ',', ')', exprseq::precedence, function::precedence); + printseq(c, '(', ',', ')', exprseq::precedence(), function::precedence()); } } diff --git a/ginac/mul.cpp b/ginac/mul.cpp index efc207bb..8e641caf 100644 --- a/ginac/mul.cpp +++ b/ginac/mul.cpp @@ -136,11 +136,11 @@ void mul::print(const print_context & c, unsigned level) const } else if (is_of_type(c, print_csrc)) { - if (precedence <= level) + if (precedence() <= level) c.s << "("; if (!overall_coeff.is_equal(_ex1())) { - overall_coeff.bp->print(c, precedence); + overall_coeff.bp->print(c, precedence()); c.s << "*"; } @@ -158,7 +158,7 @@ void mul::print(const print_context & c, unsigned level) const // If the exponent is 1 or -1, it is left out if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0) - it->rest.print(c, precedence); + it->rest.print(c, precedence()); else { // Outer parens around ex needed for broken gcc-2.95 parser: (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).print(c, level); @@ -174,12 +174,12 @@ void mul::print(const print_context & c, unsigned level) const } } - if (precedence <= level) + if (precedence() <= level) c.s << ")"; } else { - if (precedence <= level) { + if (precedence() <= level) { if (is_of_type(c, print_latex)) c.s << "{("; else @@ -201,9 +201,9 @@ void mul::print(const print_context & c, unsigned level) const coeff.print(c); } else { if (coeff.csgn() == -1) - (-coeff).print(c, precedence); + (-coeff).print(c, precedence()); else - coeff.print(c, precedence); + coeff.print(c, precedence()); } if (is_of_type(c, print_latex)) c.s << ' '; @@ -222,11 +222,11 @@ void mul::print(const print_context & c, unsigned level) const } else { first = false; } - recombine_pair_to_ex(*it).print(c, precedence); + recombine_pair_to_ex(*it).print(c, precedence()); it++; } - if (precedence <= level) { + if (precedence() <= level) { if (is_of_type(c, print_latex)) c.s << ")}"; else @@ -717,12 +717,4 @@ epvector * mul::expandchildren(unsigned options) const return 0; // nothing has changed } -////////// -// static member variables -////////// - -// protected - -unsigned mul::precedence = 50; - } // namespace GiNaC diff --git a/ginac/mul.h b/ginac/mul.h index 2d3d240f..d5a73ccf 100644 --- a/ginac/mul.h +++ b/ginac/mul.h @@ -48,6 +48,7 @@ public: // functions overriding virtual functions from bases classes public: void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 50;} bool info(unsigned inf) const; int degree(const ex & s) const; int ldegree(const ex & s) const; @@ -85,11 +86,6 @@ protected: // non-virtual functions in this class protected: epvector * expandchildren(unsigned options) const; - -// member variables - -protected: - static unsigned precedence; }; // utility functions diff --git a/ginac/ncmul.cpp b/ginac/ncmul.cpp index abe72ef1..8a0cc122 100644 --- a/ginac/ncmul.cpp +++ b/ginac/ncmul.cpp @@ -126,15 +126,15 @@ void ncmul::print(const print_context & c, unsigned level) const c.s << "ncmul("; exvector::const_iterator it = seq.begin(), itend = seq.end()-1; while (it != itend) { - it->print(c, precedence); + it->print(c, precedence()); c.s << ","; it++; } - it->print(c, precedence); + it->print(c, precedence()); c.s << ")"; } else - printseq(c, '(', '*', ')', precedence, level); + printseq(c, '(', '*', ')', precedence(), level); } bool ncmul::info(unsigned inf) const @@ -530,14 +530,6 @@ const exvector & ncmul::get_factors(void) const return seq; } -////////// -// static member variables -////////// - -// protected - -unsigned ncmul::precedence = 50; - ////////// // friend functions ////////// diff --git a/ginac/ncmul.h b/ginac/ncmul.h index 05b96da3..f3fc1019 100644 --- a/ginac/ncmul.h +++ b/ginac/ncmul.h @@ -54,6 +54,7 @@ public: // functions overriding virtual functions from bases classes public: void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 50;} bool info(unsigned inf) const; int degree(const ex & s) const; int ldegree(const ex & s) const; @@ -78,11 +79,6 @@ protected: exvector expandchildren(unsigned options) const; public: const exvector & get_factors(void) const; - -// member variables - -protected: - static unsigned precedence; }; // friend funtions diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp index ef4e3e92..a89045bd 100644 --- a/ginac/numeric.cpp +++ b/ginac/numeric.cpp @@ -418,7 +418,7 @@ void numeric::print(const print_context & c, unsigned level) const const cln::cl_R i = cln::imagpart(cln::the(value)); if (cln::zerop(i)) { // case 1, real: x or -x - if ((precedence <= level) && (!this->is_nonneg_integer())) { + if ((precedence() <= level) && (!this->is_nonneg_integer())) { c.s << par_open; print_real_number(c.s, r); c.s << par_close; @@ -428,7 +428,7 @@ void numeric::print(const print_context & c, unsigned level) const } else { if (cln::zerop(r)) { // case 2, imaginary: y*I or -y*I - if ((precedence <= level) && (i < 0)) { + if ((precedence() <= level) && (i < 0)) { if (i == -1) { c.s << par_open+imag_sym+par_close; } else { @@ -450,7 +450,7 @@ void numeric::print(const print_context & c, unsigned level) const } } else { // case 3, complex: x+y*I or x-y*I or -x+y*I or -x-y*I - if (precedence <= level) + if (precedence() <= level) c.s << par_open; print_real_number(c.s, r); if (i < 0) { @@ -469,7 +469,7 @@ void numeric::print(const print_context & c, unsigned level) const c.s << mul_sym+imag_sym; } } - if (precedence <= level) + if (precedence() <= level) c.s << par_close; } } @@ -1148,15 +1148,6 @@ int numeric::int_length(void) const return 0; } - -////////// -// static member variables -////////// - -// protected - -unsigned numeric::precedence = 30; - ////////// // global constants ////////// diff --git a/ginac/numeric.h b/ginac/numeric.h index 4f2ba1a1..1cd7d976 100644 --- a/ginac/numeric.h +++ b/ginac/numeric.h @@ -85,6 +85,7 @@ public: // functions overriding virtual functions from bases classes public: void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 30;} bool info(unsigned inf) const; bool has(const ex &other) const; ex eval(int level = 0) const; @@ -160,7 +161,6 @@ public: // member variables protected: - static unsigned precedence; cln::cl_number value; }; diff --git a/ginac/power.cpp b/ginac/power.cpp index b13665e1..eee137bb 100644 --- a/ginac/power.cpp +++ b/ginac/power.cpp @@ -189,16 +189,16 @@ void power::print(const print_context & c, unsigned level) const else c.s << ")"; } else { - if (precedence <= level) { + if (precedence() <= level) { if (is_of_type(c, print_latex)) c.s << "{("; else c.s << "("; } - basis.print(c, precedence); + basis.print(c, precedence()); c.s << "^"; - exponent.print(c, precedence); - if (precedence <= level) { + exponent.print(c, precedence()); + if (precedence() <= level) { if (is_of_type(c, print_latex)) c.s << ")}"; else @@ -830,14 +830,6 @@ ex power::expand_noncommutative(const ex & basis, const numeric & exponent, } */ -////////// -// static member variables -////////// - -// protected - -unsigned power::precedence = 60; - // helper function ex sqrt(const ex & a) diff --git a/ginac/power.h b/ginac/power.h index d9748ef9..480e091a 100644 --- a/ginac/power.h +++ b/ginac/power.h @@ -49,6 +49,7 @@ public: // functions overriding virtual functions from bases classes public: void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 60;} bool info(unsigned inf) const; unsigned nops() const; ex & let_op(int i); @@ -86,7 +87,6 @@ protected: protected: ex basis; ex exponent; - static unsigned precedence; }; // utility functions diff --git a/ginac/pseries.cpp b/ginac/pseries.cpp index ffb2cfb9..9d04f6a9 100644 --- a/ginac/pseries.cpp +++ b/ginac/pseries.cpp @@ -142,7 +142,7 @@ void pseries::print(const print_context & c, unsigned level) const } else { - if (precedence <= level) + if (precedence() <= level) c.s << "("; std::string par_open = is_of_type(c, print_latex) ? "{(" : "("; @@ -198,7 +198,7 @@ void pseries::print(const print_context & c, unsigned level) const Order(power(var-point,i->coeff)).print(c); } - if (precedence <= level) + if (precedence() <= level) c.s << ")"; } } @@ -925,12 +925,4 @@ ex ex::series(const ex & r, int order, unsigned options) const return e; } -////////// -// static member variables -////////// - -// protected - -unsigned pseries::precedence = 38; // for clarity just below add::precedence - } // namespace GiNaC diff --git a/ginac/pseries.h b/ginac/pseries.h index 345a5fd2..18baed39 100644 --- a/ginac/pseries.h +++ b/ginac/pseries.h @@ -43,6 +43,7 @@ public: // functions overriding virtual functions from base classes public: void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 38;} // for clarity just below add::precedence unsigned nops(void) const; ex op(int i) const; ex &let_op(int i); @@ -98,8 +99,6 @@ protected: /** Expansion point */ ex point; - - static unsigned precedence; }; /** Return a reference to the pseries object embedded in an expression. diff --git a/ginac/registrar.h b/ginac/registrar.h index 8cab2031..125d0998 100644 --- a/ginac/registrar.h +++ b/ginac/registrar.h @@ -81,7 +81,6 @@ public: \ classname(const classname & other); \ const classname & operator=(const classname & other); \ basic * duplicate() const; \ - unsigned get_precedence(void) const {return precedence;} \ protected: \ void copy(const classname & other); \ void destroy(bool call_parent); \ diff --git a/ginac/relational.cpp b/ginac/relational.cpp index 51fd0f18..7f6337d2 100644 --- a/ginac/relational.cpp +++ b/ginac/relational.cpp @@ -107,9 +107,9 @@ void relational::print(const print_context & c, unsigned level) const } else { - if (precedence <= level) + if (precedence() <= level) c.s << "("; - lh.print(c, precedence); + lh.print(c, precedence()); switch (o) { case equal: c.s << "=="; @@ -132,8 +132,8 @@ void relational::print(const print_context & c, unsigned level) const default: c.s << "(INVALID RELATIONAL OPERATOR)"; } - rh.print(c, precedence); - if (precedence <= level) + rh.print(c, precedence()); + if (precedence() <= level) c.s << ")"; } } @@ -282,12 +282,4 @@ relational::operator bool() const } } -////////// -// static member variables -////////// - -// protected - -unsigned relational::precedence = 20; - } // namespace GiNaC diff --git a/ginac/relational.h b/ginac/relational.h index dcbcacc5..c3b223b5 100644 --- a/ginac/relational.h +++ b/ginac/relational.h @@ -52,6 +52,7 @@ public: // functions overriding virtual functions from bases classes public: void print(const print_context & c, unsigned level = 0) const; + unsigned precedence(void) const {return 20;} bool info(unsigned inf) const; unsigned nops() const; ex & let_op(int i); @@ -78,7 +79,6 @@ protected: ex lh; ex rh; operators o; - static unsigned precedence; }; // utility functions -- 2.45.0