Algorithm-Bucketizer
view release on metacpan or search on metacpan
item into a specific bucket (e.g. in order to prefill buckets), use
"prefill_bucket()" instead, which is described below.
Returns the Algorithm::Bucketizer::Bucket object of the lucky bucket
on sucess and "undef" if something goes badly wrong (e.g. the bucket
size is smaller than the item, i.e. there's no way it's ever going
to fit in *any* bucket).
*
my @buckets = $b->buckets();
Return a list of buckets. The list contains elements of type
"Algorithm::Bucketizer::Bucket", which understand the following
methods:
*
my @items = $bucket->items();
Returns a list of names of items in the bucket. Returns an empty
list if the bucket is empty.
*
my $level = $bucket->level();
Return how full the bucket is. That's the size of all items in
the bucket combined.
*
my $bucket_index = $bucket->idx();
Return the bucket's index. The first bucket has index 0.
*
my $serial_number = $bucket->serial();
Return the bucket serial number. That's the bucket index plus 1.
*
$b->add_bucket(
maxsize => $maxsize
);
Adds a new bucket to the end of the bucket brigade. This method is
useful for building brigades with buckets of various sizes.
*
$b->current_bucket_idx( $idx );
Set/retrieve the index of the bucket that the "simple" algorithm
will use first in order to try to insert the next item.
*
$b->optimize(
algorithm => $algorithm,
maxtime => $seconds,
maxrounds => $number_of_rounds
);
Optimize bucket distribution. Currently "random" and "brute_force"
are implemented. Both can be ("random" *must* be) terminated by
either the maximum number of seconds ("maxtime") or iterations
("maxrounds").
EXAMPLE
We've got buckets which hold a weight of 100 each, and we've got 10
items weighing 30, 31, 32, ... 39. Distribute them into buckets.
use Algorithm::Bucketizer;
my $b = Algorithm::Bucketizer->new( bucketsize => 100 );
for my $i (1..10) {
$b->add_item($i, 30+$i);
}
for my $bucket ($b->buckets()) {
for my $item ($bucket->items()) {
print "Bucket ", $bucket->serial(), ": Item $item\n";
}
print "\n";
}
Output:
Bucket 1: Item 1
Bucket 1: Item 2
Bucket 1: Item 3
Bucket 2: Item 4
Bucket 2: Item 5
Bucket 3: Item 6
Bucket 3: Item 7
Bucket 4: Item 8
Bucket 4: Item 9
Bucket 5: Item 10
REQUIRES
Algorithm::Permute 0.04 if you want to use the "brute_force" method.
SCRIPTS
This distribution comes with a script *bucketize* which puts files into
directory buckets with limited size. Run "perldoc bucketize" for
details.
AUTHOR
Mike Schilli, <m@perlmeister.com>
COPYRIGHT AND LICENSE
Copyright 2002-2007 by Mike Schilli
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
( run in 0.536 second using v1.01-cache-2.11-cpan-71847e10f99 )