schleyfox-peach 0.2 → 0.3

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.
Files changed (2) hide show
  1. data/lib/peach.rb +45 -2
  2. metadata +2 -2
data/lib/peach.rb CHANGED
@@ -1,10 +1,53 @@
1
1
  module Peach
2
2
  def peach(n = nil, &b)
3
- peach_run(:each, b, n)
3
+ return [] if n == 0 or size == 0
4
+
5
+ result = Array.new(size)
6
+
7
+ n ||= $peach_default_threads || size
8
+ div = (size/n).ceil
9
+
10
+ return [] if div == 0
11
+
12
+ threads = []
13
+ max = size - 1
14
+ offset = 0
15
+ for i in (0..n-1)
16
+ threads << Thread.new(offset - div, offset > max ? max : offset) do |lower, upper|
17
+ for j in lower..upper
18
+ yield(slice(j))
19
+ end
20
+ end
21
+ offset += div
22
+ end
23
+ threads.each { |t| t.join }
24
+ self
4
25
  end
5
26
 
6
27
  def pmap(n = nil, &b)
7
- peach_run(:map, b, n)
28
+ return [] if n == 0
29
+
30
+ n ||= $peach_default_threads || size
31
+ div = (size/n).ceil
32
+
33
+ return [] if div == 0
34
+
35
+ result = Array.new(size)
36
+
37
+ threads = []
38
+ max = size - 1
39
+ offset = div
40
+ for i in (0..n-1)
41
+ threads << Thread.new(offset - div, offset > max ? max : offset) do |lower, upper|
42
+ for j in lower..upper
43
+ result[j] = yield(slice(j))
44
+ end
45
+ end
46
+ offset += div
47
+ end
48
+ threads.each { |t| t.join }
49
+
50
+ result
8
51
  end
9
52
 
10
53
  def pselect(n = nil, &b)
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.2"
4
+ version: "0.3"
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: 2009-04-05 00:00:00 -07:00
12
+ date: 2009-07-14 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15