schleyfox-peach 0.1.2 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +8 -0
- data/lib/peach.rb +20 -13
- data/test/peach_test.rb +56 -0
- data/test/test_helper.rb +8 -0
- metadata +6 -3
data/Rakefile
ADDED
data/lib/peach.rb
CHANGED
@@ -1,32 +1,37 @@
|
|
1
|
-
|
1
|
+
module Peach
|
2
2
|
def peach(n = nil, &b)
|
3
|
-
|
3
|
+
peach_run(:each, b, n)
|
4
4
|
end
|
5
|
+
|
5
6
|
def pmap(n = nil, &b)
|
6
|
-
|
7
|
+
peach_run(:map, b, n)
|
7
8
|
end
|
8
|
-
|
9
|
-
|
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
|
17
|
+
def peach_run(meth, b, n = nil)
|
14
18
|
threads, results, result = [],[],[]
|
15
|
-
|
16
|
-
|
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
|
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)
|
data/test/peach_test.rb
ADDED
@@ -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
|
data/test/test_helper.rb
ADDED
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.
|
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:
|
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
|
57
|
+
rubygems_version: 1.2.0
|
55
58
|
signing_key:
|
56
59
|
specification_version: 2
|
57
60
|
summary: Parallel Each and other parallel things
|