Mercurial > hg > octave-lojdl
changeset 8213:d5e08881bba8
Add overloading of the colon operator
author | David Bateman <dbateman@free.fr> |
---|---|
date | Sun, 12 Oct 2008 15:28:00 +0100 |
parents | ebf6f6a0f9a7 |
children | 263261b2124f |
files | scripts/ChangeLog scripts/general/Makefile.in scripts/general/colon.m src/ChangeLog src/pt-colon.cc |
diffstat | 5 files changed, 123 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2008-10-12 David Bateman <dbateman@free.fr> + + * general/colon..m: New function. + * general/Makefile.in (SOURCES): Add it here. + 2008-10-10 David Bateman <dbateman@free.fr> * image/__img__.m: Manually set the limits of th eimage
--- a/scripts/general/Makefile.in +++ b/scripts/general/Makefile.in @@ -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 \
new file mode 100644 --- /dev/null +++ b/scripts/general/colon.m @@ -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 +## <http://www.gnu.org/licenses/>. + +## -*- 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
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2008-10-12 David Bateman <dbateman@free.fr> + + * 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 <jwe@octave.org> * graphics.h.in (base_properties::adopt): Place new child at front
--- a/src/pt-colon.cc +++ b/src/pt-colon.cc @@ -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;