| 37 | | def in_transposed_chunks(n) |
|---|
| 38 | | s=self.size |
|---|
| 39 | | i=n-s%n |
|---|
| | 37 | # Returns a partition of n arrays. |
|---|
| | 38 | # Transposition is used to avoid getting arrays that are too different. |
|---|
| | 39 | # >> (0..17).to_a.in_transposed_slices(5) |
|---|
| | 40 | # => [[0, 5, 10, 15], [1, 6, 11, 16], [2, 7, 12, 17], [3, 8, 13], [4, 9, 14]] |
|---|
| | 41 | # while |
|---|
| | 42 | # >> (0..17).enum_slice(5).to_a |
|---|
| | 43 | # => [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17]] |
|---|
| | 44 | # |
|---|
| | 45 | # If some folders contain big files and some others contain small ones, |
|---|
| | 46 | # every indexing thread will get some of both! |
|---|
| | 47 | def in_transposed_slices(n) |
|---|
| | 48 | # no need to compute anything if n==1 |
|---|
| | 49 | return [self] if n==1 |
|---|
| | 50 | # Array#transpose would raise if Array is not a square array of arrays. |
|---|
| | 51 | i=n-self.size%n |
|---|
| | 52 | # Adds nils so that size is a multiple of n, |
|---|
| | 53 | # cuts array in slices of size n, |
|---|
| | 54 | # transposes to get n slices, |
|---|
| | 55 | # and removes added nils. |
|---|