From help-octave-request at bevo dot che dot wisc dot edu Mon Jan 29 10:31:42 2001 Subject: Octave_map question. From: "John W. Eaton" To: Douglas Eck Cc: "help-octave at bevo dot che dot wisc dot edu" Date: Mon, 29 Jan 2001 10:31:11 -0600 On 23-Jan-2001, Douglas Eck wrote: | In 2.1.32 this code works fine. Just call it with something like | Octave2.1>testMap(struct('member',rand(10,10))) | | #include | #include | 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 | #include | 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 * Map.h, Map.cc (CHMap::operator = (const CHMap&)): New function. (Map::operator = (const Map&)): Likewise. (Map (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 at @ -132,6 +132,42 @@ } template +CHMap& +CHMap::operator = (const CHMap& a) +{ + Map::operator = (*this); + + unsigned int old_size = a.size; + + CHNode **old_tab = tab; + old_size = a.size; + + size = old_size; + tab = new CHNode* [size]; + + for (unsigned int i = 0; i < size; ++i) + tab[i] = static_cast *> (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 *p = old_tab[i]; + old_tab[i] = static_cast *> (index_to_CHptr (i+1)); + while (p->goodCHptr ()) + { + CHNode *nxt = p->tl; + delete p; + p = nxt; + } + } + delete [] old_tab; + + return *this; +} + +template Pix CHMap::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 at @ -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 at @ -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); at @ -151,8 +163,8 @@ if (p == 0) error ("null Pix"); - return ((CHNode *) p)->cont; - } + return ((CHNode *) 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 -------------------------------------------------------------