+private:
+ mutable ptr<basic> bp; ///< pointer to basic object managed by this
+
+#ifdef OBSCURE_CINT_HACK
+public:
+ static ptr<basic> last_created_or_assigned_bp;
+ static basic * dummy_bp;
+ static long last_created_or_assigned_exp;
+#endif // def OBSCURE_CINT_HACK
+};
+
+
+// performance-critical inlined method implementations
+
+// This needs to be a basic* because we don't know that numeric is derived
+// from basic and we need a basic& for the ex default constructor
+extern const basic *_num0_bp;
+
+inline
+ex::ex() throw() : bp(*const_cast<basic *>(_num0_bp))
+{
+ GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+#ifdef OBSCURE_CINT_HACK
+ update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+}
+
+#ifdef OBSCURE_CINT_HACK
+inline
+ex::ex(const ex & other) : bp(other.bp)
+{
+ GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
+ update_last_created_or_assigned_bp();
+}
+
+inline
+ex & ex::operator=(const ex & other)
+{
+ GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+ GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
+ bp = other.bp;
+ update_last_created_or_assigned_bp();
+ return *this;
+}
+#endif // def OBSCURE_CINT_HACK
+
+inline
+ex::ex(const basic & other) : bp(construct_from_basic(other))
+{
+ GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+#ifdef OBSCURE_CINT_HACK
+ update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+}
+
+inline
+ex::ex(int i) : bp(construct_from_int(i))
+{
+ GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+#ifdef OBSCURE_CINT_HACK
+ update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+}
+
+inline
+ex::ex(unsigned int i) : bp(construct_from_uint(i))
+{
+ GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+#ifdef OBSCURE_CINT_HACK
+ update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+}
+
+inline
+ex::ex(long i) : bp(construct_from_long(i))
+{
+ GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+#ifdef OBSCURE_CINT_HACK
+ update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+}
+
+inline
+ex::ex(unsigned long i) : bp(construct_from_ulong(i))
+{
+ GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+#ifdef OBSCURE_CINT_HACK
+ update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+}
+
+inline
+ex::ex(double const d) : bp(construct_from_double(d))
+{
+ GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+#ifdef OBSCURE_CINT_HACK
+ update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+}
+
+inline
+ex::ex(const std::string &s, const ex &l) : bp(construct_from_string_and_lst(s, l))
+{
+ GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+#ifdef OBSCURE_CINT_HACK
+ update_last_created_or_assigned_bp();
+#endif // def OBSCURE_CINT_HACK
+}
+
+inline
+int ex::compare(const ex & other) const
+{
+ if (bp == other.bp) // trivial case: both expressions point to same basic
+ return 0;
+ const int cmpval = bp->compare(*other.bp);
+ if (cmpval == 0) {
+ // Expressions point to different, but equal, trees: conserve
+ // memory and make subsequent compare() operations faster by
+ // making both expression point to the same tree.
+ share(other);
+ }
+ return cmpval;
+}
+
+inline
+bool ex::is_equal(const ex & other) const
+{
+ if (bp == other.bp) // trivial case: both expressions point to same basic
+ return true;
+ return bp->is_equal(*other.bp);
+}
+
+
+// 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);
+ }
+
+#if 0
+ // How do we make this work in the context of the "reference to
+ // temporary" problem? Return an auto_ptr?
+ pointer operator->() const
+ {
+ return &(operator*());
+ }
+#endif
+
+ 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() {}
+
+ // Provide implicit conversion from const_iterator, so begin() and
+ // end() can be used to create const_preorder_iterators
+ const_preorder_iterator(const const_iterator & cit)
+ {
+ s.push(internal::_iter_rep(cit.e, cit.i, cit.e.nops()));
+ }
+
+public:
+ ex operator*() const
+ {
+ const internal::_iter_rep & r = s.top();
+ return r.e.op(r.i);
+ }
+
+ // operator->() not implemented (see above)
+
+ 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> s;
+
+ void increment()
+ {
+ internal::_iter_rep & current = s.top();
+ const ex & child = current.e.op(current.i);
+ size_t n = child.nops();
+ if (n)
+ s.push(internal::_iter_rep(child, 0, n));
+ else
+ ++current.i;
+
+ while (s.top().i == s.top().i_end && s.size() > 1) {
+ s.pop();
+ ++s.top().i;
+ }
+ }
+};
+
+class const_postorder_iterator : public std::iterator<std::forward_iterator_tag, ex, ptrdiff_t, const ex *, const ex &>
+{
+public:
+ const_postorder_iterator() throw() {}
+
+ // Provide implicit conversion from const_iterator, so begin() and
+ // end() can be used to create const_postorder_iterators
+ const_postorder_iterator(const const_iterator & cit)
+ {
+ s.push(internal::_iter_rep(cit.e, cit.i, cit.e.nops()));
+ descend();
+ }
+