pmap 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0eeba30c0921e39d013d4a2cd58c00ecd1c50e69
4
- data.tar.gz: 5422d1dabc31a8fb65329bc35a2dd671ccf8a24e
3
+ metadata.gz: 33a12fe1b02aba0a94dad82f1bcecbc127bce82d
4
+ data.tar.gz: 5dfd73a05a0cc5e20389a6a20a26e500f36e3503
5
5
  SHA512:
6
- metadata.gz: d7321a043d56bd25f3c14b8219226c4381ccc96d5c5605708205a9b8acb56b019c5b80425ba6631272677496ef78a5525e2c1c7a8134a5424a0c050ea55bab55
7
- data.tar.gz: 8f189ffaf99cf4f3d7cc7b7e97c204d63375714073559f0cbf2861e55e32e63fdfa9457bc0c79302f323ee307aa9e783100e4f5f6f945007abd2952974db3184
6
+ metadata.gz: 63512f68730969070721168974365ef805a98e854f52aa47a3e16fcb0760d0a73beb534f02f48919bc7b8b03d4edc237b3b906ac915eed812fa92351af8cdcfd
7
+ data.tar.gz: b89dbaba27beb8ce6cb8de073a6dec253ac89a87ef3ba39ecf43aa6fc473a9664990d3785199d03e0ca0d735f89ee85cfbf1d4d149586cc921f145c8abd5d2ff
@@ -5,43 +5,100 @@ require "pmap/thread_pool"
5
5
  $pmap_default_thread_count ||= 64
6
6
 
7
7
  module PMap
8
+ # @!method pmap
9
+ #
10
+ # Parallel #map for any Enumerable.
11
+ #
12
+ # When a block is given, each item is yielded to the block in a
13
+ # separate thread. When no block is given, an Enumerable is
14
+ # returned.
15
+ #
16
+ # @see http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-map
17
+ #
18
+ # @param thread_count [Integer] maximum number of threads to create
19
+ # (optional)
20
+ #
21
+ # @return [Array] of mapped objects
22
+ #
23
+
24
+ # @!method peach
25
+ #
26
+ # Parallel #each for any Enumerable.
27
+ #
28
+ # When a block is given, each item is yielded to the block in a
29
+ # separate thread. When no block is given, an Enumerable is
30
+ # returned.
31
+ #
32
+ # @see http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-each
33
+ #
34
+ # @param thead_count [Integer] maximum number of threads to create
35
+ # (optional)
36
+ #
37
+ # @return [void]
38
+ #
39
+
40
+ # @!method peach_with_index
41
+ #
42
+ # Parallel #each_with_index for any Enumerable.
43
+ #
44
+ # When a block is given, each item is yielded to the block in a
45
+ # separate thread. When no block is given, an Enumerable is
46
+ # returned.
47
+ #
48
+ # @see http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-each_with_index
49
+ #
50
+ # @param thread_count [Integer] maximum number of threads to create
51
+ # (optional)
52
+ #
53
+ # @return [void]
54
+ #
55
+
56
+ # @!method flat_pmap
57
+ #
58
+ # Parallel #flat_map for any Enumerable
59
+ #
60
+ # When a block is given, each item is yielded to the block in a
61
+ # separate thread. When no block is given, an Enumerable is
62
+ # returned.
63
+ #
64
+ # @see http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-flat_map
65
+ #
66
+ # @param thread_count [Integer] maximum number of threads to create
67
+ # (optional)
68
+ #
69
+ # @return [Array] of mapped objects
70
+ #
71
+
8
72
  def self.included(base)
9
73
  base.class_eval do
10
74
 
11
- # Public: Parallel "map" for any Enumerable.
12
- #
13
- # thread_count - maximum number of threads to create (optional)
14
- #
15
- # Requires a block of code to run for each Enumerable item.
16
- #
75
+ # @see PMap#pmap
17
76
  def pmap(thread_count=nil, &proc)
77
+ return self unless proc
78
+
79
+ array_mutex = Mutex.new
18
80
  Array.new.tap do |result|
19
81
  peach_with_index(thread_count) do |item, index|
20
- result[index] = proc.call(item)
82
+ value = proc.call(item)
83
+ array_mutex.synchronize { result[index] = value }
21
84
  end
22
85
  end
23
86
  end
24
87
 
25
- # Public: Parallel "each" for any Enumerable.
26
- #
27
- # thread_count - maximum number of threads to create (optional)
28
- #
29
- # Requires a block of code to run for each Enumerable item.
30
- #
88
+ # @see PMap#peach
31
89
  def peach(thread_count=nil, &proc)
32
- peach_with_index(thread_count) do |item, index|
33
- proc.call(item)
90
+ if proc
91
+ peach_with_index(thread_count) do |item, index|
92
+ proc.call(item)
93
+ end
34
94
  end
35
95
  self
36
96
  end
37
97
 
38
- # Public: Parallel each_with_index for any Enumerable
39
- #
40
- # thread_count - maximum number of threads to create (optional)
41
- #
42
- # Requires a block of code to run for each Enumerable item.
43
- #
98
+ # @see PMap#peach_with_index
44
99
  def peach_with_index(thread_count=nil, &proc)
100
+ return each_with_index unless proc
101
+
45
102
  thread_count ||= $pmap_default_thread_count
46
103
  pool = ThreadPool.new(thread_count)
47
104
 
@@ -51,6 +108,13 @@ module PMap
51
108
  pool.shutdown
52
109
  self
53
110
  end
111
+
112
+ # @see PMap#flat_pmap
113
+ def flat_pmap(thread_count=nil, &proc)
114
+ return self unless proc
115
+
116
+ pmap(thread_count, &proc).flatten(1)
117
+ end
54
118
  end
55
119
  end
56
120
  end
@@ -1,3 +1,3 @@
1
1
  module Pmap
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
@@ -8,9 +8,19 @@ require 'pmap'
8
8
 
9
9
  class Pmap_Test < Test::Unit::TestCase
10
10
 
11
- def bad_test_noproc_range
11
+ def test_noproc_range
12
12
  range = (1..10)
13
- assert_equal(range.map, range.pmap)
13
+ assert_equal(range.map.to_a, range.pmap.to_a)
14
+ end
15
+
16
+ def test_noproc_peach
17
+ range = (1..10)
18
+ assert_equal(range.each.to_a, range.peach.to_a)
19
+ end
20
+
21
+ def test_noproc_peach_with_index
22
+ range = (1..10)
23
+ assert_equal(range.each_with_index.to_a, range.peach_with_index.to_a)
14
24
  end
15
25
 
16
26
  def test_basic_range
@@ -19,17 +29,20 @@ class Pmap_Test < Test::Unit::TestCase
19
29
  assert_equal(range.map(&proc), range.pmap(&proc))
20
30
  end
21
31
 
22
- def bad_test_noproc_array
32
+ def test_noproc_array
23
33
  array = (1..10).to_a
24
- assert_equal(array.map, array.pmap)
34
+ assert_equal(array.map.to_a, array.pmap.to_a)
25
35
  end
26
36
 
27
37
  def test_basic_array
28
- proc = Proc.new {|x| x*x*x}
29
- array = (1..10).to_a
30
- assert_equal(array.map(&proc), array.pmap(&proc))
38
+ 1_000.times do
39
+ proc = Proc.new {|x| x*x*x}
40
+ array = (1..10).to_a
41
+ assert_equal(array.map(&proc), array.pmap(&proc))
42
+ end
31
43
  end
32
44
 
45
+
33
46
  def test_time_savings
34
47
  start = Time.now
35
48
  (1..10).peach{ sleep 1 }
@@ -71,4 +84,25 @@ class Pmap_Test < Test::Unit::TestCase
71
84
 
72
85
  assert_equal([["a", 0], ["b", 1], ["c", 2]].sort, output.sort)
73
86
  end
87
+
88
+ def test_flat_pmap
89
+ subject = [["a"], [["b"]], [[["c"]]]]
90
+ proc = Proc.new { |x| x + ["X"] }
91
+
92
+ if subject.respond_to?(:flat_map)
93
+ assert_equal(subject.flat_map(&proc), subject.flat_pmap(&proc))
94
+ else
95
+ assert_equal(subject.map(&proc).flatten(1), subject.flat_pmap(&proc))
96
+ end
97
+ end
98
+
99
+ def test_noproc_flat_pmap
100
+ subject = [["a"], [["b"]], [[["c"]]]]
101
+
102
+ if subject.respond_to?(:flat_map)
103
+ assert_equal(subject.flat_map.to_a, subject.flat_pmap.to_a)
104
+ else
105
+ assert_equal(subject.map.to_a, subject.flat_pmap.to_a)
106
+ end
107
+ end
74
108
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruce Adams
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-11-08 00:00:00.000000000 Z
13
+ date: 2015-11-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: test-unit