Assign Cell to octave map: error

John W. Eaton jwe at octave.org
Fri Mar 13 14:26:06 CDT 2009


On 12-Mar-2009, alexander.rusakov at googlemail.com (Alexander Rusakov) wrote:

| Bug report for Octave 3.0.3 configured for i686-pc-linux-gnu
| 
| Description:
| -----------
| 
| I believe that I found a bug in Octave c++ API. The problem that it is not
| possible  to assign arbitrary cell to an octave_map because of error from
| octave API.  This happens if:
| 1) Octave_map already has got some data
| 2) The size of Cell != 1.
| 
| I guess that line 461 in oct_map.cc file contains a bug. Probably the whole
| function has got some bug because I can not understand why there is  a need
| of comparison of dimension of octave_map and Cell which is only an element
| in map.
| 
| 00448 Octave_map&
| 00449 Octave_map::assign (const std::string& k, const Cell& rhs)
| 00450 {
| 00451   if (nfields () == 0)
| 00452     {
| 00453       maybe_add_to_key_list (k);
| 00454
| 00455       map[k] = rhs;
| 00456
| 00457       dimensions = rhs.dims ();
| 00458     }
| 00459   else
| 00460     {
| 00461       if (dims () == rhs.dims ())
| 00462         {
| 00463           maybe_add_to_key_list (k);
| 00464
| 00465           map[k] = rhs;
| 00466         }
| 00467       else
| 00468         error ("invalid structure assignment");
| 00469     }
| 00470
| 
| 
| Repeat-By:
| ---------
| 
| My problem that this code does not work:
| 
|  //This does not work as I expect
|  Octave_map omap;
|  cout << " Adding cell arrays to one maps \n";
| {
|   // I am trying to create 1d cell array of size 1
|    dim_vector dv(1);
|    Cell oc(dv);
|    cout << "Trying to add cell array of size " << dv(0) << " is added to
| octave_map" << endl;
|    omap.assign("cell1", oc);
|    cout << "cell array of size " << dv(0) << " is added to octave_map" <<
| endl;
|  }
| 
|  {
|   // I am trying to create 1d cell array of size 2
|   dim_vector dv(1);
|   dv(0) = 2;
|   Cell oc(dv);
|   cout << "Trying to add cell array of size " << dv(0) << " is added to
| octave_map" << endl;
|  / /Here I get an error message  "error: invalid structure assignment"
|   omap.assign("cell2", oc);
|   cout << "cell array of size " << dv(0) << " is added to octave_map" <<
| endl;
|  }
| 
| So the produced output is :
| 
| Trying to add cell array of size 1 is added to octave_map
| cell array of size 1 is added to octave_map
| Trying to add cell array of size 2 is added to octave_map
| error: invalid structure assignment             <-------------------
| Octave message
| cell array of size 2 is added to octave_map
| 
| 
| I am also sending a simple example which demonstrates the problem, and when
| it happens. To compile:
| 
| mkoctfile TestOctaveCellMap.cpp
| To execute:
| octave --eval "TestOctaveCellMap"
| 
| 
| 
| Fix:
| ---
| 
|  I guess that assertion in oct_map.cc on line 461 must be removed. Probably
| extra changes are required.

No, I think it is correct.

An Octave_map is a wrapper for a std::map<std::string, Cell> object.
Each map key accesses a Cell object.  The dimension of the structure
array is the dimension of the Cell objects in the map.  The Octave_map
object ensures that all the Cells in the struct have the same size.

If you want to insert a Cell array into a 1x1 Octave_map object, then
you have to do something like

   dim_vector dv (1);
   dv(0) = 2;
   Cell oc (dv);
   omap.assign ("cell3", Cell (octave_value (oc)));

I suggest you make this change to your test function and return omap
from it so you can examine how Octave sees the structure you've
created.

jwe


More information about the Bug-octave mailing list