X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?a=blobdiff_plain;ds=sidebyside;f=ginac%2Fcontainer.pl;h=6db886ccd234ba9e08fec3683f58260a401f4466;hb=5fc0922748250f2ce620a4fd46508011b6d7adde;hp=232d299c6fc12e0bb073ab5aa59c9a7923b1d694;hpb=e5362a33f72613b324b3714524a8c2e5f7b7f46f;p=ginac.git diff --git a/ginac/container.pl b/ginac/container.pl index 232d299c..6db886cc 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') { @@ -35,8 +35,8 @@ if ($type eq 'exprseq') { $reserve=0; $prepend=1; $let_op=1; - $open_bracket='['; - $close_bracket=']'; + $open_bracket='{'; + $close_bracket='}'; } else { die "invalid type $type"; @@ -54,6 +54,7 @@ if ($reserve) { if ($prepend) { $PREPEND_INTERFACE=< but malloc_alloc is -// unstandardized and not supported by newer GCCs. -#if defined(__GNUC__) && ((__GNUC__ == 2) && (__GNUC_MINOR__ < 97)) -typedef std::${STLHEADER} ${STLT}; -#else typedef std::${STLHEADER} ${STLT}; -#endif class ${CONTAINER} : public basic { @@ -206,33 +205,31 @@ public: ${constructors_interface} public: - basic * duplicate() const; - void printraw(std::ostream & os) const; - void print(std::ostream & os, unsigned upper_precedence=0) const; - void printtree(std::ostream & os, unsigned indent) 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 & let_op(int i); + ex map(map_function & f) const; ex expand(unsigned options=0) const; - bool has(const ex & other) 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: - int compare_same_type(const basic & other) const; bool is_equal_same_type(const basic & other) const; unsigned return_type(void) const; // new virtual functions which can be overridden by derived classes public: virtual ${CONTAINER} & append(const ex & b); + virtual ${CONTAINER} & remove_last(void); ${PREPEND_INTERFACE} protected: - virtual void printseq(std::ostream & os, char openbracket, char delim, + virtual void printseq(const print_context & c, char openbracket, char delim, char closebracket, unsigned this_precedence, - unsigned upper_precedence=0) const; + unsigned upper_precedence = 0) const; virtual ex this${CONTAINER}(${STLT} const & v) const; virtual ex this${CONTAINER}(${STLT} * vp) const; @@ -242,32 +239,33 @@ protected: ${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; }; -// global constants - -extern const ${CONTAINER} some_${CONTAINER}; -extern const std::type_info & typeid_${CONTAINER}; - // utility functions + +/** Return the ${CONTAINER} object handled by an ex. Deprecated: use ex_to<${CONTAINER}>(). + * This is unsafe: you need to check the type first. */ inline const ${CONTAINER} &ex_to_${CONTAINER}(const ex &e) { return static_cast(*e.bp); } +/** Specialization of is_exactly_a<${CONTAINER}>(obj) for ${CONTAINER} objects. */ +template<> inline bool is_exactly_a<${CONTAINER}>(const basic & obj) +{ + return obj.tinfo()==TINFO_${CONTAINER}; +} + inline ${CONTAINER} &ex_to_nonconst_${CONTAINER}(const ex &e) { return static_cast<${CONTAINER} &>(*e.bp); } -#ifndef NO_NAMESPACE_GINAC } // namespace GiNaC -#endif // ndef NO_NAMESPACE_GINAC #endif // ndef __GINAC_${CONTAINER_UC}_H__ @@ -312,26 +310,25 @@ $implementation=<(s)); } else { @@ -366,7 +363,7 @@ ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) : basic(TINFO_$ ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER}) { - debugmsg("${CONTAINER} constructor from ${STLT} *",LOGLEVEL_CONSTRUCT); + debugmsg("${CONTAINER} ctor from ${STLT} *",LOGLEVEL_CONSTRUCT); GINAC_ASSERT(vp!=0); seq.swap(*vp); delete vp; @@ -381,7 +378,7 @@ ${constructors_implementation} /** Construct object from archive_node. */ ${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) { - debugmsg("${CONTAINER} constructor from archive_node", LOGLEVEL_CONSTRUCT); + 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)) @@ -414,44 +411,25 @@ void ${CONTAINER}::archive(archive_node &n) const // public -basic * ${CONTAINER}::duplicate() const +void ${CONTAINER}::print(const print_context & c, unsigned level) const { - debugmsg("${CONTAINER} duplicate",LOGLEVEL_DUPLICATE); - return new ${CONTAINER}(*this); -} - -void ${CONTAINER}::printraw(std::ostream & os) const -{ - debugmsg("${CONTAINER} printraw",LOGLEVEL_PRINT); - - os << "${CONTAINER}("; - for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) { - (*cit).bp->printraw(os); - os << ","; - } - os << ")"; -} + debugmsg("${CONTAINER} print", LOGLEVEL_PRINT); -void ${CONTAINER}::print(std::ostream & os, unsigned upper_precedence) const -{ - debugmsg("${CONTAINER} print",LOGLEVEL_PRINT); - // always print brackets around seq, ignore upper_precedence - printseq(os,'${open_bracket}',',','${close_bracket}',precedence,precedence+1); -} + if (is_a(c)) { -void ${CONTAINER}::printtree(std::ostream & os, unsigned indent) const -{ - debugmsg("${CONTAINER} printtree",LOGLEVEL_PRINT); + 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); + c.s << std::string(level + delta_indent,' ') << "=====" << std::endl; - os << std::string(indent,' ') << "type=" << class_name() - << ", hash=" << hashvalue - << " (0x" << std::hex << hashvalue << std::dec << ")" - << ", flags=" << flags - << ", nops=" << nops() << std::endl; - for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) { - (*cit).printtree(os,indent+delta_indent); + } else { + // always print brackets around seq, ignore upper_precedence + printseq(c, '${open_bracket}', ',', '${close_bracket}', precedence(), precedence()+1); } - os << std::string(indent+delta_indent,' ') << "=====" << std::endl; } // ${CONTAINER}::info() will be implemented by user elsewhere"; @@ -463,27 +441,26 @@ unsigned ${CONTAINER}::nops() const ${LET_OP_IMPLEMENTATION} -ex ${CONTAINER}::expand(unsigned options) const +ex ${CONTAINER}::map(map_function & f) const { ${STLT} s; RESERVE(s,seq.size()); for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - s.push_back((*it).expand(options)); + s.push_back(f(*it)); } 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 +ex ${CONTAINER}::expand(unsigned options) const { - GINAC_ASSERT(other.bp!=0); - if (is_equal(*other.bp)) return true; + ${STLT} s; + RESERVE(s,seq.size()); for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) { - if ((*it).has(other)) return true; + s.push_back((*it).expand(options)); } - return false; + + return this${CONTAINER}(s); } ex ${CONTAINER}::eval(int level) const @@ -513,20 +490,20 @@ 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 *this; - } - return this${CONTAINER}(vp); + ${STLT} *vp = subschildren(ls, lr, no_pattern); + if (vp) + return this${CONTAINER}(vp).bp->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})); + GINAC_ASSERT(is_a<${CONTAINER}>(other)); ${CONTAINER} const & o=static_cast<${CONTAINER} const &> (const_cast(other)); int cmpval; @@ -547,13 +524,12 @@ int ${CONTAINER}::compare_same_type(const basic & other) const 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)); + GINAC_ASSERT(is_a<${CONTAINER}>(other)); + ${CONTAINER} const & o = static_cast<${CONTAINER} const &>(const_cast(other)); if (seq.size()!=o.seq.size()) return false; - ${STLT}::const_iterator it1=seq.begin(); - ${STLT}::const_iterator it2=o.seq.begin(); + ${STLT}::const_iterator it1 = seq.begin(); + ${STLT}::const_iterator it2 = o.seq.begin(); for (; it1!=seq.end(); ++it1, ++it2) { if (!(*it1).is_equal(*it2)) return false; @@ -580,27 +556,37 @@ ${CONTAINER} & ${CONTAINER}::append(const ex & b) return *this; } +${CONTAINER} & ${CONTAINER}::remove_last(void) +{ + ensure_if_modifiable(); + seq.pop_back(); + return *this; +} + ${PREPEND_IMPLEMENTATION} // protected -void ${CONTAINER}::printseq(std::ostream & os, char openbracket, char delim, +void ${CONTAINER}::printseq(const print_context & c, char openbracket, char delim, char closebracket, unsigned this_precedence, unsigned upper_precedence) const { - if (this_precedence<=upper_precedence) os << openbracket; - if (seq.size()!=0) { - ${STLT}::const_iterator it,it_last; - it=seq.begin(); - it_last=seq.end(); - --it_last; - for (; it!=it_last; ++it) { - (*it).bp->print(os,this_precedence); - os << delim; + if (this_precedence <= upper_precedence) + c.s << openbracket; + + if (seq.size() != 0) { + ${STLT}::const_iterator it = seq.begin(), itend = seq.end(); + --itend; + while (it != itend) { + it->print(c, this_precedence); + c.s << delim; + it++; } - (*it).bp->print(os,this_precedence); + it->print(c, this_precedence); } - if (this_precedence<=upper_precedence) os << closebracket; + + if (this_precedence <= upper_precedence) + c.s << closebracket; } ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const @@ -705,19 +691,7 @@ ${STLT} ${CONTAINER}::diffchildren(const symbol & y) const 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)); - } - 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 @@ -726,7 +700,7 @@ ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const ${STLT}::const_iterator last=seq.end(); ${STLT}::const_iterator cit=seq.begin(); while (cit!=last) { - const ex & subsed_ex=(*cit).subs(ls,lr); + 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 @@ -744,7 +718,7 @@ ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const ++cit2; // copy rest while (cit2!=last) { - s->push_back((*cit2).subs(ls,lr)); + s->push_back((*cit2).subs(ls,lr,no_pattern)); ++cit2; } return s; @@ -755,24 +729,7 @@ ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr) const return 0; // nothing has changed } -////////// -// static member variables -////////// - -// protected - -unsigned ${CONTAINER}::precedence=10; - -////////// -// global constants -////////// - -const ${CONTAINER} some_${CONTAINER}; -const std::type_info & typeid_${CONTAINER} = typeid(some_${CONTAINER}); - -#ifndef NO_NAMESPACE_GINAC } // namespace GiNaC -#endif // ndef NO_NAMESPACE_GINAC END_OF_IMPLEMENTATION