* Wrapper template for making GiNaC classes out of STL containers. */
/*
- * GiNaC Copyright (C) 1999-2008 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>
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. */
} // namespace GiNaC
-#endif // ndef __GINAC_CONTAINER_H__
+#endif // ndef GINAC_CONTAINER_H