Algorithm-BinPack
view release on metacpan or search on metacpan
lib/Algorithm/BinPack.pm view on Meta::CPAN
my @bins = @{ $self->{bins} };
for my $item (sort_items($self->{items})) {
my ($size, $label) = @{$item}{qw(size label)};
if ($size > $binsize) {
carp "'$label' too big to fit in a bin\n";
next;
}
my $i = 0;
$i++ until $bins[$i]{size} + $size <= $binsize;
push @{ $bins[$i]{items} }, $item;
$bins[$i]{size} += $size;
}
return @bins;
}
sub checkargs {
my ($href, @args) = @_;
my $success = 1;
for (@args) {
unless (exists $href->{$_}) {
carp "Missing argument '$_'";
$success = 0;
}
}
return $success;
}
sub sort_items {
my $items = shift;
sort {
# use fudgesize if it's there, otherwise use actual
my $asize = $a->{fudgesize} || $a->{size};
my $bsize = $b->{fudgesize} || $b->{size};
$bsize <=> $asize
||
$a->{label} cmp $b->{label}
} @{ $items };
}
1;
=head1 SEE ALSO
This module implements the bin packing algorithm described in 'The
Algorithm Design Manual' by Steven S. Skiena.
This module is similar to L<Algorithm::Bucketizer>, but has a few key
differences. The algorithms in Algorithm::Bucketizer are based on
optimization by multiple iterations, so the module is set up
differently. By contrast, the algorithm used in Algorithm::BinPack is
predictable, and does not require multiple iterations. The name also
reflects the well-known name of the problem. Searching for variations
on "bin packing" finds more relevant results than variations on
"bucketizer".
=head1 AUTHOR
Carey Tilden E<lt>revdiablo@wd39.comE<gt>
=head1 CONTRIBUTORS
Andrew 'Terra' Gillespie E<lt>algorithm_binpack@Tech.FutureQuest.netE<gt> - C<prefill_bin>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2004-05 by Carey Tilden
This code is dual licensed. You may choose from one of the following:
=over 4
=item http://creativecommons.org/licenses/by/1.0
A Creative Commons license that allows free use, while requiring attribution.
=item http://d.revinc.org/pages/license
The I Really Could Care Less About You Public License.
=back
=cut
( run in 0.983 second using v1.01-cache-2.11-cpan-96521ef73a4 )