Mercurial > hg > octave-avbm
changeset 4654:a9b22513b7a6
[project @ 2003-11-24 18:56:35 by jwe]
author | jwe |
---|---|
date | Mon, 24 Nov 2003 18:56:35 +0000 |
parents | 14ab7b05a572 |
children | c8829691db47 |
files | src/ChangeLog src/Makefile.in src/OPERATORS/op-fcn-handle.cc src/ov-base-mat.cc src/ov-base.h src/ov-builtin.h src/ov-fcn-handle.cc src/ov-fcn-handle.h src/ov-mapper.h src/ov-usr-fcn.h src/ov.cc src/ov.h |
diffstat | 12 files changed, 319 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,22 @@ +2003-11-24 John W. Eaton <jwe@bevo.che.wisc.edu> + + * OPERATORS/op-fcn-handle.cc: New file. + * Makefile.in (OP_XSRC): Add it to the list. + + * ov-fcn-handle.h, ov-fcn-handle.cc (octave_fcn_handle): + Now an array of function handle objects. + + * ov.h, ov.cc (octave_value::octave_value (const fcn_handle_array&), + (octave_value::octave_value (octave_function *f, const std::string&)): + New constructors. + (octave_value::octave_value (const octave_fcn_handle&)): Delete. + + * ov-base-mat.cc (octave_base_matrix<T>::print_as_scalar): + Check dims instead of rows and columns. + + * ov.h (octave_value::is_function_handle): New function. + * ov-base.h (octave_base_value::is_function_handle): New function. + 2003-11-23 John W. Eaton <jwe@bevo.che.wisc.edu> * ov-bool-mat.h (octave_bool_matrix::array_value): Construct return
--- a/src/Makefile.in +++ b/src/Makefile.in @@ -103,7 +103,7 @@ op-fil-rec.cc op-fil-str.cc op-list.cc op-m-cm.cc \ op-m-cs.cc op-m-m.cc op-m-s.cc op-range.cc op-s-cm.cc \ op-s-cs.cc op-s-m.cc op-s-s.cc op-str-m.cc \ - op-str-s.cc op-str-str.cc op-streamoff.cc + op-str-s.cc op-str-str.cc op-streamoff.cc op-fcn-handle.cc OP_SRC := $(addprefix OPERATORS/, $(OP_XSRC))
new file mode 100644 --- /dev/null +++ b/src/OPERATORS/op-fcn-handle.cc @@ -0,0 +1,63 @@ +/* + +Copyright (C) 2003 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "gripes.h" +#include "ov.h" +#include "ov-fcn-handle.h" +#include "ov-typeinfo.h" +#include "ops.h" + +// fcn_handle unary ops. + +DEFUNOP (transpose, fcn_handle) +{ + CAST_UNOP_ARG (const octave_fcn_handle&); + + return octave_value (fcn_handle_array (v.fcn_handle_array_value().transpose ())); +} + +DEFASSIGNOP (assign, fcn_handle, fcn_handle) +{ + CAST_BINOP_ARGS (octave_fcn_handle&, const octave_fcn_handle&); + + v1.assign (idx, v2.fcn_handle_array_value ()); + return octave_value (); +} + +void +install_fcn_handle_ops (void) +{ + INSTALL_UNOP (op_transpose, octave_fcn_handle, transpose); + INSTALL_UNOP (op_hermitian, octave_fcn_handle, transpose); + + INSTALL_ASSIGNOP (op_asn_eq, octave_fcn_handle, octave_fcn_handle, assign); +} + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/
--- a/src/ov-base-mat.cc +++ b/src/ov-base-mat.cc @@ -205,10 +205,9 @@ bool octave_base_matrix<MT>::print_as_scalar (void) const { - int nr = rows (); - int nc = columns (); + dim_vector dv = dims (); - return (nr == 1 && nc == 1 || (nr == 0 || nc == 0)); + return (dv.all_ones () || dv.any_zero ()); } template <class MT>
--- a/src/ov-base.h +++ b/src/ov-base.h @@ -165,6 +165,8 @@ bool is_constant (void) const { return false; } + bool is_function_handle (void) const { return false; } + bool is_function (void) const { return false; } bool is_builtin_function (void) const { return false; } @@ -230,9 +232,9 @@ streamoff_array streamoff_array_value (void) const; - octave_function *function_value (bool silent); + octave_function *function_value (bool silent = false); - octave_fcn_handle *fcn_handle_value (bool silent); + octave_fcn_handle *fcn_handle_value (bool silent = false); octave_value_list list_value (void) const;
--- a/src/ov-builtin.h +++ b/src/ov-builtin.h @@ -63,7 +63,7 @@ const std::list<octave_value_list>& idx, int nargout); - octave_function *function_value (bool) { return this; } + octave_function *function_value (bool = false) { return this; } bool is_builtin_function (void) const { return true; }
--- a/src/ov-fcn-handle.cc +++ b/src/ov-fcn-handle.cc @@ -31,18 +31,117 @@ #include <iostream> #include "defun.h" +#include "error.h" +#include "gripes.h" #include "oct-map.h" #include "ov-base.h" +#include "ov-base-mat.h" +#include "ov-base-mat.cc" #include "ov-fcn-handle.h" #include "pr-output.h" #include "variables.h" +// Instantiate Arrays of fcn_handle_elt values. + +#include "Array.h" +#include "Array.cc" + +INSTANTIATE_ARRAY_AND_ASSIGN (fcn_handle_elt); + +#include "Array2.h" + +template class Array2<fcn_handle_elt>; + +#include "ArrayN.h" +#include "ArrayN.cc" + +template class ArrayN<fcn_handle_elt>; + +template class octave_base_matrix<fcn_handle_array>; + +boolNDArray +fcn_handle_array::all (int) const +{ + error ("all: invalid call for function handle object"); + return boolNDArray (); +} + +boolNDArray +fcn_handle_array::any (int) const +{ + error ("any: invalid call for function handle object"); + return boolNDArray (); +} + DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, "function handle", "function handle"); +octave_function * +octave_fcn_handle::function_value (bool) +{ + octave_function *retval = 0; + + if (numel () > 0) + { + // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? + if (Vwarn_fortran_indexing) + gripe_implicit_conversion ("function handle array", + "scalar function handle"); + + fcn_handle_elt elt = matrix(0); + + retval = elt.function_value (); + } + else + gripe_invalid_conversion ("function handle array", + "scalar function handle"); + + return retval; +} + +std::string +octave_fcn_handle::name (void) const +{ + std::string retval; + + if (numel () > 0) + { + // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? + if (Vwarn_fortran_indexing) + gripe_implicit_conversion ("function handle array", + "scalar function handle"); + + fcn_handle_elt elt = matrix(0); + + retval = elt.name (); + } + else + gripe_invalid_conversion ("function handle array", + "scalar function handle"); + + return retval; +} + +ArrayN<std::string> +fcn_handle_array::names (void) const +{ + ArrayN<std::string> retval (dims ()); + + int nel = length (); + + for (int i = 0; i < nel; i++) + { + fcn_handle_elt elt = elem (i); + + retval(i) = elt.name (); + } + + return retval; +} + void octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const { @@ -53,8 +152,14 @@ void octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const { + dim_vector dv = matrix.dims (); + os << "<" << dv.str () << " function handle object>"; + +#if 0 indent (os); - os << name (); + octave_print_internal (os, name_array (), pr_as_read_syntax, + current_print_indent_level (), true); +#endif } octave_value @@ -65,11 +170,7 @@ octave_function *f = lookup_function (nm); if (f) - { - octave_fcn_handle fh (f, nm); - - retval = octave_value (fh); - } + return fcn_handle_array (f, nm); else error ("error creating function handle \"@%s\"", nm.c_str ());
--- a/src/ov-fcn-handle.h +++ b/src/ov-fcn-handle.h @@ -33,48 +33,39 @@ #include "oct-alloc.h" #include "ov-base.h" +#include "ov-base-mat.h" #include "ov-fcn.h" +#include "ov-typeinfo.h" #include "symtab.h" // Function handles. -class -octave_fcn_handle : public octave_base_value +class fcn_handle_elt { public: - octave_fcn_handle (void) : fcn (0), nm ("[]") { } + fcn_handle_elt (void) : fcn (0), nm ("@[]") { } - octave_fcn_handle (octave_function *f, const std::string& n) + fcn_handle_elt (octave_function *f, const std::string& n) : fcn (f), nm (std::string ("@") + n) { } - octave_fcn_handle (const octave_fcn_handle& fh) - : fcn (fh.fcn), nm (fh.nm) { } + fcn_handle_elt (const fcn_handle_elt& fhe) + : fcn (fhe.fcn), nm (fhe.nm) { } - octave_fcn_handle& operator = (const octave_fcn_handle& fh) + fcn_handle_elt& operator = (const fcn_handle_elt& fhe) { - if (this != &fh) + if (this != &fhe) { - fcn = fh.fcn; - nm = fh.nm; + fcn = fhe.fcn; + nm = fhe.nm; } return *this; } - ~octave_fcn_handle (void) { } - - bool is_defined (void) const { return fcn; } - - octave_function *function_value (bool) { return fcn; } + ~fcn_handle_elt (void) { } - octave_fcn_handle *fcn_handle_value (bool) { return this; } - - bool print_as_scalar (void) const { return true; } - - void print (std::ostream& os, bool pr_as_read_syntax = false) const; - - void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; + octave_function *function_value (void) { return fcn; } std::string name (void) const { return nm; } @@ -85,6 +76,99 @@ // The name of the handle, including the "@". std::string nm; +}; + +class fcn_handle_array : public ArrayN<fcn_handle_elt> +{ +public: + + fcn_handle_array (void) : ArrayN<fcn_handle_elt> () { } + + fcn_handle_array (const dim_vector& dv, + const fcn_handle_elt& val = resize_fill_value ()) + : ArrayN<fcn_handle_elt> (dv, val) { } + + fcn_handle_array (octave_function *f, const std::string& nm) + : ArrayN<fcn_handle_elt> (dim_vector (1, 1), fcn_handle_elt (f, nm)) { } + + fcn_handle_array (const ArrayN<fcn_handle_elt>& fa) + : ArrayN<fcn_handle_elt> (fa) { } + + fcn_handle_array (const fcn_handle_array& fa) + : ArrayN<fcn_handle_elt> (fa) { } + + ~fcn_handle_array (void) { } + + fcn_handle_array& operator = (const fcn_handle_array& fa) + { + if (this != &fa) + ArrayN<fcn_handle_elt>::operator = (fa); + + return *this; + } + + fcn_handle_array squeeze (void) const + { return ArrayN<fcn_handle_elt>::squeeze (); } + + boolNDArray all (int dim = -1) const; + boolNDArray any (int dim = -1) const; + + ArrayN<std::string> names (void) const; + + static int compute_index (Array<int>& ra_idx, + const dim_vector& dimensions); + + static fcn_handle_elt resize_fill_value (void) + { + static fcn_handle_elt nil_handle = fcn_handle_elt (); + return nil_handle; + } +}; + +class +octave_fcn_handle : public octave_base_matrix<fcn_handle_array> +{ +public: + + octave_fcn_handle (void) + : octave_base_matrix<fcn_handle_array> () { } + + octave_fcn_handle (octave_function *f, const std::string& n) + : octave_base_matrix<fcn_handle_array> + (fcn_handle_array (dim_vector (1, 1), fcn_handle_elt (f, n))) { } + + octave_fcn_handle (const fcn_handle_array& fha) + : octave_base_matrix<fcn_handle_array> (fha) { } + + octave_fcn_handle (const octave_fcn_handle& fh) + : octave_base_matrix<fcn_handle_array> (fh) { } + + ~octave_fcn_handle (void) { } + + octave_value *clone (void) const { return new octave_fcn_handle (*this); } + octave_value *empty_clone (void) const { return new octave_fcn_handle (); } + + bool is_defined (void) const { return true; } + + bool is_function_handle (void) const { return true; } + + octave_function *function_value (bool = false); + + std::string name (void) const; + + octave_fcn_handle *fcn_handle_value (bool = false) { return this; } + + fcn_handle_array fcn_handle_array_value (void) const { return matrix; } + + ArrayN<std::string> name_array (void) const { return matrix.names (); } + + bool print_as_scalar (void) const { return true; } + + void print (std::ostream& os, bool pr_as_read_syntax = false) const; + + void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; + +private: DECLARE_OCTAVE_ALLOCATOR
--- a/src/ov-mapper.h +++ b/src/ov-mapper.h @@ -66,7 +66,7 @@ ~octave_mapper (void) { } - octave_function *function_value (bool) { return this; } + octave_function *function_value (bool = false) { return this; } octave_value subsref (const std::string& type, const std::list<octave_value_list>& idx)
--- a/src/ov-usr-fcn.h +++ b/src/ov-usr-fcn.h @@ -61,7 +61,7 @@ ~octave_user_function (void); - octave_function *function_value (bool) { return this; } + octave_function *function_value (bool = false) { return this; } octave_user_function *define_param_list (tree_parameter_list *t);
--- a/src/ov.cc +++ b/src/ov.cc @@ -606,8 +606,14 @@ rep->count = 1; } -octave_value::octave_value (const octave_fcn_handle& fh) - : rep (new octave_fcn_handle (fh)) +octave_value::octave_value (const fcn_handle_array& fha) + : rep (new octave_fcn_handle (fha)) +{ + rep->count = 1; +} + +octave_value::octave_value (octave_function *f, const std::string& nm) + : rep (new octave_fcn_handle (f, nm)) { rep->count = 1; }
--- a/src/ov.h +++ b/src/ov.h @@ -42,6 +42,7 @@ #include "str-vec.h" class Cell; +class fcn_handle_array; class streamoff_array; class Octave_map; class octave_stream; @@ -213,7 +214,8 @@ octave_value (const octave_stream& s, int n); octave_value (const streamoff_array& off); octave_value (octave_function *f); - octave_value (const octave_fcn_handle& fh); + octave_value (const fcn_handle_array& fha); + octave_value (octave_function *f, const std::string &nm); // function handle octave_value (const octave_value_list& m, bool is_cs_list = false); octave_value (octave_value::magic_colon); octave_value (octave_value::all_va_args); @@ -452,6 +454,9 @@ virtual bool is_constant (void) const { return rep->is_constant (); } + virtual bool is_function_handle (void) const + { return rep->is_function_handle (); } + virtual bool is_function (void) const { return rep->is_function (); }