This file is indexed.

/usr/include/ginac/container.h is in libginac-dev 1.6.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/** @file container.h
 *
 *  Wrapper template for making GiNaC classes out of STL containers. */

/*
 *  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
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef GINAC_CONTAINER_H
#define GINAC_CONTAINER_H

#include "ex.h"
#include "print.h"
#include "archive.h"
#include "assertion.h"

#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>
class container_storage {
protected:
	typedef C<ex> STLT;

	container_storage() {}
	container_storage(size_t n, const ex & e) : seq(n, e) {}

	template <class In>
	container_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_storage() {}
};

template <>
inline void container_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); }


/** 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 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:
	typedef typename STLT::const_iterator const_iterator;
	typedef typename STLT::const_reverse_iterator const_reverse_iterator;

protected:
	// helpers
	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)
	{
		setflag(get_default_flags());

		if (discardable)
			this->seq.swap(const_cast<STLT &>(s));
		else
			this->seq = s;
	}

	explicit container(std::auto_ptr<STLT> vp)
	{
		setflag(get_default_flags());
		this->seq.swap(*vp);
	}

	container(exvector::const_iterator b, exvector::const_iterator e)
	 : container_storage<C>(b, e)
	{
		setflag(get_default_flags());
	}

	explicit container(const ex & p1)
	 : container_storage<C>(1, p1)
	{
		setflag(get_default_flags());
	}

	container(const ex & p1, const ex & p2)
	{
		setflag(get_default_flags());
		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());
		this->reserve(this->seq, 3);
		this->seq.push_back(p1); this->seq.push_back(p2); this->seq.push_back(p3);
	}

	container(const ex & p1, const ex & p2, const ex & p3,
	          const ex & p4)
	{
		setflag(get_default_flags());
		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);
	}

	container(const ex & p1, const ex & p2, const ex & p3,
	          const ex & p4, const ex & p5)
	{
		setflag(get_default_flags());
		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);
	}

	container(const ex & p1, const ex & p2, const ex & p3,
	          const ex & p4, const ex & p5, const ex & p6)
	{
		setflag(get_default_flags());
		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);
	}

	container(const ex & p1, const ex & p2, const ex & p3,
	          const ex & p4, const ex & p5, const ex & p6,
	          const ex & p7)
	{
		setflag(get_default_flags());
		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);
	}

	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)
	{
		setflag(get_default_flags());
		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);
	}

	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)
	{
		setflag(get_default_flags());
		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);
	}

	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)
	{
		setflag(get_default_flags());
		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);
		this->seq.push_back(p10);
	}

	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)
	{
		setflag(get_default_flags());
		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);
		this->seq.push_back(p10); this->seq.push_back(p11);
	}

	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)
	{
		setflag(get_default_flags());
		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);
		this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
	}

	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,
	          const ex & p13)
	{
		setflag(get_default_flags());
		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);
		this->seq.push_back(p10); this->seq.push_back(p11); this->seq.push_back(p12);
		this->seq.push_back(p13);
	}

	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,
	          const ex & p13, const ex & p14)
	{
		setflag(get_default_flags());
		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);
		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(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)
	{
		setflag(get_default_flags());
		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);
		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(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)
	{
		setflag(get_default_flags());
		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);
		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 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); }
	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;

	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
	{
		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;
			this->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;
		this->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;
		this->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(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(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 & prepend(const ex & b);
	container & append(const ex & b);
	container & remove_first();
	container & remove_last();
	container & remove_all();
	container & sort();
	container & 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 T, class = std::allocator<T> > class C>
container<C>::container()
{
	setflag(get_default_flags());
}


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 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
	    << 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 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 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 T, class = std::allocator<T> > class C>
ex container<C>::op(size_t i) const
{
	GINAC_ASSERT(i < nops());

	const_iterator it = this->seq.begin();
	advance(it, i);
	return *it;
}

template <template <class T, class = std::allocator<T> > class C>
ex & container<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 T, class = std::allocator<T> > class C>
ex container<C>::eval(int level) const
{
	if (level == 1)
		return hold();
	else
		return thiscontainer(evalchildren(level));
}

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()) {
		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. */
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));
	const container & o = static_cast<const container &>(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 T, class = std::allocator<T> > class C>
bool container<C>::is_equal_same_type(const basic & other) const
{
	GINAC_ASSERT(is_a<container>(other));
	const container & o = static_cast<const container &>(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 T, class = std::allocator<T> > class C>
container<C> & container<C>::prepend(const ex & b)
{
	ensure_if_modifiable();
	this->seq.push_front(b);
	return *this;
}

/** Add element at back. */
template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::append(const ex & b)
{
	ensure_if_modifiable();
	this->seq.push_back(b);
	return *this;
}

/** Remove first element. */
template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::remove_first()
{
	ensure_if_modifiable();
	this->seq.pop_front();
	return *this;
}

/** Remove last element. */
template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::remove_last()
{
	ensure_if_modifiable();
	this->seq.pop_back();
	return *this;
}

/** Remove all elements. */
template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::remove_all()
{
	ensure_if_modifiable();
	this->seq.clear();
	return *this;
}

/** Sort elements. */
template <template <class T, class = std::allocator<T> > class C>
container<C> & container<C>::sort()
{
	ensure_if_modifiable();
	sort_(typename std::iterator_traits<typename STLT::iterator>::iterator_category());
	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 T, class = std::allocator<T> > class C>
container<C> & container<C>::unique()
{
	ensure_if_modifiable();
	unique_();
	return *this;
}

/** Print sequence of contained elements. */
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
{
	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 T, class = std::allocator<T> > class C>
typename container<C>::STLT container<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;
	this->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 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
	// 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));
			this->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