# HG changeset patch # User David Bateman # Date 1223821680 -3600 # Node ID d5e08881bba88fa781db014c8fc8a30710e5ae90 # Parent ebf6f6a0f9a7c69268a0950c9d8070bcd3c848c7 Add overloading of the colon operator diff -r ebf6f6a0f9a7 -r d5e08881bba8 scripts/ChangeLog --- a/scripts/ChangeLog Thu Oct 09 20:09:13 2008 +0100 +++ b/scripts/ChangeLog Sun Oct 12 15:28:00 2008 +0100 @@ -1,3 +1,8 @@ +2008-10-12 David Bateman + + * general/colon..m: New function. + * general/Makefile.in (SOURCES): Add it here. + 2008-10-10 David Bateman * image/__img__.m: Manually set the limits of th eimage diff -r ebf6f6a0f9a7 -r d5e08881bba8 scripts/general/Makefile.in --- a/scripts/general/Makefile.in Thu Oct 09 20:09:13 2008 +0100 +++ b/scripts/general/Makefile.in Sun Oct 12 15:28:00 2008 +0100 @@ -35,7 +35,7 @@ SOURCES = __isequal__.m __splinen__.m accumarray.m arrayfun.m \ bicubic.m bitcmp.m bitget.m bitset.m blkdiag.m cart2pol.m \ - cart2sph.m cellidx.m cell2mat.m celldisp.m circshift.m common_size.m \ + cart2sph.m cellidx.m cell2mat.m celldisp.m circshift.m colon.m common_size.m \ cplxpair.m cumtrapz.m dblquad.m deal.m del2.m diff.m display.m flipdim.m \ fliplr.m flipud.m genvarname.m gradient.m idivide.m ind2sub.m int2str.m \ interp1.m interp1q.m interp2.m interp3.m interpn.m interpft.m \ diff -r ebf6f6a0f9a7 -r d5e08881bba8 scripts/general/colon.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/general/colon.m Sun Oct 12 15:28:00 2008 +0100 @@ -0,0 +1,37 @@ +## 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{r} =} colon (@var{a}, @var{b}) +## @deftypefnx {Function File} {@var{r} =} colon (@var{a}, @var{b}, @var{c}) +## Method of a class to construct a range with the @code{:} operator. For +## example. +## +## @example +## @group +## a = myclass (@dots{}) +## b = myclass (@dots{}) +## c = a : b +## @end example +## +## @seealso{class, subsref, subsasgn} +## @end deftypefn + +function r = colon (varargin) + error ("colon: not defined for class \"%s\"", class(varargin{1})); +endfunction diff -r ebf6f6a0f9a7 -r d5e08881bba8 src/ChangeLog --- a/src/ChangeLog Thu Oct 09 20:09:13 2008 +0100 +++ b/src/ChangeLog Sun Oct 12 15:28:00 2008 +0100 @@ -1,3 +1,10 @@ +2008-10-12 David Bateman + + * pt-colon.cc (octave_value tree_colon_expression::make_range + (const octave_value&, const octave_value&, const octave_value&)): + Treating class overloading of colon operator. + (octave_value tree_colon_expression::rvalue (void)): Ditto. + 2008-10-10 John W. Eaton * graphics.h.in (base_properties::adopt): Place new child at front diff -r ebf6f6a0f9a7 -r d5e08881bba8 src/pt-colon.cc --- a/src/pt-colon.cc Thu Oct 09 20:09:13 2008 +0100 +++ b/src/pt-colon.cc Sun Oct 12 15:28:00 2008 +0100 @@ -113,28 +113,51 @@ { octave_value retval; - bool result_is_str = (ov_base.is_string () && ov_limit.is_string ()); - bool dq_str = (ov_base.is_dq_string () || ov_limit.is_dq_string ()); + if (ov_base.is_object () || ov_limit.is_object () || + ov_increment.is_object ()) + { + octave_value_list tmp1; + tmp1(2) = ov_limit; + tmp1(1) = ov_increment; + tmp1(0) = ov_base; + + octave_value fcn = symbol_table::find_function ("colon", tmp1); - Matrix m_base = ov_base.matrix_value (true); - - if (error_state) - eval_error ("invalid base value in colon expression"); + if (fcn.is_defined ()) + { + octave_value_list tmp2 = fcn.do_multi_index_op (1, tmp1); + + if (! error_state) + retval = tmp2 (0); + } + else + ::error ("can not find overloaded colon function"); + } else { - Matrix m_limit = ov_limit.matrix_value (true); + bool result_is_str = (ov_base.is_string () && ov_limit.is_string ()); + bool dq_str = (ov_base.is_dq_string () || ov_limit.is_dq_string ()); + + Matrix m_base = ov_base.matrix_value (true); if (error_state) - eval_error ("invalid limit value in colon expression"); + eval_error ("invalid base value in colon expression"); else { - Matrix m_increment = ov_increment.matrix_value (true); + Matrix m_limit = ov_limit.matrix_value (true); if (error_state) - eval_error ("invalid increment value in colon expression"); + eval_error ("invalid limit value in colon expression"); else - retval = make_range (m_base, m_limit, m_increment, - result_is_str, dq_str); + { + Matrix m_increment = ov_increment.matrix_value (true); + + if (error_state) + eval_error ("invalid increment value in colon expression"); + else + retval = make_range (m_base, m_limit, m_increment, + result_is_str, dq_str); + } } } @@ -161,6 +184,44 @@ if (error_state || ov_limit.is_undefined ()) eval_error ("invalid limit value in colon expression"); + else if (ov_base.is_object () || ov_limit.is_object ()) + { + octave_value_list tmp1; + + if (op_increment) + { + octave_value ov_increment = op_increment->rvalue (); + + if (error_state || ov_increment.is_undefined ()) + eval_error ("invalid increment value in colon expression"); + else + { + tmp1(2) = ov_limit; + tmp1(1) = ov_increment; + tmp1(0) = ov_base; + } + } + else + { + tmp1(1) = ov_limit; + tmp1(0) = ov_base; + } + + if (!error_state) + { + octave_value fcn = symbol_table::find_function ("colon", tmp1); + + if (fcn.is_defined ()) + { + octave_value_list tmp2 = fcn.do_multi_index_op (1, tmp1); + + if (! error_state) + retval = tmp2 (0); + } + else + ::error ("can not find overloaded colon function"); + } + } else { octave_value ov_increment = 1.0;