octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Changeset]: Overloading of the colon operator


From: David Bateman
Subject: [Changeset]: Overloading of the colon operator
Date: Sun, 12 Oct 2008 20:23:35 +0100
User-agent: Mozilla-Thunderbird 2.0.0.16 (X11/20080724)

The attached changeset adds overloading of the colon operator. I believe this is the last overloaded operator that the class code was missing.

D.

--
David Bateman                                address@hidden
35 rue Gambetta                              +33 1 46 04 02 18 (Home)
92100 Boulogne-Billancourt FRANCE            +33 6 72 01 06 33 (Mob)

# HG changeset patch
# User David Bateman <address@hidden>
# Date 1223821680 -3600
# Node ID 36c7668e21a366d540950a758d3d2119b84b3edf
# Parent  4d863a66a2b99f19d26875e5dc9ff56a8b63cb1d
Add overloading of the colon operator

diff --git a/scripts/ChangeLog b/scripts/ChangeLog
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,8 @@
+2008-10-12  David Bateman  <address@hidden>
+
+       * general/colon..m: New function.
+       * general/Makefile.in (SOURCES): Add it here.
+
 2008-10-10  David Bateman  <address@hidden>
 
        * general/loadobj.m, general/saveobj.m, general/display: New functions
diff --git a/scripts/general/Makefile.in b/scripts/general/Makefile.in
--- 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 \
diff --git a/scripts/general/colon.m b/scripts/general/colon.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} address@hidden =} colon (@var{a}, @var{b})
+## @deftypefnx {Function File} address@hidden =} 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 --git a/src/ChangeLog b/src/ChangeLog
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,10 @@
+2008-10-12  David Bateman  <address@hidden>
+
+       * 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  <address@hidden>
 
        * graphics.h.in (base_properties::adopt): Place new child at front
diff --git a/src/pt-colon.cc b/src/pt-colon.cc
--- 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;
 
-  Matrix m_base = ov_base.matrix_value (true);
+      octave_value fcn = symbol_table::find_function ("colon", tmp1);
 
-  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;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]