Class: BinPacking::Packer
- Inherits:
-
Object
- Object
- BinPacking::Packer
- Defined in:
- lib/bin_packing/packer.rb
Instance Method Summary collapse
-
#initialize(bins) ⇒ Packer
constructor
A new instance of Packer.
- #pack(boxes, options = {}) ⇒ Object
- #pack!(boxes) ⇒ Object
Constructor Details
#initialize(bins) ⇒ Packer
Returns a new instance of Packer.
3 4 5 6 |
# File 'lib/bin_packing/packer.rb', line 3 def initialize(bins) @bins = bins @unpacked_boxes = [] end |
Instance Method Details
#pack(boxes, options = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/bin_packing/packer.rb', line 8 def pack(boxes, = {}) packed_boxes = [] boxes = boxes.reject(&:packed?) return packed_boxes if boxes.none? limit = [:limit] || BinPacking::Score::MAX_INT board = BinPacking::ScoreBoard.new(@bins, boxes) while entry = board.best_fit entry.bin.insert!(entry.box) board.remove_box(entry.box) board.recalculate_bin(entry.bin) packed_boxes << entry.box break if packed_boxes.size >= limit end packed_boxes end |
#pack!(boxes) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/bin_packing/packer.rb', line 26 def pack!(boxes) packed_boxes = pack(boxes) if packed_boxes.size != boxes.size raise ArgumentError, "#{boxes.size - packed_boxes.size} boxes not "\ "packed into #{@bins.size} bins!" end end |