changeset 8902:5d5db7a347c6

erase closed files from file list & cache lookup
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 03 Mar 2009 08:01:07 +0100
parents 821f0242e8c1
children c174a1fc3fde
files src/ChangeLog src/oct-stream.cc src/oct-stream.h
diffstat 3 files changed, 55 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Tue Mar 03 00:45:00 2009 -0500
+++ b/src/ChangeLog	Tue Mar 03 08:01:07 2009 +0100
@@ -1,3 +1,13 @@
+2009-03-03  Jaroslav Hajek  <highegg@gmail.com>
+
+	* oct-stream.h (octave_stream_list::lookup_cache): New member field.
+	(octave_stream_list::octave_stream_list): Initialize it.
+	(octave_stream_list::do_lookup): Use it.
+	(octave_stream_list::clear): Make flush optional. Do physically erase
+	entries from the map. Close files.
+	(octave_stream_list::do_remove): Call clear on "all". Do erase deleted
+	entry from the map.
+
 2009-03-02  John W. Eaton  <jwe@octave.org>
 
 	* graphics.cc (Fget, F__get__): Return a column vector of property
--- a/src/oct-stream.cc	Tue Mar 03 00:45:00 2009 -0500
+++ b/src/oct-stream.cc	Tue Mar 03 08:01:07 2009 +0100
@@ -3983,10 +3983,10 @@
 }
 
 void
-octave_stream_list::clear (void)
+octave_stream_list::clear (bool flush)
 {
   if (instance)
-    instance->do_clear ();
+    instance->do_clear (flush);
 }
 
 string_vector
@@ -4068,12 +4068,20 @@
 
   if (fid >= 0)
     {
-      ostrl_map::const_iterator iter = list.find (fid);
-
-      if (iter != list.end ())
-	retval = iter->second;
+      if (lookup_cache != list.end () && lookup_cache->first == fid)
+        retval = lookup_cache->second;
       else
-	gripe_invalid_file_id (fid, who);
+        {
+          ostrl_map::const_iterator iter = list.find (fid);
+
+          if (iter != list.end ())
+            {
+              retval = iter->second;
+              lookup_cache = iter;
+            }
+          else
+            gripe_invalid_file_id (fid, who);
+        }
     }
   else
     gripe_invalid_file_id (fid, who);
@@ -4110,11 +4118,13 @@
       if (iter != list.end ())
 	{
 	  octave_stream os = iter->second;
-
+          list.erase (iter);
+          lookup_cache = list.end ();
+
+          // FIXME: is this check redundant?
 	  if (os.is_valid ())
 	    {
 	      os.close ();
-	      iter->second = octave_stream ();
 	      retval = 0;
 	    }
 	  else
@@ -4136,18 +4146,7 @@
 
   if (fid.is_string () && fid.string_value () == "all")
     {
-      for (ostrl_map::iterator p = list.begin (); p != list.end (); p++)
-	{
-	  // Skip stdin, stdout, and stderr.
-
-	  if (p->first > 2)
-	    {
-	      octave_stream os = p->second;
-
-	      if (os.is_valid ())
-		do_remove (p->first, who);
-	    }
-	}
+      do_clear (false);
 
       retval = 0;
     }
@@ -4163,22 +4162,30 @@
 }
 
 void
-octave_stream_list::do_clear (void)
+octave_stream_list::do_clear (bool flush)
 {
-  // Do flush stdout and stderr.
-
-  list[0].flush ();
-  list[1].flush ();
-
+  if (flush)
+    {
+      // Do flush stdout and stderr.
+
+      list[0].flush ();
+      list[1].flush ();
+    }
+
+  octave_stream saved_os[3];
   // But don't delete them or stdin.
-
-  for (ostrl_map::iterator p = list.begin (); p != list.end (); p++)
+  for (ostrl_map::iterator iter = list.begin (); iter != list.end (); iter++)
     {
-      // Skip stdin, stdout, and stderr.
-
-      if (p->first > 2)
-	p->second = octave_stream ();
+      int fid = iter->first;
+      octave_stream os = iter->second;
+      if (fid < 3)
+        saved_os[fid] = os;
+      else if (os.is_valid ())
+        os.close ();
     }
+  list.clear ();
+  for (int fid = 0; fid < 3; fid++) list[fid] = saved_os[fid];
+  lookup_cache = list.end ();
 }
 
 string_vector
--- a/src/oct-stream.h	Tue Mar 03 00:45:00 2009 -0500
+++ b/src/oct-stream.h	Tue Mar 03 08:01:07 2009 +0100
@@ -629,7 +629,7 @@
 {
 protected:
 
-  octave_stream_list (void) : list () { }
+  octave_stream_list (void) : list (), lookup_cache (list.end ()) { }
 
 public:
 
@@ -649,7 +649,7 @@
   static int remove (const octave_value& fid,
 		     const std::string& who = std::string ());
 
-  static void clear (void);
+  static void clear (bool flush = true);
 
   static string_vector get_info (int fid);
   static string_vector get_info (const octave_value& fid);
@@ -666,6 +666,8 @@
 
   ostrl_map list;
 
+  mutable ostrl_map::const_iterator lookup_cache;
+
   static octave_stream_list *instance;
 
   int do_insert (octave_stream& os);
@@ -677,7 +679,7 @@
   int do_remove (int fid, const std::string& who = std::string ());
   int do_remove (const octave_value& fid, const std::string& who = std::string ());
 
-  void do_clear (void);
+  void do_clear (bool flush = true);
 
   string_vector do_get_info (int fid) const;
   string_vector do_get_info (const octave_value& fid) const;