* Wrapper template for making GiNaC classes out of STL containers. */
/*
- * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
+ * GiNaC Copyright (C) 1999-2004 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
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> {
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); }
ex subs(const exmap & m, unsigned options = 0) const;
protected:
+ ex conjugate() const
+ {
+ STLT *newcont = NULL;
+ for (const_iterator i=this->seq.begin(); i!=this->seq.end(); ++i) {
+ if (newcont) {
+ newcont->push_back(i->conjugate());
+ continue;
+ }
+ ex x = i->conjugate();
+ if (are_ex_trivially_equal(x, *i)) {
+ continue;
+ }
+ newcont = new STLT;
+ reserve(*newcont, this->seq.size());
+ for (const_iterator j=this->seq.begin(); j!=i; ++j) {
+ newcont->push_back(*j);
+ }
+ newcont->push_back(x);
+ }
+ if (newcont) {
+ ex result = thiscontainer(*newcont);
+ delete newcont;
+ return result;
+ }
+ return *this;
+ }
+
bool is_equal_same_type(const basic & other) const;
// new virtual functions which can be overridden by derived classes
template <template <class> class C>
void container<C>::do_print_tree(const print_tree & c, unsigned level) const
{
- c.s << std::string(level, ' ') << class_name()
+ c.s << std::string(level, ' ') << class_name() << " @" << this
<< std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
<< ", nops=" << nops()
<< std::endl;
}
/** Specialization of container::unique_() for std::list. */
-inline void container<std::list>::unique_()
+template<> inline void container<std::list>::unique_()
{
this->seq.unique(ex_is_equal());
}
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
- // (and relinquishes responsibility for the epvector)
+ // returns a pointer to a newly created STLT otherwise
+ // (and relinquishes responsibility for the STLT)
const_iterator cit = this->seq.begin(), end = this->seq.end();
while (cit != end) {