* Wrapper template for making GiNaC classes out of STL containers. */
/*
- * GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
+ * GiNaC Copyright (C) 1999-2011 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>
class container : public basic, public container_storage<C> {
GINAC_DECLARE_REGISTERED_CLASS(container, basic)
-
protected:
typedef typename container_storage<C>::STLT STLT;
container(const ex & p1, const ex & p2)
{
setflag(get_default_flags());
- reserve(this->seq, 2);
+ this->reserve(this->seq, 2);
this->seq.push_back(p1); this->seq.push_back(p2);
}
container(const ex & p1, const ex & p2, const ex & p3)
{
setflag(get_default_flags());
- reserve(this->seq, 3);
+ this->reserve(this->seq, 3);
this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
}
const ex & p4)
{
setflag(get_default_flags());
- reserve(this->seq, 4);
+ this->reserve(this->seq, 4);
this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
this->seq.push_back(p4);
}
const ex & p4, const ex & p5)
{
setflag(get_default_flags());
- reserve(this->seq, 5);
+ this->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);
}
const ex & p4, const ex & p5, const ex & p6)
{
setflag(get_default_flags());
- reserve(this->seq, 6);
+ this->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);
}
const ex & p7)
{
setflag(get_default_flags());
- reserve(this->seq, 7);
+ this->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);
const ex & p7, const ex & p8)
{
setflag(get_default_flags());
- reserve(this->seq, 8);
+ this->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);
const ex & p7, const ex & p8, const ex & p9)
{
setflag(get_default_flags());
- reserve(this->seq, 9);
+ this->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);
const ex & p10)
{
setflag(get_default_flags());
- reserve(this->seq, 10);
+ this->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);
const ex & p10, const ex & p11)
{
setflag(get_default_flags());
- reserve(this->seq, 11);
+ this->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);
const ex & p10, const ex & p11, const ex & p12)
{
setflag(get_default_flags());
- reserve(this->seq, 12);
+ this->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);
const ex & p13)
{
setflag(get_default_flags());
- reserve(this->seq, 13);
+ this->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);
const ex & p13, const ex & p14)
{
setflag(get_default_flags());
- reserve(this->seq, 14);
+ this->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);
const ex & p13, const ex & p14, const ex & p15)
{
setflag(get_default_flags());
- reserve(this->seq, 15);
+ this->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);
const ex & p16)
{
setflag(get_default_flags());
- reserve(this->seq, 16);
+ this->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);
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;
+ this->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
{
continue;
}
newcont = new STLT;
- reserve(*newcont, this->seq.size());
+ this->reserve(*newcont, this->seq.size());
for (const_iterator j=this->seq.begin(); j!=i; ++j) {
newcont->push_back(*j);
}
ex real_part() const
{
STLT cont;
- reserve(cont, nops());
+ this->reserve(cont, nops());
const_iterator b = begin();
const_iterator e = end();
for(const_iterator i=b; i!=e; ++i)
ex imag_part() const
{
STLT cont;
- reserve(cont, nops());
+ this->reserve(cont, nops());
const_iterator b = begin();
const_iterator e = end();
for(const_iterator i=b; i!=e; ++i)
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)
-{
- 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
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. */
throw std::runtime_error("max recursion level reached");
STLT s;
- reserve(s, this->seq.size());
+ this->reserve(s, this->seq.size());
--level;
const_iterator it = this->seq.begin(), itend = this->seq.end();
// copy first part of seq which hasn't changed
std::auto_ptr<STLT> s(new STLT(this->seq.begin(), cit));
- reserve(*s, this->seq.size());
+ this->reserve(*s, this->seq.size());
// insert changed element
s->push_back(subsed_ex);
} // namespace GiNaC
-#endif // ndef __GINAC_CONTAINER_H__
+#endif // ndef GINAC_CONTAINER_H