# HG changeset patch # User Jaroslav Hajek # Date 1270545114 -7200 # Node ID 806cfe2c81a37bfb381a497177e34379ff92fb23 # Parent d47802f0e55712f0aeaa3e0f51d3349a81953925 allow non-integer ranges as indices conditionally diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -0,0 +1,9 @@ +2010-04-06 Jaroslav Hajek + + * ov-range.cc (allow_noninteger_ranges_as_indices): New internal + variable. + (octave_range::index_vector): Use it here to possibly allow + non-integer ranges. + * ov-range.h: Declare it. + * utils.cc (Fisindex): Temporarily disable it here. + diff --git a/src/octave.cc b/src/octave.cc --- a/src/octave.cc +++ b/src/octave.cc @@ -572,6 +572,7 @@ "%%-- %D %I:%M %p --%%"); bind_internal_variable ("page_screen_output", false); bind_internal_variable ("print_empty_dimensions", false); + bind_internal_variable ("allow_noninteger_ranges_as_indices", true); disable_warning ("Octave:abbreviated-property-match"); disable_warning ("Octave:fopen-file-in-path"); diff --git a/src/ov-range.cc b/src/ov-range.cc --- a/src/ov-range.cc +++ b/src/ov-range.cc @@ -30,6 +30,8 @@ #include "lo-ieee.h" #include "lo-utils.h" +#include "defun.h" +#include "variables.h" #include "gripes.h" #include "ops.h" #include "oct-obj.h" @@ -47,6 +49,8 @@ DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_range, "range", "double"); +bool Vallow_noninteger_ranges_as_indices = false; + static octave_base_value * default_numeric_conversion_function (const octave_base_value& a) { @@ -142,6 +146,20 @@ } } +idx_vector +octave_range::index_vector (void) const +{ + if (idx_cache) + return *idx_cache; + else if (! Vallow_noninteger_ranges_as_indices) + return set_idx_cache (idx_vector (range)); + else + { + warning ("Non-integer range used as index. Values are rounded to nearest integer."); + return octave_value (matrix_value ()).round ().index_vector (); + } +} + double octave_range::double_value (bool) const { @@ -568,3 +586,15 @@ return retval; } + +DEFUN (allow_noninteger_ranges_as_indices, args, nargout, + "-*- texinfo -*-\n\ address@hidden {Built-in Function} address@hidden =} allow_noninteger_ranges_as_indices ()\n\ address@hidden {Built-in Function} address@hidden =} allow_noninteger_ranges_as_indices (@var{new_val})\n\ +Query or set the internal variable that controls whether non-integer ranges are allowed as indices.\n\ +This might be useful for Matlab compatibility; however, it is still not entirely compatible\n\ +because Matlab treats the range expression differently in different contexts.\n\ address@hidden deftypefn") +{ + return SET_INTERNAL_VARIABLE (allow_noninteger_ranges_as_indices); +} diff --git a/src/ov-range.h b/src/ov-range.h --- a/src/ov-range.h +++ b/src/ov-range.h @@ -50,6 +50,8 @@ // Range values. +extern OCTINTERP_API bool Vallow_noninteger_ranges_as_indices; + class octave_range : public octave_base_value { @@ -106,8 +108,7 @@ octave_value do_index_op (const octave_value_list& idx, bool resize_ok = false); - idx_vector index_vector (void) const - { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (range)); } + idx_vector index_vector (void) const; dim_vector dims (void) const { diff --git a/src/utils.cc b/src/utils.cc --- a/src/utils.cc +++ b/src/utils.cc @@ -64,6 +64,7 @@ #include "unwind-prot.h" #include "utils.h" #include "variables.h" +#include "ov-range.h" // Return TRUE if S is a valid identifier. @@ -1324,6 +1325,8 @@ if (! error_state) { unwind_protect frame; + frame.protect_var (Vallow_noninteger_ranges_as_indices); + Vallow_noninteger_ranges_as_indices = false; frame.protect_var (error_state); frame.protect_var (discard_error_messages); discard_error_messages = true;