]> www.ginac.de Git - ginac.git/blobdiff - ginac/ex.h
inlined ex::swap() and provided versions of iter_swap() for ex and expair
[ginac.git] / ginac / ex.h
index 0aa96ebc0382f69fb92b9e11fbaad9990147c7a7..f59cd7aea21b5486a3aee142067b0350d3487467 100644 (file)
@@ -97,7 +97,19 @@ public:
        
        // non-virtual functions in this class
 public:
-       void swap(ex & other);
+       /** Efficiently swap the contents of two expressions. */
+       void swap(ex & other)
+       {
+               GINAC_ASSERT(bp!=0);
+               GINAC_ASSERT(bp->flags & status_flags::dynallocated);
+               GINAC_ASSERT(other.bp!=0);
+               GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
+       
+               basic * tmpbp = bp;
+               bp = other.bp;
+               other.bp = tmpbp;
+       }
+
        void print(const print_context & c, unsigned level = 0) const;
        void printtree(std::ostream & os) const;
        void dbgprint(void) const;
@@ -457,6 +469,10 @@ inline bool is_zero(const ex & thisex)
 inline void swap(ex & e1, ex & e2)
 { e1.swap(e2); }
 
+// This makes STL algorithms use the more efficient swap operation for ex objects
+inline void iter_swap(std::vector<ex>::iterator i1, std::vector<ex>::iterator i2)
+{ i1->swap(*i2); }
+
 
 /* Function objects for STL sort() etc. */
 struct ex_is_less : public std::binary_function<ex, ex, bool> {