changeset 5013:1eb9ce5c0152

[project @ 2004-09-21 22:18:07 by jwe]
author jwe
date Tue, 21 Sep 2004 22:22:13 +0000
parents ed25bed43409
children fd5871c5f85b
files liboctave/oct-alloc.cc src/ChangeLog src/data.cc src/parse.y src/symtab.cc src/symtab.h
diffstat 6 files changed, 86 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/oct-alloc.cc	Tue Sep 21 15:45:48 2004 +0000
+++ b/liboctave/oct-alloc.cc	Tue Sep 21 22:22:13 2004 +0000
@@ -49,6 +49,9 @@
   return tmp;
 }
 
+// XXX FIXME XXX -- if we free the last item on the list, shouldn't we
+// also free the underlying character array used for storage?
+
 void
 octave_allocator::free (void *p, size_t size)
 {
--- a/src/ChangeLog	Tue Sep 21 15:45:48 2004 +0000
+++ b/src/ChangeLog	Tue Sep 21 22:22:13 2004 +0000
@@ -1,3 +1,17 @@
+2004-09-21  David Bateman  <dbateman@free.fr>
+
+	* data.cc (Freshape): Allow a single empty dimension argument
+	to flag unknown dimension and calculate its value from the
+	other dimensions.
+
+2004-09-21  John W. Eaton  <jwe@octave.org>
+
+	* symtab.h (symbol_record::~symbol_record): Delete definition if
+	count goes to zero.
+	* symtab.cc (symbol_table::~symbol_table): Move here from symtab.h.
+	Call delete on each symbol record in the table instead of just
+	clearing them.
+
 2004-09-17 David Bateman <dbateman@free.fr>
 
 	* DLD-FUNCTIONS/sort.cc (ascending_compare, descending_compare):
--- a/src/data.cc	Tue Sep 21 15:45:48 2004 +0000
+++ b/src/data.cc	Tue Sep 21 22:22:13 2004 +0000
@@ -1646,6 +1646,9 @@
 @noindent\n\
 Note that the total number of elements in the original\n\
 matrix must match the total number of elements in the new matrix.\n\
+\n\
+A single dimension of the return matrix can be unknown and is flagged\n\
+by an empty argument.\n\
 @end deftypefn")
 {
   octave_value retval;
@@ -1659,13 +1662,47 @@
   else if (nargin > 2)
     {
       new_size.resize (nargin-1);
-
+      int empty_dim = -1;
+      
       for (int i = 1; i < nargin; i++)
 	{
-	  new_size(i-1) = args(i).int_value ();
+	  if (args(i).is_empty ())
+	    if (empty_dim > 0)
+	      {
+		error ("reshape: only a single dimension can be unknown");
+		break;
+	      }
+	    else
+	      {
+		empty_dim = i;
+		new_size(i-1) = 1;
+	      }
+	  else
+	    {
+	      new_size(i-1) = args(i).int_value ();
 
-	  if (error_state)
-	    break;
+	      if (error_state)
+		break;
+	    }
+	}
+
+      if (! error_state && (empty_dim > 0))
+	{
+	  int nel = 1;
+	  for (int i = 0; i < nargin - 1; i++)
+	    nel *= new_size(i);
+
+	  if (nel == 0)
+	    new_size(empty_dim-1) = 0;
+	  else
+	    {
+	      int size_empty_dim = args(0).numel () / nel;
+	      
+	      if (args(0).numel () != size_empty_dim * nel)
+		error ("reshape: size is not divisble by the product of known dimensions (= %d)", nel);
+	      else
+		new_size(empty_dim-1) = size_empty_dim;
+	    }
 	}
     }
   else
--- a/src/parse.y	Tue Sep 21 15:45:48 2004 +0000
+++ b/src/parse.y	Tue Sep 21 22:22:13 2004 +0000
@@ -1175,6 +1175,7 @@
 		        symtab_context.push (curr_sym_tab);
 
 			tmp_local_sym_tab = new symbol_table ();
+
 			curr_sym_tab = tmp_local_sym_tab;
 
 			lexer_flags.looking_at_function_handle--;
@@ -2034,8 +2035,8 @@
 
   body->mark_as_function_body ();
 
-  octave_user_function *fcn
-    = new octave_user_function (param_list, ret_list, body, curr_sym_tab);
+  octave_value fcn (new octave_user_function (param_list, ret_list,
+					      body, curr_sym_tab));
 
   if (symtab_context.empty ())
     panic_impossible ();
--- a/src/symtab.cc	Tue Sep 21 15:45:48 2004 +0000
+++ b/src/symtab.cc	Tue Sep 21 22:22:13 2004 +0000
@@ -700,6 +700,25 @@
 
 // A symbol table.
 
+symbol_table::~symbol_table (void)
+{
+  for (unsigned int i = 0; i < table_size; i++)
+    {
+      symbol_record *ptr = table[i].next ();
+
+      while (ptr)
+	{
+	  symbol_record *tmp = ptr;
+
+	  ptr = ptr->next ();
+
+	  delete tmp;
+	}
+    }
+
+  delete [] table;
+}
+
 symbol_record *
 symbol_table::lookup (const std::string& nm, bool insert, bool warn)
 {
--- a/src/symtab.h	Tue Sep 21 15:45:48 2004 +0000
+++ b/src/symtab.h	Tue Sep 21 22:22:13 2004 +0000
@@ -258,7 +258,11 @@
       can_hide_function (n != "__end__"), nm (n), chg_fcn (0),
       definition (new symbol_def ()), next_elem (nxt) { }
 
-  ~symbol_record (void) { }
+  ~symbol_record (void)
+    {
+      if (--definition->count <= 0)
+	delete definition;
+    }
 
   std::string name (void) const { return nm; }
 
@@ -467,11 +471,7 @@
 	}
     }
 
-  ~symbol_table (void)
-    {
-      clear ();
-      delete [] table;
-    }
+  ~symbol_table (void);
 
   symbol_record *lookup (const std::string& nm, bool insert = false,
 			 bool warn = false);