[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Structure assignments
From: |
John W. Eaton |
Subject: |
Structure assignments |
Date: |
Mon, 27 Aug 2007 13:22:39 -0400 |
On 27-Aug-2007, David Bateman wrote:
| In looking at how Octave and Matlab handle comma separated lists as
| input and output arguments of functions, I noted that structure arrays
| can equally be used to create a comma separated list. So I then thought
| that something like
|
| x = ceil (randn (10, 1));
| in = struct ('call1', {x, Inf, 'last'}, 'call2', {x, Inf, 'first'});
| out = struct ('call1', cell (2, 1), 'call2', cell (2, 1));
| [out.call1] = find (in.call1);
| [out.call2] = find (in.call2);
|
| should be valid and in fact it is in Matlab R20007a. However, in Octave
| 2.9.13 a comma separated list created from a structure array is only
| valid as an input to a function, and not at all on the LHS... What would
| be involved in adding this feature? Should it be done before 3.0?
Try the following patch. It should fix the problem you mention but not
the additional [x(idx).a] = ... case.
jwe
src/ChangeLog:
2007-08-27 John W. Eaton <address@hidden>
* Cell.h (Cell::Cell (const octave_value_list&)): Create row
vector instead of column vector.
* pt-idx.cc (tree_index_expression::lvalue): Handle [x.a] =
... style assignments.
* ov-struct.cc (octave_struct::subsasgn): Handle case of RHS as
comma-separated list.
Index: src/Cell.h
===================================================================
RCS file: /cvs/octave/src/Cell.h,v
retrieving revision 1.31
diff -u -u -r1.31 Cell.h
--- src/Cell.h 15 Nov 2006 16:12:58 -0000 1.31
+++ src/Cell.h 27 Aug 2007 17:09:50 -0000
@@ -45,7 +45,7 @@
: ArrayN<octave_value> (dim_vector (1, 1), val) { }
Cell (const octave_value_list& ovl)
- : ArrayN<octave_value> (dim_vector (ovl.length (), 1))
+ : ArrayN<octave_value> (dim_vector (1, ovl.length ()))
{
for (octave_idx_type i = 0; i < ovl.length (); i++)
elem (i) = ovl (i);
Index: src/ov-struct.cc
===================================================================
RCS file: /cvs/octave/src/ov-struct.cc,v
retrieving revision 1.84
diff -u -u -r1.84 ov-struct.cc
--- src/ov-struct.cc 10 Aug 2007 19:07:35 -0000 1.84
+++ src/ov-struct.cc 27 Aug 2007 17:09:54 -0000
@@ -363,7 +363,18 @@
std::string key = key_idx(0).string_value ();
- map.assign (key, t_rhs);
+ if (t_rhs.is_cs_list ())
+ {
+ Cell tmp_cell = Cell (t_rhs.list_value ());
+
+ // FIXME -- shouldn't care if the dimensions of the
+ // RHS don't match the dimensions of the subscriped
+ // LHS.
+
+ map.assign (key, tmp_cell);
+ }
+ else
+ map.assign (key, t_rhs);
if (! error_state)
{
Index: src/pt-idx.cc
===================================================================
RCS file: /cvs/octave/src/pt-idx.cc,v
retrieving revision 1.32
diff -u -u -r1.32 pt-idx.cc
--- src/pt-idx.cc 10 Aug 2007 19:07:35 -0000 1.32
+++ src/pt-idx.cc 27 Aug 2007 17:09:54 -0000
@@ -476,9 +476,22 @@
case '.':
{
- idx.push_back (octave_value (get_struct_index (p_arg_nm,
p_dyn_field)));
+ octave_value tidx = get_struct_index (p_arg_nm, p_dyn_field);
- if (error_state)
+ if (! error_state)
+ {
+ idx.push_back (tidx);
+
+ if (i == n-1)
+ {
+ // Last indexing element. Will this result in a
+ // comma-separated list?
+
+ if (first_retval_object.is_map ())
+ retval.numel (first_retval_object.numel ());
+ }
+ }
+ else
eval_error ();
}
break;