home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Str.pm < prev    next >
Encoding:
Perl POD Document  |  2003-12-04  |  2.1 KB  |  107 lines

  1. package Heap::Elem::Str;
  2.  
  3. use strict;
  4. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  5.  
  6. require Exporter;
  7. require AutoLoader;
  8.  
  9. @ISA = qw(Exporter AutoLoader Heap::Elem);
  10.  
  11. # No names exported.
  12. @EXPORT = ( );
  13.  
  14. # Available for export: StrElem (to allocate a new Heap::Elem::Str value)
  15. @EXPORT_OK = qw( StrElem );
  16.  
  17. $VERSION = '0.70';
  18.  
  19.  
  20. # Preloaded methods go here.
  21.  
  22. sub new {
  23.     my $class = shift;
  24.     $class = ref($class) || $class;
  25.  
  26.     # two slot array, 0 for the string value, 1 for use by Heap
  27.     my $self = [ shift, undef ];
  28.  
  29.     return bless $self, $class;
  30. }
  31.  
  32. sub StrElem {    # exportable synonym for new
  33.     Heap::Elem::Str->new(@_);
  34. }
  35.  
  36. # get or set value slot
  37. sub val {
  38.     my $self = shift;
  39.     @_ ? ($self->[0] = shift) : $self->[0];
  40. }
  41.  
  42. # get or set heap slot
  43. sub heap {
  44.     my $self = shift;
  45.     @_ ? ($self->[1] = shift) : $self->[1];
  46. }
  47.  
  48. # compare two Str elems
  49. sub cmp {
  50.     my $self = shift;
  51.     my $other = shift;
  52.     return $self->[0] cmp $other->[0];
  53. }
  54.  
  55. # Autoload methods go after =cut, and are processed by the autosplit program.
  56.  
  57. 1;
  58. __END__
  59.  
  60. =head1 NAME
  61.  
  62. Heap::Elem::Str - Perl extension for String Heap Elements
  63.  
  64. =head1 SYNOPSIS
  65.  
  66.   use Heap::Elem::Str( StrElem );
  67.   use Heap::Fibonacci;
  68.  
  69.   my $heap = Heap::Fibonacci->new;
  70.   my $elem;
  71.  
  72.   foreach $i ( 'aa'..'bz' ) {
  73.       $elem = StrElem( $i );
  74.       $heap->add( $elem );
  75.   }
  76.  
  77.   while( defined( $elem = $heap->extract_top ) ) {
  78.       print "Smallest is ", $elem->val, "\n";
  79.   }
  80.  
  81. =head1 DESCRIPTION
  82.  
  83. Heap::Elem::Str is used to wrap string values into an element
  84. that can be managed on a heap.  The top of the heap will have
  85. the smallest element still remaining.  (See L<Heap::Elem::StrRev>
  86. if you want the heap to always return the largest element.)
  87.  
  88. The details of the Elem interface are described in L<Heap::Elem>.
  89.  
  90. The details of using a Heap interface are described in L<Heap>.
  91.  
  92. =head1 AUTHOR
  93.  
  94. John Macdonald, jmm@perlwolf.com
  95.  
  96. =head1 COPYRIGHT
  97.  
  98. Copyright 1998-2003, O'Reilly & Associates.
  99.  
  100. This code is distributed under the same copyright terms as perl itself.
  101.  
  102. =head1 SEE ALSO
  103.  
  104. Heap(3), Heap::Elem(3), Heap::Elem::StrRev(3).
  105.  
  106. =cut
  107.