1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FunctionalPHP\Parallel; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @param int $threads number of threads to use |
7
|
|
|
* @param callable $callable the callable used for mapping |
8
|
|
|
* @param array|\Traversable $collection the collection you want to map over |
9
|
|
|
* @return mixed the result |
10
|
|
|
*/ |
11
|
|
|
function map($threads, callable $callable, $collection) |
12
|
|
|
{ |
13
|
|
|
$results = _parallel($threads, function(array $chunk) use($callable) { |
14
|
|
|
return array_map($callable, $chunk); |
15
|
|
|
}, $collection); |
16
|
|
|
|
17
|
|
|
return call_user_func_array('array_merge', $results); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param int $threads number of threads to use |
22
|
|
|
* @param callable $predicate the predicate used for filtering |
23
|
|
|
* @param array|\Traversable $collection the collection you want to filter |
24
|
|
|
* @return mixed the result |
25
|
|
|
*/ |
26
|
|
|
function filter($threads, callable $predicate, $collection) |
27
|
|
|
{ |
28
|
|
|
$results = _parallel($threads, function(array $chunk) use($predicate) { |
29
|
|
|
return array_filter($chunk, $predicate); |
30
|
|
|
}, $collection); |
31
|
|
|
|
32
|
|
|
return call_user_func_array('array_merge', $results); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int $threads number of threads to use |
37
|
|
|
* @param callable $callable the callable used for folding |
38
|
|
|
* @param array|\Traversable $collection the collection you want to fold |
39
|
|
|
* @param mixed $initial initial value for the fold |
40
|
|
|
* @return mixed the result |
41
|
|
|
*/ |
42
|
|
|
function fold($threads, callable $callable, $collection, $initial) |
43
|
|
|
{ |
44
|
|
|
$func = function(array $chunk) use($callable, $initial) { |
45
|
|
|
return array_reduce($chunk, $callable, $initial); |
46
|
|
|
}; |
47
|
|
|
|
48
|
|
|
$results = _parallel($threads, $func, $collection); |
49
|
|
|
|
50
|
|
|
return $func($results); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param int $threads |
55
|
|
|
* @param callable $callable |
56
|
|
|
* @param array|\Traversable $collection |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
function _parallel($threads, callable $callable, $collection) |
60
|
|
|
{ |
61
|
|
|
$threads = array_map(function($chunk) use($callable) { |
|
|
|
|
62
|
|
|
return new Parallel($callable, $chunk); |
63
|
|
|
}, _chunks($threads, $collection)); |
64
|
|
|
|
65
|
|
|
return array_map(function(Parallel $t) { |
66
|
|
|
return $t->get(); |
67
|
|
|
}, $threads); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param int $size |
72
|
|
|
* @param array|\Traversable $collection |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
function _chunks($size, $collection) |
76
|
|
|
{ |
77
|
|
|
$collection = is_array($collection) ? $collection : iterator_to_array($collection); |
|
|
|
|
78
|
|
|
return array_chunk($collection, ceil(count($collection) / $size)); |
79
|
|
|
} |
80
|
|
|
|