Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | package Graph::HeapElem; |
2 | ||
3 | use strict; | |
4 | use vars qw($VERSION @ISA); | |
5 | use Heap::Elem; | |
6 | ||
7 | require Exporter; | |
8 | ||
9 | @ISA = qw(Exporter Heap::Elem); | |
10 | ||
11 | $VERSION = 0.01; | |
12 | ||
13 | =head1 NAME | |
14 | ||
15 | Graph::HeapElem - internal use only | |
16 | ||
17 | =head1 DESCRIPTION | |
18 | ||
19 | B<INTERNAL USE ONLY> for the Graph module | |
20 | ||
21 | =head1 COPYRIGHT | |
22 | ||
23 | Copyright 1999, O'Reilly & Associates. | |
24 | ||
25 | This code is distributed under the same copyright terms as Perl itself. | |
26 | ||
27 | =cut | |
28 | ||
29 | # Preloaded methods go here. | |
30 | ||
31 | sub new { | |
32 | my $class = shift; | |
33 | $class = ref($class) || $class; | |
34 | ||
35 | # two slot array, 0 for the vertex, 1 for use by Heap | |
36 | my $self = [ [ @_ ], undef ]; | |
37 | ||
38 | return bless $self, $class; | |
39 | } | |
40 | ||
41 | # get or set vertex slot | |
42 | sub vertex { | |
43 | my $self = shift; | |
44 | @_ ? ($self->[0]->[0] = shift) : $self->[0]->[0]; | |
45 | } | |
46 | ||
47 | # get or set weight slot | |
48 | sub weight { | |
49 | my $self = shift; | |
50 | @_ ? | |
51 | ($self->[0]->[1]->{ $self->vertex } = shift) : | |
52 | $self->[0]->[1]->{ $self->vertex }; | |
53 | } | |
54 | ||
55 | # get or set parent slot | |
56 | sub parent { | |
57 | my $self = shift; | |
58 | @_ ? | |
59 | ($self->[0]->[2]->{ $self->vertex } = shift) : | |
60 | $self->[0]->[2]->{ $self->vertex }; | |
61 | } | |
62 | ||
63 | # get or set heap slot | |
64 | sub heap { | |
65 | my $self = shift; | |
66 | @_ ? ($self->[1] = shift) : $self->[1]; | |
67 | } | |
68 | ||
69 | # compare two vertices | |
70 | sub cmp { | |
71 | my ($u, $v) = @_; | |
72 | ||
73 | my ($uw, $vw) = ( $u->weight, $v->weight ); | |
74 | ||
75 | if ( defined $uw ) { | |
76 | return defined $vw ? $uw <=> $vw : -1; | |
77 | } else { | |
78 | return defined $vw ? 1 : 0; | |
79 | } | |
80 | } | |
81 | ||
82 | 1; |