]> www.ginac.de Git - ginac.git/commitdiff
- implemented object fusion as proposed by Richy
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Sun, 24 Aug 2003 22:55:54 +0000 (22:55 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Sun, 24 Aug 2003 22:55:54 +0000 (22:55 +0000)
- unit(), content() and primpart() take a "const ex &" instead of a
  "const symbol &". degree(), coeff(), collect() etc. have dropped the
  restriction to symbols a long time ago, so there's no reason for these
  function to keep it.

ginac/ex.cpp
ginac/ex.h

index e3e54c132d2d8fb27348d56efc457e1fdf6b4ff8..1af7f214efa49d76340d78fe8ae0ac679e179f09 100644 (file)
@@ -242,6 +242,19 @@ void ex::makewriteable()
        GINAC_ASSERT(bp->refcount == 1);
 }
 
+/** Share equal objects between expressions.
+ *  @see ex::compare(const basic &) */
+void ex::share(const ex & other) const
+{
+       if ((bp->flags & status_flags::not_shareable) || (other.bp->flags & status_flags::not_shareable))
+               return;
+
+       if (bp->refcount <= other.bp->refcount)
+               bp = other.bp;
+       else
+               other.bp = bp;
+}
+
 /** Helper function for the ex-from-basic constructor. This is where GiNaC's
  *  automatic evaluator and memory management are implemented.
  *  @see ex::ex(const basic &) */
index 38dcf732f29f23203c6b1de8be57e01aabbd1d93..ee891a2fb9c431e0c66f7440a00ad5bc74c6e654 100644 (file)
@@ -304,11 +304,11 @@ public:
        ex numer_denom() const;
 
        // polynomial algorithms
-       ex unit(const symbol &x) const;
-       ex content(const symbol &x) const;
+       ex unit(const ex &x) const;
+       ex content(const ex &x) const;
        numeric integer_content() const;
-       ex primpart(const symbol &x) const;
-       ex primpart(const symbol &x, const ex &cont) const;
+       ex primpart(const ex &x) const;
+       ex primpart(const ex &x, const ex &cont) const;
        ex smod(const numeric &xi) const { return bp->smod(xi); }
        numeric max_coefficient() const;
 
@@ -345,6 +345,7 @@ private:
        static basic & construct_from_double(double d);
        static ptr<basic> construct_from_string_and_lst(const std::string &s, const ex &l);
        void makewriteable();
+       void share(const ex & other) const;
 
 #ifdef OBSCURE_CINT_HACK
 public:
@@ -368,7 +369,7 @@ protected:
 // member variables
 
 private:
-       ptr<basic> bp;      ///< pointer to basic object managed by this
+       mutable ptr<basic> bp;  ///< pointer to basic object managed by this
 
 #ifdef OBSCURE_CINT_HACK
 public:
@@ -481,7 +482,14 @@ int ex::compare(const ex & other) const
 {
        if (bp == other.bp)  // trivial case: both expressions point to same basic
                return 0;
-       return bp->compare(*other.bp);
+       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