File: HeapElem.pm

package info (click to toggle)
libgraph-perl 0.20102-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 184 kB
  • ctags: 132
  • sloc: perl: 1,455; makefile: 38
file content (82 lines) | stat: -rw-r--r-- 1,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package Graph::HeapElem;

use strict;
use vars qw($VERSION @ISA);
use Heap::Elem;

require Exporter;

@ISA = qw(Exporter Heap::Elem);

$VERSION = 0.01;

=head1 NAME

Graph::HeapElem - internal use only

=head1 DESCRIPTION

B<INTERNAL USE ONLY> for the Graph module

=head1 COPYRIGHT

Copyright 1999, O'Reilly & Associates.

This code is distributed under the same copyright terms as Perl itself.

=cut

# Preloaded methods go here.

sub new {
    my $class = shift;
    $class = ref($class) || $class;

    # two slot array, 0 for the vertex, 1 for use by Heap
    my $self = [ [ @_ ], undef ];

    return bless $self, $class;
}

# get or set vertex slot
sub vertex {
    my $self = shift;
    @_ ? ($self->[0]->[0] = shift) : $self->[0]->[0];
}

# get or set weight slot
sub weight {
    my $self = shift;
    @_ ?
      ($self->[0]->[1]->{ $self->vertex } = shift) :
       $self->[0]->[1]->{ $self->vertex };
}

# get or set parent slot
sub parent {
    my $self = shift;
    @_ ?
      ($self->[0]->[2]->{ $self->vertex } = shift) :
       $self->[0]->[2]->{ $self->vertex };
}

# get or set heap slot
sub heap {
    my $self = shift;
    @_ ? ($self->[1] = shift) : $self->[1];
}

# compare two vertices
sub cmp {
    my ($u, $v) = @_;

    my ($uw, $vw) = ( $u->weight, $v->weight );

    if ( defined $uw ) {
        return defined $vw ? $uw <=> $vw : -1;
    } else {
        return defined $vw ? 1 : 0;
    }
}

1;