help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Octave_map question.


From: John W. Eaton
Subject: Octave_map question.
Date: Mon, 29 Jan 2001 10:31:11 -0600

On 23-Jan-2001, Douglas Eck <address@hidden> wrote:

| In 2.1.32 this code works fine. Just call it with something like
| Octave2.1>testMap(struct('member',rand(10,10)))    
|  
|   #include <octave/oct.h>
|   #include <octave/ov-struct.h>
|   DEFUN_DLD (testMap, args, ,"Just testing.")
|   {
|     octave_value_list retval;
|     Octave_map s = args(0).map_value();
|     cout << s["member"].matrix_value() << endl;
|     return retval;
|   }  
| 
| *But* if we declare s and then assign it, the
| program segfaults:
|   #include <octave/oct.h>
|   #include <octave/ov-struct.h>
|   DEFUN_DLD (testMap, args, ,"Just testing.")
|   {
|     octave_value_list retval;
|     Octave_map s;
|     s = args(0).map_value();
|     cout << s["member"].matrix_value() << endl;
|     return retval;
|   }  
| 
| Thus, I cannot do this (i.e. declare Octave_map s outside of an if statement
| and assign it inside the if statement):
| 
|   Octave_map s;
|   if (nargin>0  && args(0).is_map()
| )
|     s = args(0).map_value();
| 
| What should I change?

Please try the following patch (it has been checked in to the CVS
archive).

Thanks,

jwe


2001-01-29  John W. Eaton  <address@hidden>

        * Map.h, Map.cc (CHMap<C>::operator = (const CHMap&)): New function.
        (Map<C>::operator = (const Map&)): Likewise.
        (Map<C> (const Map&)): Likewise.

Index: Map.cc
===================================================================
RCS file: /usr/local/cvsroot/octave/src/Map.cc,v
retrieving revision 1.24
diff -u -r1.24 Map.cc
--- Map.cc      2000/02/03 03:28:49     1.24
+++ Map.cc      2001/01/29 16:28:14
@@ -132,6 +132,42 @@
 }
 
 template <class C>
+CHMap<C>&
+CHMap<C>::operator = (const CHMap& a)
+{
+  Map<C>::operator = (*this);
+
+  unsigned int old_size = a.size;
+
+  CHNode<C> **old_tab = tab;
+  old_size = a.size;
+
+  size = old_size;
+  tab = new CHNode<C>* [size];
+
+  for (unsigned int i = 0; i < size; ++i)
+    tab[i] = static_cast<CHNode<C> *> (index_to_CHptr (i+1));
+
+  for (Pix p = a.first (); p; a.next (p))
+    (*this) [a.key (p)] = a.contents (p);
+
+  for (unsigned int i = 0; i < old_size; ++i)
+    {
+      CHNode<C> *p = old_tab[i];
+      old_tab[i] = static_cast<CHNode<C> *> (index_to_CHptr (i+1));
+      while (p->goodCHptr ())
+       {
+         CHNode<C> *nxt = p->tl;
+         delete p;
+         p = nxt;
+       }
+    }
+  delete [] old_tab;
+
+  return *this;
+}
+
+template <class C>
 Pix
 CHMap<C>::seek (const std::string& key) const
 {
Index: Map.h
===================================================================
RCS file: /usr/local/cvsroot/octave/src/Map.h,v
retrieving revision 1.18
diff -u -r1.18 Map.h
--- Map.h       2000/02/08 04:35:46     1.18
+++ Map.h       2001/01/29 16:28:17
@@ -53,8 +53,18 @@
   C def;
 
 public:
-  Map (const C& dflt) : def (dflt) { count = 0; }
+  Map (const C& dflt) : count (0), def (dflt) { }
 
+  Map (const Map& m) : count (m.count), def (m.def) { }
+
+  Map& operator = (const Map& m)
+    {
+      count = m.count;
+      def = m.def;
+
+      return *this;
+    }
+
   virtual ~Map (void) { }
 
   int length (void) const { return count; }    // current number of items
@@ -125,10 +135,12 @@
 
   CHMap (const CHMap& a);
 
+  CHMap& operator = (const CHMap& a);
+
   ~CHMap (void)
     {
       clear ();
-      delete tab;
+      delete [] tab;
     }
 
   C& operator [] (const std::string& key);
@@ -151,8 +163,8 @@
       if (p == 0)
        error ("null Pix");
 
-     return ((CHNode<C> *) p)->cont;
-   }
+      return ((CHNode<C> *) p)->cont;
+    }
 
   Pix seek (const std::string& key) const;
 



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]