]> www.ginac.de Git - ginac.git/blobdiff - ginac/container.h
Be more careful about final top-level substitution.
[ginac.git] / ginac / container.h
index 67dc654954303d4746421fba541892abcaf46f50..456cf5c41b69a6005971e6d0680d81efa8d1f4eb 100644 (file)
@@ -3,7 +3,7 @@
  *  Wrapper template for making GiNaC classes out of STL containers. */
 
 /*
- *  GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2010 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
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#ifndef __GINAC_CONTAINER_H__
-#define __GINAC_CONTAINER_H__
-
-#include <iterator>
-#include <stdexcept>
-#include <algorithm>
-#include <vector>
-#include <list>
-#include <memory>
+#ifndef GINAC_CONTAINER_H
+#define GINAC_CONTAINER_H
 
 #include "ex.h"
 #include "print.h"
 #include "archive.h"
 #include "assertion.h"
 
-namespace GiNaC {
+#include <algorithm>
+#include <iterator>
+#include <list>
+#include <memory>
+#include <stdexcept>
+#include <vector>
 
+namespace GiNaC {
 
 /** Helper template for encapsulating the reserve() mechanics of STL containers. */
 template <template <class T, class = std::allocator<T> > class C>
@@ -128,7 +127,6 @@ private:
 template <template <class T, class = std::allocator<T> > class C>
 class container : public basic, public container_storage<C> {
        GINAC_DECLARE_REGISTERED_CLASS(container, basic)
-
 protected:
        typedef typename container_storage<C>::STLT STLT;
 
@@ -138,14 +136,13 @@ public:
 
 protected:
        // helpers
-       static tinfo_t get_tinfo() { return NULL; }
        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) : inherited(get_tinfo())
+       container(STLT const & s, bool discardable = false)
        {
                setflag(get_default_flags());
 
@@ -155,32 +152,32 @@ public:
                        this->seq = s;
        }
 
-       explicit container(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
+       explicit container(std::auto_ptr<STLT> 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)
+        : container_storage<C>(b, e)
        {
                setflag(get_default_flags());
        }
 
        explicit container(const ex & p1)
-        : inherited(get_tinfo()), container_storage<C>(1, p1)
+        : container_storage<C>(1, p1)
        {
                setflag(get_default_flags());
        }
 
-       container(const ex & p1, const ex & p2) : inherited(get_tinfo())
+       container(const ex & p1, const ex & 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())
+       container(const ex & p1, const ex & p2, const ex & p3)
        {
                setflag(get_default_flags());
                reserve(this->seq, 3);
@@ -188,7 +185,7 @@ public:
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
-                 const ex & p4) : inherited(get_tinfo())
+                 const ex & p4)
        {
                setflag(get_default_flags());
                reserve(this->seq, 4);
@@ -197,7 +194,7 @@ public:
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
-                 const ex & p4, const ex & p5) : inherited(get_tinfo())
+                 const ex & p4, const ex & p5)
        {
                setflag(get_default_flags());
                reserve(this->seq, 5);
@@ -206,7 +203,7 @@ public:
        }
 
        container(const ex & p1, const ex & p2, const ex & p3,
-                 const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
+                 const ex & p4, const ex & p5, const ex & p6)
        {
                setflag(get_default_flags());
                reserve(this->seq, 6);
@@ -216,7 +213,7 @@ public:
 
        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())
+                 const ex & p7)
        {
                setflag(get_default_flags());
                reserve(this->seq, 7);
@@ -227,7 +224,7 @@ public:
 
        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())
+                 const ex & p7, const ex & p8)
        {
                setflag(get_default_flags());
                reserve(this->seq, 8);
@@ -238,7 +235,7 @@ public:
 
        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())
+                 const ex & p7, const ex & p8, const ex & p9)
        {
                setflag(get_default_flags());
                reserve(this->seq, 9);
@@ -250,7 +247,7 @@ public:
        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,
-                 const ex & p10) : inherited(get_tinfo())
+                 const ex & p10)
        {
                setflag(get_default_flags());
                reserve(this->seq, 10);
@@ -263,7 +260,7 @@ public:
        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,
-                 const ex & p10, const ex & p11) : inherited(get_tinfo())
+                 const ex & p10, const ex & p11)
        {
                setflag(get_default_flags());
                reserve(this->seq, 11);
@@ -276,7 +273,7 @@ public:
        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,
-                 const ex & p10, const ex & p11, const ex & p12) : inherited(get_tinfo())
+                 const ex & p10, const ex & p11, const ex & p12)
        {
                setflag(get_default_flags());
                reserve(this->seq, 12);
@@ -290,7 +287,7 @@ public:
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11, const ex & p12,
-                 const ex & p13) : inherited(get_tinfo())
+                 const ex & p13)
        {
                setflag(get_default_flags());
                reserve(this->seq, 13);
@@ -305,7 +302,7 @@ public:
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11, const ex & p12,
-                 const ex & p13, const ex & p14) : inherited(get_tinfo())
+                 const ex & p13, const ex & p14)
        {
                setflag(get_default_flags());
                reserve(this->seq, 14);
@@ -320,7 +317,7 @@ public:
                  const ex & p4, const ex & p5, const ex & p6,
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11, const ex & p12,
-                 const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
+                 const ex & p13, const ex & p14, const ex & p15)
        {
                setflag(get_default_flags());
                reserve(this->seq, 15);
@@ -336,7 +333,7 @@ public:
                  const ex & p7, const ex & p8, const ex & p9,
                  const ex & p10, const ex & p11, const ex & p12,
                  const ex & p13, const ex & p14, const ex & p15,
-                 const ex & p16) : inherited(get_tinfo())
+                 const ex & p16)
        {
                setflag(get_default_flags());
                reserve(this->seq, 16);
@@ -367,6 +364,33 @@ public:
        ex eval(int level = 0) const;
        ex subs(const exmap & m, unsigned options = 0) const;
 
+       void read_archive(const archive_node &n, lst &sym_lst) 
+       {
+               inherited::read_archive(n, sym_lst);
+               setflag(get_default_flags());
+
+               archive_node::archive_node_cit first = n.find_first("seq");
+               archive_node::archive_node_cit last = n.find_last("seq");
+               ++last;
+               reserve(this->seq, last - first);
+               for (archive_node::archive_node_cit i=first; i<last; ++i) {
+                       ex e;
+                       n.find_ex_by_loc(i, e, sym_lst);
+                       this->seq.push_back(e);
+               }
+       }
+
+       /** Archive the object. */
+       void archive(archive_node &n) const
+       {
+               inherited::archive(n);
+               const_iterator i = this->seq.begin(), end = this->seq.end();
+               while (i != end) {
+                       n.add_ex("seq", *i);
+                       ++i;
+               }
+       }
+
 protected:
        ex conjugate() const
        {
@@ -476,46 +500,11 @@ protected:
 
 /** Default constructor */
 template <template <class T, class = std::allocator<T> > class C>
-container<C>::container() : inherited(get_tinfo())
-{
-       setflag(get_default_flags());
-}
-
-/** Construct object from archive_node. */
-template <template <class T, class = std::allocator<T> > class C>
-container<C>::container(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
+container<C>::container()
 {
        setflag(get_default_flags());
-
-       archive_node::archive_node_cit first = n.find_first("seq");
-       archive_node::archive_node_cit last = n.find_last("seq");
-       ++last;
-       reserve(this->seq, last - first);
-       for (archive_node::archive_node_cit i=first; i<last; ++i) {
-               ex e;
-               n.find_ex_by_loc(i, e, sym_lst);
-               this->seq.push_back(e);
-       }
-}
-
-/** Unarchive the object. */
-template <template <class T, class = std::allocator<T> > class C>
-ex container<C>::unarchive(const archive_node &n, lst &sym_lst)
-{
-       return (new container(n, sym_lst))->setflag(status_flags::dynallocated);
 }
 
-/** Archive the object. */
-template <template <class T, class = std::allocator<T> > class C>
-void container<C>::archive(archive_node &n) const
-{
-       inherited::archive(n);
-       const_iterator i = this->seq.begin(), end = this->seq.end();
-       while (i != end) {
-               n.add_ex("seq", *i);
-               ++i;
-       }
-}
 
 template <template <class T, class = std::allocator<T> > class C>
 void container<C>::do_print(const print_context & c, unsigned level) const
@@ -585,11 +574,27 @@ ex container<C>::eval(int level) const
 template <template <class T, class = std::allocator<T> > class C>
 ex container<C>::subs(const exmap & m, unsigned options) const
 {
+       // After having subs'ed all children, this method subs'es one final
+       // level, but only if the intermediate result is a container! This is
+       // because if the intermediate result has eval'ed to a non-container a
+       // last level substitution would be wrong, as this example involving a
+       // function f and its inverse f^-1 shows:
+       // f(x).subs(x==f^-1(x))
+       //   -> f(f^-1(x))  [subschildren]
+       //   -> x           [eval]   /* must not subs(x==f^-1(x))! */
        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);
+       if (vp.get()) {
+               ex result(thiscontainer(vp));
+               if (is_a<container<C> >(result))
+                       return ex_to<basic>(result).subs_one_level(m, options);
+               else
+                       return result;
+       } else {
+               if (is_a<container<C> >(*this))
+                       return subs_one_level(m, options);
+               else
+                       return *this;
+       }
 }
 
 /** Compare two containers of the same type. */
@@ -782,4 +787,4 @@ std::auto_ptr<typename container<C>::STLT> container<C>::subschildren(const exma
 
 } // namespace GiNaC
 
-#endif // ndef __GINAC_CONTAINER_H__
+#endif // ndef GINAC_CONTAINER_H