Class: JsDuck::Columns
- Inherits:
-
Object
- Object
- JsDuck::Columns
- Defined in:
- lib/jsduck/columns.rb
Overview
Splits array of items with subitems into roughly equal size groups.
Instance Method Summary collapse
-
#initialize(subitems_field) ⇒ Columns
constructor
Initialized with the name of subitems field.
-
#split(items, n) ⇒ Object
Splits the array of items into n chunks so that the sum of largest chunk is as small as possible.
Constructor Details
#initialize(subitems_field) ⇒ Columns
Initialized with the name of subitems field.
6 7 8 9 |
# File 'lib/jsduck/columns.rb', line 6 def initialize(subitems_field) @header_size = 3 @subitems_field = subitems_field end |
Instance Method Details
#split(items, n) ⇒ Object
Splits the array of items into n chunks so that the sum of largest chunk is as small as possible.
This is a brute-force implementation - we just try all the combinations and choose the best one.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jsduck/columns.rb', line 16 def split(items, n) if n == 1 [items] elsif items.length <= n Array.new(n) {|i| items[i] ? [items[i]] : [] } else min_max = nil min_arr = nil i = 0 while i <= items.length-n i += 1 # Try placing 1, 2, 3, ... items to first chunk. # Calculate the remaining chunks recursively. cols = [items[0,i]] + split(items[i, items.length], n-1) max = max_sum(cols) # Is this the optimal solution so far? Remember it. if !min_max || max < min_max min_max = max min_arr = cols end end min_arr end end |