Algorithm-Bucketizer
view release on metacpan or search on metacpan
Bucketizer.pm view on Meta::CPAN
# fisher-yates shuffle
$next = sub { $self->shuffle(@items) };
die "Need maxrounds|maxtime for 'random' optimizer"
if !exists $options{maxrounds} and !exists $options{maxtime};
}
my $round = 0;
my $minbuckets;
my @minitems;
my $start_time = time();
# Run through different setups and determine the one
# requiring a minimum of buckets.
while (my @res = $next->()) {
my $b = Algorithm::Bucketizer->new(bucketsize => $self->{bucketsize},
algorithm => 'retry');
for (@res) {
my($name, $weight) = @$_;
$b->add_item($name, $weight);
Bucketizer.pm view on Meta::CPAN
my $nof_buckets = scalar $b->buckets;
if(! defined $minbuckets or $nof_buckets < $minbuckets) {
$minbuckets = $nof_buckets;
@minitems = @res;
}
++$round;
last if exists $options{maxrounds} and $round >= $options{maxrounds};
last if exists $options{maxtime} and
time() > $start_time + $options{maxtime};
}
# We should have a ideal distribution now, nuke all buckets and refill
$self->{buckets} = [];
$self->{cur_bucket_idx} = 0;
$self->{algorithm} = "retry"; # We're optimizing
for (@minitems) {
my($name, $weight) = @$_;
$self->add_item($name, $weight);
Bucketizer.pm view on Meta::CPAN
my $smart = Algorithm::Bucketizer->new( algorithm => "retry" );
In addition to these inserting algorithms, check L<"Optimize">
to optimize the distribution, minimizing the number of required buckets.
=head2 Prefilling Buckets
Sometimes you will have preexisting buckets, which you need to
tell the algorithm
about before it starts adding new items. The C<prefill_bucket()> method
does exactly that, simply putting an item into a specified bucket:
$b->prefill_bucket($bucket_idx, $item, $itemsize);
C<$bucket_idx> is the index of the bucket, starting from 0. Non-existing buckets
are automatically created for you. Make sure you have a consecutive number
of buckets at the end of the prefill.
=head2 Optimize
Once you've inserted all items, you might choose to optimize the distribution
over the buckets, in order to I<minimize> the number of required buckets
to hold all the elements.
Optimally distributing a number discrete-sized items into a
my $dumb = Algorithm::Bucketizer->new( algorithm => "simple" );
my $smart = Algorithm::Bucketizer->new( algorithm => "retry" );
In addition to these inserting algorithms, check "Optimize" to optimize
the distribution, minimizing the number of required buckets.
Prefilling Buckets
Sometimes you will have preexisting buckets, which you need to tell the
algorithm about before it starts adding new items. The
"prefill_bucket()" method does exactly that, simply putting an item into
a specified bucket:
$b->prefill_bucket($bucket_idx, $item, $itemsize);
$bucket_idx is the index of the bucket, starting from 0. Non-existing
buckets are automatically created for you. Make sure you have a
consecutive number of buckets at the end of the prefill.
Optimize
Once you've inserted all items, you might choose to optimize the
distribution over the buckets, in order to *minimize* the number of
required buckets to hold all the elements.
Optimally distributing a number discrete-sized items into a number of
discrete-sized buckets, however, is a non-trivial task. It's the
eg/bucketize view on Meta::CPAN
In this case, C<bucketize> will create the following directory structure:
./001:
wienerschnitzel 02.avi wienerschnitzel 04.avi wienerschnitzel 06.avi
wienerschnitzel 03.avi wienerschnitzel 05.avi wienerschnitzel 07.avi
./002:
wienerschnitzel 08.avi wienerschnitzel 09.avi
It is an error if a numbered directory already exists, make sure you
start with a clean slate.
=head1 EXAMPLES
$ bucketize *.jpg
$ bucketize -s 2g *.jpg
=head1 LEGALESE
Copyright 2007 by Mike Schilli, all rights reserved.
( run in 0.283 second using v1.01-cache-2.11-cpan-0d8aa00de5b )