Mercurial > hg > octave-lyh
changeset 4762:bec345670e56
[project @ 2004-02-16 05:07:23 by jwe]
author | jwe |
---|---|
date | Mon, 16 Feb 2004 05:07:23 +0000 |
parents | c62b067609f8 |
children | ac927178fce7 |
files | liboctave/Array-C.cc liboctave/Array-ch.cc liboctave/Array-d.cc liboctave/Array.cc liboctave/Array.h liboctave/CNDArray.cc liboctave/CNDArray.h liboctave/chNDArray.cc liboctave/chNDArray.h liboctave/dNDArray.cc liboctave/dNDArray.h liboctave/mx-inlines.cc src/ChangeLog src/data.cc src/ov-cell.cc |
diffstat | 15 files changed, 149 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Array-C.cc +++ b/liboctave/Array-C.cc @@ -33,6 +33,8 @@ INSTANTIATE_ARRAY_AND_ASSIGN (Complex); +INSTANTIATE_CAT (Complex); + INSTANTIATE_ARRAY_ASSIGN (Complex, double); INSTANTIATE_ARRAY_ASSIGN (Complex, int); INSTANTIATE_ARRAY_ASSIGN (Complex, short);
--- a/liboctave/Array-ch.cc +++ b/liboctave/Array-ch.cc @@ -31,6 +31,8 @@ INSTANTIATE_ARRAY_AND_ASSIGN (char); +INSTANTIATE_CAT (char); + #include "Array2.h" template class Array2<char>;
--- a/liboctave/Array-d.cc +++ b/liboctave/Array-d.cc @@ -31,6 +31,8 @@ INSTANTIATE_ARRAY_AND_ASSIGN (double); +INSTANTIATE_CAT (double); + INSTANTIATE_ARRAY_ASSIGN (double, int); INSTANTIATE_ARRAY_ASSIGN (double, short); INSTANTIATE_ARRAY_ASSIGN (double, char);
--- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -2999,6 +2999,38 @@ } template <class T> +bool +cat_ra (Array<T>& ra_cat, const Array<T>& ra_arg, int dim, int add_dim) +{ + bool retval = false; + + dim_vector dv = ra_arg.dims (); + + Array<int> ra_idx (dv.length (), 0); + + for (int i = 0; i < ra_arg.length (); i++) + { + if (i != 0) + increment_index (ra_idx, dv); + + Array<int> ra_idx2 = ra_idx; + + if (dim >= ra_idx2.length ()) + { + ra_idx2.resize_and_fill (dim + 1, 0); + + retval = true; + } + + ra_idx2(dim) = ra_idx2(dim) + add_dim; + + ra_cat(ra_idx2) = ra_arg(ra_idx); + } + + return retval; +} + +template <class T> void Array<T>::print_info (std::ostream& os, const std::string& prefix) const {
--- a/liboctave/Array.h +++ b/liboctave/Array.h @@ -523,6 +523,10 @@ // Array<T>::dimensions should be protected, not public, but we can't // do that because of bugs in gcc prior to 3.3. +template <class T> +bool +cat_ra (Array<T>& ra_cat, const Array<T>& ra_arg, int dim, int add_dim); + template <class LT, class RT> /* friend */ int assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); @@ -553,9 +557,14 @@ template int assignN (Array<LT>&, const Array<RT>&, const LT&); \ template int assign (Array<LT>&, const Array<RT>&) + #define INSTANTIATE_ARRAY(T) \ template class Array<T>; \ - template T resize_fill_value (const T&) + template T resize_fill_value (const T&); \ + +#define INSTANTIATE_CAT(T) \ + template bool cat_ra (Array<T>& ra_cat, const Array<T>& ra_arg, \ + int dim, int add_dim) #define INSTANTIATE_ARRAY_AND_ASSIGN(T) \ INSTANTIATE_ARRAY (T); \
--- a/liboctave/CNDArray.cc +++ b/liboctave/CNDArray.cc @@ -232,7 +232,7 @@ } bool -ComplexNDArray::cat (ComplexNDArray& cat_arr, int dim, int add_dim) const +ComplexNDArray::cat (const ComplexNDArray& ra_arg, int dim, int add_dim) { MX_ND_CAT; }
--- a/liboctave/CNDArray.h +++ b/liboctave/CNDArray.h @@ -87,7 +87,7 @@ ComplexNDArray prod (int dim = -1) const; ComplexNDArray sum (int dim = -1) const; ComplexNDArray sumsq (int dim = -1) const; - bool cat (ComplexNDArray& cat_array, int dim, int add_dim) const; + bool cat (const ComplexNDArray& ra_arg, int dim, int add_dim); NDArray abs (void) const;
--- a/liboctave/chNDArray.cc +++ b/liboctave/chNDArray.cc @@ -49,7 +49,7 @@ } bool -charNDArray::cat (charNDArray& cat_arr, int dim, int add_dim) const +charNDArray::cat (const charNDArray& ra_arg, int dim, int add_dim) { MX_ND_CAT; }
--- a/liboctave/chNDArray.h +++ b/liboctave/chNDArray.h @@ -71,7 +71,7 @@ boolNDArray all (int dim = -1) const; boolNDArray any (int dim = -1) const; - bool cat (charNDArray& cat_array, int dim, int add_dim) const; + bool cat (const charNDArray& ra_arg, int dim, int add_dim); charMatrix matrix_value (void) const;
--- a/liboctave/dNDArray.cc +++ b/liboctave/dNDArray.cc @@ -213,9 +213,10 @@ } bool -NDArray::cat (NDArray& cat_arr, int dim, int add_dim) const +NDArray::cat (const NDArray& ra_arg, int dim, int add_dim) { - MX_ND_CAT; + // MX_ND_CAT; + return ::cat_ra (*this, ra_arg, dim, add_dim); } NDArray
--- a/liboctave/dNDArray.h +++ b/liboctave/dNDArray.h @@ -86,7 +86,7 @@ NDArray prod (int dim = -1) const; NDArray sum (int dim = -1) const; NDArray sumsq (int dim = -1) const; - bool cat (NDArray& cat_array, int dim, int add_dim) const; + bool cat (const NDArray& ra_arg, int dim, int add_dim); NDArray abs (void) const;
--- a/liboctave/mx-inlines.cc +++ b/liboctave/mx-inlines.cc @@ -610,11 +610,11 @@ #define MX_ND_CAT \ bool retval = false;\ \ - dim_vector dv = dims (); \ + dim_vector dv = ra_arg.dims (); \ \ Array<int> ra_idx (dv.length (), 0); \ \ - for (int i = 0; i < length (); i++) \ + for (int i = 0; i < ra_arg.length (); i++) \ { \ if (i != 0) \ increment_index (ra_idx, dv, 0); \ @@ -630,7 +630,7 @@ \ ra_idx2.elem (dim) = ra_idx2.elem (dim) + add_dim; \ \ - cat_arr.elem (ra_idx2) = elem (ra_idx); \ + elem (ra_idx2) = ra_arg.elem (ra_idx); \ } \ \ return retval
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2004-02-15 Petter Risholm <risholm@stud.ntnu.no> + + * ov-cell.cc (Fstruct2cell): New function. + 2004-02-14 John W. Eaton <jwe@bevo.che.wisc.edu> * Makefile.in (OCTINTERP_LINK_DEPS, OCT_LINK_DEPS): Always define.
--- a/src/data.cc +++ b/src/data.cc @@ -808,7 +808,7 @@ cat_cx = ComplexNDArray (cat_re); extended_dims = - tmp.complex_array_value ().cat (cat_cx, dim, curr_add_dims); + cat_cx.cat (tmp.complex_array_value (), dim, curr_add_dims); t = COMPLEX; } @@ -819,18 +819,18 @@ cat_ch = charNDArray (octave_value (cat_re).char_array_value ()); extended_dims = - tmp.char_array_value ().cat (cat_ch, dim, curr_add_dims); + cat_ch.cat (tmp.char_array_value (), dim, curr_add_dims); t = CHAR; } else extended_dims = - tmp.array_value().cat (cat_re, dim, curr_add_dims); + cat_re.cat (tmp.array_value(), dim, curr_add_dims); } else if (t == COMPLEX) { extended_dims = - tmp.complex_array_value ().cat (cat_cx, dim, curr_add_dims); + cat_cx.cat (tmp.complex_array_value (), dim, curr_add_dims); } else if (t == CHAR) { @@ -841,7 +841,7 @@ } else extended_dims = - tmp.char_array_value ().cat (cat_ch, dim, curr_add_dims); + cat_ch.cat (tmp.char_array_value (), dim, curr_add_dims); } if (error_state) return retval; // Wrong conversion in the last if statement
--- a/src/ov-cell.cc +++ b/src/ov-cell.cc @@ -44,6 +44,7 @@ #include "ov-base-mat.cc" #include "ov-re-mat.h" #include "ov-scalar.h" +#include "Array-util.h" #include "byte-swap.h" #include "ls-oct-ascii.h" @@ -1010,6 +1011,86 @@ return retval; } +DEFUN (struct2cell, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} struct2cell (@var{S})\n\ +Create a new cell array from the objects stored in the struct object.\n\ +If F is the number of fields in the structure, the resulting cell array will\n\ +have a dimension vector corresponding to [F size(S)].\n\ +\n\ +@end deftypefn\n\ +@seealso{cell2struct, fieldnames}") +{ + octave_value retval; + + int nargin = args.length (); + + if (nargin == 1) + { + Octave_map m = args(0).map_value(); + + if (! error_state) + { + dim_vector m_dv = m.dims (); + + string_vector keys = m.keys (); + + int fields_numel = keys.length (); + + // The resulting dim_vector should have dimensions: + // [numel(fields) size(struct)] + dim_vector result_dv; + result_dv.resize (m_dv.length () + 1); //Add 1 for the fields + + result_dv(0) = fields_numel; + + for (int i = 1; i < result_dv.length (); i++) + result_dv(i) = m_dv(i-1); + + // Squeeze to be sure that a (3,1) vector doesn't + // get turned into a (3,3,1) vector. + result_dv = result_dv.squeeze (); + + Cell c (result_dv); + + // Use ra_idx both for counting and for assignments, + // so ra_idx(0) will both contain fields_numel for + // each call to increment_index and j for each assignment. + Array<int> ra_idx (result_dv.length (), 0); + ra_idx(0) = fields_numel; + + for (int i = 0; i < m_dv.numel (); i++) + { + for (int j = 0; j < fields_numel; j++) + { + ra_idx(0) = j; + + Cell c_tmp = m.contents (keys(j)); + + if (c_tmp.length () > 1) // Is a cell + c(ra_idx) = c_tmp; + else if (c_tmp.length () == 1) // Get octave_value + c(ra_idx) = c_tmp(0); + else // c.tmp.length () == 0, Create empty Cell + c(ra_idx) = Cell(); + + ra_idx(0) = fields_numel; + } + + increment_index(ra_idx, result_dv); + } + + retval = c; + } + else + error ("struct2cell: expecting argument to be a cell array"); + } + else + print_usage ("struct2cell"); + + return retval; +} + /* ;;; Local Variables: *** ;;; mode: C++ ***