changeset 11900:9e07dffc8a60 release-3-0-x

generate new fractional parts for recycled graphics handles
author John W. Eaton <jwe@octave.org>
date Mon, 05 Jan 2009 08:20:15 +0100
parents bfac13fcb6fe
children dabbfac27a45
files src/ChangeLog src/graphics.cc
diffstat 2 files changed, 22 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,10 @@
 2008-10-16  John W. Eaton  <jwe@octave.org>
 
+	* graphics.cc (make_handle_fraction): New static function.
+	(gh_manager::get_handle): Use it.
+	(gh_manager::do_free): Call make_handle_fraction to replace
+	fractional part of non-figure handles.
+
 	* graphics.cc (base_properties::remove_child): Handle children as
 	a column vector instead of a row vector.
 
--- a/src/graphics.cc
+++ b/src/graphics.cc
@@ -430,6 +430,13 @@
     error ("set: invalid number of arguments");
 }
 
+static double
+make_handle_fraction (void)
+{
+  static double maxrand = RAND_MAX + 2.0;
+
+  return (rand () + 1.0) / maxrand;
+}
 
 graphics_handle
 gh_manager::get_handle (const std::string& go_name)
@@ -438,6 +445,9 @@
 
   if (go_name == "figure")
     {
+      // Figure handles are positive integers corresponding to the
+      // figure number.
+
       // We always want the lowest unused figure number.
 
       retval = 1;
@@ -447,6 +457,11 @@
     }
   else
     {
+      // Other graphics handles are negative integers plus some random
+      // fractional part.  To avoid running out of integers, we
+      // recycle the integer part but tack on a new random part each
+      // time.
+
       free_list_iterator p = handle_free_list.begin ();
 
       if (p != handle_free_list.end ())
@@ -456,11 +471,9 @@
 	}
       else
 	{
-	  static double maxrand = RAND_MAX + 2.0;
-
 	  retval = graphics_handle (next_handle);
 
-	  next_handle = ceil (next_handle) - 1.0 - (rand () + 1.0) / maxrand;
+	  next_handle = ceil (next_handle) - 1.0 - make_handle_fraction ();
 	}
     }
 
@@ -481,7 +494,7 @@
 	      handle_map.erase (p);
 
 	      if (h.value () < 0)
-		handle_free_list.insert (h);
+                handle_free_list.insert (ceil (h.value ()) - make_handle_fraction ());
 	    }
 	  else
 	    error ("graphics_handle::free: invalid object %g", h.value ());