changeset 8154:265a821f6555

Add subsindex and ismethod functions
author David Bateman <dbateman@free.fr>
date Fri, 26 Sep 2008 13:29:29 -0400
parents ec0a13863eb7
children 344c9b6532a2
files scripts/ChangeLog scripts/general/Makefile.in scripts/general/subsindex.m src/ChangeLog src/ov-class.cc src/ov-class.h
diffstat 6 files changed, 157 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Fri Sep 26 13:23:02 2008 -0400
+++ b/scripts/ChangeLog	Fri Sep 26 13:29:29 2008 -0400
@@ -1,3 +1,9 @@
+2008-09-26  David Bateman  <dbateman@free.fr>
+
+	* 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  <jwe@octave.org>
 
 	* image/imfinfo.m: Delete temporary file.
--- a/scripts/general/Makefile.in	Fri Sep 26 13:23:02 2008 -0400
+++ b/scripts/general/Makefile.in	Fri Sep 26 13:29:29 2008 -0400
@@ -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))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/general/subsindex.m	Fri Sep 26 13:29:29 2008 -0400
@@ -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
+## <http://www.gnu.org/licenses/>.
+
+## -*- 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
--- a/src/ChangeLog	Fri Sep 26 13:23:02 2008 -0400
+++ b/src/ChangeLog	Fri Sep 26 13:29:29 2008 -0400
@@ -1,3 +1,11 @@
+2008-09-26  David Bateman  <dbateman@free.fr>
+
+	* 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  <jwe@octave.org>
 
 	* DLD-FUNCTIONS/urlwrite.cc (urlwrite_cleanup_file) New function.
--- a/src/ov-class.cc	Fri Sep 26 13:23:02 2008 -0400
+++ b/src/ov-class.cc	Fri Sep 26 13:29:29 2008 -0400
@@ -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\
--- a/src/ov-class.h	Fri Sep 26 13:23:02 2008 -0400
+++ b/src/ov-class.h	Fri Sep 26 13:29:29 2008 -0400
@@ -82,6 +82,8 @@
 			 const std::list<octave_value_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;