# HG changeset patch # User jwe # Date 1068510215 0 # Node ID 2c9de1be042a55488cf6abc9a2f29e30b8ca282b # Parent 2cb70e1559391d93c72acb0767a768e2d3157509 [project @ 2003-11-11 00:23:35 by jwe] diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,23 @@ 2003-11-10 John W. Eaton + * oct-obj.h (octave_value_list): Internal representation is now + std::vector instead of Array. + + * ov-list.h, ov-list.cc: Internal representation is now Cell, not + octave_value_list. + (Flist): Print one-time warning. + + * ov.cc (octave_value::octave_value (const octave_value_list&, bool)): + Cope with octave_cs_list not being derived from octave_list. + + * ov-cs-list.cc (octave_cs_list): Handle conversion of Cell array + to octave_value_list here. + + * ov-cs-list.h (class octave_cs_list): Derive from + octave_base_value, not octave_list. + + * ov-cs-list.h, ov-cs-list.cc: Remove commented-out code. + * TEMPLATE-INST/Array-tc.cc (resize_fill_value): Use modern specialization syntax. diff --git a/src/oct-obj.cc b/src/oct-obj.cc --- a/src/oct-obj.cc +++ b/src/oct-obj.cc @@ -37,15 +37,31 @@ bool octave_value_list::valid_scalar_indices (void) const { - int n = data.length (); + int n = length (); for (int i = 0; i < n; i++) - if (! data(i).valid_as_scalar_index ()) + if (! data[i].valid_as_scalar_index ()) return false; return true; } +void +octave_value_list::resize (int n, const octave_value& val) +{ + int len = length (); + + if (n > len) + { + data.resize (n); + + for (int i = len; i < n; i++) + data[i] = val; + } + else if (n < len) + data.resize (n); +} + octave_value_list& octave_value_list::prepend (const octave_value& val) { @@ -148,22 +164,6 @@ return retval; } -octave_value_list -octave_value_list::index (idx_vector& i, int resize_ok) const -{ - return octave_value_list (data.index (i, resize_ok, octave_value ())); -} - -octave_value_list& -octave_value_list::assign (const idx_vector& i, - const octave_value_list& rhs, - const octave_value& fill_val) -{ - data.set_index (i); - ::assign (data, rhs.data, fill_val); - return *this; -} - bool octave_value_list::all_strings_p (void) const { diff --git a/src/oct-obj.h b/src/oct-obj.h --- a/src/oct-obj.h +++ b/src/oct-obj.h @@ -28,8 +28,8 @@ #endif #include +#include -#include "Array.h" #include "oct-alloc.h" #include "str-vec.h" @@ -93,13 +93,13 @@ octave_value operator () (int n) const { return elem (n); } - int length (void) const { return data.length (); } + int length (void) const { return data.size (); } bool empty (void) const { return length () == 0; } void resize (int n) { data.resize (n); } - void resize (int n, const octave_value& val) { data.resize_and_fill (n, val); } + void resize (int n, const octave_value& val); octave_value_list& prepend (const octave_value& val); @@ -112,12 +112,6 @@ octave_value_list splice (int offset, int length, const octave_value_list& lst) const; - octave_value_list index (idx_vector& i, int resize_ok = 0) const; - - octave_value_list& assign (const idx_vector& i, - const octave_value_list& rhs, - const octave_value& fill_val = octave_value ()); - bool all_strings_p (void) const; string_vector make_argv (const std::string&) const; @@ -130,7 +124,7 @@ static octave_allocator allocator; - Array data; + std::vector data; // This list of strings can be used to tag each element of data with // a name. By default, it is empty. @@ -152,24 +146,25 @@ octave_value_list (int n); - octave_value_list (const Array& d) - : data (d) { } - - void maybe_resize (int n) - { - if (n >= length ()) - data.resize_and_fill (n + 1, Matrix ()); - } + octave_value_list (const Array& d); octave_value& elem (int n) { - maybe_resize (n); - return data.elem (n); + static Matrix empty_matrix; + + if (n >= length ()) + resize (n+1, empty_matrix); + + return data[n]; } octave_value elem (int n) const { - return data.elem (n); +#if defined (BOUNDS_CHECKING) + return data.at (n); +#else + return data[n]; +#endif } }; diff --git a/src/ov-cs-list.cc b/src/ov-cs-list.cc --- a/src/ov-cs-list.cc +++ b/src/ov-cs-list.cc @@ -42,196 +42,20 @@ DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cs_list, "cs-list"); -#if 0 - -octave_value -octave_list::subsref (const std::string& type, - const std::list& idx) +octave_cs_list::octave_cs_list (const Cell& c) { - octave_value retval; - - switch (type[0]) - { - case '(': - { - octave_value_list tmp_idx = idx.front (); - - if (tmp_idx.length () == 1) - { - idx_vector i = tmp_idx (0).index_vector (); - - retval = octave_value_list (lst.index (i)); - } - else - error ("only one index allowed for lists"); - } - break; - - case '{': - { - octave_value_list tmp_idx = idx.front (); - - if (tmp_idx.length () == 1) - { - idx_vector i = tmp_idx (0).index_vector (); - - octave_value_list tmp = lst.index (i); - - if (tmp.length () == 1) - retval = tmp(0); - } - else - error ("only one index allowed for lists"); - } - break; - - case '.': - { - std::string nm = type_name (); - error ("%s cannot be indexed with %c", nm.c_str (), type[0]); - } - break; - - default: - panic_impossible (); - } - - return retval.next_subsref (type, idx); -} - -octave_value -octave_list::do_index_op (const octave_value_list& idx, int resize_ok) -{ - octave_value retval; - - if (idx.length () == 1) - { - idx_vector i = idx (0).index_vector (); - - retval = octave_value_list (lst.index (i, resize_ok)); - } - else - error ("lists may only be indexed by a single scalar"); - - return retval; -} - -octave_value -octave_list::subsasgn (const std::string& type, - const std::list& idx, - const octave_value& rhs) -{ - octave_value retval; - - int n = type.length (); - - octave_value t_rhs = rhs; - - if (n > 1) - { - switch (type[0]) - { - case '(': - { - octave_value tmp = do_index_op (idx.front (), true); + int n = c.length (); - if (! tmp.is_defined ()) - tmp = octave_value::empty_conv (type.substr (1), rhs); - - if (! error_state) - { - std::list next_idx (idx); - - next_idx.erase (next_idx.begin ()); - - t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); - } - } - break; - - case '{': - case '.': - { - std::string nm = type_name (); - error ("%s cannot be indexed with %c", nm.c_str (), type[0]); - } - break; - - default: - panic_impossible (); - } - } - - if (! error_state) - { - switch (type[0]) - { - case '(': - { - octave_value_list i = idx.front (); - - assign (i, t_rhs); - - retval = octave_value (this, count + 1); - } - break; + lst.resize (n); - case '{': - case '.': - { - std::string nm = type_name (); - error ("%s cannot be indexed with %c", nm.c_str (), type[0]); - } - break; - - default: - panic_impossible (); - } - } - - return retval; + for (int i = 0; i < n; i++) + lst(i) = c(i); } void -octave_list::assign (const octave_value_list& idx, const octave_value& rhs) -{ - if (idx.length () == 1) - { - int i = idx(0).int_value (true); - - if (! error_state) - { - int n = lst.length (); - - if (i > 0) - { - if (Vwarn_resize_on_range_error && i > n) - warning ("list index = %d out of range", i); - - lst(i-1) = rhs; - } - else - error ("list index = %d out of range", i); - } - else - error ("list index must be an integer"); - } - else - error ("lists may only be indexed by a single scalar"); -} - -void -octave_list::print (std::ostream& os, bool) const -{ - print_raw (os); -} - -#endif - -void octave_cs_list::print_raw (std::ostream& os, bool) const { - unwind_protect::begin_frame ("octave_list_print"); + unwind_protect::begin_frame ("octave_cs_list_print"); int n = lst.length (); @@ -265,208 +89,9 @@ newline (os); - unwind_protect::run_frame ("octave_list_print"); -} - -#if 0 - -bool -octave_list::print_name_tag (std::ostream& os, const std::string& name) const -{ - indent (os); - if (lst.length () == 0) - os << name << " = "; - else - { - os << name << " ="; - newline (os); - } - return false; -} - -DEFUN (list, args, , - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} list (@var{a1}, @var{a2}, @dots{})\n\ -Create a new list with elements given by the arguments @var{a1},\n\ -@var{a2}, @dots{}.\n\ -@end deftypefn") -{ - return octave_value (args); -} - -DEFUN (nth, args, , - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} nth (@var{list}, @var{n})\n\ -Return the @var{n}-th element of @var{list}.\n\ -@end deftypefn") -{ - octave_value retval; - - if (args.length () == 2) - { - octave_value_list lst = args(0).list_value (); - - if (! error_state) - { - int n = args(1).int_value (true); - - if (! error_state) - { - if (n > 0 && n <= lst.length ()) - retval = lst(n-1); - else - error ("nth: index = %d out of range", n); - } - else - error ("nth: second argument must be an integer"); - } - else - error ("nth: first argument must be a list"); - } - else - print_usage ("nth"); - - return retval; + unwind_protect::run_frame ("octave_cs_list_print"); } -DEFUN (append, args, , - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} append (@var{list}, @var{a1}, @var{a2}, @dots{})\n\ -Return a new list created by appending @var{a1}, @var{a1}, @dots{}, to\n\ -@var{list}. If any of the arguments to be appended is a list, its\n\ -elements are appended individually. For example,\n\ -\n\ -@example\n\ -x = list (1, 2);\n\ -y = list (3, 4);\n\ -append (x, y);\n\ -@end example\n\ -\n\ -@noindent\n\ -results in the list containing the four elements @samp{(1 2 3 4)}, not\n\ -a list containing the three elements @samp{(1 2 (3 4))}.\n\ -@end deftypefn") -{ - octave_value retval; - - int nargin = args.length (); - - if (nargin > 1) - { - octave_value_list tmp = args(0).list_value (); - - if (! error_state) - { - for (int i = 1; i < nargin; i++) - { - octave_value ov = args(i); - - if (ov.is_list ()) - tmp.append (ov.list_value ()); - else - tmp.append (ov); - } - - retval = tmp; - } - } - else - print_usage ("append"); - - return retval; -} - -DEFUN (reverse, args, , - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} reverse (@var{list})\n\ -Return a new list created by reversing the elements of @var{list}.\n\ -@end deftypefn") -{ - octave_value retval; - - int nargin = args.length (); - - if (nargin == 1) - { - octave_value_list tmp = args(0).list_value (); - - if (! error_state) - retval = tmp.reverse (); - } - else - print_usage ("reverse"); - - return retval; -} - -DEFUN (splice, args, , - "-*- texinfo -*-\n\ -@deftypefn {Built-in Function} {} splice (@var{list_1}, @var{offset}, @var{length}, @var{list_2})\n\ -Replace @var{length} elements of @var{list_1} beginning at\n\ -@var{offset} with the contents of @var{list_2} (if any). If\n\ -@var{length} is omitted, all elements from @var{offset} to the end of\n\ -@var{list_1} are replaced. As a special case, if @var{offset} is one\n\ -greater than the length of @var{list_1} and @var{length} is 0, splice\n\ -is equivalent to @code{append (@var{list_1}, @var{list_2})}.\n\ -@end deftypefn") -{ - octave_value retval; - - int nargin = args.length (); - - if (nargin > 1 && nargin < 5) - { - octave_value_list list_1 = args(0).list_value (); - - if (! error_state) - { - int offset = args(1).int_value (true); - - if (! error_state) - { - offset--; - - int length = 0; - - octave_value_list list_2; - - if (nargin < 3) - length = list_1.length () - offset; - else - { - length = args(2).int_value (true); - - if (! error_state) - { - if (nargin == 4) - { - list_2 = args(3).list_value (); - - if (error_state) - error ("splice: fourth argument must be a list"); - } - } - else - error ("splice: LENGTH must be an integer"); - } - - if (! error_state) - retval = list_1.splice (offset, length, list_2); - } - else - error ("splice: OFFSET must be an integer"); - } - else - error ("splice: first argument must be a list"); - } - else - print_usage ("splice"); - - return retval; -} - -#endif - /* ;;; Local Variables: *** ;;; mode: C++ *** diff --git a/src/ov-cs-list.h b/src/ov-cs-list.h --- a/src/ov-cs-list.h +++ b/src/ov-cs-list.h @@ -35,6 +35,7 @@ #include "mx-base.h" #include "str-vec.h" +#include "Cell.h" #include "error.h" #include "oct-alloc.h" #include "oct-obj.h" @@ -46,66 +47,42 @@ // Lists. class -octave_cs_list : public octave_list +octave_cs_list : public octave_base_value { public: octave_cs_list (void) - : octave_list () { } + : lst () { } octave_cs_list (const octave_value_list& l) - : octave_list (l) { } + : lst (l) { } - octave_cs_list (const Cell& c) - : octave_list (c) { } + octave_cs_list (const Cell& c); octave_cs_list (const octave_cs_list& l) - : octave_list (l) { } + : lst (l) { } ~octave_cs_list (void) { } octave_value *clone (void) const { return new octave_cs_list (*this); } octave_value *empty_clone (void) const { return new octave_cs_list (); } -#if 0 - octave_value subsref (const std::string& type, - const std::list& idx); - - octave_value do_index_op (const octave_value_list& idx, int resize_ok); - - octave_value subsasgn (const std::string& type, - const std::list& idx, - const octave_value& rhs); - - void assign (const octave_value_list& idx, const octave_value& rhs); + dim_vector dims (void) const { return dim_vector (1, lst.length ()); } bool is_defined (void) const { return true; } bool is_constant (void) const { return true; } -#endif - bool is_cs_list (void) const { return true; } -#if 0 - - bool is_list (void) const { return true; } - octave_value_list list_value (void) const { return lst; } - void print (std::ostream& os, bool pr_as_read_syntax = false) const; - -#endif - void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; -#if 0 - - bool print_name_tag (std::ostream& os, const std::string& name) const; +private: -#endif - -private: + // The list of Octave values. + octave_value_list lst; DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA diff --git a/src/ov-list.cc b/src/ov-list.cc --- a/src/ov-list.cc +++ b/src/ov-list.cc @@ -44,14 +44,14 @@ DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_list, "list"); octave_list::octave_list (const Cell& c) - : octave_base_value (), lst () + : octave_base_value (), data () { int n = c.length (); - lst.resize (n); + data.resize (dim_vector (1, n)); for (int i = 0; i < n; i++) - lst(i) = c(i); + data(i) = c(i); } octave_value @@ -70,7 +70,7 @@ { idx_vector i = tmp_idx (0).index_vector (); - retval = octave_value (octave_value_list (lst.index (i))); + retval = octave_list (data.index (i)); } else error ("only one index allowed for lists"); @@ -85,7 +85,7 @@ { idx_vector i = tmp_idx (0).index_vector (); - octave_value_list tmp = lst.index (i); + Cell tmp = data.index (i); if (tmp.length () == 1) retval = tmp(0); @@ -118,7 +118,7 @@ { idx_vector i = idx (0).index_vector (); - retval = octave_value (octave_value_list (lst.index (i, resize_ok))); + retval = octave_list (data.index (i, resize_ok)); } else error ("lists may only be indexed by a single scalar"); @@ -211,14 +211,14 @@ if (! error_state) { - int n = lst.length (); + int n = data.length (); if (i > 0) { if (Vwarn_resize_on_range_error && i > n) warning ("list index = %d out of range", i); - lst(i-1) = rhs; + data(i-1) = rhs; } else error ("list index = %d out of range", i); @@ -230,6 +230,21 @@ error ("lists may only be indexed by a single scalar"); } +octave_value_list +octave_list::list_value (void) const +{ + octave_value_list retval; + + int n = data.length (); + + retval.resize (n); + + for (int i = 0; i < n; i++) + retval(i) = data(i); + + return retval; +} + void octave_list::print (std::ostream& os, bool) const { @@ -241,7 +256,7 @@ { unwind_protect::begin_frame ("octave_list_print"); - int n = lst.length (); + int n = data.length (); if (n > 0) { @@ -257,7 +272,7 @@ buf << "[" << i+1 << "]" << OSSTREAM_ENDS; - octave_value val = lst(i); + octave_value val = data(i); val.print_with_name (os, OSSTREAM_STR (buf)); @@ -281,7 +296,7 @@ octave_list::print_name_tag (std::ostream& os, const std::string& name) const { indent (os); - if (lst.length () == 0) + if (data.length () == 0) os << name << " = "; else { @@ -298,6 +313,14 @@ @var{a2}, @dots{}.\n\ @end deftypefn") { + static bool warned = false; + + if (! warned) + { + warning ("list objects are deprecated; use cell arrays instead"); + warned = true; + } + return octave_value (args); } diff --git a/src/ov-list.h b/src/ov-list.h --- a/src/ov-list.h +++ b/src/ov-list.h @@ -35,9 +35,9 @@ #include "mx-base.h" #include "str-vec.h" +#include "Cell.h" #include "error.h" #include "oct-alloc.h" -#include "oct-obj.h" #include "ov-base.h" #include "ov-typeinfo.h" @@ -54,12 +54,12 @@ : octave_base_value () { } octave_list (const octave_value_list& l) - : octave_base_value (), lst (l) { } + : octave_base_value (), data (l) { } octave_list (const Cell& c); octave_list (const octave_list& l) - : octave_base_value (), lst (l.lst) { } + : octave_base_value (), data (l.data) { } ~octave_list (void) { } @@ -85,7 +85,7 @@ void assign (const octave_value_list& idx, const octave_value& rhs); - dim_vector dims (void) const { return dim_vector (1, lst.length ()); } + dim_vector dims (void) const { return dim_vector (1, data.length ()); } bool is_defined (void) const { return true; } @@ -93,7 +93,7 @@ bool is_list (void) const { return true; } - octave_value_list list_value (void) const { return lst; } + octave_value_list list_value (void) const; void print (std::ostream& os, bool pr_as_read_syntax = false) const; @@ -104,7 +104,7 @@ protected: // The list of Octave values. - octave_value_list lst; + Cell data; private: diff --git a/src/ov.cc b/src/ov.cc --- a/src/ov.cc +++ b/src/ov.cc @@ -605,8 +605,13 @@ } octave_value::octave_value (const octave_value_list& l, bool is_csl) - : rep (is_csl ? new octave_cs_list (l) : new octave_list (l)) + : rep (0) { + if (is_csl) + rep = new octave_cs_list (l); + else + rep = new octave_list (l); + rep->count = 1; }