Changeset 211

Show
Ignore:
Timestamp:
04/21/08 13:15:29 (7 months ago)
Author:
eric.dumin..@gmail.com
Message:

Modified Array#in_transposed_slices.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/picolena/templates/app/models/indexer.rb

    r190 r211  
    6060      } 
    6161       
    62       indexing_list_chunks=indexing_list.in_transposed_chunks(threads_number) 
     62      indexing_list_chunks=indexing_list.in_transposed_slices(threads_number) 
    6363       
    6464      indexing_list_chunks.each_with_thread{|chunk| 
  • trunk/lib/picolena/templates/lib/core_exts.rb

    r167 r211  
    3535 
    3636class Array 
    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. 
    4056    (self+[nil]*i).enum_slice(n).to_a.transpose.collect{|e| e.compact} 
    4157  end