Skip to content

Commit

Permalink
a little POD for ExternalSorter.pm
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuels committed Feb 4, 2012
1 parent a14bbd7 commit d375124
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions lib/ExternalSorter.pm
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
=head1 NAME
ExternalSorter - efficiently sort arrayrefs with a given comparison function
=head1 SYNOPSIS
# make a new sorter that sorts by column 4, then column 3
my $sorter = ExternalSorter->new(
sub ($$) {
$_[0]->[4] <=> $_[1]->[4]
||
$_[1]->[3] <=> $_[0]->[3];
}, $sortMem);
for my $arrayref ( @arrayrefs ) {
$sorter->add( $arrayref );
}
# finalize sort
$sorter->finish;
# iterate through the sorted arrayrefs
while( my $arrayref = $sorter->get ) {
}
=head1 METHODS
=cut


package ExternalSorter;

use strict;
Expand All @@ -10,6 +41,14 @@ use Devel::Size qw(size total_size);
use Heap::Simple;
use File::Temp;

=head1 new( \&comparison, $ramInBytes, $tmpDir )
Make a new sorter using the given comparison function, using at most
$ramInBytes bytes of RAM. Optionally, can also pass $tmpDir, a path
to the temporary directory to use for intermediate files.
=cut

# the comparison function must have an ($$) prototype
# $ram is the amount of RAM to use before writing to disk (i.e., the size
# of each external sort segment)
Expand All @@ -29,7 +68,12 @@ sub new {
return $self;
}

# add new item
=head1 add( $item )
Add a new item to the sort buffer.
=cut

sub add {
my ($self, $item) = @_;
$self->{curSize} += total_size($item);
Expand All @@ -39,7 +83,12 @@ sub add {
}
}

# to be called when all items have been added
=head1 finish()
Call when all items have been added. Finalizes the sort.
=cut

sub finish {
my ($self) = @_;
my $compare = $self->{compare};
Expand Down Expand Up @@ -68,7 +117,12 @@ sub finish {
$self->{finished} = 1;
}

# write out a sorted version of the current list
=head1 flush()
Write a sorted version of the list to temporary storage.
=cut

sub flush {
my ($self) = @_;
my $compare = $self->{compare};
Expand Down Expand Up @@ -121,6 +175,8 @@ sub get {
}
}



# read one item from a file handle
sub readOne {
my ($fh) = @_;
Expand Down

0 comments on commit d375124

Please sign in to comment.