Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | package Heap::Elem::RefRev; |
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: RefRElem (to allocate a new Heap::Elem::RefRev value) | |
15 | @EXPORT_OK = qw( RefRElem ); | |
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 reference value, 1 for use by Heap | |
27 | my $self = [ shift, undef ]; | |
28 | ||
29 | return bless $self, $class; | |
30 | } | |
31 | ||
32 | sub RefRElem { # exportable synonym for new | |
33 | Heap::Elem::RefRev->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 RefRev elems - the objects must have a compatible cmp method | |
49 | sub cmp { | |
50 | my $self = shift; | |
51 | my $other = shift; | |
52 | return $other->[0]->cmp( $self->[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::RefRev - Perl extension for reversed Object Reverence Heap Elements | |
63 | ||
64 | =head1 SYNOPSIS | |
65 | ||
66 | use Heap::Elem::RefRev( RefRElem ); | |
67 | use Heap::Fibonacci; | |
68 | ||
69 | my $heap = Heap::Fibonacci->new; | |
70 | my $elem; | |
71 | ||
72 | foreach $i ( 1..100 ) { | |
73 | $obj = myObject->new( $i ); | |
74 | $elem = RefRElem( $obj ); | |
75 | $heap->add( $elem ); | |
76 | } | |
77 | ||
78 | while( defined( $elem = $heap->extract_top ) ) { | |
79 | # assume that myObject object have a method I<printable> | |
80 | print "Largest is ", $elem->val->printable, "\n"; | |
81 | } | |
82 | ||
83 | =head1 DESCRIPTION | |
84 | ||
85 | Heap::Elem::RefRev is used to wrap object reference values into an | |
86 | element that can be managed on a heap. Each referenced object must | |
87 | have a method I<cmp> which can compare itself with any of the other | |
88 | objects that have references on the same heap. These comparisons | |
89 | must be consistant with normal arithmetic. The top of the heap will | |
90 | have the largest (according to I<cmp>) element still remaining. | |
91 | (See L<Heap::Elem::Ref> if you want the heap to always return the | |
92 | smallest element.) | |
93 | ||
94 | The details of the Elem interface are described in L<Heap::Elem>. | |
95 | ||
96 | The details of using a Heap interface are described in L<Heap>. | |
97 | ||
98 | =head1 AUTHOR | |
99 | ||
100 | John Macdonald, jmm@perlwolf.com | |
101 | ||
102 | =head1 COPYRIGHT | |
103 | ||
104 | Copyright 1998-2003, O'Reilly & Associates. | |
105 | ||
106 | This code is distributed under the same copyright terms as perl itself. | |
107 | ||
108 | =head1 SEE ALSO | |
109 | ||
110 | Heap(3), Heap::Elem(3), Heap::Elem::Ref(3). | |
111 | ||
112 | =cut |