From f82099178e27212fdc6801bc609202c0f9c2193e Mon Sep 17 00:00:00 2001 From: Christian Bauer Date: Wed, 27 Mar 2002 21:24:32 +0000 Subject: [PATCH] inlined ex::swap() and provided versions of iter_swap() for ex and expair objects to make the STL sorting algorithms a little more efficient --- ginac/ex.cpp | 13 ------------- ginac/ex.h | 18 +++++++++++++++++- ginac/expair.h | 7 +++++++ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/ginac/ex.cpp b/ginac/ex.cpp index b22ceef8..c41abee7 100644 --- a/ginac/ex.cpp +++ b/ginac/ex.cpp @@ -48,19 +48,6 @@ namespace GiNaC { // public -/** Efficiently swap the contents of two expressions. */ -void ex::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; -} - /** Print expression to stream. The formatting of the output is determined * by the kind of print_context object that is passed. Possible formattings * include ginsh-parsable output (the default), tree-like output for diff --git a/ginac/ex.h b/ginac/ex.h index 0aa96ebc..f59cd7ae 100644 --- a/ginac/ex.h +++ b/ginac/ex.h @@ -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::iterator i1, std::vector::iterator i2) +{ i1->swap(*i2); } + /* Function objects for STL sort() etc. */ struct ex_is_less : public std::binary_function { diff --git a/ginac/expair.h b/ginac/expair.h index 95444250..925db972 100644 --- a/ginac/expair.h +++ b/ginac/expair.h @@ -111,6 +111,13 @@ struct expair_swap : public std::binary_function { void operator()(expair &lh, expair &rh) const { lh.swap(rh); } }; +inline void swap(expair & e1, expair & e2) +{ e1.swap(e2); } + +// This makes STL algorithms use the more efficient swap operation for ex objects +inline void iter_swap(std::vector::iterator i1, std::vector::iterator i2) +{ i1->swap(*i2); } + } // namespace GiNaC #endif // ndef __GINAC_EXPAIR_H__ -- 2.44.0