+
+// Iterators
+
+class const_iterator : public std::iterator<std::random_access_iterator_tag, ex, ptrdiff_t, const ex *, const ex &> {
+ friend class ex;
+ friend class const_preorder_iterator;
+ friend class const_postorder_iterator;
+
+public:
+ const_iterator() throw() {}
+
+private:
+ const_iterator(const ex &e_, size_t i_) throw() : e(e_), i(i_) {}
+
+public:
+ // This should return an ex&, but that would be a reference to a
+ // temporary value
+ ex operator*() const
+ {
+ return e.op(i);
+ }
+
+ // This should return an ex*, but that would be a pointer to a
+ // temporary value
+ std::auto_ptr<ex> operator->() const
+ {
+ return std::auto_ptr<ex>(new ex(operator*()));
+ }
+
+ ex operator[](difference_type n) const
+ {
+ return e.op(i + n);
+ }
+
+ const_iterator &operator++() throw()
+ {
+ ++i;
+ return *this;
+ }
+
+ const_iterator operator++(int) throw()
+ {
+ const_iterator tmp = *this;
+ ++i;
+ return tmp;
+ }
+
+ const_iterator &operator+=(difference_type n) throw()
+ {
+ i += n;
+ return *this;
+ }
+
+ const_iterator operator+(difference_type n) const throw()
+ {
+ return const_iterator(e, i + n);
+ }
+
+ inline friend const_iterator operator+(difference_type n, const const_iterator &it) throw()
+ {
+ return const_iterator(it.e, it.i + n);
+ }
+
+ const_iterator &operator--() throw()
+ {
+ --i;
+ return *this;
+ }
+
+ const_iterator operator--(int) throw()
+ {
+ const_iterator tmp = *this;
+ --i;
+ return tmp;
+ }
+
+ const_iterator &operator-=(difference_type n) throw()
+ {
+ i -= n;
+ return *this;
+ }
+
+ const_iterator operator-(difference_type n) const throw()
+ {
+ return const_iterator(e, i - n);
+ }
+
+ inline friend difference_type operator-(const const_iterator &lhs, const const_iterator &rhs) throw()
+ {
+ return lhs.i - rhs.i;
+ }
+
+ bool operator==(const const_iterator &other) const throw()
+ {
+ return are_ex_trivially_equal(e, other.e) && i == other.i;
+ }
+
+ bool operator!=(const const_iterator &other) const throw()
+ {
+ return !(*this == other);
+ }
+
+ bool operator<(const const_iterator &other) const throw()
+ {
+ return i < other.i;
+ }
+
+ bool operator>(const const_iterator &other) const throw()
+ {
+ return other < *this;
+ }
+
+ bool operator<=(const const_iterator &other) const throw()
+ {
+ return !(other < *this);
+ }
+
+ bool operator>=(const const_iterator &other) const throw()
+ {
+ return !(*this < other);
+ }
+
+protected:
+ ex e; // this used to be a "const basic *", but in view of object fusion that wouldn't be safe
+ size_t i;
+};
+
+namespace internal {
+
+struct _iter_rep {
+ _iter_rep(const ex &e_, size_t i_, size_t i_end_) : e(e_), i(i_), i_end(i_end_) {}
+
+ bool operator==(const _iter_rep &other) const throw()
+ {
+ return are_ex_trivially_equal(e, other.e) && i == other.i;
+ }
+
+ bool operator!=(const _iter_rep &other) const throw()
+ {
+ return !(*this == other);
+ }
+
+ ex e;
+ size_t i;
+ size_t i_end;
+};
+
+} // namespace internal
+
+class const_preorder_iterator : public std::iterator<std::forward_iterator_tag, ex, ptrdiff_t, const ex *, const ex &> {
+public:
+ const_preorder_iterator() throw() {}
+
+ const_preorder_iterator(const ex &e, size_t n)
+ {
+ s.push(internal::_iter_rep(e, 0, n));
+ }
+
+public:
+ reference operator*() const
+ {
+ return s.top().e;
+ }
+
+ pointer operator->() const
+ {
+ return &(s.top().e);
+ }
+
+ const_preorder_iterator &operator++()
+ {
+ increment();
+ return *this;
+ }
+
+ const_preorder_iterator operator++(int)
+ {
+ const_preorder_iterator tmp = *this;
+ increment();
+ return tmp;
+ }
+
+ bool operator==(const const_preorder_iterator &other) const throw()
+ {
+ return s == other.s;
+ }
+
+ bool operator!=(const const_preorder_iterator &other) const throw()
+ {
+ return !(*this == other);
+ }
+
+private:
+ std::stack<internal::_iter_rep, std::vector<internal::_iter_rep> > s;
+
+ void increment()
+ {
+ while (!s.empty() && s.top().i == s.top().i_end) {
+ s.pop();
+ if (s.empty())
+ return;
+ ++s.top().i;
+ }
+
+ internal::_iter_rep & current = s.top();
+
+ if (current.i != current.i_end) {
+ const ex & child = current.e.op(current.i);
+ s.push(internal::_iter_rep(child, 0, child.nops()));
+ }
+ }
+};
+
+class const_postorder_iterator : public std::iterator<std::forward_iterator_tag, ex, ptrdiff_t, const ex *, const ex &> {
+public:
+ const_postorder_iterator() throw() {}
+
+ const_postorder_iterator(const ex &e, size_t n)
+ {
+ s.push(internal::_iter_rep(e, 0, n));
+ descend();
+ }
+
+public:
+ reference operator*() const
+ {
+ return s.top().e;
+ }
+
+ pointer operator->() const
+ {
+ return &(s.top().e);
+ }
+
+ const_postorder_iterator &operator++()
+ {
+ increment();
+ return *this;
+ }
+
+ const_postorder_iterator operator++(int)
+ {
+ const_postorder_iterator tmp = *this;
+ increment();
+ return tmp;
+ }
+
+ bool operator==(const const_postorder_iterator &other) const throw()
+ {
+ return s == other.s;
+ }
+
+ bool operator!=(const const_postorder_iterator &other) const throw()
+ {
+ return !(*this == other);
+ }
+
+private:
+ std::stack<internal::_iter_rep, std::vector<internal::_iter_rep> > s;
+
+ void descend()
+ {
+ while (s.top().i != s.top().i_end) {
+ internal::_iter_rep & current = s.top();
+ const ex & child = current.e.op(current.i);
+ s.push(internal::_iter_rep(child, 0, child.nops()));
+ }
+ }
+
+ void increment()
+ {
+ if (s.top().i == s.top().i_end)
+ s.pop();
+ if (!s.empty()) {
+ ++s.top().i;
+ descend();
+ }
+ }
+};
+
+inline const_iterator ex::begin() const throw()