[Pkg-octave-commit] [SCM] octave-symbolic branch, master, updated. a718b53403d9f164f8c2a3df521385a01d28a11b

watsma watsma at 416fae20-06d0-4450-9b69-c6c34d4b5f03
Mon Jan 3 03:40:37 UTC 2011


The following commit has been merged in the master branch:
commit 093d7a9444bd9c69129e3d874b0cd1232f310d7d
Author: watsma <watsma at 416fae20-06d0-4450-9b69-c6c34d4b5f03>
Date:   Mon May 5 21:39:28 2003 +0000

    main/symbolic/ov-ex.h
    main/symbolic/ov-ex.cc
      Modified the constructors and the destructor and added a symbol list
      and symbol reference count. New members are:
           void assign_symbol_to_list(GiNaC::symbol &sym);
           class symbol_list_item;
           static std::vector<symbol_list_item> symbol_list;
      This is a work-around for the fact that GiNaC can have different
      symbols with the same string name, while in octave you want a symbol
      that appears the same to actually be the same. It works great as long as
      symbols are declared in octave. It is still possible to have same-name
      symbols that are different to GiNaC, if a symbol goes out of scope in
      octave without it being defined in octave's workspace. This could be
      fixed by also keeping reference counts for symbols in expressions.
    
    
    git-svn-id: https://octave.svn.sourceforge.net/svnroot/octave/trunk/octave-forge/main/symbolic@922 416fae20-06d0-4450-9b69-c6c34d4b5f03

diff --git a/ov-ex.cc b/ov-ex.cc
index c348231..ba5dd6f 100644
--- a/ov-ex.cc
+++ b/ov-ex.cc
@@ -18,24 +18,72 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 */
 
+// 2003-05-1 Willem Atsma <watsma at users.sf.net>
+// * Modified the constructors and the destructor and added a symbol list
+//   and symbol reference count. New members are:
+//        void assign_symbol_to_list(GiNaC::symbol &sym);
+//        class symbol_list_item;
+//        static std::vector<symbol_list_item> symbol_list;
+//   This is a work-around for the fact that GiNaC can have different symbols
+//   with the same string name, while in octave you want a symbol that
+//   appears the same to actually be the same. It works great as long as
+//   symbols are declared in octave. It is still possible to have same-name
+//   symbols that are different to GiNaC, if a symbol goes out of scope in
+//   octave without it being defined in octave's workspace. This could be fixed
+//   by also keeping reference counts for symbols in expressions.
+
+
 #include <octave/config.h>
 #include <ginac/ginac.h>
 #include "ov-ex.h"
 #include "ov-vpa.h"
 
+std::vector<octave_ex::symbol_list_item> octave_ex::symbol_list;
+
+// A list of symbols is maintained to ensure all symbols that look the
+// same in octave are in fact the same to GiNaC. The destructor removes
+// items if the reference count goes to zero.
+void octave_ex::assign_symbol_to_list(GiNaC::symbol &sym)
+{
+	unsigned int i;
+	std::string name = sym.get_name();
+	// check if new symbol already exists
+	for(i=0;i<symbol_list.size();i++) {
+		if(symbol_list[i].sym.get_name() == name) {
+			symbol_list[i].refcount++;
+			sym = symbol_list[i].sym;
+			return;
+		}
+	}
+	// add new symbol to list
+	symbol_list_item new_item(sym,1);
+	symbol_list.push_back(new_item);
+}
+
 octave_ex::octave_ex(octave_ex &ox)
 {
-  x = ox.x;
+  if(GiNaC::is_a<GiNaC::symbol>(ox.x)) {
+  	GiNaC::symbol sym = GiNaC::ex_to<GiNaC::symbol>(ox.x);
+	assign_symbol_to_list(sym);
+	x = sym;
+  } else
+    x = ox.x;
 }
 
 octave_ex::octave_ex(GiNaC::ex expression)
 {
-  x = expression;
+  if(GiNaC::is_a<GiNaC::symbol>(expression)) {
+  	GiNaC::symbol sym = GiNaC::ex_to<GiNaC::symbol>(expression);
+	assign_symbol_to_list(sym);
+	x = sym;
+  } else
+    x = expression;
 }
 
 octave_ex::octave_ex(GiNaC::symbol sym)
 {
-  x = sym;
+	assign_symbol_to_list(sym);
+	x = sym;
 }
 
 octave_ex::octave_ex(octave_complex &cmplx)
@@ -60,6 +108,22 @@ octave_ex::octave_ex(double d)
   x = d;
 }
 
+// If this is a symbol, remove list entry if it exists.
+octave_ex::~octave_ex()
+{
+	if((GiNaC::is_a<GiNaC::symbol>(x)) && (!symbol_list.empty())) {
+		GiNaC::symbol sym = GiNaC::ex_to<GiNaC::symbol>(x);
+		std::vector<symbol_list_item>::iterator iter_symlist;
+		for(iter_symlist=symbol_list.begin();iter_symlist<symbol_list.end();iter_symlist++) {
+			if(sym == iter_symlist->sym) {
+				iter_symlist->refcount --;
+				if(iter_symlist->refcount==0)
+					symbol_list.erase(iter_symlist);
+			}
+		}
+	}
+}
+
 void 
 octave_ex::print(std::ostream& os,bool pr_as_read_syntax) const
 {
diff --git a/ov-ex.h b/ov-ex.h
index 0817b01..d1a22ae 100644
--- a/ov-ex.h
+++ b/ov-ex.h
@@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #if !defined (octave_ex_h)
 #define octave_ex_h 1
 
+#include <vector>
 #include <ginac/ginac.h>
 #include <octave/ov-complex.h>
 #include <octave/ov-scalar.h>
@@ -44,7 +45,7 @@ public:
   octave_ex(octave_scalar &s);
   octave_ex(double d);
 
-  ~octave_ex() {}
+  ~octave_ex();
   
   octave_ex& operator=(const octave_ex&);
 
@@ -78,6 +79,18 @@ public:
   
 private:
   GiNaC::ex x;
+  
+  void assign_symbol_to_list(GiNaC::symbol &sym);
+
+  class symbol_list_item {
+  public:
+  	symbol_list_item() {}
+	symbol_list_item(GiNaC::symbol s, int c) {sym=s; refcount=c;}
+  	GiNaC::symbol sym;
+	int refcount;
+  };
+  
+  static std::vector<symbol_list_item> symbol_list;
 
   DECLARE_OCTAVE_ALLOCATOR
 

-- 
octave-symbolic



More information about the Pkg-octave-commit mailing list