]> www.ginac.de Git - ginac.git/blobdiff - ginac/container.pl
- exprseq and lst are commutative (they shouldn't appear in products anyway,
[ginac.git] / ginac / container.pl
index 02893995da07e588f1346624f536799d72a48269..9b02154097836fb0f4718eb3b3f275018e8148bd 100755 (executable)
-#!/usr/bin/perl -w
-
-if ($#ARGV!=0) {
-    die 'usage: container.pl type (type=lst or exprseq)';
+if (($#ARGV!=0) and ($#ARGV!=1)) {
+       die 'usage: container.pl type [maxargs] (type=lst or exprseq)';
 }
 
 if ($ARGV[0] eq 'lst') {
-    $type='lst';
+       $type='lst';
 } elsif ($ARGV[0] eq 'exprseq') {
-    $type='exprseq';
+       $type='exprseq';
 } else {
-    die 'only lst and exprseq supported';
+       die 'only lst and exprseq supported';
 }
 
-#$type='lst';
-#$type='exprseq';
+if ($#ARGV==1) {
+       $maxargs=$ARGV[1];
+} else {
+       $maxargs=16; # must be greater or equal than the value used in function.pl
+}
 
 if ($type eq 'exprseq') {
 
-    # settings for exprseq
-    $CONTAINER="exprseq";
-    $STLHEADER="vector";
-    $reserve=1;
-    $prepend=0;
-    $let_op=0;
-    $open_bracket='(';
-    $close_bracket=')';
-    
+       # settings for exprseq
+       $CONTAINER="exprseq";
+       $STLHEADER="vector";
+       $reserve=1;
+       $prepend=0;
+       $sort=0;
+       $let_op=0;
+       $open_bracket='(';
+       $close_bracket=')';
+       
 } elsif ($type eq 'lst') {
  
-    # settings for lst
-    $CONTAINER="lst";
-    $STLHEADER="list";
-    $reserve=0;
-    $prepend=1;
-    $let_op=1;
-    $open_bracket='[';
-    $close_bracket=']';
+       # settings for lst
+       $CONTAINER="lst";
+       $STLHEADER="list";
+       $reserve=0;
+       $prepend=1;
+       $sort=1;
+       $let_op=1;
+       $open_bracket='{';
+       $close_bracket='}';
 
 } else {
-    die "invalid type $type";
+       die "invalid type $type";
 }
 
 $CONTAINER_UC=uc(${CONTAINER});
 $STLT="ex".$STLHEADER;
 
 if ($reserve) {
-    $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) (s).reserve(size)";
+       $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) (s).reserve(size)";
 } else {
-    $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) // no reserve needed for ${STLHEADER}";
+       $RESERVE_IMPLEMENTATION="#define RESERVE(s,size) // no reserve needed for ${STLHEADER}";
 }
 
 if ($prepend) {
-    $PREPEND_INTERFACE=<<END_OF_PREPEND_INTERFACE;
-    virtual ${CONTAINER} & prepend(ex const & b);
+       $PREPEND_INTERFACE=<<END_OF_PREPEND_INTERFACE;
+       virtual ${CONTAINER} & prepend(const ex & b);
+       virtual ${CONTAINER} & remove_first(void);
 END_OF_PREPEND_INTERFACE
 
-    $PREPEND_IMPLEMENTATION=<<END_OF_PREPEND_IMPLEMENTATION;
-${CONTAINER} & ${CONTAINER}::prepend(ex const & b)
+       $PREPEND_IMPLEMENTATION=<<END_OF_PREPEND_IMPLEMENTATION;
+${CONTAINER} & ${CONTAINER}::prepend(const ex & b)
 {
-    ensure_if_modifiable();
-    seq.push_front(b);
-    return *this;
+       ensure_if_modifiable();
+       seq.push_front(b);
+       return *this;
+}
+
+${CONTAINER} & ${CONTAINER}::remove_first(void)
+{
+       ensure_if_modifiable();
+       seq.pop_front();
+       return *this;
 }
 END_OF_PREPEND_IMPLEMENTATION
 } else {
-    $PREPEND_INTERFACE="    // no prepend possible for ${CONTAINER}";
-    $PREPEND_IMPLEMENTATION="";
+       $PREPEND_INTERFACE="    // no prepend possible for ${CONTAINER}";
+       $PREPEND_IMPLEMENTATION="";
+}
+
+if ($sort) {
+       $SORT_INTERFACE=<<END_OF_SORT_INTERFACE;
+       virtual ${CONTAINER} & sort(void);
+       virtual ${CONTAINER} & unique(void);
+END_OF_SORT_INTERFACE
+
+       $SORT_IMPLEMENTATION=<<END_OF_SORT_IMPLEMENTATION;
+${CONTAINER} & ${CONTAINER}::sort(void)
+{
+       ensure_if_modifiable();
+       seq.sort(ex_is_less());
+       return *this;
+}
+
+${CONTAINER} & ${CONTAINER}::unique(void)
+{
+       ensure_if_modifiable();
+       seq.unique(ex_is_equal());
+       return *this;
+}
+END_OF_SORT_IMPLEMENTATION
+} else {
+       $SORT_INTERFACE="    // no sort possible for ${CONTAINER}";
+       $SORT_IMPLEMENTATION="";
 }
 
 if ($let_op) {
-    $LET_OP_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION
-ex & ${CONTAINER}::let_op(int const i)
+       $LET_OP_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION
+ex & ${CONTAINER}::let_op(int i)
 {
-    GINAC_ASSERT(i>=0);
-    GINAC_ASSERT(i<nops());
+       GINAC_ASSERT(i>=0);
+       GINAC_ASSERT(i<nops());
 
-    ${STLT}::iterator it=seq.begin();
-    for (int j=0; j<i; j++) {
-        ++it;
-    }
-    return *it;
+       ${STLT}::iterator it=seq.begin();
+       for (int j=0; j<i; j++) {
+               ++it;
+       }
+       return *it;
 }
 END_OF_LET_OP_IMPLEMENTATION
 } else {
-    $LET_OP_IMPLEMENTATION="// ${CONTAINER}::let_op() will be implemented by user elsewhere";
+       $LET_OP_IMPLEMENTATION="// ${CONTAINER}::let_op() will be implemented by user elsewhere";
+}
+
+sub generate_seq {
+       my ($seq_template,$n,$separator)=@_;
+       my ($res,$N);
+       
+       $res='';
+       for ($N=1; $N<=$n; $N++) {
+               $res .= eval('"' . $seq_template . '"');
+               if ($N!=$n) {
+                       $res .= $separator;
+               }
+       }
+       return $res;
+}
+
+sub generate_from_to {
+       my ($template,$seq_template1,$seq_separator1,$seq_template2,
+           $seq_separator2,$from,$to)=@_;
+       my ($res,$N,$SEQ);
+
+       $res='';
+       for ($N=$from; $N<=$to; $N++) {
+               $SEQ1=generate_seq($seq_template1,$N,$seq_separator1);
+               $SEQ2=generate_seq($seq_template2,$N,$seq_separator2);
+               $res .= eval('"' . $template . '"');
+               $SEQ1=''; # to avoid main::SEQ1 used only once warning
+               $SEQ2=''; # same as above
+       }
+       return $res;
+}
+
+sub generate {
+       my ($template,$seq_template1,$seq_separator1,$seq_template2,
+           $seq_separator2)=@_;
+       return generate_from_to($template,$seq_template1,$seq_separator1,
+                                                       $seq_template2,$seq_separator2,1,$maxargs);
 }
 
+$constructors_interface=generate(
+'      explicit ${CONTAINER}(${SEQ1});'."\n",
+'const ex & param${N}',', ','','');
+
+$constructors_implementation=generate(
+       <<'END_OF_CONSTRUCTORS_IMPLEMENTATION','const ex & param${N}',', ','    seq.push_back(param${N});',"\n");
+${CONTAINER}::${CONTAINER}(${SEQ1}) : basic(TINFO_${CONTAINER})
+{
+       RESERVE(seq,${N});
+${SEQ2}
+}
+END_OF_CONSTRUCTORS_IMPLEMENTATION
+
 $interface=<<END_OF_INTERFACE;
 /** \@file ${CONTAINER}.h
  *
@@ -98,11 +185,13 @@ $interface=<<END_OF_INTERFACE;
  *                        \$STLHEADER=${STLHEADER}
  *                        \$reserve=${reserve}
  *                        \$prepend=${prepend}
+ *                        \$sort=${sort}
  *                        \$let_op=${let_op}
  *                        \$open_bracket=${open_bracket}
  *                        \$close_bracket=${close_bracket}
+ *                        \$maxargs=${maxargs}
  *
- *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2003 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
@@ -127,112 +216,66 @@ $interface=<<END_OF_INTERFACE;
 // CINT needs <algorithm> to work properly with <vector> and <list> 
 #include <algorithm>
 
-#include <ginac/basic.h>
-#include <ginac/ex.h>
+#include "basic.h"
+#include "ex.h"
 
-#ifndef NO_GINAC_NAMESPACE
 namespace GiNaC {
-#endif // ndef NO_GINAC_NAMESPACE
 
-// typedef ${STLHEADER}<ex> ${STLT};
-typedef ${STLHEADER}<ex,malloc_alloc> ${STLT}; // CINT does not like ${STLHEADER}<...,default_alloc>
+
+typedef std::${STLHEADER}<ex> ${STLT};
 
 class ${CONTAINER} : public basic
 {
+       GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
 
 public:
-    ${CONTAINER}();
-    ~${CONTAINER}();
-    ${CONTAINER}(${CONTAINER} const & other);
-    ${CONTAINER} const & operator=(${CONTAINER} const & other);
-protected:
-    void copy(${CONTAINER} const & other);
-    void destroy(bool call_parent);
-
-public:
-    ${CONTAINER}(${STLT} const & s, bool discardable=0);
-    ${CONTAINER}(${STLT} * vp); // vp will be deleted
-    explicit ${CONTAINER}(ex const & e1);
-    explicit ${CONTAINER}(ex const & e1, ex const & e2);
-    explicit ${CONTAINER}(ex const & e1, ex const & e2, ex const & e3);
-    explicit ${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-             ex const & e4);
-    explicit ${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-             ex const & e4, ex const & e5);
-    explicit ${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-             ex const & e4, ex const & e5, ex const & e6);
-    explicit ${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-             ex const & e4, ex const & e5, ex const & e6,
-             ex const & e7);
-    explicit ${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-             ex const & e4, ex const & e5, ex const & e6,
-             ex const & e7, ex const & e8);
-    explicit ${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-             ex const & e4, ex const & e5, ex const & e6,
-             ex const & e7, ex const & e8, ex const & e9);
-    explicit ${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-             ex const & e4, ex const & e5, ex const & e6,
-             ex const & e7, ex const & e8, ex const & e9,
-             ex const &e10);
+       ${CONTAINER}(${STLT} const & s, bool discardable = false);
+       ${CONTAINER}(${STLT} * vp); // vp will be deleted
+${constructors_interface}
 
 public:
-    basic * duplicate() const;
-    void printraw(ostream & os) const;
-    void print(ostream & os, unsigned upper_precedence=0) const;
-    void printtree(ostream & os, unsigned indent) const;
-    bool info(unsigned inf) const;
-    int nops() const;
-    ex & let_op(int const i);
-    ex expand(unsigned options=0) const;
-    bool has(ex const & 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 diff(symbol const & s) const;
-    ex subs(lst const & ls, lst const & lr) 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 eval(int level=0) const;
+       ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
 protected:
-    int compare_same_type(basic const & other) const;
-    bool is_equal_same_type(basic const & other) const;
-    unsigned return_type(void) const;
+       bool is_equal_same_type(const basic & other) const;
 
-    // new virtual functions which can be overridden by derived classes
+       // new virtual functions which can be overridden by derived classes
 public:
-    virtual ${CONTAINER} & append(ex const & b);
+       virtual ${CONTAINER} & append(const ex & b);
+       virtual ${CONTAINER} & remove_last(void);
 ${PREPEND_INTERFACE}
+${SORT_INTERFACE}
 protected:
-    virtual void printseq(ostream & os, char openbracket, char delim,
-                          char closebracket, unsigned this_precedence,
-                          unsigned upper_precedence=0) const;
-    virtual ex this${CONTAINER}(${STLT} const & v) const;
-    virtual ex this${CONTAINER}(${STLT} * vp) const;
+       virtual void printseq(const print_context & c, char openbracket, char delim,
+                             char closebracket, unsigned this_precedence,
+                             unsigned upper_precedence = 0) const;
+       virtual ex this${CONTAINER}(${STLT} const & v) const;
+       virtual ex this${CONTAINER}(${STLT} * vp) const;
 
 protected:
-    bool is_canonical() const;
-    ${STLT} evalchildren(int level) const;
-    ${STLT} evalfchildren(int level) const;
-    ${STLT} normalchildren(int level) const;
-    ${STLT} diffchildren(symbol const & s) const;
-    ${STLT} * subschildren(lst const & ls, lst const & lr) const;
+       bool is_canonical() const;
+       ${STLT} evalchildren(int level) const;
+       ${STLT} * subschildren(const lst & ls, const lst & lr, bool no_pattern = false) const;
 
 protected:
-    ${STLT} seq;
-    static unsigned precedence;
+       ${STLT} seq;
 };
 
-// global constants
-
-extern const ${CONTAINER} some_${CONTAINER};
-extern type_info const & typeid_${CONTAINER};
-
 // utility functions
-inline const ${CONTAINER} &ex_to_${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<const ${CONTAINER} &>(*e.bp);
+       return obj.tinfo()==TINFO_${CONTAINER};
 }
 
-#ifndef NO_GINAC_NAMESPACE
 } // namespace GiNaC
-#endif // ndef NO_GINAC_NAMESPACE
 
 #endif // ndef __GINAC_${CONTAINER_UC}_H__
 
@@ -253,8 +296,9 @@ $implementation=<<END_OF_IMPLEMENTATION;
  *                        \$let_op=${let_op}
  *                        \$open_bracket=${open_bracket}
  *                        \$close_bracket=${close_bracket}
+ *                        \$maxargs=${maxargs}
  *
- *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2003 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
@@ -276,383 +320,205 @@ $implementation=<<END_OF_IMPLEMENTATION;
 
 #include "${CONTAINER}.h"
 #include "ex.h"
-#include "debugmsg.h"
+#include "print.h"
+#include "archive.h"
 
-#ifndef NO_GINAC_NAMESPACE
 namespace GiNaC {
-#endif // ndef NO_GINAC_NAMESPACE
+
+GINAC_IMPLEMENT_REGISTERED_CLASS(${CONTAINER}, basic)
 
 ${RESERVE_IMPLEMENTATION}
 
 //////////
-// default constructor, destructor, copy constructor assignment operator and helpers
+// default ctor, dtor, copy ctor, assignment operator and helpers
 //////////
 
 // public
 
-${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER})
-{
-    debugmsg("${CONTAINER} default constructor",LOGLEVEL_CONSTRUCT);
-}
-
-${CONTAINER}::~${CONTAINER}()
-{
-    debugmsg("${CONTAINER} destructor",LOGLEVEL_DESTRUCT);
-    destroy(0);
-}
-
-${CONTAINER}::${CONTAINER}(${CONTAINER} const & other)
-{
-    debugmsg("${CONTAINER} copy constructor",LOGLEVEL_CONSTRUCT);
-    copy(other);
-}
-
-${CONTAINER} const & ${CONTAINER}::operator=(${CONTAINER} const & other)
-{
-    debugmsg("${CONTAINER} operator=",LOGLEVEL_ASSIGNMENT);
-    if (this != &other) {
-        destroy(1);
-        copy(other);
-    }
-    return *this;
-}
+${CONTAINER}::${CONTAINER}() : basic(TINFO_${CONTAINER}) {}
 
 // protected
 
 void ${CONTAINER}::copy(${CONTAINER} const & other)
 {
-    basic::copy(other);
-    seq=other.seq;
+       inherited::copy(other);
+       seq=other.seq;
 }
 
 void ${CONTAINER}::destroy(bool call_parent)
 {
-    seq.clear();
-    if (call_parent) basic::destroy(call_parent);
+       seq.clear();
+       if (call_parent) inherited::destroy(call_parent);
 }
 
 //////////
-// other constructors
+// other ctors
 //////////
 
 // public
 
 ${CONTAINER}::${CONTAINER}(${STLT} const & s, bool discardable) :  basic(TINFO_${CONTAINER})
 {
-    debugmsg("${CONTAINER} constructor from ${STLT}",
-             LOGLEVEL_CONSTRUCT);
-    if (discardable) {
-        seq.swap(const_cast<${STLT} &>(s));
-    } else {
-        seq=s;
-    }
+       if (discardable) {
+               seq.swap(const_cast<${STLT} &>(s));
+       } else {
+               seq=s;
+       }
 }
 
 ${CONTAINER}::${CONTAINER}(${STLT} * vp) : basic(TINFO_${CONTAINER})
 {
-    debugmsg("${CONTAINER} constructor from ${STLT} *",LOGLEVEL_CONSTRUCT);
-    GINAC_ASSERT(vp!=0);
-    seq.swap(*vp);
-    delete vp;
+       GINAC_ASSERT(vp!=0);
+       seq.swap(*vp);
+       delete vp;
 }
 
-${CONTAINER}::${CONTAINER}(ex const & e1) :  basic(TINFO_${CONTAINER})
-{
-    debugmsg("${CONTAINER} constructor from 1 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,1);
-    seq.push_back(e1);
-}
+${constructors_implementation}
 
-${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2) : basic(TINFO_${CONTAINER})
-{
-    debugmsg("${CONTAINER} constructor from 2 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,2);
-    seq.push_back(e1);
-    seq.push_back(e2);
-}
+//////////
+// archiving
+//////////
 
-${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3)
-    : basic(TINFO_${CONTAINER})
+/** Construct object from archive_node. */
+${CONTAINER}::${CONTAINER}(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
 {
-    debugmsg("${CONTAINER} constructor from 3 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,3);
-    seq.push_back(e1);
-    seq.push_back(e2);
-    seq.push_back(e3);
+       for (unsigned int i=0; true; i++) {
+               ex e;
+               if (n.find_ex("seq", e, sym_lst, i))
+                       seq.push_back(e);
+               else
+                       break;
+       }
 }
 
-${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-                     ex const & e4) : basic(TINFO_${CONTAINER})
+/** Unarchive the object. */
+ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
 {
-    debugmsg("${CONTAINER} constructor from 4 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,4);
-    seq.push_back(e1);
-    seq.push_back(e2);
-    seq.push_back(e3);
-    seq.push_back(e4);
+       return (new ${CONTAINER}(n, sym_lst))->setflag(status_flags::dynallocated);
 }
 
-${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-                     ex const & e4, ex const & e5) : basic(TINFO_${CONTAINER})
-{
-    debugmsg("${CONTAINER} constructor from 5 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,5);
-    seq.push_back(e1);
-    seq.push_back(e2);
-    seq.push_back(e3);
-    seq.push_back(e4);
-    seq.push_back(e5);
-}
-
-${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-                     ex const & e4, ex const & e5, ex const & e6)
-    : basic(TINFO_${CONTAINER})
-{
-    debugmsg("${CONTAINER} constructor from 6 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,6);
-    seq.push_back(e1);
-    seq.push_back(e2);
-    seq.push_back(e3);
-    seq.push_back(e4);
-    seq.push_back(e5);
-    seq.push_back(e6);
-}
-
-${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-                     ex const & e4, ex const & e5, ex const & e6,
-                     ex const & e7) : basic(TINFO_${CONTAINER})
-{
-    debugmsg("${CONTAINER} constructor from 7 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,7);
-    seq.push_back(e1);
-    seq.push_back(e2);
-    seq.push_back(e3);
-    seq.push_back(e4);
-    seq.push_back(e5);
-    seq.push_back(e6);
-    seq.push_back(e7);
-}
-
-${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-                     ex const & e4, ex const & e5, ex const & e6,
-                     ex const & e7, ex const & e8) : basic(TINFO_${CONTAINER})
+/** Archive the object. */
+void ${CONTAINER}::archive(archive_node &n) const
 {
-    debugmsg("${CONTAINER} constructor from 8 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,8);
-    seq.push_back(e1);
-    seq.push_back(e2);
-    seq.push_back(e3);
-    seq.push_back(e4);
-    seq.push_back(e5);
-    seq.push_back(e6);
-    seq.push_back(e7);
-    seq.push_back(e8);
-}
-
-${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-                     ex const & e4, ex const & e5, ex const & e6,
-                     ex const & e7, ex const & e8, ex const & e9)
-    : basic(TINFO_${CONTAINER})
-{
-    debugmsg("${CONTAINER} constructor from 9 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,9);
-    seq.push_back(e1);
-    seq.push_back(e2);
-    seq.push_back(e3);
-    seq.push_back(e4);
-    seq.push_back(e5);
-    seq.push_back(e6);
-    seq.push_back(e7);
-    seq.push_back(e8);
-    seq.push_back(e9);
-}
-
-${CONTAINER}::${CONTAINER}(ex const & e1, ex const & e2, ex const & e3,
-                     ex const & e4, ex const & e5, ex const & e6,
-                     ex const & e7, ex const & e8, ex const & e9,
-                     ex const &e10)
-    : basic(TINFO_${CONTAINER})
-{
-    debugmsg("${CONTAINER} constructor from 10 ex",
-             LOGLEVEL_CONSTRUCT);
-    RESERVE(seq,10);
-    seq.push_back(e1);
-    seq.push_back(e2);
-    seq.push_back(e3);
-    seq.push_back(e4);
-    seq.push_back(e5);
-    seq.push_back(e6);
-    seq.push_back(e7);
-    seq.push_back(e8);
-    seq.push_back(e9);
-    seq.push_back(e10);
+       inherited::archive(n);
+       ${STLT}::const_iterator i = seq.begin(), end = seq.end();
+       while (i != end) {
+               n.add_ex("seq", *i);
+               ++i;
+       }
 }
 
 //////////
-// functions overriding virtual functions from bases classes
+// functions overriding virtual functions from base classes
 //////////
 
 // public
 
-basic * ${CONTAINER}::duplicate() const
-{
-    debugmsg("${CONTAINER} duplicate",LOGLEVEL_DUPLICATE);
-    return new ${CONTAINER}(*this);
-}
-
-void ${CONTAINER}::printraw(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 << ")";
-}
-
-void ${CONTAINER}::print(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);
-}
-
-void ${CONTAINER}::printtree(ostream & os, unsigned indent) const
-{
-    debugmsg("${CONTAINER} printtree",LOGLEVEL_PRINT);
-
-    os << string(indent,' ') << "type=" << typeid(*this).name()
-       << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
-       << ", flags=" << flags
-       << ", nops=" << nops() << endl;
-    for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
-        (*cit).printtree(os,indent+delta_indent);
-    }
-    os << string(indent+delta_indent,' ') << "=====" << endl;
+void ${CONTAINER}::print(const print_context & c, unsigned level) const
+{
+       if (is_a<print_tree>(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<const print_tree &>(c).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<print_python>(c)) {
+               printseq(c, '[', ',', ']', precedence(), precedence()+1);
+       } else if (is_a<print_python_repr>(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);
+       }
 }
 
 // ${CONTAINER}::info() will be implemented by user elsewhere";
 
-int ${CONTAINER}::nops() const
+unsigned ${CONTAINER}::nops() const
 {
-    return seq.size();
+       return seq.size();
 }
 
 ${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));
-    }
+       // This implementation is here because basic::map() uses let_op()
+       // which is not defined for all containers
+       ${STLT} s;
+       RESERVE(s,seq.size());
+       ${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(ex const & 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;
+       return this${CONTAINER}(s);
 }
 
 ex ${CONTAINER}::eval(int level) const
 {
-    if (level==1) {
-        return this->hold();
-    }
-    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}::diff(symbol const & s) const
-{
-    return this${CONTAINER}(diffchildren(s));
+       if (level==1) {
+               return this->hold();
+       }
+       return this${CONTAINER}(evalchildren(level));
 }
 
-ex ${CONTAINER}::subs(lst const & ls, lst const & 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 ex_to<basic>(this${CONTAINER}(vp)).basic::subs(ls, lr, no_pattern);
+       else
+               return basic::subs(ls, lr, no_pattern);
 }
 
 // protected
 
-int ${CONTAINER}::compare_same_type(basic const & other) const
+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<basic &>(other));
-    int cmpval;
-    ${STLT}::const_iterator it1=seq.begin();
-    ${STLT}::const_iterator it2=o.seq.begin();
+       GINAC_ASSERT(is_a<${CONTAINER}>(other));
+       ${CONTAINER} const & o = static_cast<const ${CONTAINER} &>(other);
 
-    for (; (it1!=seq.end())&&(it2!=o.seq.end()); ++it1, ++it2) {
-        cmpval=(*it1).compare(*it2);
-        if (cmpval!=0) return cmpval;
-    }
+       ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
+                               it2 = o.seq.begin(), it2end = o.seq.end();
 
-    if (it1==seq.end()) {
-        return (it2==o.seq.end() ? 0 : -1);
-    }
+       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(basic const & 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<basic &>(other));
-    if (seq.size()!=o.seq.size()) return false;
+       GINAC_ASSERT(is_a<${CONTAINER}>(other));
+       ${CONTAINER} const &o = static_cast<const ${CONTAINER} &>(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();
 
-    return true;
-}
+       while (it1 != it1end) {
+               if (!it1->is_equal(*it2))
+                       return false;
+               ++it1; ++it2;
+       }
 
-unsigned ${CONTAINER}::return_type(void) const
-{
-    return return_types::noncommutative_composite;
+       return true;
 }
 
 //////////
@@ -661,44 +527,56 @@ unsigned ${CONTAINER}::return_type(void) const
 
 // public
 
-${CONTAINER} & ${CONTAINER}::append(ex const & b)
+${CONTAINER} & ${CONTAINER}::append(const ex & b)
 {
-    ensure_if_modifiable();
-    seq.push_back(b);
-    return *this;
+       ensure_if_modifiable();
+       seq.push_back(b);
+       return *this;
+}
+
+${CONTAINER} & ${CONTAINER}::remove_last(void)
+{
+       ensure_if_modifiable();
+       seq.pop_back();
+       return *this;
 }
 
 ${PREPEND_IMPLEMENTATION}
 
+${SORT_IMPLEMENTATION}
+
 // protected
 
-void ${CONTAINER}::printseq(ostream & os, char openbracket, char delim,
-                         char closebracket, unsigned this_precedence,
-                         unsigned upper_precedence) const
+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;
-        }
-        (*it).bp->print(os,this_precedence);
-    }
-    if (this_precedence<=upper_precedence) os << closebracket;
+       if (this_precedence <= upper_precedence)
+               c.s << openbracket;
+
+       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->print(c, this_precedence);
+       }
+
+       if (this_precedence <= upper_precedence)
+               c.s << closebracket;
 }
 
 ex ${CONTAINER}::this${CONTAINER}(${STLT} const & v) const
 {
-    return ${CONTAINER}(v);
+       return ${CONTAINER}(v);
 }
 
 ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
 {
-    return ${CONTAINER}(vp);
+       return ${CONTAINER}(vp);
 }
 
 //////////
@@ -713,154 +591,82 @@ ex ${CONTAINER}::this${CONTAINER}(${STLT} * vp) const
 
 bool ${CONTAINER}::is_canonical() const
 {
-    if (seq.size()<=1) { return 1; }
+       if (seq.size()<=1) { return 1; }
 
-    ${STLT}::const_iterator it=seq.begin();
-    ${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) {
-                cout << *it_last << ">" << *it << "\\n";
-                return 0;
-               }
-        }
-    }
-    return 1;
+       ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
+       ${STLT}::const_iterator it_last=it;
+       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;
+                       }
+               }
+       }
+       return 1;
 }
 
 
 ${STLT} ${CONTAINER}::evalchildren(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).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(symbol const & 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(lst const & ls, lst const & 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} s;
+       RESERVE(s,seq.size());
+
+       if (level==1) {
+               return seq;
+       }
+       if (level == -max_recursion_level) {
+               throw(std::runtime_error("max recursion level reached"));
+       }
+       --level;
+       ${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, 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 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());
+
+                       // copy parts of seq which are known not to have changed
+                       ${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 != end) {
+                               s->push_back(cit2->subs(ls, lr, no_pattern));
+                               ++cit2;
+                       }
+                       return s;
+               }
+               ++cit;
+       }
+       
+       return 0; // nothing has changed
 }
-*/
-
-${STLT} * ${CONTAINER}::subschildren(lst const & ls, lst const & lr) 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) {
-        ex const & subsed_ex=(*cit).subs(ls,lr);
-        if (!are_ex_trivially_equal(*cit,subsed_ex)) {
-
-            // something changed, copy seq, subs and return it
-            ${STLT} *s=new ${STLT};
-            RESERVE(*s,seq.size());
-
-            // copy parts of seq which are known not to have changed
-            ${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));
-                ++cit2;
-           }
-            return s;
-        }
-        ++cit;
-    }
-    
-    return 0; // nothing has changed
-}
-
-//////////
-// static member variables
-//////////
-
-// protected
-
-unsigned ${CONTAINER}::precedence=10;
-
-//////////
-// global constants
-//////////
-
-const ${CONTAINER} some_${CONTAINER};
-type_info const & typeid_${CONTAINER}=typeid(some_${CONTAINER});
 
-#ifndef NO_GINAC_NAMESPACE
 } // namespace GiNaC
-#endif // ndef NO_GINAC_NAMESPACE
 
 END_OF_IMPLEMENTATION