* Wrapper template for making GiNaC classes out of STL containers. */
/*
- * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
+ * GiNaC Copyright (C) 1999-2009 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
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * 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> class C>
+template <template <class T, class = std::allocator<T> > class C>
class container_storage {
protected:
typedef C<ex> STLT;
};
/** Wrapper template for making GiNaC classes out of STL containers. */
-template <template <class> 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;
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) : inherited(get_tinfo())
+ container(STLT const & s, bool discardable = false)
{
setflag(get_default_flags());
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);
}
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);
}
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);
}
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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
{
return *this;
}
+ ex real_part() const
+ {
+ STLT cont;
+ reserve(cont, nops());
+ const_iterator b = begin();
+ const_iterator e = end();
+ for(const_iterator i=b; i!=e; ++i)
+ cont.push_back(i->real_part());
+ return thiscontainer(cont);
+ }
+
+ ex imag_part() const
+ {
+ STLT cont;
+ reserve(cont, nops());
+ const_iterator b = begin();
+ const_iterator e = end();
+ for(const_iterator i=b; i!=e; ++i)
+ cont.push_back(i->imag_part());
+ return thiscontainer(cont);
+ }
+
bool is_equal_same_type(const basic & other) const;
// new virtual functions which can be overridden by derived classes
};
/** Default constructor */
-template <template <class> class C>
-container<C>::container() : inherited(get_tinfo())
+template <template <class T, class = std::allocator<T> > class C>
+container<C>::container()
{
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))
- this->seq.push_back(e);
- else
- break;
- }
-}
-
-/** Unarchive the object. */
-template <template <class> 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> 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> class C>
+template <template <class T, class = std::allocator<T> > class C>
void container<C>::do_print(const print_context & c, unsigned level) const
{
// always print brackets around seq, ignore upper_precedence
printseq(c, get_open_delim(), ',', get_close_delim(), precedence(), precedence()+1);
}
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
void container<C>::do_print_tree(const print_tree & c, unsigned level) const
{
c.s << std::string(level, ' ') << class_name() << " @" << this
c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
}
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > 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>
+template <template <class T, class = std::allocator<T> > 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>
+template <template <class T, class = std::allocator<T> > class C>
ex container<C>::op(size_t i) const
{
GINAC_ASSERT(i < nops());
return *it;
}
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
ex & container<C>::let_op(size_t i)
{
GINAC_ASSERT(i < nops());
return *it;
}
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
ex container<C>::eval(int level) const
{
if (level == 1)
return thiscontainer(evalchildren(level));
}
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
ex container<C>::subs(const exmap & m, unsigned options) const
{
std::auto_ptr<STLT> vp = subschildren(m, options);
}
/** Compare two containers of the same type. */
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
int container<C>::compare_same_type(const basic & other) const
{
GINAC_ASSERT(is_a<container>(other));
return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
}
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
bool container<C>::is_equal_same_type(const basic & other) const
{
GINAC_ASSERT(is_a<container>(other));
}
/** Add element at front. */
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::prepend(const ex & b)
{
ensure_if_modifiable();
}
/** Add element at back. */
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::append(const ex & b)
{
ensure_if_modifiable();
}
/** Remove first element. */
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::remove_first()
{
ensure_if_modifiable();
}
/** Remove last element. */
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::remove_last()
{
ensure_if_modifiable();
}
/** Remove all elements. */
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::remove_all()
{
ensure_if_modifiable();
}
/** Sort elements. */
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::sort()
{
ensure_if_modifiable();
}
/** Remove adjacent duplicate elements. */
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::unique()
{
ensure_if_modifiable();
}
/** Print sequence of contained elements. */
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
void container<C>::printseq(const print_context & c, char openbracket, char delim,
char closebracket, unsigned this_precedence,
unsigned upper_precedence) const
c.s << closebracket;
}
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
typename container<C>::STLT container<C>::evalchildren(int level) const
{
if (level == 1)
return s;
}
-template <template <class> class C>
+template <template <class T, class = std::allocator<T> > class C>
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
} // namespace GiNaC
-#endif // ndef __GINAC_CONTAINER_H__
+#endif // ndef GINAC_CONTAINER_H