help-octave
[Top][All Lists]
Advanced

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

Problem with array of structures in 2.1.69 (worked in 2.1.50)


From: John W. Eaton
Subject: Problem with array of structures in 2.1.69 (worked in 2.1.50)
Date: Mon, 2 May 2005 07:10:13 -0400

On  2-May-2005, address@hidden wrote:

| I am experiencing a problem with the way octave handles arrays of
| structures, which appears to have changed between version 2.1.50 and
| 2.1.69.
| The following works in 2.1.50:
| b(2).a=3;
| b(3).x=[0 1 2];
| b(2).x=5;
| At this point I get:
| b =
| {
|   a =
|   (
|     [1] = []
|     [2] = 3
|     [3] = []
|   )
|   x =
|   (
|     [1] = []
|     [2] = 5
|     [3] =
| 
|             0        1        2
| 
|   )
| }
| Which is fine. 
| I also get:
| >> b(2)
| ans =
| {
|   a = 3
|   x = 5
| }
| And
| >> b(3)
| ans =
| {
|   a = []
|   x =
| 
|           0        1        2
| 
| }
| 
| This looks good to me.
| Now in 2.1.69 I get:
| b =
| {
|   a =
| 
|   (,
|     [1] = [](0x0)
|     [2] = 3                <<< notice that there is no entry for [3]=[]
| like in 2.1.50
|   ,)
| 
|   x =
| 
|   (,
|     [1] = [](0x0)
|     [2] = 5
|     [3] =
| 
|       0  1  2
| 
|   ,)
| 
| }
| Still good, do the commas mean anything or is this just a print
| formatting?
| Now comes the problem:
| octave:12> b(2)
| ans =
| {
|   a = 3
|   x = 5
| }
| But
| octave:13> b(3)
| error: invalid vector index = 3
| 
| Any explanation?
| It appears that while 2.1.50 initialises unused array entries
| automatically, 2.1.69 is not doing it any more, showing also in this
| output:
| 2.1.50:
| >> b.a
| ans =
| (
|   [1] = []
|   [2] = 3
|   [3] = []
| )
| But 2.1.69 (same input!)
| octave:3> b.a
| ans =
| 
| (,
|   [1] = [](0x0)
|   [2] = 3
| ,)
| 
| Obviously octave has not initialised the third entry for b.a.
| 
| What can I do to solve this problem?

It looks like a bug.

Please try the following patch.

Thanks,

jwe


src/ChangeLog:

2005-05-02  John W. Eaton  <address@hidden>

        * oct-map.h, oct-map.cc (Octave_map::seek, Octave_map::contents):
        New non-const versions.
        (Octave_map::assign (const octave_value_list&,
        const std::string&, const Cell&)): Allow both tmp RHS and LHS to
        be resized.  For clarity, always resize to new_dims.


Index: src/oct-map.h
===================================================================
RCS file: /usr/local/cvsroot/octave/src/oct-map.h,v
retrieving revision 1.35.2.2
diff -u -r1.35.2.2 oct-map.h
--- src/oct-map.h       26 Apr 2005 19:43:58 -0000      1.35.2.2
+++ src/oct-map.h       2 May 2005 10:44:55 -0000
@@ -95,8 +95,12 @@
 
   std::string key (const_iterator p) const { return p->first; }
 
+  Cell& contents (const std::string& k);
   Cell contents (const std::string& k) const;
 
+  Cell& contents (const_iterator p)
+    { return contents (key(p)); }
+
   Cell contents (const_iterator p) const
     { return contents (key(p)); }
 
@@ -105,6 +109,7 @@
   std::string stringfield (const std::string& k,
                           const std::string& def_val = std::string ()) const;
 
+  iterator seek (const std::string& k) { return map.find (k); }
   const_iterator seek (const std::string& k) const { return map.find (k); }
 
   bool contains (const std::string& k) const
Index: src/oct-map.cc
===================================================================
RCS file: /usr/local/cvsroot/octave/src/oct-map.cc,v
retrieving revision 1.37.2.2
diff -u -r1.37.2.2 oct-map.cc
--- src/oct-map.cc      26 Apr 2005 19:43:58 -0000      1.37.2.2
+++ src/oct-map.cc      2 May 2005 11:08:18 -0000
@@ -35,6 +35,12 @@
 #include "oct-map.h"
 #include "utils.h"
 
+Cell&
+Octave_map::contents (const std::string& k)
+{
+  return map[k];
+}
+
 Cell
 Octave_map::contents (const std::string& k) const
 {
@@ -272,10 +278,11 @@
        {
          tmp.resize (new_dims, fill_value);
        }
-      else if (new_dims != curr_dims)
+
+      if (new_dims != curr_dims)
        {
          for (iterator p = begin (); p != end (); p++)
-           contents(p).resize (rhs_dims, fill_value);
+           contents(p).resize (new_dims, fill_value);
        }
 
       dimensions = new_dims;



-------------------------------------------------------------
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]