]> www.ginac.de Git - ginac.git/blobdiff - ginac/container.h
added example for namespace bracing
[ginac.git] / ginac / container.h
index c6a72e276d9226d76c06fe492a9879aa664248e3..2459b364ffb0d36c70004f530cdc1cd147dd1751 100644 (file)
@@ -28,6 +28,7 @@
 #include <algorithm>
 #include <vector>
 #include <list>
+#include <memory>
 
 #include "ex.h"
 #include "print.h"
@@ -66,6 +67,64 @@ 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,6 +139,7 @@ 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 ')'; }
 
@@ -87,37 +147,42 @@ protected:
 public:
        container(STLT const & s, bool discardable = false) : inherited(get_tinfo())
        {
+               setflag(get_default_flags());
+
                if (discardable)
                        this->seq.swap(const_cast<STLT &>(s));
                else
                        this->seq = s;
        }
 
-       explicit container(STLT * vp) : inherited(get_tinfo())
+       explicit container(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
        {
-               if (vp == 0) {
-                       // lst(0) ends up here
-                       this->seq.push_back(0);
-               } else {
-                       this->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())
        {
+               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())
        {
+               setflag(get_default_flags());
                reserve(this->seq, 3);
                this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
        }
@@ -125,6 +190,7 @@ public:
        container(const ex & p1, const ex & p2, const ex & p3,
                           const ex & p4) : inherited(get_tinfo())
        {
+               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);
@@ -133,6 +199,7 @@ public:
        container(const ex & p1, const ex & p2, const ex & p3,
                  const ex & p4, const ex & p5) : inherited(get_tinfo())
        {
+               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);
@@ -141,6 +208,7 @@ public:
        container(const ex & p1, const ex & p2, const ex & p3,
                  const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
        {
+               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);
@@ -150,6 +218,7 @@ public:
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7) : inherited(get_tinfo())
        {
+               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);
@@ -160,6 +229,7 @@ public:
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7, const ex & p8) : inherited(get_tinfo())
        {
+               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);
@@ -170,6 +240,7 @@ public:
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7, const ex & p8, const ex & p9) : inherited(get_tinfo())
        {
+               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);
@@ -181,6 +252,7 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10) : inherited(get_tinfo())
        {
+               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);
@@ -193,6 +265,7 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11) : inherited(get_tinfo())
        {
+               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);
@@ -205,6 +278,7 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
        {
+               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);
@@ -218,6 +292,7 @@ public:
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13) : inherited(get_tinfo())
        {
+               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);
@@ -232,6 +307,7 @@ public:
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13, const ex & p14) : inherited(get_tinfo())
        {
+               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);
@@ -246,6 +322,7 @@ public:
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
        {
+               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);
@@ -261,6 +338,7 @@ public:
                  const ex & p13, const ex & p14, const ex & p15,
                  const ex & p16) : inherited(get_tinfo())
        {
+               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);
@@ -270,6 +348,15 @@ public:
                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:
        bool info(unsigned inf) const { return inherited::info(inf); }
@@ -291,7 +378,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,
@@ -335,17 +422,22 @@ protected:
        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))
@@ -442,8 +534,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);
@@ -603,11 +695,11 @@ 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)
+       // (and relinquishes responsibility for the epvector)
 
        const_iterator cit = this->seq.begin(), end = this->seq.end();
        while (cit != end) {
@@ -615,7 +707,7 @@ typename container<C>::STLT *container<C>::subschildren(const exmap & m, unsigne
                if (!are_ex_trivially_equal(*cit, subsed_ex)) {
 
                        // copy first part of seq which hasn't changed
-                       STLT *s = new STLT(this->seq.begin(), cit);
+                       std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
                        reserve(*s, this->seq.size());
 
                        // insert changed element
@@ -634,7 +726,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