[macruby-changes] [3541] MacRuby/trunk

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


Revision: 3541
          http://trac.macosforge.org/projects/ruby/changeset/3541
Author:   ernest.prabhakar at gmail.com
Date:     2010-02-15 17:30:15 -0800 (Mon, 15 Feb 2010)
Log Message:
-----------
Move Dispatch.upto to Integer#p_times

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

Modified: MacRuby/trunk/lib/dispatch/dispatch.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-16 01:30:04 UTC (rev 3540)
+++ MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-16 01:30:15 UTC (rev 3541)
@@ -63,25 +63,6 @@
     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
   #
@@ -94,6 +75,6 @@
     Dispatch::Actor.new( (obj.is_a? Class) ? obj.new : obj)
   end
 
-  module_function :async, :fork, :group, :queue, :wrap, :labelize
+  module_function :async, :fork, :group, :labelize, :queue,:wrap
 
 end

Modified: MacRuby/trunk/lib/dispatch/enumerable.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/enumerable.rb	2010-02-16 01:30:04 UTC (rev 3540)
+++ MacRuby/trunk/lib/dispatch/enumerable.rb	2010-02-16 01:30:15 UTC (rev 3541)
@@ -1,5 +1,28 @@
 # Additional parallel operations for any object supporting +each+
 
+class Integer
+  # Applies the +&block+ +Integer+ number of times in parallel
+  # -- passing in stride (default 1) iterations at a time --
+  # on a concurrent queue of the given (optional) +priority+
+  # 
+  #   @sum = 0
+  #   10.p_times(3) { |j| @sum += j }
+  #   p @sum # => 55
+  #
+  def p_times(stride=1, priority=nil, &block)
+    n_times = self.to_int
+    puts "\np_times: n_times=#{n_times}, stride=#{stride}"
+    q = Dispatch::Queue.concurrent(priority)
+    n_strides = (n_times / stride).to_int
+    q.apply(n_strides) do |i|
+      j0 = i*stride
+      stride.times { |j| block.call(j0+j); puts "\n#{i}=>#{j0}:j=#{j}" }
+    end
+    # Runs the remainder (if any) sequentially on the current thread
+    (n_strides*stride).upto(n_times - 1) { |j| block.call(j); puts "\nj'=#{j}" }
+  end
+end
+
 module Enumerable
   # Parallel +each+
   def p_each(&block)

Modified: MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb	2010-02-16 01:30:04 UTC (rev 3540)
+++ MacRuby/trunk/spec/macruby/library/dispatch/dispatch_spec.rb	2010-02-16 01:30:15 UTC (rev 3541)
@@ -53,7 +53,7 @@
         s2 = Dispatch.labelize @actee
         s1.should_not == s2
       end
-  end
+    end
 
     describe :queue do
       it "should return a dispatch queue" do
@@ -74,48 +74,6 @@
       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)

Modified: MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb	2010-02-16 01:30:04 UTC (rev 3540)
+++ MacRuby/trunk/spec/macruby/library/dispatch/enumerable_spec.rb	2010-02-16 01:30:15 UTC (rev 3541)
@@ -2,6 +2,47 @@
 require 'dispatch'
 
 if MACOSX_VERSION >= 10.6
+  
+  describe "parallel Integer#times" do
+    describe :p_times do
+      before :each do
+        @count = 4
+        @sum = 0
+      end
+
+      it "runs the block +count+ number of times" do
+        @sum = 0
+        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(@count, 3) { |j| @sum += j }
+        @sum.should == (@count*(@count-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
+  end
+
   describe "parallel Enumerable" do
     before :each do
       @ary = (1..3).to_a
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100215/0f93f4b0/attachment-0001.html>


More information about the macruby-changes mailing list