Mercurial > hg > octave-lyh
diff src/ov.cc @ 5759:ce79d238f142
[project @ 2006-04-13 13:04:32 by jwe]
author | jwe |
---|---|
date | Thu, 13 Apr 2006 13:04:33 +0000 |
parents | 65208d959bf1 |
children | ace8d8d26933 |
line wrap: on
line diff
--- a/src/ov.cc +++ b/src/ov.cc @@ -82,10 +82,6 @@ // make the grow_size large. DEFINE_OCTAVE_ALLOCATOR2(octave_value, 1024); -// If TRUE, turn off printing of results in functions (as if a -// semicolon has been appended to each statement). -static bool Vsilent_functions; - // If TRUE, print a warning for assignments like // // octave> A(1) = 3; A(2) = 5 @@ -96,17 +92,6 @@ // Should we warn about conversions from complex to real? int Vwarn_imag_to_real; -// Should we print a warning when converting `[97, 98, 99, "123"]' -// to a character string? -bool Vwarn_num_to_str; - -// If TRUE, print the name along with the value. -bool Vprint_answer_id_name; - -// If TRUE, print a warning when a matrix is resized by an indexed -// assignment with indices outside the current bounds. -bool Vwarn_resize_on_range_error; - // XXX FIXME XXX // Octave's value type. @@ -325,60 +310,45 @@ return retval; } -octave_value * -octave_value::nil_rep (void) const +octave_value::octave_value (void) + : rep (new octave_base_value ()) { - static octave_base_value *nr = new octave_base_value (); - return nr; -} - -octave_value::octave_value (void) - : rep (nil_rep ()) -{ - rep->count++; } octave_value::octave_value (short int i) : rep (new octave_scalar (i)) { - rep->count = 1; } octave_value::octave_value (unsigned short int i) : rep (new octave_scalar (i)) { - rep->count = 1; } octave_value::octave_value (int i) : rep (new octave_scalar (i)) { - rep->count = 1; } octave_value::octave_value (unsigned int i) : rep (new octave_scalar (i)) { - rep->count = 1; } octave_value::octave_value (long int i) : rep (new octave_scalar (i)) { - rep->count = 1; } octave_value::octave_value (unsigned long int i) : rep (new octave_scalar (i)) { - rep->count = 1; } #if defined (HAVE_LONG_LONG_INT) octave_value::octave_value (long long int i) : rep (new octave_scalar (i)) { - rep->count = 1; } #endif @@ -386,146 +356,125 @@ octave_value::octave_value (unsigned long long int i) : rep (new octave_scalar (i)) { - rep->count = 1; } #endif octave_value::octave_value (octave_time t) : rep (new octave_scalar (t)) { - rep->count = 1; } octave_value::octave_value (double d) : rep (new octave_scalar (d)) { - rep->count = 1; } octave_value::octave_value (const Cell& c, bool is_csl) : rep (is_csl - ? dynamic_cast<octave_value *> (new octave_cs_list (c)) - : dynamic_cast<octave_value *> (new octave_cell (c))) + ? dynamic_cast<octave_base_value *> (new octave_cs_list (c)) + : dynamic_cast<octave_base_value *> (new octave_cell (c))) { - rep->count = 1; } octave_value::octave_value (const ArrayN<octave_value>& a, bool is_csl) : rep (is_csl - ? dynamic_cast<octave_value *> (new octave_cs_list (Cell (a))) - : dynamic_cast<octave_value *> (new octave_cell (Cell (a)))) + ? dynamic_cast<octave_base_value *> (new octave_cs_list (Cell (a))) + : dynamic_cast<octave_base_value *> (new octave_cell (Cell (a)))) { - rep->count = 1; } octave_value::octave_value (const Matrix& m) : rep (new octave_matrix (m)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const NDArray& a) : rep (new octave_matrix (a)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const ArrayN<double>& a) : rep (new octave_matrix (a)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const DiagMatrix& d) : rep (new octave_matrix (d)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const RowVector& v) : rep (new octave_matrix (v)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const ColumnVector& v) : rep (new octave_matrix (v)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const Complex& C) : rep (new octave_complex (C)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const ComplexMatrix& m) : rep (new octave_complex_matrix (m)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const ComplexNDArray& a) : rep (new octave_complex_matrix (a)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const ArrayN<Complex>& a) : rep (new octave_complex_matrix (a)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const ComplexDiagMatrix& d) : rep (new octave_complex_matrix (d)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const ComplexRowVector& v) : rep (new octave_complex_matrix (v)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const ComplexColumnVector& v) : rep (new octave_complex_matrix (v)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (bool b) : rep (new octave_bool (b)) { - rep->count = 1; } octave_value::octave_value (const boolMatrix& bm) : rep (new octave_bool_matrix (bm)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const boolNDArray& bnda) : rep (new octave_bool_matrix (bnda)) { - rep->count = 1; maybe_mutate (); } @@ -534,7 +483,6 @@ ? new octave_char_matrix_dq_str (c) : new octave_char_matrix_sq_str (c)) { - rep->count = 1; maybe_mutate (); } @@ -543,7 +491,6 @@ ? new octave_char_matrix_dq_str (s) : new octave_char_matrix_sq_str (s)) { - rep->count = 1; maybe_mutate (); } @@ -552,7 +499,6 @@ ? new octave_char_matrix_dq_str (s) : new octave_char_matrix_sq_str (s)) { - rep->count = 1; maybe_mutate (); } @@ -561,7 +507,6 @@ ? new octave_char_matrix_dq_str (s) : new octave_char_matrix_sq_str (s)) { - rep->count = 1; maybe_mutate (); } @@ -572,7 +517,6 @@ : new octave_char_matrix_sq_str (chm)) : new octave_char_matrix (chm)) { - rep->count = 1; maybe_mutate (); } @@ -583,7 +527,6 @@ : new octave_char_matrix_sq_str (chm)) : new octave_char_matrix (chm)) { - rep->count = 1; maybe_mutate (); } @@ -594,213 +537,179 @@ : new octave_char_matrix_sq_str (chm)) : new octave_char_matrix (chm)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const SparseMatrix& m, const SparseType &t) : rep (new octave_sparse_matrix (m, t)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const SparseComplexMatrix& m, const SparseType &t) : rep (new octave_sparse_complex_matrix (m, t)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const SparseBoolMatrix& bm, const SparseType &t) : rep (new octave_sparse_bool_matrix (bm, t)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const octave_int8& i) : rep (new octave_int8_scalar (i)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const octave_uint8& i) : rep (new octave_uint8_scalar (i)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const octave_int16& i) : rep (new octave_int16_scalar (i)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const octave_uint16& i) : rep (new octave_uint16_scalar (i)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const octave_int32& i) : rep (new octave_int32_scalar (i)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const octave_uint32& i) : rep (new octave_uint32_scalar (i)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const octave_int64& i) : rep (new octave_int64_scalar (i)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const octave_uint64& i) : rep (new octave_uint64_scalar (i)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const int8NDArray& inda) : rep (new octave_int8_matrix (inda)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const uint8NDArray& inda) : rep (new octave_uint8_matrix (inda)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const int16NDArray& inda) : rep (new octave_int16_matrix (inda)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const uint16NDArray& inda) : rep (new octave_uint16_matrix (inda)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const int32NDArray& inda) : rep (new octave_int32_matrix (inda)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const uint32NDArray& inda) : rep (new octave_uint32_matrix (inda)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const int64NDArray& inda) : rep (new octave_int64_matrix (inda)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const uint64NDArray& inda) : rep (new octave_uint64_matrix (inda)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (double base, double limit, double inc) : rep (new octave_range (base, limit, inc)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const Range& r) : rep (new octave_range (r)) { - rep->count = 1; maybe_mutate (); } octave_value::octave_value (const Octave_map& m) : rep (new octave_struct (m)) { - rep->count = 1; } octave_value::octave_value (const streamoff_array& off) : rep (new octave_streamoff (off)) { - rep->count = 1; } octave_value::octave_value (const octave_value_list& l, bool is_csl) : rep (is_csl - ? dynamic_cast<octave_value *> (new octave_cs_list (l)) - : dynamic_cast<octave_value *> (new octave_list (l))) + ? dynamic_cast<octave_base_value *> (new octave_cs_list (l)) + : dynamic_cast<octave_base_value *> (new octave_list (l))) { - rep->count = 1; } octave_value::octave_value (octave_value::magic_colon) : rep (new octave_magic_colon ()) { - rep->count = 1; } octave_value::octave_value (octave_value::all_va_args) : rep (new octave_all_va_args ()) { - rep->count = 1; } -octave_value::octave_value (octave_value *new_rep, int cnt) +octave_value::octave_value (octave_base_value *new_rep) : rep (new_rep) { - rep->count = cnt; } octave_value::~octave_value (void) { #if defined (MDEBUG) - if (rep) - std::cerr << "~octave_value: rep: " << rep - << " rep->count: " << rep->count << std::endl; - else - std::cerr << "~octave_value: rep is 0!" << std::endl; + std::cerr << "~octave_value: rep: " << rep + << " rep->count: " << rep->count << std::endl; #endif - if (rep && --rep->count == 0) - { - delete rep; - rep = 0; - } + if (--rep->count == 0) + delete rep; } -octave_value * +octave_base_value * octave_value::clone (void) const { panic_impossible (); @@ -810,7 +719,7 @@ void octave_value::maybe_mutate (void) { - octave_value *tmp = rep->try_narrowing_conversion (); + octave_base_value *tmp = rep->try_narrowing_conversion (); if (tmp && tmp != rep) { @@ -818,7 +727,6 @@ delete rep; rep = tmp; - rep->count = 1; } } @@ -881,14 +789,6 @@ return rep->do_multi_index_op (nargout, idx); } -static void -gripe_no_conversion (const std::string& on, const std::string& tn1, - const std::string& tn2) -{ - error ("operator %s: no conversion for assignment of `%s' to indexed `%s'", - on.c_str (), tn2.c_str (), tn1.c_str ()); -} - #if 0 static void gripe_assign_failed (const std::string& on, const std::string& tn1, @@ -1000,22 +900,6 @@ } octave_idx_type -octave_value::rows (void) const -{ - dim_vector dv = dims (); - - return (dv.length () > 0) ? dv(0) : -1; -} - -octave_idx_type -octave_value::columns (void) const -{ - dim_vector dv = dims (); - - return (dv.length () > 1) ? dv(1) : -1; -} - -octave_idx_type octave_value::length (void) const { int retval = 0; @@ -1043,31 +927,6 @@ return retval; } -int -octave_value::ndims (void) const -{ - dim_vector dv = dims (); - - int n_dims = dv.length (); - - // Remove trailing singleton dimensions. - - for (int i = n_dims; i > 2; i--) - { - if (dv(i-1) == 1) - n_dims--; - else - break; - } - - // The result is always >= 2. - - if (n_dims < 2) - n_dims = 2; - - return n_dims; -} - Matrix octave_value::size (void) const { @@ -1447,51 +1306,6 @@ return retval; } -octave_value -octave_value::convert_to_str (bool pad, bool force, char type) const -{ - octave_value retval = convert_to_str_internal (pad, force, type); - - if (! force && is_numeric_type () && Vwarn_num_to_str) - gripe_implicit_conversion (type_name (), retval.type_name ()); - - return retval; -} - -void -octave_value::print_with_name (std::ostream& output_buf, - const std::string& name, - bool print_padding) const -{ - if (! (evaluating_function_body && Vsilent_functions)) - { - bool pad_after = false; - - if (Vprint_answer_id_name) - pad_after = print_name_tag (output_buf, name); - - print (output_buf); - - if (print_padding && pad_after) - newline (output_buf); - } -} - -static void -gripe_indexed_assignment (const std::string& tn1, const std::string& tn2) -{ - error ("assignment of `%s' to indexed `%s' not implemented", - tn2.c_str (), tn1.c_str ()); -} - -static void -gripe_assign_conversion_failed (const std::string& tn1, - const std::string& tn2) -{ - error ("type conversion for assignment of `%s' to indexed `%s' failed", - tn2.c_str (), tn1.c_str ()); -} - int octave_value::write (octave_stream& os, int block_size, oct_data_conv::data_type output_type, int skip, @@ -1500,117 +1314,6 @@ return rep->write (os, block_size, output_type, skip, flt_fmt); } -octave_value -octave_value::numeric_assign (const std::string& type, - const std::list<octave_value_list>& idx, - const octave_value& rhs) -{ - octave_value retval; - - int t_lhs = type_id (); - int t_rhs = rhs.type_id (); - - assign_op_fcn f - = octave_value_typeinfo::lookup_assign_op (op_asn_eq, t_lhs, t_rhs); - - bool done = false; - - if (f) - { - f (*this, idx.front (), rhs.get_rep ()); - - done = (! error_state); - } - - if (done) - retval = octave_value (this, count + 1); - else - { - int t_result - = octave_value_typeinfo::lookup_pref_assign_conv (t_lhs, t_rhs); - - if (t_result >= 0) - { - type_conv_fcn cf - = octave_value_typeinfo::lookup_widening_op (t_lhs, t_result); - - if (cf) - { - octave_value *tmp (cf (*this)); - - if (tmp) - { - retval = tmp->subsasgn (type, idx, rhs); - - done = (! error_state); - } - else - gripe_assign_conversion_failed (type_name (), - rhs.type_name ()); - } - else - gripe_indexed_assignment (type_name (), rhs.type_name ()); - } - - if (! (done || error_state)) - { - octave_value tmp_rhs; - type_conv_fcn cf_rhs = rhs.numeric_conversion_function (); - - if (cf_rhs) - { - octave_value *tmp = cf_rhs (rhs.get_rep ()); - - if (tmp) - tmp_rhs = octave_value (tmp); - else - { - gripe_assign_conversion_failed (type_name (), - rhs.type_name ()); - return octave_value (); - } - } - else - tmp_rhs = rhs; - - type_conv_fcn cf_this = numeric_conversion_function (); - - octave_value *tmp_lhs = this; - - if (cf_this) - { - octave_value *tmp = cf_this (*this); - - if (tmp) - tmp_lhs = tmp; - else - { - gripe_assign_conversion_failed (type_name (), - rhs.type_name ()); - return octave_value (); - } - } - - if (cf_this || cf_rhs) - { - retval = tmp_lhs->subsasgn (type, idx, tmp_rhs); - - done = (! error_state); - } - else - gripe_no_conversion (assign_op_as_string (op_asn_eq), - type_name (), rhs.type_name ()); - } - } - - // The assignment may have converted to a type that is wider than - // necessary. - - retval.maybe_mutate (); - - return retval; -} - static void gripe_binary_op (const std::string& on, const std::string& tn1, const std::string& tn2) @@ -1634,18 +1337,19 @@ int t1 = v1.type_id (); int t2 = v2.type_id (); - binary_op_fcn f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); + octave_value_typeinfo::binary_op_fcn f + = octave_value_typeinfo::lookup_binary_op (op, t1, t2); if (f) retval = f (*v1.rep, *v2.rep); else { octave_value tv1; - type_conv_fcn cf1 = v1.numeric_conversion_function (); + octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); if (cf1) { - octave_value *tmp = cf1 (*v1.rep); + octave_base_value *tmp = cf1 (*v1.rep); if (tmp) { @@ -1662,11 +1366,11 @@ tv1 = v1; octave_value tv2; - type_conv_fcn cf2 = v2.numeric_conversion_function (); + octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); if (cf2) { - octave_value *tmp = cf2 (*v2.rep); + octave_base_value *tmp = cf2 (*v2.rep); if (tmp) { @@ -1729,18 +1433,19 @@ int t1 = v1.type_id (); int t2 = v2.type_id (); - cat_op_fcn f = octave_value_typeinfo::lookup_cat_op (t1, t2); + octave_value_typeinfo::cat_op_fcn f + = octave_value_typeinfo::lookup_cat_op (t1, t2); if (f) retval = f (*v1.rep, *v2.rep, ra_idx); else { octave_value tv1; - type_conv_fcn cf1 = v1.numeric_conversion_function (); + octave_base_value::type_conv_fcn cf1 = v1.numeric_conversion_function (); if (cf1) { - octave_value *tmp = cf1 (*v1.rep); + octave_base_value *tmp = cf1 (*v1.rep); if (tmp) { @@ -1757,11 +1462,11 @@ tv1 = v1; octave_value tv2; - type_conv_fcn cf2 = v2.numeric_conversion_function (); + octave_base_value::type_conv_fcn cf2 = v2.numeric_conversion_function (); if (cf2) { - octave_value *tmp = cf2 (*v2.rep); + octave_base_value *tmp = cf2 (*v2.rep); if (tmp) { @@ -1823,18 +1528,19 @@ int t = v.type_id (); - unary_op_fcn f = octave_value_typeinfo::lookup_unary_op (op, t); + octave_value_typeinfo::unary_op_fcn f + = octave_value_typeinfo::lookup_unary_op (op, t); if (f) retval = f (*v.rep); else { octave_value tv; - type_conv_fcn cf = v.numeric_conversion_function (); + octave_base_value::type_conv_fcn cf = v.numeric_conversion_function (); if (cf) { - octave_value *tmp = cf (*v.rep); + octave_base_value *tmp = cf (*v.rep); if (tmp) { @@ -1875,7 +1581,7 @@ int t = type_id (); - non_const_unary_op_fcn f + octave_value_typeinfo::non_const_unary_op_fcn f = octave_value_typeinfo::lookup_non_const_unary_op (op, t); if (f) @@ -1886,17 +1592,16 @@ } else { - type_conv_fcn cf = numeric_conversion_function (); + octave_base_value::type_conv_fcn cf = numeric_conversion_function (); if (cf) { - octave_value *tmp = cf (*rep); + octave_base_value *tmp = cf (*rep); if (tmp) { - octave_value *old_rep = rep; + octave_base_value *old_rep = rep; rep = tmp; - rep->count = 1; t = type_id (); @@ -1976,52 +1681,6 @@ return retval; } -// Current indentation. -int octave_value::curr_print_indent_level = 0; - -// TRUE means we are at the beginning of a line. -bool octave_value::beginning_of_line = true; - -// Each print() function should call this before printing anything. -// -// This doesn't need to be fast, but isn't there a better way? - -void -octave_value::indent (std::ostream& os) const -{ - assert (curr_print_indent_level >= 0); - - if (beginning_of_line) - { - // XXX FIXME XXX -- do we need this? - // os << prefix; - - for (int i = 0; i < curr_print_indent_level; i++) - os << " "; - - beginning_of_line = false; - } -} - -// All print() functions should use this to print new lines. - -void -octave_value::newline (std::ostream& os) const -{ - os << "\n"; - - beginning_of_line = true; -} - -// For ressetting print state. - -void -octave_value::reset (void) const -{ - beginning_of_line = true; - curr_print_indent_level = 0; -} - octave_value::assign_op octave_value::unary_op_to_assign_op (unary_op op) { @@ -2257,73 +1916,9 @@ return 0; } -static int -warn_num_to_str (void) -{ - Vwarn_num_to_str = check_preference ("warn_num_to_str"); - - return 0; -} - -static int -print_answer_id_name (void) -{ - Vprint_answer_id_name = check_preference ("print_answer_id_name"); - - return 0; -} - -static int -warn_resize_on_range_error (void) -{ - Vwarn_resize_on_range_error - = check_preference ("warn_resize_on_range_error"); - - liboctave_wrore_flag = Vwarn_resize_on_range_error; - - return 0; -} - -static int -silent_functions (void) -{ - Vsilent_functions = check_preference ("silent_functions"); - - return 0; -} - void symbols_of_ov (void) { - DEFVAR (print_answer_id_name, true, print_answer_id_name, - "-*- texinfo -*-\n\ -@defvr {Built-in Variable} print_answer_id_name\n\ -If the value of @code{print_answer_id_name} is nonzero, variable\n\ -names are printed along with the result. Otherwise, only the result\n\ -values are printed. The default value is 1.\n\ -@end defvr"); - - DEFVAR (silent_functions, false, silent_functions, - "-*- texinfo -*-\n\ -@defvr {Built-in Variable} silent_functions\n\ -If the value of @code{silent_functions} is nonzero, internal output\n\ -from a function is suppressed. Otherwise, the results of expressions\n\ -within a function body that are not terminated with a semicolon will\n\ -have their values printed. The default value is 0.\n\ -\n\ -For example, if the function\n\ -\n\ -@example\n\ -function f ()\n\ - 2 + 2\n\ -endfunction\n\ -@end example\n\ -\n\ -@noindent\n\ -is executed, Octave will either print @samp{ans = 4} or nothing\n\ -depending on the value of @code{silent_functions}.\n\ -@end defvr"); - DEFVAR (warn_fortran_indexing, false, warn_fortran_indexing, "-*- texinfo -*-\n\ @defvr {Built-in Variable} warn_fortran_indexing\n\ @@ -2339,33 +1934,6 @@ printed for implicit conversions of complex numbers to real numbers.\n\ The default value is 0.\n\ @end defvr"); - - DEFVAR (warn_num_to_str, true, warn_num_to_str, - "-*- texinfo -*-\n\ -@defvr {Built-in Variable} warn_num_to_str\n\ -If the value of @code{warn_num_to_str} is nonzero, a warning is\n\ -printed for implicit conversions of numbers to their ASCII character\n\ -equivalents when strings are constructed using a mixture of strings and\n\ -numbers in matrix notation. For example,\n\ -\n\ -@example\n\ -@group\n\ -[ \"f\", 111, 111 ]\n\ - @result{} \"foo\"\n\ -@end group\n\ -@end example\n\ -elicits a warning if @code{warn_num_to_str} is nonzero. The default\n\ -value is 1.\n\ -@end defvr"); - - DEFVAR (warn_resize_on_range_error, false, warn_resize_on_range_error, - "-*- texinfo -*-\n\ -@defvr {Built-in Variable} warn_resize_on_range_error\n\ -If the value of @code{warn_resize_on_range_error} is nonzero, print a\n\ -warning when a matrix is resized by an indexed assignment with\n\ -indices outside the current bounds. The default value is 0.\n\ -@end defvr"); - } /*