Class: ColumnPack::BinPacker
- Inherits:
-
Object
- Object
- ColumnPack::BinPacker
- Defined in:
- lib/column_pack/bin_packer.rb
Overview
Arranges elements into bins using a simple one dimensional bin packing algorithm.
Instance Method Summary collapse
-
#add(size, content) ⇒ Object
Adds element to be packed.
-
#bins ⇒ Object
Returns a packed multi-dimensional array of elements.
-
#empty_space ⇒ Object
Total empty space left over by uneven packing.
-
#initialize(total_bins, options = {}) ⇒ BinPacker
constructor
Uses a fixed number of bins (total_bins).
Constructor Details
#initialize(total_bins, options = {}) ⇒ BinPacker
Uses a fixed number of bins (total_bins).
Options: :algorithm specify a different bin packing algorithm (default :best_fit_decreasing)
available algorithms are :best_fit_decreasing, :best_fit_increasing
:shuffle_in_col after packing columns, shuffle the elements in each column (defaults to true)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/column_pack/bin_packer.rb', line 14 def initialize(total_bins, = {}) raise ArgumentError.new("Must choose a number of bins greater than zero") if total_bins <= 0 @total_bins = total_bins @algorithm = [:algorithm] || :best_fit_decreasing if .has_key? :shuffle_in_col @shuffle_in_col = [:shuffle_in_col] else @shuffle_in_col = true end @elements = [] @needs_packing = true end |
Instance Method Details
#add(size, content) ⇒ Object
Adds element to be packed.
31 32 33 34 35 36 |
# File 'lib/column_pack/bin_packer.rb', line 31 def add(size, content) raise ArgumentError.new("Bin size must be greater than zero") if size <= 0 @elements << {:size => size.to_i, :content => content} @needs_packing = true end |
#bins ⇒ Object
Returns a packed multi-dimensional array of elements.
39 40 41 42 |
# File 'lib/column_pack/bin_packer.rb', line 39 def bins pack_all if @needs_packing @bins end |
#empty_space ⇒ Object
Total empty space left over by uneven packing.
45 46 47 48 49 50 51 52 53 |
# File 'lib/column_pack/bin_packer.rb', line 45 def empty_space pack_all if @needs_packing max = @sizes.each.max space = 0 @sizes.each { |size| space += max - size } space end |