changeset 28524:455fe4a6f22c

deprecate arithmetic operators for ranges; eliminate use in Octave * Range.h, Range.cc: Deprecate unary -, binary +, -, *, and / operators for Range objects. (Range::m_cache): Delete mutable data member. Remove all uses. (Range::clear_cache): Delete function and all uses. * op-range.cc: Eliminate special arithmetic operators for ranges. All computations will now be performed as arrays. Eliminate special concatenation functions.
author John W. Eaton <jwe@octave.org>
date Wed, 01 Jul 2020 15:33:53 -0400
parents 68000a4df5a8
children 1151ed869686
files libinterp/operators/op-range.cc liboctave/array/Range.cc liboctave/array/Range.h
diffstat 3 files changed, 43 insertions(+), 147 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/operators/op-range.cc	Wed Jul 01 15:27:22 2020 -0400
+++ b/libinterp/operators/op-range.cc	Wed Jul 01 15:33:53 2020 -0400
@@ -31,63 +31,13 @@
 #include "ovl.h"
 #include "ov.h"
 #include "ov-range.h"
-#include "ov-ch-mat.h"
-#include "ov-scalar.h"
 #include "ov-re-mat.h"
-#include "ov-flt-re-mat.h"
-#include "ov-complex.h"
-#include "ov-cx-mat.h"
-#include "ov-bool.h"
-#include "ov-bool-mat.h"
 #include "ov-typeinfo.h"
 #include "ov-null-mat.h"
 #include "ops.h"
-#include "xpow.h"
 
-// range unary ops.
-
-DEFUNOP (not, range)
-{
-  const octave_range& v = dynamic_cast<const octave_range&> (a);
-
-  return octave_value (! v.matrix_value ());
-}
-
+// Allow +RNG_VAL to avoid conversion to array.
 DEFUNOP_OP (uplus, range, /* no-op */)
-DEFUNOP_OP (uminus, range, -)
-
-DEFUNOP (transpose, range)
-{
-  const octave_range& v = dynamic_cast<const octave_range&> (a);
-
-  return octave_value (v.matrix_value ().transpose ());
-}
-
-DEFBINOP_OP (addrs, range, scalar, +)
-DEFBINOP_OP (addsr, scalar, range, +)
-DEFBINOP_OP (subrs, range, scalar, -)
-DEFBINOP_OP (subsr, scalar, range, -)
-DEFBINOP_OP (mulrs, range, scalar, *)
-DEFBINOP_OP (mulsr, scalar, range, *)
-
-DEFBINOP_FN (el_powsr, scalar, range, elem_xpow)
-DEFBINOP_FN (el_powcsr, complex, range, elem_xpow)
-
-DEFNDCATOP_FN (r_r, range, range, array, array, concat)
-DEFNDCATOP_FN (r_s, range, scalar, array, array, concat)
-DEFNDCATOP_FN (r_m, range, matrix, array, array, concat)
-DEFNDCATOP_FN (r_cs, range, complex, array, complex_array, concat)
-DEFNDCATOP_FN (r_cm, range, complex_matrix, array, complex_array, concat)
-DEFNDCATOP_FN (r_b, range, bool, array, array, concat)
-DEFNDCATOP_FN (r_bm, range, bool_matrix, array, array, concat)
-DEFNDCATOP_FN (r_chm, range, char_matrix, array, char_array, concat)
-DEFNDCATOP_FN (s_r, scalar, range, array, array, concat)
-DEFNDCATOP_FN (m_r, matrix, range, array, array, concat)
-DEFNDCATOP_FN (cs_r, complex, range, complex_array, array, concat)
-DEFNDCATOP_FN (cm_r, complex_matrix, range, complex_array, array, concat)
-DEFNDCATOP_FN (b_r, bool, range, array, array, concat)
-DEFNDCATOP_FN (bm_r, bool_matrix, range, array, array, concat)
-DEFNDCATOP_FN (chm_r, char_matrix, range, char_array, array, concat)
 
 CONVDECL (range_to_matrix)
 {
@@ -99,39 +49,7 @@
 void
 install_range_ops (octave::type_info& ti)
 {
-  INSTALL_UNOP_TI (ti, op_not, octave_range, not);
   INSTALL_UNOP_TI (ti, op_uplus, octave_range, uplus);
-  INSTALL_UNOP_TI (ti, op_uminus, octave_range, uminus);
-  INSTALL_UNOP_TI (ti, op_transpose, octave_range, transpose);
-  INSTALL_UNOP_TI (ti, op_hermitian, octave_range, transpose);
-
-  INSTALL_BINOP_TI (ti, op_add, octave_range, octave_scalar, addrs);
-  INSTALL_BINOP_TI (ti, op_add, octave_scalar, octave_range, addsr);
-  INSTALL_BINOP_TI (ti, op_sub, octave_range, octave_scalar, subrs);
-  INSTALL_BINOP_TI (ti, op_sub, octave_scalar, octave_range, subsr);
-  INSTALL_BINOP_TI (ti, op_mul, octave_range, octave_scalar, mulrs);
-  INSTALL_BINOP_TI (ti, op_mul, octave_scalar, octave_range, mulsr);
-
-  INSTALL_BINOP_TI (ti, op_el_mul, octave_range, octave_scalar, mulrs);
-  INSTALL_BINOP_TI (ti, op_el_mul, octave_scalar, octave_range, mulsr);
-  INSTALL_BINOP_TI (ti, op_el_pow, octave_scalar, octave_range, el_powsr);
-  INSTALL_BINOP_TI (ti, op_el_pow, octave_complex, octave_range, el_powcsr);
-
-  INSTALL_CATOP_TI (ti, octave_range, octave_range, r_r);
-  INSTALL_CATOP_TI (ti, octave_range, octave_scalar, r_s);
-  INSTALL_CATOP_TI (ti, octave_range, octave_matrix, r_m);
-  INSTALL_CATOP_TI (ti, octave_range, octave_complex, r_cs);
-  INSTALL_CATOP_TI (ti, octave_range, octave_complex_matrix, r_cm);
-  INSTALL_CATOP_TI (ti, octave_range, octave_bool, r_b);
-  INSTALL_CATOP_TI (ti, octave_range, octave_bool_matrix, r_bm);
-  INSTALL_CATOP_TI (ti, octave_range, octave_char_matrix, r_chm);
-  INSTALL_CATOP_TI (ti, octave_scalar, octave_range, s_r);
-  INSTALL_CATOP_TI (ti, octave_matrix, octave_range, m_r);
-  INSTALL_CATOP_TI (ti, octave_complex, octave_range, cs_r);
-  INSTALL_CATOP_TI (ti, octave_complex_matrix, octave_range, cm_r);
-  INSTALL_CATOP_TI (ti, octave_bool, octave_range, b_r);
-  INSTALL_CATOP_TI (ti, octave_bool_matrix, octave_range, bm_r);
-  INSTALL_CATOP_TI (ti, octave_char_matrix, octave_range, chm_r);
 
   // FIXME: this would be unnecessary if
   // octave_base_value::numeric_assign always tried converting lhs
@@ -141,7 +59,7 @@
   INSTALL_ASSIGNCONV_TI (ti, octave_range, octave_null_str, octave_matrix);
   INSTALL_ASSIGNCONV_TI (ti, octave_range, octave_null_sq_str, octave_matrix);
 
-  // However, this should probably be here just in case we need it.
+  // Hmm, this one also seems to be needed.
 
   INSTALL_WIDENOP_TI (ti, octave_range, octave_matrix, range_to_matrix);
 }
--- a/liboctave/array/Range.cc	Wed Jul 01 15:27:22 2020 -0400
+++ b/liboctave/array/Range.cc	Wed Jul 01 15:33:53 2020 -0400
@@ -89,23 +89,20 @@
 Matrix
 Range::matrix_value (void) const
 {
-  if (m_numel > 0 && m_cache.isempty ())
-    {
-      m_cache.resize (1, m_numel);
+  Matrix retval (1, m_numel);
 
-      // The first element must always be *exactly* the base.
-      // E.g, -0 would otherwise become +0 in the loop (-0 + 0*increment).
-      m_cache(0) = m_base;
+  // The first element must always be *exactly* the base.
+  // E.g, -0 would otherwise become +0 in the loop (-0 + 0*increment).
+  retval(0) = m_base;
 
-      double b = m_base;
-      double increment = m_inc;
-      for (octave_idx_type i = 1; i < m_numel - 1; i++)
-        m_cache.xelem (i) = b + i * increment;
+  double b = m_base;
+  double increment = m_inc;
+  for (octave_idx_type i = 1; i < m_numel - 1; i++)
+    retval.xelem (i) = b + i * increment;
 
-      m_cache.xelem (m_numel - 1) = m_limit;
-    }
+  retval.xelem (m_numel - 1) = m_limit;
 
-  return m_cache;
+  return retval;
 }
 
 double
@@ -259,7 +256,6 @@
     {
       std::swap (m_base, m_limit);
       m_inc = -m_inc;
-      clear_cache ();
     }
 }
 
@@ -279,7 +275,6 @@
     {
       std::swap (m_base, m_limit);
       m_inc = -m_inc;
-      clear_cache ();
       reverse = true;
     }
 
@@ -427,59 +422,32 @@
 
 Range operator + (double x, const Range& r)
 {
-  Range result (x + r.base (), x + r.limit (), r.inc (), r.numel ());
-  // Check whether new range was constructed properly.  A non-finite
-  // value (Inf or NaN) requires that the output be of the same size
-  // as the original range with all values set to the non-finite value.
-  if (result.m_numel < 0)
-    result.m_cache = x + r.matrix_value ();
-
-  return result;
+  return Range (x + r.base (), x + r.limit (), r.inc (), r.numel ());
 }
 
 Range operator + (const Range& r, double x)
 {
-  Range result (r.base () + x, r.limit () + x, r.inc (), r.numel ());
-  if (result.m_numel < 0)
-    result.m_cache = r.matrix_value () + x;
-
-  return result;
+  return Range (r.base () + x, r.limit () + x, r.inc (), r.numel ());
 }
 
 Range operator - (double x, const Range& r)
 {
-  Range result (x - r.base (), x - r.limit (), -r.inc (), r.numel ());
-  if (result.m_numel < 0)
-    result.m_cache = x - r.matrix_value ();
-
-  return result;
+  return Range (x - r.base (), x - r.limit (), -r.inc (), r.numel ());
 }
 
 Range operator - (const Range& r, double x)
 {
-  Range result (r.base () - x, r.limit () - x, r.inc (), r.numel ());
-  if (result.m_numel < 0)
-    result.m_cache = r.matrix_value () - x;
-
-  return result;
+  return Range (r.base () - x, r.limit () - x, r.inc (), r.numel ());
 }
 
 Range operator * (double x, const Range& r)
 {
-  Range result (x * r.base (), x * r.limit (), x * r.inc (), r.numel ());
-  if (result.m_numel < 0)
-    result.m_cache = x * r.matrix_value ();
-
-  return result;
+  return Range (x * r.base (), x * r.limit (), x * r.inc (), r.numel ());
 }
 
 Range operator * (const Range& r, double x)
 {
-  Range result (r.base () * x, r.limit () * x, r.inc () * x, r.numel ());
-  if (result.m_numel < 0)
-    result.m_cache = r.matrix_value () * x;
-
-  return result;
+  return Range (r.base () * x, r.limit () * x, r.inc () * x, r.numel ());
 }
 
 // C  See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5.
@@ -620,6 +588,4 @@
 {
   m_numel = numel_internal ();
   m_limit = limit_internal ();
-
-  clear_cache ();
 }
--- a/liboctave/array/Range.h	Wed Jul 01 15:27:22 2020 -0400
+++ b/liboctave/array/Range.h	Wed Jul 01 15:33:53 2020 -0400
@@ -41,7 +41,7 @@
 public:
 
   Range (void)
-    : m_base (0), m_limit (0), m_inc (0), m_numel (0), m_cache (1, 0)
+    : m_base (0), m_limit (0), m_inc (0), m_numel (0)
   { }
 
   Range (const Range& r) = default;
@@ -51,23 +51,24 @@
   ~Range (void) = default;
 
   Range (double b, double l)
-    : m_base (b), m_limit (l), m_inc (1), m_numel (numel_internal ()),
-      m_cache ()
+    : m_base (b), m_limit (l), m_inc (1), m_numel (numel_internal ())
   {
     m_limit = limit_internal ();
   }
 
   Range (double b, double l, double i)
-    : m_base (b), m_limit (l), m_inc (i), m_numel (numel_internal ()),
-      m_cache ()
+    : m_base (b), m_limit (l), m_inc (i), m_numel (numel_internal ())
   {
     m_limit = limit_internal ();
   }
 
+  // NOTE: The following constructor may be deprecated and removed after
+  // the arithmetic operators are removed.
+
   // For operators' usage (to preserve element count).
+
   Range (double b, double i, octave_idx_type n)
-    : m_base (b), m_limit (b + (n-1) * i), m_inc (i),
-      m_numel (n), m_cache ()
+    : m_base (b), m_limit (b + (n-1) * i), m_inc (i), m_numel (n)
   {
     if (! octave::math::isfinite (b) || ! octave::math::isfinite (i)
         || ! octave::math::isfinite (m_limit))
@@ -157,22 +158,20 @@
 
   octave_idx_type m_numel;
 
-  mutable Matrix m_cache;
-
   octave_idx_type numel_internal (void) const;
 
   double limit_internal (void) const;
 
   void init (void);
 
-  void clear_cache (void) const { m_cache.resize (0, 0); }
+protected:
 
-protected:
+  // NOTE: The following constructor may be removed when the arithmetic
+  // operators are removed.
 
   // For operators' usage (to allow all values to be set directly).
   Range (double b, double l, double i, octave_idx_type n)
-    : m_base (b), m_limit (l), m_inc (i),
-      m_numel (n), m_cache ()
+    : m_base (b), m_limit (l), m_inc (i), m_numel (n)
   {
     if (! octave::math::isfinite (b) || ! octave::math::isfinite (i)
         || ! octave::math::isfinite (l))
@@ -180,12 +179,25 @@
   }
 };
 
+OCTAVE_DEPRECATED (7, "arithmetic operations on Range objects are unreliable")
 extern OCTAVE_API Range operator - (const Range& r);
+
+OCTAVE_DEPRECATED (7, "arithmetic operations on Range objects are unreliable")
 extern OCTAVE_API Range operator + (double x, const Range& r);
+
+OCTAVE_DEPRECATED (7, "arithmetic operations on Range objects are unreliable")
 extern OCTAVE_API Range operator + (const Range& r, double x);
+
+OCTAVE_DEPRECATED (7, "arithmetic operations on Range objects are unreliable")
 extern OCTAVE_API Range operator - (double x, const Range& r);
+
+OCTAVE_DEPRECATED (7, "arithmetic operations on Range objects are unreliable")
 extern OCTAVE_API Range operator - (const Range& r, double x);
+
+OCTAVE_DEPRECATED (7, "arithmetic operations on Range objects are unreliable")
 extern OCTAVE_API Range operator * (double x, const Range& r);
+
+OCTAVE_DEPRECATED (7, "arithmetic operations on Range objects are unreliable")
 extern OCTAVE_API Range operator * (const Range& r, double x);
 
 #endif