changeset 2882:05926e1b367d

[project @ 1997-04-24 09:48:59 by jwe]
author jwe
date Thu, 24 Apr 1997 09:51:05 +0000
parents b99a6a2619aa
children 0b3954110c77
files src/ov-base.cc src/ov-base.h src/ov-list.cc src/ov-list.h src/ov-typeinfo.cc src/ov-typeinfo.h
diffstat 6 files changed, 337 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/ov-base.cc
+++ b/src/ov-base.cc
@@ -41,6 +41,7 @@
 #include "ov-ch-mat.h"
 #include "ov-str-mat.h"
 #include "ov-range.h"
+#include "ov-list.h"
 
 int octave_base_value::t_id = -1;
 
@@ -177,6 +178,14 @@
   return retval;
 }
 
+octave_value_list
+octave_base_value::list_value (void) const
+{
+  octave_value_list retval;
+  gripe_wrong_type_arg ("octave_base_value::list_value()", type_name ());
+  return retval;
+}
+
 bool
 octave_base_value::bool_value (void) const
 {
--- a/src/ov-base.h
+++ b/src/ov-base.h
@@ -100,6 +100,8 @@
 
   bool is_map (void) const { return false; }
 
+  bool is_list (void) const { return false; }
+
   bool is_magic_colon (void) const { return false; }
 
   bool is_all_va_args (void) const { return false; }
@@ -150,6 +152,8 @@
 
   Octave_map map_value (void) const;
 
+  octave_value_list list_value (void) const;
+
   bool bool_value (void) const;
 
   boolMatrix bool_matrix_value (void) const;
new file mode 100644
--- /dev/null
+++ b/src/ov-list.cc
@@ -0,0 +1,188 @@
+/*
+
+Copyright (C) 1996, 1997 John W. Eaton
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 2, or (at your option) any
+later version.
+
+Octave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, write to the Free
+Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+*/
+
+#if defined (__GNUG__)
+#pragma implementation
+#endif
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <iostream.h>
+
+#include "lo-utils.h"
+
+#include "defun.h"
+#include "error.h"
+#include "help.h"
+#include "ov-list.h"
+#include "unwind-prot.h"
+
+octave_allocator
+octave_list::allocator (sizeof (octave_list));
+
+int
+octave_list::t_id (-1);
+
+const string
+octave_list::t_name ("list");
+
+octave_value
+octave_list::index (const octave_value_list& idx) const
+{
+  octave_value retval;
+
+  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 && i <= n)
+		retval = lst(i-1);
+	      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");
+
+  return retval;
+}
+
+void
+octave_list::print (ostream& os, bool)
+{
+  begin_unwind_frame ("octave_list_print");
+
+  unwind_protect_int (list_indent);
+
+  os.form ("\n%*s{\n", list_indent, "");
+
+  increment_list_indent ();
+
+  int n = lst.length ();
+
+  for (int i = 0; i < n; i++)
+    {
+      bool pad_after = false;
+
+      octave_value val = lst(i);
+
+      os.form ("%*s", list_indent, "");
+
+      if (val.print_as_scalar ())
+	os << " ";
+      else if (val.is_list ())
+	pad_after = true;
+      else
+	{
+	  pad_after = true;
+
+	  os << "\n\n";
+	}
+
+      val.print (os);
+
+      if (pad_after)
+	os << "\n";
+    }
+
+  decrement_list_indent ();
+
+  os.form ("%*s%s", list_indent, "", "}\n");
+
+  run_unwind_frame ("octave_list_print");
+}
+
+DEFUN (make_list, args, ,
+  "make_list (ARGS)\n\
+\n\
+Create a new list from ARGS.")
+{
+  return octave_value (args);
+}
+
+DEFUN (append, args, ,
+  "append (LIST, ARGS)\n\
+\n\
+Return a new list created by appending ARGS to LIST")
+{
+  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++)
+	    tmp.append (args(i));
+
+	  retval = tmp;
+	}
+    }
+  else
+    print_usage ("append");
+
+  return retval;
+}
+
+DEFUN (reverse, args, ,
+  "reverse (LIST)\n\
+\n\
+Return a new list created by reversing the elements of LIST")
+{
+  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;
+}
+
+/*
+;;; Local Variables: ***
+;;; mode: C++ ***
+;;; End: ***
+*/
new file mode 100644
--- /dev/null
+++ b/src/ov-list.h
@@ -0,0 +1,113 @@
+/*
+
+Copyright (C) 1996, 1997 John W. Eaton
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 2, or (at your option) any
+later version.
+
+Octave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, write to the Free
+Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+*/
+
+#if !defined (octave_list_h)
+#define octave_list_h 1
+
+#if defined (__GNUG__)
+#pragma interface
+#endif
+
+#include <cstdlib>
+
+#include <string>
+
+class ostream;
+
+#include "mx-base.h"
+#include "str-vec.h"
+
+#include "error.h"
+#include "oct-alloc.h"
+#include "oct-obj.h"
+#include "ov-base.h"
+#include "ov-typeinfo.h"
+
+class tree_walker;
+
+// Lists.
+
+class
+octave_list : public octave_base_value
+{
+public:
+
+  octave_list (void)
+    : octave_base_value () { }
+
+  octave_list (const octave_value_list& l)
+    : octave_base_value (), lst (l) { }
+
+  octave_list (const octave_list& l)
+    : octave_base_value (), lst (l.lst) { }
+
+  ~octave_list (void) { }
+
+  octave_value *clone (void) { return new octave_list (*this); }
+
+  void *operator new (size_t size)
+    { return allocator.alloc (size); }
+
+  void operator delete (void *p, size_t size)
+    { allocator.free (p, size); }
+
+  octave_value index (const octave_value_list& idx) const;
+
+  bool is_defined (void) const { return true; }
+
+  bool is_list (void) const { return true; }
+
+  octave_value_list list_value (void) const { return lst; }
+
+  void print (ostream& os, bool pr_as_read_syntax = false);
+
+  int type_id (void) const { return t_id; }
+
+  string type_name (void) const { return t_name; }
+
+  static int static_type_id (void) { return t_id; }
+
+  static void register_type (void)
+    { t_id = octave_value_typeinfo::register_type (t_name); }
+
+private:
+
+  // The list of Octave values.
+  octave_value_list lst;
+
+  // For custom memory management.
+  static octave_allocator allocator;
+
+  // Type id of list objects, set by register_type().
+  static int t_id;
+
+  // Type name of list objects, defined in ov-list.cc.
+  static const string t_name;
+};
+
+#endif
+
+/*
+;;; Local Variables: ***
+;;; mode: C++ ***
+;;; End: ***
+*/
--- a/src/ov-typeinfo.cc
+++ b/src/ov-typeinfo.cc
@@ -51,6 +51,7 @@
 
 template class Array<assign_op_fcn>;
 template class Array2<assign_op_fcn>;
+template class Array3<assign_op_fcn>;
 
 template class Array<type_conv_fcn>;
 template class Array2<type_conv_fcn>;
@@ -76,13 +77,14 @@
 }
 
 bool
-octave_value_typeinfo::register_assign_op (int t_lhs, int t_rhs,
+octave_value_typeinfo::register_assign_op (octave_value::assign_op op,
+					   int t_lhs, int t_rhs,
 					   assign_op_fcn f)
 {
   if (! instance)
     instance = new octave_value_typeinfo ();
 
-  return instance->do_register_assign_op (t_lhs, t_rhs, f);
+  return instance->do_register_assign_op (op, t_lhs, t_rhs, f);
 }
 
 bool
@@ -125,7 +127,8 @@
       binary_ops.resize (static_cast<int> (octave_value::num_binary_ops),
 			 len, len, static_cast<binary_op_fcn> (0));
 
-      assign_ops.resize (len, len, static_cast<assign_op_fcn> (0));
+      assign_ops.resize (static_cast<int> (octave_value::num_assign_ops),
+			 len, len, static_cast<assign_op_fcn> (0));
 
       pref_assign_conv.resize (len, len, -1);
 
@@ -150,10 +153,11 @@
 }
 
 bool
-octave_value_typeinfo::do_register_assign_op (int t_lhs, int t_rhs,
+octave_value_typeinfo::do_register_assign_op (octave_value::assign_op op,
+					      int t_lhs, int t_rhs,
 					      assign_op_fcn f)
 {
-  assign_ops.checkelem (t_lhs, t_rhs) = f;
+  assign_ops.checkelem (static_cast<int> (op), t_lhs, t_rhs) = f;
 
   return false;
 }
@@ -186,9 +190,10 @@
 }
 
 assign_op_fcn
-octave_value_typeinfo::do_lookup_assign_op (int t_lhs, int t_rhs)
+octave_value_typeinfo::do_lookup_assign_op (octave_value::assign_op op,
+					    int t_lhs, int t_rhs)
 {
-  return assign_ops.checkelem (t_lhs, t_rhs);
+  return assign_ops.checkelem (static_cast<int> (op), t_lhs, t_rhs);
 }
 
 int
--- a/src/ov-typeinfo.h
+++ b/src/ov-typeinfo.h
@@ -47,7 +47,8 @@
   static bool register_binary_op (octave_value::binary_op, int, int,
 				  binary_op_fcn);
 
-  static bool register_assign_op (int, int, assign_op_fcn);
+  static bool register_assign_op (octave_value::assign_op, int, int,
+				  assign_op_fcn);
 
   static bool register_pref_assign_conv (int, int, int);
 
@@ -60,9 +61,9 @@
   }
 
   static assign_op_fcn
-  lookup_assign_op (int t_lhs, int t_rhs)
+  lookup_assign_op (octave_value::assign_op op, int t_lhs, int t_rhs)
   {
-    return instance->do_lookup_assign_op (t_lhs, t_rhs);
+    return instance->do_lookup_assign_op (op, t_lhs, t_rhs);
   }
 
   static int
@@ -88,7 +89,8 @@
     : num_types (0), types (init_tab_sz, string ()),
       binary_ops (octave_value::num_binary_ops, init_tab_sz,
 		  init_tab_sz, (binary_op_fcn) 0),
-      assign_ops (init_tab_sz, init_tab_sz, (assign_op_fcn) 0),
+      assign_ops (octave_value::num_assign_ops, init_tab_sz,
+		  init_tab_sz, (assign_op_fcn) 0),
       pref_assign_conv (init_tab_sz, init_tab_sz, -1),
       widening_ops (init_tab_sz, init_tab_sz, (type_conv_fcn) 0)  { }
 
@@ -104,7 +106,7 @@
 
   Array3<binary_op_fcn> binary_ops;
 
-  Array2<assign_op_fcn> assign_ops;
+  Array3<assign_op_fcn> assign_ops;
 
   Array2<int> pref_assign_conv;
 
@@ -115,7 +117,8 @@
   bool do_register_binary_op (octave_value::binary_op, int, int,
 			      binary_op_fcn);
 
-  bool do_register_assign_op (int, int, assign_op_fcn);
+  bool do_register_assign_op (octave_value::assign_op, int, int,
+			      assign_op_fcn);
 
   bool do_register_pref_assign_conv (int, int, int);
 
@@ -124,7 +127,8 @@
   binary_op_fcn
   do_lookup_binary_op (octave_value::binary_op, int, int);
 
-  assign_op_fcn do_lookup_assign_op (int, int);
+  assign_op_fcn
+  do_lookup_assign_op (octave_value::assign_op, int, int);
 
   int do_lookup_pref_assign_conv (int, int);