# HG changeset patch # User David Bateman # Date 1222450169 14400 # Node ID 265a821f65557a6925bb84df943473159e7e82e4 # Parent ec0a13863eb7aa777d20ec8d6f73af2eb6099706 Add subsindex and ismethod functions diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,9 @@ +2008-09-26 David Bateman + + * general/subsindex.m: Dummy subsindex function for help string + and to throw error for use outside of a class + * general/Makefile.in (SOURCES): Include it here. + 2008-09-26 John W. Eaton * image/imfinfo.m: Delete temporary file. diff --git a/scripts/general/Makefile.in b/scripts/general/Makefile.in --- a/scripts/general/Makefile.in +++ b/scripts/general/Makefile.in @@ -45,7 +45,8 @@ nargoutchk.m nextpow2.m nthroot.m num2str.m perror.m pol2cart.m \ polyarea.m postpad.m prepad.m quadgk.m quadl.m quadv.m randperm.m rat.m \ rem.m repmat.m rot90.m rotdim.m runlength.m shift.m shiftdim.m sortrows.m \ - sph2cart.m strerror.m structfun.m sub2ind.m triplequad.m trapz.m tril.m triu.m + sph2cart.m strerror.m structfun.m sub2ind.m subsindex.m triplequad.m \ + trapz.m tril.m triu.m DISTFILES = $(addprefix $(srcdir)/, Makefile.in $(SOURCES)) diff --git a/scripts/general/subsindex.m b/scripts/general/subsindex.m new file mode 100644 --- /dev/null +++ b/scripts/general/subsindex.m @@ -0,0 +1,64 @@ +## Copyright (C) 2008 David Bateman +## +## 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 3 of the License, 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, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{idx} =} subsindex (@var{a}) +## Convert an object to an index vector. When @var{a} is a class object +## defined with a class constructor, then @code{subsindex} is the +## overloading method that allows the conversion of this class object to +## a valid indexing vector. It is important to note that +## @code{subsindex} must return a zero-based real integer vector of the +## class "double". For example, if the class constructor +## +## @example +## @group +## function b = myclass (a) +## b = myclass (struct ("a", a), "myclass"); +## endfunction +## @end group +## @end example +## +## @noindent +## then the @code{subsindex} function +## +## @example +## @group +## function idx = subsindex (a) +## idx = double (a.a) - 1.0; +## endfunction +## @end group +## @end example +## +## @noindent +## can then be used as follows +## +## @example +## @group +## a = myclass (1:4); +## b = 1:10; +## b(a) +## @result{} 1 2 3 4 +## @end group +## @end example +## +## @seealso{class, subsref, subsasgn} +## @end deftypefn + +function idx = subsindex (a) + error ("subsindex: not defined for class \"%s\"", class(a)); +endfunction diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2008-09-26 David Bateman + + * ov-class.h (idx_vector index_vector (void) const): Declare new + maethod. + * ov-class.cc (idx_vector index_vector (void) const): Define new + method. + * (Fismethod): New function. + 2008-09-26 John W. Eaton * DLD-FUNCTIONS/urlwrite.cc (urlwrite_cleanup_file) New function. diff --git a/src/ov-class.cc b/src/ov-class.cc --- a/src/ov-class.cc +++ b/src/ov-class.cc @@ -561,6 +561,40 @@ return retval; } +idx_vector +octave_class::index_vector (void) const +{ + idx_vector retval; + + octave_value meth = symbol_table::find_method ("subsindex", class_name ()); + + if (meth.is_defined ()) + { + octave_value_list args; + args(0) = octave_value (new octave_class (map, c_name)); + + octave_value_list tmp = feval (meth.function_value (), args, 1); + + if (!error_state && tmp.length () >= 1) + { + if (tmp(0).is_object()) + error ("subsindex function must return a valid index vector"); + else + // Index vector returned by subsindex is zero based + // (why this inconsistency Mathworks?), and so we must + // add one to the value returned as the index_vector method + // expects it to be one based. + retval = do_binary_op (octave_value::op_add, tmp (0), + octave_value (1.0)).index_vector (); + } + } + else + error ("no subsindex method defined for class %s", + class_name().c_str ()); + + return retval; +} + size_t octave_class::byte_size (void) const { @@ -1025,6 +1059,47 @@ return retval; } +DEFUN (ismethod, args, , + "-*- texinfo -*-\n\ +@deftypefn {Built-in Function} {} ismethod (@var{x}, @var{method})\n\ +Return true if @var{x} is a class object and the string @var{method}\n\ +is a method of this class.\n\ +@end deftypefn") +{ + octave_value retval; + + if (args.length () == 2) + { + octave_value arg = args(0); + + std::string class_name; + + if (arg.is_object ()) + class_name = arg.class_name (); + else if (arg.is_string ()) + class_name = arg.string_value (); + else + error ("ismethod: expecting object or class name as first argument"); + + if (! error_state) + { + std::string method = args(1).string_value (); + + if (! error_state) + { + if (load_path::find_method (class_name, method) != std::string ()) + retval = true; + else + retval = false; + } + } + } + else + print_usage (); + + return retval; +} + DEFCMD (methods, args, nargout, "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} methods (@var{x})\n\ diff --git a/src/ov-class.h b/src/ov-class.h --- a/src/ov-class.h +++ b/src/ov-class.h @@ -82,6 +82,8 @@ const std::list& idx, const octave_value& rhs); + idx_vector index_vector (void) const; + dim_vector dims (void) const { return map.dims (); } size_t byte_size (void) const;