Index: ginac/container.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/container.h,v
retrieving revision 1.20
diff -u -r1.20 container.h
--- ginac/container.h	21 Apr 2006 12:16:01 -0000	1.20
+++ ginac/container.h	9 May 2006 21:00:59 -0000
@@ -59,12 +59,32 @@
 protected:
 	~container_storage() {}
 };
+template <template <class, class> class C>
+class container_with_allocator_storage {
+protected:
+	typedef C<ex, std::allocator<ex> > STLT;
+
+	container_with_allocator_storage() {}
+	container_with_allocator_storage(size_t n, const ex & e) : seq(n, e) {}
+
+	template <class In>
+	container_with_allocator_storage(In b, In e) : seq(b, e) {}
+
+	void reserve(size_t) {}
+	static void reserve(STLT &, size_t) {}
+
+	STLT seq;
+
+	// disallow destruction of container through a container_storage*
+protected:
+	~container_with_allocator_storage() {}
+};
 
 template <>
-inline void container_storage<std::vector>::reserve(size_t n) { seq.reserve(n); }
+inline void container_with_allocator_storage<std::vector>::reserve(size_t n) { seq.reserve(n); }
 
 template <>
-inline void container_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
+inline void container_with_allocator_storage<std::vector>::reserve(std::vector<ex> & v, size_t n) { v.reserve(n); }
 
 
 /** Helper template to allow initialization of containers via an overloaded
@@ -124,6 +144,63 @@
 	STLT & stlt;
 };
 
+/** Helper template to allow initialization of containers via an overloaded
+ *  comma operator (idea stolen from Blitz++). */
+template <typename T, typename STLT>
+class container_with_allocator_init {
+public:
+	container_with_allocator_init(STLT & s) : stlt(s) {}
+
+	container_with_allocator_init<T, STLT> operator,(const T & x)
+	{
+		stlt.push_back(x);
+		return container_with_allocator_init<T, STLT>(stlt);
+	}
+
+	// The following specializations produce much tighter code than the
+	// general case above
+
+	container_with_allocator_init<T, STLT> operator,(int x)
+	{
+		stlt.push_back(x);
+		return container_with_allocator_init<T, STLT>(stlt);
+	}
+
+	container_with_allocator_init<T, STLT> operator,(unsigned int x)
+	{
+		stlt.push_back(x);
+		return container_with_allocator_init<T, STLT>(stlt);
+	}
+
+	container_with_allocator_init<T, STLT> operator,(long x)
+	{
+		stlt.push_back(x);
+		return container_with_allocator_init<T, STLT>(stlt);
+	}
+
+	container_with_allocator_init<T, STLT> operator,(unsigned long x)
+	{
+		stlt.push_back(x);
+		return container_with_allocator_init<T, STLT>(stlt);
+	}
+
+	container_with_allocator_init<T, STLT> operator,(double x)
+	{
+		stlt.push_back(x);
+		return container_with_allocator_init<T, STLT>(stlt);
+	}
+
+	container_with_allocator_init<T, STLT> operator,(const symbol & x)
+	{
+		stlt.push_back(T(x));
+		return container_with_allocator_init<T, STLT>(stlt);
+	}
+
+private:
+	container_with_allocator_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> {
@@ -473,6 +550,7 @@
 	std::auto_ptr<STLT> subschildren(const exmap & m, unsigned options = 0) const;
 };
 
+
 /** Default constructor */
 template <template <class> class C>
 container<C>::container() : inherited(get_tinfo())
@@ -682,12 +760,6 @@
 	return *this;
 }
 
-/** Specialization of container::unique_() for std::list. */
-template<> inline void container<std::list>::unique_()
-{
-	this->seq.unique(ex_is_equal());
-}
-
 /** Remove adjacent duplicate elements. */
 template <template <class> class C>
 container<C> & container<C>::unique()
@@ -777,6 +849,659 @@
 	return std::auto_ptr<STLT>(0); // nothing has changed
 }
 
+/** Wrapper template for making GiNaC classes out of STL containers. */
+template <template <class, class> class C>
+class container_with_allocator : public basic, public container_with_allocator_storage<C> {
+	GINAC_DECLARE_REGISTERED_CLASS(container_with_allocator, basic)
+
+	typedef typename container_with_allocator_storage<C>::STLT STLT;
+
+public:
+	typedef typename STLT::const_iterator const_iterator;
+	typedef typename STLT::const_reverse_iterator const_reverse_iterator;
+
+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_with_allocator(STLT const & s, bool discardable = false) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+
+		if (discardable)
+			this->seq.swap(const_cast<STLT &>(s));
+		else
+			this->seq = s;
+	}
+
+	explicit container_with_allocator(std::auto_ptr<STLT> vp) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		this->seq.swap(*vp);
+	}
+
+	container_with_allocator(exvector::const_iterator b, exvector::const_iterator e)
+	 : inherited(get_tinfo()), container_with_allocator_storage<C>(b, e)
+	{
+		setflag(get_default_flags());
+	}
+
+	explicit container_with_allocator(const ex & p1)
+	 : inherited(get_tinfo()), container_with_allocator_storage<C>(1, p1)
+	{
+		setflag(get_default_flags());
+	}
+
+	container_with_allocator(const ex & p1, const ex & p2) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		reserve(this->seq, 2);
+		this->seq.push_back(p1); this->seq.push_back(p2);
+	}
+
+	container_with_allocator(const ex & p1, const ex & p2, const ex & p3) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		reserve(this->seq, 3);
+		this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+	}
+
+	container_with_allocator(const ex & p1, const ex & p2, const ex & p3,
+	          const ex & p4) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		reserve(this->seq, 4);
+		this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
+		this->seq.push_back(p4);
+	}
+
+	container_with_allocator(const ex & p1, const ex & p2, const ex & p3,
+	          const ex & p4, const ex & p5) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		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);
+	}
+
+	container_with_allocator(const ex & p1, const ex & p2, const ex & p3,
+	          const ex & p4, const ex & p5, const ex & p6) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		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);
+	}
+
+	container_with_allocator(const ex & p1, const ex & p2, const ex & p3,
+	          const ex & p4, const ex & p5, const ex & p6,
+	          const ex & p7) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		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);
+	}
+
+	container_with_allocator(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())
+	{
+		setflag(get_default_flags());
+		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);
+	}
+
+	container_with_allocator(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())
+	{
+		setflag(get_default_flags());
+		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);
+	}
+
+	container_with_allocator(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())
+	{
+		setflag(get_default_flags());
+		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);
+		this->seq.push_back(p10);
+	}
+
+	container_with_allocator(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())
+	{
+		setflag(get_default_flags());
+		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);
+		this->seq.push_back(p10); this->seq.push_back(p11);
+	}
+
+	container_with_allocator(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())
+	{
+		setflag(get_default_flags());
+		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);
+		this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
+	}
+
+	container_with_allocator(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,
+	          const ex & p13) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		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);
+		this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
+		this->seq.push_back(p13);
+	}
+
+	container_with_allocator(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,
+	          const ex & p13, const ex & p14) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		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);
+		this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
+		this->seq.push_back(p13); this->seq.push_back(p14);
+	}
+
+	container_with_allocator(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,
+	          const ex & p13, const ex & p14, const ex & p15) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		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);
+		this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
+		this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
+	}
+
+	container_with_allocator(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,
+	          const ex & p13, const ex & p14, const ex & p15,
+	          const ex & p16) : inherited(get_tinfo())
+	{
+		setflag(get_default_flags());
+		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);
+		this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
+		this->seq.push_back(p13); this->seq.push_back(p14); this->seq.push_back(p15);
+		this->seq.push_back(p16);
+	}
+
+	// First step of initialization of container_with_allocator with a comma-separated
+	// sequence of expressions. Subsequent steps are handled by
+	// container_with_allocator_init<>::operator,().
+	container_with_allocator_init<ex, STLT> operator=(const ex & x)
+	{
+		this->seq.push_back(x);
+		return container_with_allocator_init<ex, STLT>(this->seq);
+	}
+
+	// functions overriding virtual functions from base classes
+public:
+	bool info(unsigned inf) const { return inherited::info(inf); }
+	unsigned precedence() const { return 10; }
+	size_t nops() const { return this->seq.size(); }
+	ex op(size_t i) const;
+	ex & let_op(size_t i);
+	ex eval(int level = 0) const;
+	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;
+	}
+
+	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
+protected:
+	/** Similar to duplicate(), but with a preset sequence. Must be
+	 *  overridden by derived classes. */
+	virtual ex thiscontainer(const STLT & v) const { return container_with_allocator(v); }
+
+	/** Similar to duplicate(), but with a preset sequence (which gets
+	 *  deleted). Must be overridden by derived classes. */
+	virtual ex thiscontainer(std::auto_ptr<STLT> vp) const { return container_with_allocator(vp); }
+
+	virtual void printseq(const print_context & c, char openbracket, char delim,
+	                      char closebracket, unsigned this_precedence,
+	                      unsigned upper_precedence = 0) const;
+
+	// non-virtual functions in this class
+private:
+	void sort_(std::random_access_iterator_tag)
+	{
+		std::sort(this->seq.begin(), this->seq.end(), ex_is_less());
+	}
+
+	void sort_(std::input_iterator_tag)
+	{
+		this->seq.sort(ex_is_less());
+	}
+
+	void unique_()
+	{
+		typename STLT::iterator p = std::unique(this->seq.begin(), this->seq.end(), ex_is_equal());
+		this->seq.erase(p, this->seq.end());
+	}
+
+public:
+	container_with_allocator & prepend(const ex & b);
+	container_with_allocator & append(const ex & b);
+	container_with_allocator & remove_first();
+	container_with_allocator & remove_last();
+	container_with_allocator & remove_all();
+	container_with_allocator & sort();
+	container_with_allocator & unique();
+
+	const_iterator begin() const {return this->seq.begin();}
+	const_iterator end() const {return this->seq.end();}
+	const_reverse_iterator rbegin() const {return this->seq.rbegin();}
+	const_reverse_iterator rend() const {return this->seq.rend();}
+
+protected:
+	void do_print(const print_context & c, unsigned level) const;
+	void do_print_tree(const print_tree & c, unsigned level) const;
+	void do_print_python(const print_python & c, unsigned level) const;
+	void do_print_python_repr(const print_python_repr & c, unsigned level) const;
+	STLT evalchildren(int level) const;
+	std::auto_ptr<STLT> subschildren(const exmap & m, unsigned options = 0) const;
+};
+
+/** Default constructor */
+template <template <class, class> class C>
+container_with_allocator<C>::container_with_allocator() : inherited(get_tinfo())
+{
+	setflag(get_default_flags());
+}
+
+/** Construct object from archive_node. */
+template <template <class, class> class C>
+container_with_allocator<C>::container_with_allocator(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> class C>
+ex container_with_allocator<C>::unarchive(const archive_node &n, lst &sym_lst)
+{
+	return (new container_with_allocator(n, sym_lst))->setflag(status_flags::dynallocated);
+}
+
+/** Archive the object. */
+template <template <class, class> class C>
+void container_with_allocator<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> class C>
+void container_with_allocator<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> class C>
+void container_with_allocator<C>::do_print_tree(const print_tree & c, unsigned level) const
+{
+	c.s << std::string(level, ' ') << class_name() << " @" << this
+	    << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
+	    << ", nops=" << nops()
+	    << std::endl;
+	const_iterator i = this->seq.begin(), end = this->seq.end();
+	while (i != end) {
+		i->print(c, level + c.delta_indent);
+		++i;
+	}
+	c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl;
+}
+
+template <template <class, class> class C>
+void container_with_allocator<C>::do_print_python(const print_python & c, unsigned level) const
+{
+	printseq(c, '[', ',', ']', precedence(), precedence()+1);
+}
+
+template <template <class, class> class C>
+void container_with_allocator<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> class C>
+ex container_with_allocator<C>::op(size_t i) const
+{
+	GINAC_ASSERT(i < nops());
+
+	const_iterator it = this->seq.begin();
+	advance(it, i);
+	return *it;
+}
+
+template <template <class, class> class C>
+ex & container_with_allocator<C>::let_op(size_t i)
+{
+	GINAC_ASSERT(i < nops());
+
+	ensure_if_modifiable();
+	typename STLT::iterator it = this->seq.begin();
+	advance(it, i);
+	return *it;
+}
+
+template <template <class, class> class C>
+ex container_with_allocator<C>::eval(int level) const
+{
+	if (level == 1)
+		return hold();
+	else
+		return thiscontainer(evalchildren(level));
+}
+
+template <template <class, class> class C>
+ex container_with_allocator<C>::subs(const exmap & m, unsigned options) const
+{
+	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);
+}
+
+/** Compare two container_with_allocators of the same type. */
+template <template <class, class> class C>
+int container_with_allocator<C>::compare_same_type(const basic & other) const
+{
+	GINAC_ASSERT(is_a<container_with_allocator>(other));
+	const container_with_allocator & o = static_cast<const container_with_allocator &>(other);
+
+	const_iterator it1 = this->seq.begin(), it1end = this->seq.end(),
+	               it2 = o.seq.begin(), it2end = o.seq.end();
+
+	while (it1 != it1end && it2 != it2end) {
+		int cmpval = it1->compare(*it2);
+		if (cmpval)
+			return cmpval;
+		++it1; ++it2;
+	}
+
+	return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
+}
+
+template <template <class, class> class C>
+bool container_with_allocator<C>::is_equal_same_type(const basic & other) const
+{
+	GINAC_ASSERT(is_a<container_with_allocator>(other));
+	const container_with_allocator & o = static_cast<const container_with_allocator &>(other);
+
+	if (this->seq.size() != o.seq.size())
+		return false;
+
+	const_iterator it1 = this->seq.begin(), it1end = this->seq.end(), it2 = o.seq.begin();
+	while (it1 != it1end) {
+		if (!it1->is_equal(*it2))
+			return false;
+		++it1; ++it2;
+	}
+
+	return true;
+}
+
+/** Add element at front. */
+template <template <class, class> class C>
+container_with_allocator<C> & container_with_allocator<C>::prepend(const ex & b)
+{
+	ensure_if_modifiable();
+	this->seq.push_front(b);
+	return *this;
+}
+
+/** Add element at back. */
+template <template <class, class> class C>
+container_with_allocator<C> & container_with_allocator<C>::append(const ex & b)
+{
+	ensure_if_modifiable();
+	this->seq.push_back(b);
+	return *this;
+}
+
+/** Remove first element. */
+template <template <class, class> class C>
+container_with_allocator<C> & container_with_allocator<C>::remove_first()
+{
+	ensure_if_modifiable();
+	this->seq.pop_front();
+	return *this;
+}
+
+/** Remove last element. */
+template <template <class, class> class C>
+container_with_allocator<C> & container_with_allocator<C>::remove_last()
+{
+	ensure_if_modifiable();
+	this->seq.pop_back();
+	return *this;
+}
+
+/** Remove all elements. */
+template <template <class, class> class C>
+container_with_allocator<C> & container_with_allocator<C>::remove_all()
+{
+	ensure_if_modifiable();
+	this->seq.clear();
+	return *this;
+}
+
+/** Sort elements. */
+template <template <class, class> class C>
+container_with_allocator<C> & container_with_allocator<C>::sort()
+{
+	ensure_if_modifiable();
+	sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
+	return *this;
+}
+
+/** Specialization of container_with_allocator::unique_() for std::list. */
+template<> inline void container_with_allocator<std::list>::unique_()
+{
+	this->seq.unique(ex_is_equal());
+}
+
+/** Remove adjacent duplicate elements. */
+template <template <class, class> class C>
+container_with_allocator<C> & container_with_allocator<C>::unique()
+{
+	ensure_if_modifiable();
+	unique_();
+	return *this;
+}
+
+/** Print sequence of contained elements. */
+template <template <class, class> class C>
+void container_with_allocator<C>::printseq(const print_context & c, char openbracket, char delim,
+                            char closebracket, unsigned this_precedence,
+                            unsigned upper_precedence) const
+{
+	if (this_precedence <= upper_precedence)
+		c.s << openbracket;
+
+	if (!this->seq.empty()) {
+		const_iterator it = this->seq.begin(), itend = this->seq.end();
+		--itend;
+		while (it != itend) {
+			it->print(c, this_precedence);
+			c.s << delim;
+			++it;
+		}
+		it->print(c, this_precedence);
+	}
+
+	if (this_precedence <= upper_precedence)
+		c.s << closebracket;
+}
+
+template <template <class, class> class C>
+typename container_with_allocator<C>::STLT container_with_allocator<C>::evalchildren(int level) const
+{
+	if (level == 1)
+		return this->seq;
+	else if (level == -max_recursion_level)
+		throw std::runtime_error("max recursion level reached");
+
+	STLT s;
+	reserve(s, this->seq.size());
+
+	--level;
+	const_iterator it = this->seq.begin(), itend = this->seq.end();
+	while (it != itend) {
+		s.push_back(it->eval(level));
+		++it;
+	}
+
+	return s;
+}
+
+template <template <class, class> class C>
+std::auto_ptr<typename container_with_allocator<C>::STLT> container_with_allocator<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 STLT otherwise
+	// (and relinquishes responsibility for the STLT)
+
+	const_iterator cit = this->seq.begin(), end = this->seq.end();
+	while (cit != end) {
+		const ex & subsed_ex = cit->subs(m, options);
+		if (!are_ex_trivially_equal(*cit, subsed_ex)) {
+
+			// 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());
+
+			// insert changed element
+			s->push_back(subsed_ex);
+			++cit;
+
+			// copy rest
+			while (cit != end) {
+				s->push_back(cit->subs(m, options));
+				++cit;
+			}
+
+			return s;
+		}
+
+		++cit;
+	}
+	
+	return std::auto_ptr<STLT>(0); // nothing has changed
+}
+
 } // namespace GiNaC
 
 #endif // ndef __GINAC_CONTAINER_H__
Index: ginac/exprseq.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/exprseq.h,v
retrieving revision 1.12
diff -u -r1.12 exprseq.h
--- ginac/exprseq.h	19 Jan 2006 16:36:07 -0000	1.12
+++ ginac/exprseq.h	9 May 2006 21:00:59 -0000
@@ -29,7 +29,7 @@
 
 namespace GiNaC {
 
-typedef container<std::vector> exprseq;
+typedef container_with_allocator<std::vector> exprseq;
 
 /** Specialization of container::get_tinfo() for exprseq. */
 template<> inline tinfo_t exprseq::get_tinfo() { return &exprseq::tinfo_static; }
Index: ginac/lst.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/lst.h,v
retrieving revision 1.13
diff -u -r1.13 lst.h
--- ginac/lst.h	19 Jan 2006 16:36:07 -0000	1.13
+++ ginac/lst.h	9 May 2006 21:00:59 -0000
@@ -29,7 +29,7 @@
 
 namespace GiNaC {
 
-typedef container<std::list> lst;
+typedef container_with_allocator<std::list> lst;
 
 /** Specialization of container::get_tinfo() for lst. */
 template<> inline tinfo_t lst::get_tinfo() { return &lst::tinfo_static; }
Index: ginac/registrar.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/registrar.h,v
retrieving revision 1.26
diff -u -r1.26 registrar.h
--- ginac/registrar.h	19 Jan 2006 16:36:07 -0000	1.26
+++ ginac/registrar.h	9 May 2006 21:01:00 -0000
@@ -36,7 +36,8 @@
 class archive_node;
 
 template <template <class> class> class container;
-typedef container<std::list> lst;
+template <template <class, class> class> class container_with_allocator;
+typedef container_with_allocator<std::list> lst;
 
 /** Definitions for the tinfo mechanism. */
 typedef const void * tinfo_t;
Index: ginsh/ginsh_parser.yy
===================================================================
RCS file: /home/cvs/GiNaC/ginsh/ginsh_parser.yy,v
retrieving revision 1.83
diff -u -r1.83 ginsh_parser.yy
--- ginsh/ginsh_parser.yy	22 Apr 2006 15:42:57 -0000	1.83
+++ ginsh/ginsh_parser.yy	9 May 2006 21:01:01 -0000
@@ -714,8 +714,9 @@
 	return function(serial, es).eval(1);
 }
 
+namespace GiNaC {
 // All registered GiNaC functions
-void GiNaC::ginsh_get_ginac_functions(void)
+void ginsh_get_ginac_functions(void)
 {
 	vector<function_options>::const_iterator i = function::registered_functions().begin(), end = function::registered_functions().end();
 	unsigned serial = 0;
@@ -725,6 +726,7 @@
 		serial++;
 	}
 }
+}
 
 
 /*
