]> www.ginac.de Git - ginac.git/blobdiff - ginac/container.h
Synced to HEAD
[ginac.git] / ginac / container.h
index 9a40f2edea9b7954667fa26820c18794b37ce9fd..a3fc3989e19da130926dd943c75647148c1bc4ce 100644 (file)
@@ -3,7 +3,7 @@
  *  Wrapper template for making GiNaC classes out of STL containers. */
 
 /*
- *  GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2004 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
@@ -28,6 +28,7 @@
 #include <algorithm>
 #include <vector>
 #include <list>
+#include <memory>
 
 #include "ex.h"
 #include "print.h"
@@ -66,6 +67,63 @@ template <>
 inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
 
 
+/** Helper template to allow initialization of containers via an overloaded
+ *  comma operator (idea stolen from Blitz++). */
+template <typename T, typename STLT>
+class container_init {
+public:
+       container_init(STLT & s) : stlt(s) {}
+
+       container_init<T, STLT> operator,(const T & x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       // The following specializations produce much tighter code than the
+       // general case above
+
+       container_init<T, STLT> operator,(int x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(unsigned int x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(long x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(unsigned long x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(double x)
+       {
+               stlt.push_back(x);
+               return container_init<T, STLT>(stlt);
+       }
+
+       container_init<T, STLT> operator,(const symbol & x)
+       {
+               stlt.push_back(T(x));
+               return container_init<T, STLT>(stlt);
+       }
+
+private:
+       container_init();
+       STLT & stlt;
+};
+
 /** Wrapper template for making GiNaC classes out of STL containers. */
 template <template <class> class C>
 class container : public basic, public container_storage<C> {
@@ -80,96 +138,112 @@ public:
 protected:
        // helpers
        static unsigned get_tinfo() { return TINFO_fail; }
+       static unsigned get_default_flags() { return 0; }
        static char get_open_delim() { return '('; }
        static char get_close_delim() { return ')'; }
 
        // constructors
 public:
-       container(STLT const & s, bool discardable = false)
+       container(STLT const & s, bool discardable = false) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
+
                if (discardable)
-                       seq.swap(const_cast<STLT &>(s));
+                       this->seq.swap(const_cast<STLT &>(s));
                else
-                       seq = s;
+                       this->seq = s;
        }
 
-       explicit container(STLT * vp)
+       explicit container(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
        {
-               GINAC_ASSERT(vp);
-               seq.swap(*vp);
-               delete vp;
+               setflag(get_default_flags());
+               this->seq.swap(*vp);
        }
 
        container(exvector::const_iterator b, exvector::const_iterator e)
-        : inherited(get_tinfo()), container_storage<C>(b, e) {}
+        : inherited(get_tinfo()), container_storage<C>(b, e)
+       {
+               setflag(get_default_flags());
+       }
 
        explicit container(const ex & p1)
-        : inherited(get_tinfo()), container_storage<C>(1, p1) {}
+        : inherited(get_tinfo()), container_storage<C>(1, p1)
+       {
+               setflag(get_default_flags());
+       }
 
        container(const ex & p1, const ex & p2) : inherited(get_tinfo())
        {
-               reserve(seq, 2);
-               seq.push_back(p1); seq.push_back(p2);
+               setflag(get_default_flags());
+               reserve(this->seq, 2);
+               this->seq.push_back(p1); this->seq.push_back(p2);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3) : inherited(get_tinfo())
        {
-               reserve(seq, 3);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
+               setflag(get_default_flags());
+               reserve(this->seq, 3);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
-                          const ex & p4) : inherited(get_tinfo())
+                 const ex & p4) : inherited(get_tinfo())
        {
-               reserve(seq, 4);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4);
+               setflag(get_default_flags());
+               reserve(this->seq, 4);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
                  const ex & p4, const ex & p5) : inherited(get_tinfo())
        {
-               reserve(seq, 5);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5);
+               setflag(get_default_flags());
+               reserve(this->seq, 5);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
                  const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
        {
-               reserve(seq, 6);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
+               setflag(get_default_flags());
+               reserve(this->seq, 6);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7) : inherited(get_tinfo())
        {
-               reserve(seq, 7);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7);
+               setflag(get_default_flags());
+               reserve(this->seq, 7);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7, const ex & p8) : inherited(get_tinfo())
        {
-               reserve(seq, 8);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7); seq.push_back(p8);
+               setflag(get_default_flags());
+               reserve(this->seq, 8);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7); this->seq.push_back(p8);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7, const ex & p8, const ex & p9) : inherited(get_tinfo())
        {
-               reserve(seq, 9);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7); seq.push_back(p8); seq.push_back(p9);
+               setflag(get_default_flags());
+               reserve(this->seq, 9);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
@@ -177,11 +251,12 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10) : inherited(get_tinfo())
        {
-               reserve(seq, 10);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7); seq.push_back(p8); seq.push_back(p9);
-               seq.push_back(p10);
+               setflag(get_default_flags());
+               reserve(this->seq, 10);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
+               this->seq.push_back(p10);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
@@ -189,11 +264,12 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11) : inherited(get_tinfo())
        {
-               reserve(seq, 11);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7); seq.push_back(p8); seq.push_back(p9);
-               seq.push_back(p10); seq.push_back(p11);
+               setflag(get_default_flags());
+               reserve(this->seq, 11);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
+               this->seq.push_back(p10); this->seq.push_back(p11);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
@@ -201,11 +277,12 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
        {
-               reserve(seq, 12);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7); seq.push_back(p8); seq.push_back(p9);
-               seq.push_back(p10); seq.push_back(p11); seq.push_back(p12);
+               setflag(get_default_flags());
+               reserve(this->seq, 12);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
+               this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
@@ -214,12 +291,13 @@ public:
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13) : inherited(get_tinfo())
        {
-               reserve(seq, 13);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7); seq.push_back(p8); seq.push_back(p9);
-               seq.push_back(p10); seq.push_back(p11); seq.push_back(p12);
-               seq.push_back(p13);
+               setflag(get_default_flags());
+               reserve(this->seq, 13);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
+               this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
+               this->seq.push_back(p13);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
@@ -228,12 +306,13 @@ public:
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13, const ex & p14) : inherited(get_tinfo())
        {
-               reserve(seq, 14);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7); seq.push_back(p8); seq.push_back(p9);
-               seq.push_back(p10); seq.push_back(p11); seq.push_back(p12);
-               seq.push_back(p13); seq.push_back(p14);
+               setflag(get_default_flags());
+               reserve(this->seq, 14);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
+               this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
+               this->seq.push_back(p13); this->seq.push_back(p14);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
@@ -242,12 +321,13 @@ public:
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
        {
-               reserve(seq, 15);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7); seq.push_back(p8); seq.push_back(p9);
-               seq.push_back(p10); seq.push_back(p11); seq.push_back(p12);
-               seq.push_back(p13); seq.push_back(p14); seq.push_back(p15);
+               setflag(get_default_flags());
+               reserve(this->seq, 15);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
+               this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
+               this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
@@ -257,27 +337,63 @@ public:
                  const ex & p13, const ex & p14, const ex & p15,
                  const ex & p16) : inherited(get_tinfo())
        {
-               reserve(seq, 16);
-               seq.push_back(p1); seq.push_back(p2); seq.push_back(p3);
-               seq.push_back(p4); seq.push_back(p5); seq.push_back(p6);
-               seq.push_back(p7); seq.push_back(p8); seq.push_back(p9);
-               seq.push_back(p10); seq.push_back(p11); seq.push_back(p12);
-               seq.push_back(p13); seq.push_back(p14); seq.push_back(p15);
-               seq.push_back(p16);
+               setflag(get_default_flags());
+               reserve(this->seq, 16);
+               this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+               this->seq.push_back(p4); this->seq.push_back(p5); this->seq.push_back(p6);
+               this->seq.push_back(p7); this->seq.push_back(p8); this->seq.push_back(p9);
+               this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
+               this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
+               this->seq.push_back(p16);
+       }
+
+       // First step of initialization of container with a comma-separated
+       // sequence of expressions. Subsequent steps are handled by
+       // container_init<>::operator,().
+       container_init<ex, STLT> operator=(const ex & x)
+       {
+               this->seq.push_back(x);
+               return container_init<ex, STLT>(this->seq);
        }
 
        // functions overriding virtual functions from base classes
 public:
-       void print(const print_context & c, unsigned level = 0) const;
        bool info(unsigned inf) const { return inherited::info(inf); }
        unsigned precedence() const { return 10; }
-       size_t nops() const { return seq.size(); }
+       size_t nops() const { return this->seq.size(); }
        ex op(size_t i) const;
        ex & let_op(size_t i);
        ex eval(int level = 0) const;
        ex subs(const exmap & m, unsigned options = 0) const;
 
 protected:
+       ex conjugate() const
+       {
+               STLT *newcont = NULL;
+               for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
+                       if (newcont) {
+                               newcont->push_back(i->conjugate());
+                               continue;
+                       }
+                       ex x = i->conjugate();
+                       if (are_ex_trivially_equal(x, *i)) {
+                               continue;
+                       }
+                       newcont = new STLT;
+                       reserve(*newcont, this->seq.size());
+                       for (const_iterator j=this->seq.begin(); j!=i; ++j) {
+                               newcont->push_back(*j);
+                       }
+                       newcont->push_back(x);
+               }
+               if (newcont) {
+                       ex result = thiscontainer(*newcont);
+                       delete newcont;
+                       return result;
+               }
+               return *this;
+       }
+
        bool is_equal_same_type(const basic & other) const;
 
        // new virtual functions which can be overridden by derived classes
@@ -288,7 +404,7 @@ protected:
 
        /** Similar to duplicate(), but with a preset sequence (which gets
         *  deleted). Must be overridden by derived classes. */
-       virtual ex thiscontainer(STLT * vp) const { return container(vp); }
+       virtual ex thiscontainer(std::auto_ptr<STLT> vp) const { return container(vp); }
 
        virtual void printseq(const print_context & c, char openbracket, char delim,
                              char closebracket, unsigned this_precedence,
@@ -298,18 +414,18 @@ protected:
 private:
        void sort_(std::random_access_iterator_tag)
        {
-               std::sort(seq.begin(), seq.end(), ex_is_less());
+               std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
        }
 
        void sort_(std::input_iterator_tag)
        {
-               seq.sort(ex_is_less());
+               this->seq.sort(ex_is_less());
        }
 
        void unique_()
        {
-               typename STLT::iterator p = std::unique(seq.begin(), seq.end(), ex_is_equal());
-               seq.erase(p, seq.end());
+               typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
+               this->seq.erase(p, this->seq.end());
        }
 
 public:
@@ -321,28 +437,37 @@ public:
        container & sort();
        container & unique();
 
-       const_iterator begin() const {return seq.begin();}
-       const_iterator end() const {return seq.end();}
-       const_reverse_iterator rbegin() const {return seq.rbegin();}
-       const_reverse_iterator rend() const {return seq.rend();}
+       const_iterator begin() const {return this->seq.begin();}
+       const_iterator end() const {return this->seq.end();}
+       const_reverse_iterator rbegin() const {return this->seq.rbegin();}
+       const_reverse_iterator rend() const {return this->seq.rend();}
 
 protected:
+       void do_print(const print_context & c, unsigned level) const;
+       void do_print_tree(const print_tree & c, unsigned level) const;
+       void do_print_python(const print_python & c, unsigned level) const;
+       void do_print_python_repr(const print_python_repr & c, unsigned level) const;
        STLT evalchildren(int level) const;
-       STLT *subschildren(const exmap & m, unsigned options = 0) const;
+       std::auto_ptr<STLT> subschildren(const exmap & m, unsigned options = 0) const;
 };
 
 /** Default constructor */
 template <template <class> class C>
-container<C>::container() : inherited(get_tinfo()) {}
+container<C>::container() : inherited(get_tinfo())
+{
+       setflag(get_default_flags());
+}
 
 /** Construct object from archive_node. */
 template <template <class> class C>
 container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
 {
+       setflag(get_default_flags());
+
        for (unsigned int i=0; true; i++) {
                ex e;
                if (n.find_ex("seq", e, sym_lst, i))
-                       seq.push_back(e);
+                       this->seq.push_back(e);
                else
                        break;
        }
@@ -360,7 +485,7 @@ template <template <class> class C>
 void container<C>::archive(archive_node &n) const
 {
        inherited::archive(n);
-       const_iterator i = seq.begin(), end = seq.end();
+       const_iterator i = this->seq.begin(), end = this->seq.end();
        while (i != end) {
                n.add_ex("seq", *i);
                ++i;
@@ -368,29 +493,38 @@ void container<C>::archive(archive_node &n) const
 }
 
 template <template <class> class C>
-void container<C>::print(const print_context & c, unsigned level) const
+void container<C>::do_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;
-               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, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
+       // always print brackets around seq, ignore upper_precedence
+       printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
+}
+
+template <template <class> class C>
+void container<C>::do_print_tree(const print_tree & c, unsigned level) const
+{
+       c.s << std::string(level, ' ') << class_name() << " @" << this
+           << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
+           << ", nops=" << nops()
+           << std::endl;
+       const_iterator i = this->seq.begin(), end = this->seq.end();
+       while (i != end) {
+               i->print(c, level + c.delta_indent);
+               ++i;
        }
+       c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
+}
+
+template <template <class> class C>
+void container<C>::do_print_python(const print_python & c, unsigned level) const
+{
+       printseq(c, '[', ',', ']', precedence(), precedence()+1);
+}
+
+template <template <class> class C>
+void container<C>::do_print_python_repr(const print_python_repr & c, unsigned level) const
+{
+       c.s << class_name();
+       printseq(c, '(', ',', ')', precedence(), precedence()+1);
 }
 
 template <template <class> class C>
@@ -398,7 +532,7 @@ ex container<C>::op(size_t i) const
 {
        GINAC_ASSERT(i < nops());
 
-       const_iterator it = seq.begin();
+       const_iterator it = this->seq.begin();
        advance(it, i);
        return *it;
 }
@@ -409,7 +543,7 @@ ex & container<C>::let_op(size_t i)
        GINAC_ASSERT(i < nops());
 
        ensure_if_modifiable();
-       typename STLT::iterator it = seq.begin();
+       typename STLT::iterator it = this->seq.begin();
        advance(it, i);
        return *it;
 }
@@ -426,8 +560,8 @@ ex container<C>::eval(int level) const
 template <template <class> class C>
 ex container<C>::subs(const exmap & m, unsigned options) const
 {
-       STLT *vp = subschildren(m, options);
-       if (vp)
+       std::auto_ptr<STLT> vp = subschildren(m, options);
+       if (vp.get())
                return ex_to<basic>(thiscontainer(vp)).subs_one_level(m, options);
        else
                return subs_one_level(m, options);
@@ -440,7 +574,7 @@ int container<C>::compare_same_type(const basic & other) const
        GINAC_ASSERT(is_a<container>(other));
        const container & o = static_cast<const container &>(other);
 
-       const_iterator it1 = seq.begin(), it1end = seq.end(),
+       const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
                       it2 = o.seq.begin(), it2end = o.seq.end();
 
        while (it1 != it1end && it2 != it2end) {
@@ -459,10 +593,10 @@ bool container<C>::is_equal_same_type(const basic & other) const
        GINAC_ASSERT(is_a<container>(other));
        const container & o = static_cast<const container &>(other);
 
-       if (seq.size() != o.seq.size())
+       if (this->seq.size() != o.seq.size())
                return false;
 
-       const_iterator it1 = seq.begin(), it1end = seq.end(), it2 = o.seq.begin();
+       const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
        while (it1 != it1end) {
                if (!it1->is_equal(*it2))
                        return false;
@@ -477,7 +611,7 @@ template <template <class> class C>
 container<C> & container<C>::prepend(const ex & b)
 {
        ensure_if_modifiable();
-       seq.push_front(b);
+       this->seq.push_front(b);
        return *this;
 }
 
@@ -486,7 +620,7 @@ template <template <class> class C>
 container<C> & container<C>::append(const ex & b)
 {
        ensure_if_modifiable();
-       seq.push_back(b);
+       this->seq.push_back(b);
        return *this;
 }
 
@@ -495,7 +629,7 @@ template <template <class> class C>
 container<C> & container<C>::remove_first()
 {
        ensure_if_modifiable();
-       seq.pop_front();
+       this->seq.pop_front();
        return *this;
 }
 
@@ -504,7 +638,7 @@ template <template <class> class C>
 container<C> & container<C>::remove_last()
 {
        ensure_if_modifiable();
-       seq.pop_back();
+       this->seq.pop_back();
        return *this;
 }
 
@@ -513,7 +647,7 @@ template <template <class> class C>
 container<C> & container<C>::remove_all()
 {
        ensure_if_modifiable();
-       seq.clear();
+       this->seq.clear();
        return *this;
 }
 
@@ -522,14 +656,14 @@ template <template <class> class C>
 container<C> & container<C>::sort()
 {
        ensure_if_modifiable();
-       sort_(std::iterator_traits<typename STLT::iterator>::iterator_category());
+       sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
        return *this;
 }
 
 /** Specialization of container::unique_() for std::list. */
-inline void container<std::list>::unique_()
+template<> inline void container<std::list>::unique_()
 {
-       seq.unique(ex_is_equal());
+       this->seq.unique(ex_is_equal());
 }
 
 /** Remove adjacent duplicate elements. */
@@ -550,8 +684,8 @@ void container<C>::printseq(const print_context & c, char openbracket, char deli
        if (this_precedence <= upper_precedence)
                c.s << openbracket;
 
-       if (!seq.empty()) {
-               const_iterator it = seq.begin(), itend = seq.end();
+       if (!this->seq.empty()) {
+               const_iterator it = this->seq.begin(), itend = this->seq.end();
                --itend;
                while (it != itend) {
                        it->print(c, this_precedence);
@@ -569,15 +703,15 @@ template <template <class> class C>
 typename container<C>::STLT container<C>::evalchildren(int level) const
 {
        if (level == 1)
-               return seq;
+               return this->seq;
        else if (level == -max_recursion_level)
                throw std::runtime_error("max recursion level reached");
 
        STLT s;
-       reserve(s, seq.size());
+       reserve(s, this->seq.size());
 
        --level;
-       const_iterator it = seq.begin(), itend = seq.end();
+       const_iterator it = this->seq.begin(), itend = this->seq.end();
        while (it != itend) {
                s.push_back(it->eval(level));
                ++it;
@@ -587,20 +721,20 @@ typename container<C>::STLT container<C>::evalchildren(int level) const
 }
 
 template <template <class> class C>
-typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigned options) const
+std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exmap & m, unsigned options) 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)
+       // returns a pointer to a newly created STLT otherwise
+       // (and relinquishes responsibility for the STLT)
 
-       const_iterator cit = seq.begin(), end = seq.end();
+       const_iterator cit = this->seq.begin(), end = this->seq.end();
        while (cit != end) {
                const ex & subsed_ex = cit->subs(m, options);
                if (!are_ex_trivially_equal(*cit, subsed_ex)) {
 
                        // copy first part of seq which hasn't changed
-                       STLT *s = new STLT(seq.begin(), cit);
-                       reserve(*s, seq.size());
+                       std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
+                       reserve(*s, this->seq.size());
 
                        // insert changed element
                        s->push_back(subsed_ex);
@@ -618,7 +752,7 @@ typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigne
                ++cit;
        }
        
-       return 0; // nothing has changed
+       return std::auto_ptr<STLT>(0); // nothing has changed
 }
 
 } // namespace GiNaC