X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fcontainer.pl;h=d187d0654d6973076b9bacc2431d732678b73d53;hp=af9d5372ba4616cb441ed5c399a090c776fd490c;hb=835af1e00da6ec929466e961357d4ef4917b1fb9;hpb=ac58769a3265fa850c74f5d601b6f68077276d84 diff --git a/ginac/container.pl b/ginac/container.pl index af9d5372..d187d065 100755 --- a/ginac/container.pl +++ b/ginac/container.pl @@ -13,7 +13,7 @@ if ($ARGV[0] eq 'lst') { if ($#ARGV==1) { $maxargs=$ARGV[1]; } else { - $maxargs=15; # must be greater or equal than the value used in function.pl + $maxargs=16; # must be greater or equal than the value used in function.pl } if ($type eq 'exprseq') { @@ -23,6 +23,7 @@ if ($type eq 'exprseq') { $STLHEADER="vector"; $reserve=1; $prepend=0; + $sort=0; $let_op=0; $open_bracket='('; $close_bracket=')'; @@ -34,9 +35,10 @@ if ($type eq 'exprseq') { $STLHEADER="list"; $reserve=0; $prepend=1; + $sort=1; $let_op=1; - $open_bracket='['; - $close_bracket=']'; + $open_bracket='{'; + $close_bracket='}'; } else { die "invalid type $type"; @@ -54,6 +56,7 @@ if ($reserve) { if ($prepend) { $PREPEND_INTERFACE=< but malloc_alloc is -// unstandardized and not supported by newer GCCs. This ugly hack will go -// away soon! -#if (defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ < 97)) || (defined(G__GNUC) && (G__GNUC == 2) && (G__GNUC_MINOR < 97)) -typedef std::${STLHEADER} ${STLT}; -#else typedef std::${STLHEADER} ${STLT}; -#endif class ${CONTAINER} : public basic { GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic) public: - ${CONTAINER}(${STLT} const & s, bool discardable=0); + ${CONTAINER}(${STLT} const & s, bool discardable = false); ${CONTAINER}(${STLT} * vp); // vp will be deleted ${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); - ex expand(unsigned options=0) const; - bool has(const ex & other) const; + ex map(map_function & f) const; ex eval(int level=0) const; - ex evalf(int level=0) const; - ex normal(lst &sym_lst, lst &repl_lst, int level=0) const; - ex derivative(const symbol & s) const; - ex subs(const lst & ls, const lst & lr) const; + ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const; protected: bool is_equal_same_type(const basic & other) const; unsigned return_type(void) const; @@ -223,7 +249,9 @@ protected: // new virtual functions which can be overridden by derived classes public: virtual ${CONTAINER} & append(const ex & b); + virtual ${CONTAINER} & remove_last(void); ${PREPEND_INTERFACE} +${SORT_INTERFACE} protected: virtual void printseq(const print_context & c, char openbracket, char delim, char closebracket, unsigned this_precedence, @@ -234,25 +262,18 @@ protected: protected: bool is_canonical() const; ${STLT} evalchildren(int level) const; - ${STLT} evalfchildren(int level) const; - ${STLT} normalchildren(int level) const; - ${STLT} diffchildren(const symbol & s) const; - ${STLT} * subschildren(const lst & ls, const lst & lr) const; + ${STLT} * subschildren(const lst & ls, const lst & lr, bool no_pattern = false) const; protected: ${STLT} seq; - static unsigned precedence; }; // utility functions -inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e) -{ - return static_cast(*e.bp); -} -inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e) +/** Specialization of is_exactly_a<${CONTAINER}>(obj) for ${CONTAINER} objects. */ +template<> inline bool is_exactly_a<${CONTAINER}>(const basic & obj) { - return static_cast<${CONTAINER} &>(*e.bp); + return obj.tinfo()==TINFO_${CONTAINER}; } } // namespace GiNaC @@ -278,7 +299,7 @@ $implementation=<(s)); } else { @@ -353,7 +369,6 @@ ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_$ ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER}) { - debugmsg("${CONTAINER} ctor from ${STLT} *",LOGLEVEL_CONSTRUCT); GINAC_ASSERT(vp!=0); seq.swap(*vp); delete vp; @@ -368,7 +383,6 @@ ${constructors_implementation} /** Construct object from archive_node. */ ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) { - debugmsg("${CONTAINER} ctor from archive_node", LOGLEVEL_CONSTRUCT); for (unsigned int i=0; true; i++) { ex e; if (n.find_ex("seq", e, sym_lst, i)) @@ -388,37 +402,42 @@ ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst) void ${CONTAINER}::archive(archive_node &n) const { inherited::archive(n); - ${STLT}::const_iterator i = seq.begin(), iend = seq.end(); - while (i != iend) { + ${STLT}::const_iterator i = seq.begin(), end = seq.end(); + while (i != end) { n.add_ex("seq", *i); - i++; + ++i; } } ////////// -// functions overriding virtual functions from bases classes +// functions overriding virtual functions from base classes ////////// // public void ${CONTAINER}::print(const print_context & c, unsigned level) const { - debugmsg("${CONTAINER} print", LOGLEVEL_PRINT); - - if (is_of_type(c, print_tree)) { + if (is_a(c)) { c.s << std::string(level, ' ') << class_name() << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec << ", nops=" << nops() << std::endl; unsigned delta_indent = static_cast(c).delta_indent; - for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) - cit->print(c, level + delta_indent); + ${STLT}::const_iterator i = seq.begin(), end = seq.end(); + while (i != end) { + i->print(c, level + delta_indent); + ++i; + } c.s << std::string(level + delta_indent,' ') << "=====" << std::endl; - + } else if (is_a(c)) { + printseq(c, '[', ',', ']', precedence(), precedence()+1); + } else if (is_a(c)) { + c.s << class_name (); + printseq(c, '(', ',', ')', precedence(), precedence()+1); } 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); } } @@ -431,29 +450,21 @@ unsigned ${CONTAINER}::nops() const ${LET_OP_IMPLEMENTATION} -ex ${CONTAINER}::expand(unsigned options) const +ex ${CONTAINER}::map(map_function & f) const { + // This implementation is here because basic::map() uses let_op() + // which is not defined for all containers ${STLT} s; RESERVE(s,seq.size()); - for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - s.push_back((*it).expand(options)); + ${STLT}::const_iterator i = seq.begin(), end = seq.end(); + while (i != end) { + s.push_back(f(*i)); + ++i; } return this${CONTAINER}(s); } -// a ${CONTAINER} 'has' an expression if it is this expression itself or a child 'has' it - -bool ${CONTAINER}::has(const ex & other) const -{ - GINAC_ASSERT(other.bp!=0); - if (is_equal(*other.bp)) return true; - for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - if ((*it).has(other)) return true; - } - return false; -} - ex ${CONTAINER}::eval(int level) const { if (level==1) { @@ -462,69 +473,50 @@ ex ${CONTAINER}::eval(int level) const return this${CONTAINER}(evalchildren(level)); } -ex ${CONTAINER}::evalf(int level) const -{ - return this${CONTAINER}(evalfchildren(level)); -} - -/** Implementation of ex::normal() for ${CONTAINER}s. It normalizes the arguments - * and replaces the ${CONTAINER} by a temporary symbol. - * \@see ex::normal */ -ex ${CONTAINER}::normal(lst &sym_lst, lst &repl_lst, int level) const -{ - ex n=this${CONTAINER}(normalchildren(level)); - return n.bp->basic::normal(sym_lst,repl_lst,level); -} - -ex ${CONTAINER}::derivative(const symbol & s) const -{ - return this${CONTAINER}(diffchildren(s)); -} - -ex ${CONTAINER}::subs(const lst & ls, const lst & lr) const +ex ${CONTAINER}::subs(const lst & ls, const lst & lr, bool no_pattern) const { - ${STLT} * vp=subschildren(ls,lr); - if (vp==0) - return inherited::subs(ls, lr); - - return this${CONTAINER}(vp); + ${STLT} *vp = subschildren(ls, lr, no_pattern); + if (vp) + return ex_to(this${CONTAINER}(vp)).basic::subs(ls, lr, no_pattern); + else + return basic::subs(ls, lr, no_pattern); } // protected int ${CONTAINER}::compare_same_type(const basic & other) const { - GINAC_ASSERT(is_of_type(other,${CONTAINER})); - ${CONTAINER} const & o=static_cast<${CONTAINER} const &> - (const_cast(other)); - int cmpval; - ${STLT}::const_iterator it1=seq.begin(); - ${STLT}::const_iterator it2=o.seq.begin(); - - for (; (it1!=seq.end())&&(it2!=o.seq.end()); ++it1, ++it2) { - cmpval=(*it1).compare(*it2); - if (cmpval!=0) return cmpval; - } + GINAC_ASSERT(is_a<${CONTAINER}>(other)); + ${CONTAINER} const & o = static_cast(other); - if (it1==seq.end()) { - return (it2==o.seq.end() ? 0 : -1); + ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(), + it2 = o.seq.begin(), it2end = o.seq.end(); + + while (it1 != it1end && it2 != it2end) { + int cmpval = it1->compare(*it2); + if (cmpval) + return cmpval; + ++it1; ++it2; } - return 1; + return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1; } bool ${CONTAINER}::is_equal_same_type(const basic & other) const { - GINAC_ASSERT(is_of_type(other,${CONTAINER})); - ${CONTAINER} const & o=static_cast<${CONTAINER} const &> - (const_cast(other)); - if (seq.size()!=o.seq.size()) return false; + GINAC_ASSERT(is_a<${CONTAINER}>(other)); + ${CONTAINER} const &o = static_cast(other); - ${STLT}::const_iterator it1=seq.begin(); - ${STLT}::const_iterator it2=o.seq.begin(); + if (seq.size() != o.seq.size()) + return false; - for (; it1!=seq.end(); ++it1, ++it2) { - if (!(*it1).is_equal(*it2)) return false; + ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(), + it2 = o.seq.begin(); + + while (it1 != it1end) { + if (!it1->is_equal(*it2)) + return false; + ++it1; ++it2; } return true; @@ -548,8 +540,17 @@ ${CONTAINER} & ${CONTAINER}::append(const ex & b) return *this; } +${CONTAINER} & ${CONTAINER}::remove_last(void) +{ + ensure_if_modifiable(); + seq.pop_back(); + return *this; +} + ${PREPEND_IMPLEMENTATION} +${SORT_IMPLEMENTATION} + // protected void ${CONTAINER}::printseq(const print_context & c, char openbracket, char delim, @@ -559,13 +560,13 @@ void ${CONTAINER}::printseq(const print_context & c, char openbracket, char deli if (this_precedence <= upper_precedence) c.s << openbracket; - if (seq.size() != 0) { + if (!seq.empty()) { ${STLT}::const_iterator it = seq.begin(), itend = seq.end(); --itend; while (it != itend) { it->print(c, this_precedence); c.s << delim; - it++; + ++it; } it->print(c, this_precedence); } @@ -598,11 +599,11 @@ bool ${CONTAINER}::is_canonical() const { if (seq.size()<=1) { return 1; } - ${STLT}::const_iterator it=seq.begin(); + ${STLT}::const_iterator it = seq.begin(), itend = seq.end(); ${STLT}::const_iterator it_last=it; - for (++it; it!=seq.end(); it_last=it, ++it) { - if ((*it_last).compare(*it)>0) { - if ((*it_last).compare(*it)>0) { + for (++it; it!=itend; it_last=it, ++it) { + if (it_last->compare(*it)>0) { + if (it_last->compare(*it)>0) { std::cout << *it_last << ">" << *it << "\\n"; return 0; } @@ -624,98 +625,43 @@ ${STLT} ${CONTAINER}::evalchildren(int level) const throw(std::runtime_error("max recursion level reached")); } --level; - for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - s.push_back((*it).eval(level)); - } - return s; -} - -${STLT} ${CONTAINER}::evalfchildren(int level) const -{ - ${STLT} s; - RESERVE(s,seq.size()); - - if (level==1) { - return seq; - } - if (level == -max_recursion_level) { - throw(std::runtime_error("max recursion level reached")); - } - --level; - for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - s.push_back((*it).evalf(level)); - } - return s; -} - -${STLT} ${CONTAINER}::normalchildren(int level) const -{ - ${STLT} s; - RESERVE(s,seq.size()); - - if (level==1) { - return seq; - } - if (level == -max_recursion_level) { - throw(std::runtime_error("max recursion level reached")); - } - --level; - for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - s.push_back((*it).normal(level)); - } - return s; -} - -${STLT} ${CONTAINER}::diffchildren(const symbol & y) const -{ - ${STLT} s; - RESERVE(s,seq.size()); - for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - s.push_back((*it).diff(y)); - } - return s; -} - -/* obsolete subschildren -${STLT} ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const -{ - ${STLT} s; - RESERVE(s,seq.size()); - for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - s.push_back((*it).subs(ls,lr)); + ${STLT}::const_iterator it = seq.begin(), itend = seq.end(); + while (it != itend) { + s.push_back(it->eval(level)); + ++it; } return s; } -*/ -${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const +${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr, bool no_pattern) const { // returns a NULL pointer if nothing had to be substituted // returns a pointer to a newly created epvector otherwise // (which has to be deleted somewhere else) - ${STLT}::const_iterator last=seq.end(); - ${STLT}::const_iterator cit=seq.begin(); - while (cit!=last) { - const ex & subsed_ex=(*cit).subs(ls,lr); - if (!are_ex_trivially_equal(*cit,subsed_ex)) { + ${STLT}::const_iterator cit = seq.begin(), end = seq.end(); + while (cit != end) { + const ex & subsed_ex = cit->subs(ls, lr, no_pattern); + if (!are_ex_trivially_equal(*cit, subsed_ex)) { // something changed, copy seq, subs and return it ${STLT} *s=new ${STLT}; - RESERVE(*s,seq.size()); + RESERVE(*s, seq.size()); // copy parts of seq which are known not to have changed - ${STLT}::const_iterator cit2=seq.begin(); - while (cit2!=cit) { + ${STLT}::const_iterator cit2 = seq.begin(); + while (cit2 != cit) { s->push_back(*cit2); ++cit2; } + // copy first changed element s->push_back(subsed_ex); ++cit2; + // copy rest - while (cit2!=last) { - s->push_back((*cit2).subs(ls,lr)); + while (cit2 != end) { + s->push_back(cit2->subs(ls, lr, no_pattern)); ++cit2; } return s; @@ -726,14 +672,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