Mercurial > hg > octave-lyh
diff src/ov-list.cc @ 3196:3ac3e8edc258
[project @ 1998-10-28 23:04:39 by jwe]
author | jwe |
---|---|
date | Wed, 28 Oct 1998 23:11:11 +0000 |
parents | 38de16594cb4 |
children | 44d82b369c78 |
line wrap: on
line diff
--- a/src/ov-list.cc +++ b/src/ov-list.cc @@ -80,6 +80,34 @@ } void +octave_list::assign (const octave_value_list& idx, const octave_value& rhs) +{ + if (idx.length () == 1) + { + double d = idx(0).double_value (); + + if (! error_state) + { + if (D_NINT (d) == d) + { + int n = lst.length (); + + int i = static_cast<int> (d); + + if (i > 0 && (Vresize_on_range_error || i <= n)) + lst(i) = 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 (ostream& os, bool) const { print_raw (os); @@ -90,31 +118,37 @@ { unwind_protect::begin_frame ("octave_list_print"); - indent (os); - os << "("; - newline (os); - - increment_indent_level (); - int n = lst.length (); - for (int i = 0; i < n; i++) + if (n > 0) { - ostrstream buf; - buf << "[" << i+1 << "]" << ends; - const char *nm = buf.str (); + indent (os); + os << "("; + newline (os); + + increment_indent_level (); - octave_value val = lst(i); + for (int i = 0; i < n; i++) + { + ostrstream buf; + buf << "[" << i+1 << "]" << ends; + const char *nm = buf.str (); - val.print_with_name (os, nm); + octave_value val = lst(i); - delete [] nm; - } + val.print_with_name (os, nm); + + delete [] nm; + } - decrement_indent_level (); + decrement_indent_level (); - indent (os); - os << ")"; + indent (os); + os << ")"; + } + else + os << "()"; + newline (os); unwind_protect::run_frame ("octave_list_print"); @@ -124,8 +158,13 @@ octave_list::print_name_tag (ostream& os, const string& name) const { indent (os); - os << name << " ="; - newline (os); + if (lst.length () == 0) + os << name << " = "; + else + { + os << name << " ="; + newline (os); + } return false; } @@ -186,6 +225,82 @@ return retval; } +DEFUN (splice, args, , + "splice (LIST_1, OFFSET, LENGTH, LIST_2)\n\ +splice (LIST_1, OFFSET, LENGTH, LIST_2)\n\ +splice (LIST_1, OFFSET, LENGTH)\n\ +splice (LIST_1, OFFSET)\n\ +\n\ +Replace LENGTH elements of LIST_1 beginning at OFFSET with the +contents of LIST_2 (if any). If LENGTH is omitted, ") +{ + octave_value retval; + + int nargin = args.length (); + + if (nargin > 1 && nargin < 5) + { + octave_value_list list_1 = args(0).list_value (); + + if (! error_state) + { + double d_offset = args(1).double_value (); + + if (! error_state) + { + if (D_NINT (d_offset) == d_offset) + { + int offset = static_cast<int> (d_offset) - 1; + + int length = 0; + + if (nargin < 3) + length = list_1.length () - offset; + else + { + double d_length = args(2).double_value (); + + if (error_state) + return retval; + else + { + if (D_NINT (d_length) == d_length) + length = static_cast<int> (d_length); + else + error ("splice: LENGTH must be an integer"); + } + } + + octave_value_list list_2; + + if (nargin == 4) + { + list_2 = args(3).list_value (); + + if (error_state) + { + error ("splice: fourth argument must be a list"); + return retval; + } + } + + retval = list_1.splice (offset, length, list_2); + } + else + error ("splice: OFFSET must be an integer"); + } + else + error ("splice: OFFSET must be an integer"); + } + else + error ("splice: first argument must be a list"); + } + else + print_usage ("splice"); + + return retval; +} + /* ;;; Local Variables: *** ;;; mode: C++ ***