schleyfox-peach 0.1.2 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'lib'
6
+ t.pattern = 'test/*_test.rb'
7
+ t.verbose = true
8
+ end
data/lib/peach.rb CHANGED
@@ -1,32 +1,37 @@
1
- class Array
1
+ module Peach
2
2
  def peach(n = nil, &b)
3
- peachrun(:each, b, n)
3
+ peach_run(:each, b, n)
4
4
  end
5
+
5
6
  def pmap(n = nil, &b)
6
- peachrun(:map, b, n)
7
+ peach_run(:map, b, n)
7
8
  end
8
- def pdelete_if(n = nil, &b)
9
- peachrun(:delete_if, b, n)
9
+
10
+ def pselect(n = nil, &b)
11
+ peach_run(:select, b, n)
10
12
  end
11
13
 
14
+
15
+
12
16
  protected
13
- def peachrun(meth, b, n = nil)
17
+ def peach_run(meth, b, n = nil)
14
18
  threads, results, result = [],[],[]
15
- divvy(n).each_with_index do |x,i|
16
- if x.size > 0
17
- threads << Thread.new { results[i] = x.send(meth, &b)}
18
- else
19
- results[i] = []
20
- end
19
+ peach_divvy(n).each_with_index do |x,i|
20
+ threads << Thread.new { results[i] = x.send(meth, &b)}
21
21
  end
22
22
  threads.each {|t| t.join }
23
23
  results.each {|x| result += x if x}
24
24
  result
25
25
  end
26
26
 
27
- def divvy(n = nil)
27
+ def peach_divvy(n = nil)
28
+ return [] if size == 0
29
+
28
30
  n ||= $peach_default_threads || size
31
+ n = size if n > size
32
+
29
33
  lists = []
34
+
30
35
  div = (size/n).floor
31
36
  offset = 0
32
37
  for i in (0...n-1)
@@ -37,3 +42,5 @@ class Array
37
42
  lists
38
43
  end
39
44
  end
45
+
46
+ Array.send(:include, Peach)
@@ -0,0 +1,56 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ require File.join(File.dirname(__FILE__), "..", "lib", "peach")
4
+
5
+ class PeachTest < Test::Unit::TestCase
6
+ [:peach, :pmap, :pselect].each do |f|
7
+ context "Parallel function #{f}" do
8
+ normal_f = f.to_s[1..-1].to_sym
9
+
10
+ setup do
11
+ @data = [1, 2, 3, 5, 8]
12
+ @block = lambda{|i| i**2}
13
+ end
14
+ should "return the same result as #{normal_f}" do
15
+ assert_equal @data.send(normal_f, &@block),
16
+ @data.send(f, nil, &@block)
17
+ end
18
+ end
19
+ end
20
+
21
+ context "divvy" do
22
+ setup do
23
+ @data = [1, 2, 3, 4, 5]
24
+ end
25
+
26
+ context "on empty list" do
27
+ should "return empty list" do
28
+ assert_equal [], [].send(:peach_divvy)
29
+ end
30
+ end
31
+
32
+ context "when n is nil" do
33
+ should "put 1 element into each division" do
34
+ assert_equal @data.size, @data.send(:peach_divvy).size
35
+ end
36
+ end
37
+
38
+ context "when n is less than array size" do
39
+ should "put create n divisions" do
40
+ assert_equal 2, @data.send(:peach_divvy, 2).size
41
+ end
42
+
43
+ should "not lose any array elements" do
44
+ assert_equal @data.size, @data.send(:peach_divvy, 2).inject(0) {|sum, i|
45
+ sum + i.size
46
+ }
47
+ end
48
+ end
49
+
50
+ context "when n is greater than array size" do
51
+ should "only create 'array size' divisions" do
52
+ assert_equal @data.size, @data.send(:peach_divvy, 42).size
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ $:.unshift(File.dirname(__FILE__)) unless
3
+ $:.include?(File.dirname(__FILE__)) ||
4
+ $:.include?(File.expand_path(File.dirname(__FILE__)))
5
+
6
+ require 'rubygems'
7
+ require 'test/unit'
8
+ require 'shoulda'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schleyfox-peach
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hughes
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-05-06 00:00:00 -07:00
12
+ date: 2009-04-05 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,9 +24,12 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - README
26
26
  - LICENSE
27
+ - Rakefile
27
28
  - lib/peach.rb
28
29
  - bn/peach_bn.rb
29
30
  - bn/peach_test.rb
31
+ - test/test_helper.rb
32
+ - test/peach_test.rb
30
33
  - web/index.html
31
34
  - web/Peach.sketch.png
32
35
  has_rdoc: false
@@ -51,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
54
  requirements: []
52
55
 
53
56
  rubyforge_project:
54
- rubygems_version: 1.0.1
57
+ rubygems_version: 1.2.0
55
58
  signing_key:
56
59
  specification_version: 2
57
60
  summary: Parallel Each and other parallel things