[macruby-changes] [3537] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 15 17:29:34 PST 2010


Revision: 3537
          http://trac.macosforge.org/projects/ruby/changeset/3537
Author:   ernest.prabhakar at gmail.com
Date:     2010-02-15 17:29:34 -0800 (Mon, 15 Feb 2010)
Log Message:
-----------
Moved Queue#stride to Dispatch.upto

Modified Paths:
--------------
    MacRuby/trunk/lib/dispatch/dispatch.rb
    MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb

Removed Paths:
-------------
    MacRuby/trunk/spec/macruby/library/dispatch/queue_spec.rb

Modified: MacRuby/trunk/lib/dispatch/dispatch.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-16 01:28:21 UTC (rev 3536)
+++ MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-16 01:29:34 UTC (rev 3537)
@@ -63,6 +63,25 @@
     q
   end
 
+  # Applies the +&block+ +count+ number of times in parallel
+  # -- passing step (default 1) iterations at a time --
+  # on a concurrent queue of the given (optional) +priority+
+  # 
+  #   @sum = 0
+  #   Dispatch.upto(10, 3) { |j| @sum += j }
+  #   p @sum # => 55
+  #
+  def upto(count, step=1, priority=nil, &block)
+    q = Dispatch::Queue.concurrent(priority)
+    n_steps = (count / step).to_int
+    q.apply(n_steps) do |i|
+      j0 = i*step
+      j0.upto(j0+step) { |j| block.call(j); puts "j=#{j}" }
+    end
+    # Runs the remainder (if any) sequentially
+    (n_steps*step).upto(count) { |j| block.call(j); puts "j'=#{j}" }
+  end
+
   # Wrap the passed +obj+ (or its instance, if a Class) inside an Actor
   # to serialize access and allow asynchronous returns
   #

Modified: MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb	2010-02-16 01:28:21 UTC (rev 3536)
+++ MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb	2010-02-16 01:29:34 UTC (rev 3537)
@@ -74,6 +74,48 @@
       end
     end
 
+    describe :upto do
+      before :each do
+        @count = 4
+        @sum = 0
+      end
+
+      it "expects a count, step and block " do
+        lambda { Dispatch.upto(@count) { |j| @sum += 1 } }.should raise_error(ArgumentError)
+        #lambda { Dispatch.upto(@count) }.should raise_error(NoMethodError)
+      end
+
+      it "runs the block +count+ number of times" do
+        Dispatch.upto(@count) { |j| @sum += 1 }
+        @sum.should == @count
+      end
+
+      it "runs the block passing the current index" do
+        Dispatch.upto(@count) { |j| @sum += j }
+        @sum.should == (@count*(@count-1)/2)
+      end
+
+      it "does not run the block if the count is zero" do
+        Dispatch.upto(0) { |j| @sum += 1 }
+        @sum.should == 0
+      end
+      
+      it "properly combines blocks with even stride > 1" do
+        Dispatch.upto(@count, 2) { |j| @sum += j }
+        @sum.should == (@count*(@count-1)/2)
+      end
+
+      it "properly combines blocks with uneven stride > 1" do
+        Dispatch.upto(5, 2) { |j| @sum += j }
+        @sum.should == (5*(5-1)/2)
+      end
+
+      it "properly rounds stride fractions > 0.5" do
+        Dispatch.upto(7, 4) { |j| @sum += j }
+        @sum.should == (7*(7-1)/2)
+      end
+    end
+
     describe :wrap do
       it "should return an Actor wrapping an instance of a passed class" do
         actor = Dispatch.wrap(Actee)

Deleted: MacRuby/trunk/spec/macruby/library/dispatch/queue_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/queue_spec.rb	2010-02-16 01:28:21 UTC (rev 3536)
+++ MacRuby/trunk/spec/macruby/library/dispatch/queue_spec.rb	2010-02-16 01:29:34 UTC (rev 3537)
@@ -1,49 +0,0 @@
-require File.dirname(__FILE__) + "/../../spec_helper"
-require 'dispatch'
-
-if MACOSX_VERSION >= 10.6
-  describe "Dispatch::Queue" do
-    before :each do
-      @q = Dispatch::Queue.new('org.macruby.gcd_spec.prelude')
-      @count = 4
-      @sum = 0
-    end
-    
-    describe "stride" do
-      it "expects a count, stride and block " do
-        lambda { @q.stride(@count) { |j| @sum += 1 } }.should raise_error(ArgumentError)
-        #lambda { @q.stride(@count, 1) }.should raise_error(NoMethodError)
-      end
-
-      it "runs the block +count+ number of times" do
-        @q.stride(@count, 1) { |j| @sum += 1 }
-        @sum.should == @count
-      end
-
-      it "runs the block passing the current index" do
-        @q.stride(@count, 1) { |j| @sum += j }
-        @sum.should == (@count*(@count-1)/2)
-      end
-
-      it "does not run the block if the count is zero" do
-        @q.stride(0, 1) { |j| @sum += 1 }
-        @sum.should == 0
-      end
-      
-      it "properly combines blocks with even stride > 1" do
-        @q.stride(@count, 2) { |j| @sum += j }
-        @sum.should == (@count*(@count-1)/2)
-      end
-
-      it "properly combines blocks with uneven stride > 1" do
-        @q.stride(5, 2) { |j| @sum += j }
-        @sum.should == (5*(5-1)/2)
-      end
-
-      it "properly rounds stride fractions > 0.5" do
-        @q.stride(7, 4) { |j| @sum += j }
-        @sum.should == (7*(7-1)/2)
-      end
-    end
-  end
-end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100215/e12ff895/attachment-0001.html>


More information about the macruby-changes mailing list