NASM 0.99.05
[nasm/avx512.git] / perllib / Heap071 / Elem.pm
blob40ae5dc9e224c9c67bf3321409452efe82369632
1 package Heap071::Elem;
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
6 require Exporter;
7 require AutoLoader;
9 @ISA = qw(Exporter AutoLoader);
11 # No names exported.
12 # No names available for export.
14 @EXPORT = ( );
16 $VERSION = '0.71';
19 # Preloaded methods go here.
21 # new will usually be superceded by child,
22 # but provide an empty hash as default and
23 # accept any provided filling for it.
24 sub new {
25 my $self = shift;
26 my $class = ref($self) || $self;
28 return bless { heap=>undef, @_ }, $class;
31 sub heap {
32 my $self = shift;
33 @_ ? ($self->{heap} = shift) : $self->{heap};
36 sub cmp {
37 die "This cmp method must be superceded by one that knows how to compare elements."
40 # Autoload methods go after =cut, and are processed by the autosplit program.
43 __END__
44 # Below is the stub of documentation for your module. You better edit it!
46 =head1 NAME
48 Heap::Elem - Perl extension for elements to be put in Heaps
50 =head1 SYNOPSIS
52 use Heap::Elem::SomeInheritor;
54 use Heap::SomeHeapClass;
56 $elem = Heap::Elem::SomeInheritor->new( $value );
57 $heap = Heap::SomeHeapClass->new;
59 $heap->add($elem);
61 =head1 DESCRIPTION
63 This is an inheritable class for Heap Elements. It provides
64 the interface documentation and some inheritable methods.
65 Only a child classes can be used - this class is not complete.
67 =head1 METHODS
69 =over 4
71 =item $elem = Heap::Elem::SomeInheritor->new( [args] );
73 Creates a new Elem.
75 =item $elem->heap( $val ); $elem->heap;
77 Provides a method for use by the Heap processing routines.
78 If a value argument is provided, it will be saved. The
79 new saved value is always returned. If no value argument
80 is provided, the old saved value is returned.
82 The Heap processing routines use this method to map an element
83 into its internal structure. This is needed to support the
84 Heap methods that affect elements that are not are the top
85 of the heap - I<decrease_key> and I<delete>.
87 The Heap processing routines will ensure that this value is
88 undef when this elem is removed from a heap, and is not undef
89 after it is inserted into a heap. This means that you can
90 check whether an element is currently contained within a heap
91 or not. (It cannot be used to determine which heap an element
92 is contained in, if you have multiple heaps. Keeping that
93 information accurate would make the operation of merging two
94 heaps into a single one take longer - it would have to traverse
95 all of the elements in the merged heap to update them; for
96 Binomial and Fibonacci heaps that would turn an O(1) operation
97 into an O(n) one.)
99 =item $elem1->cmp($elem2)
101 A routine to compare two elements. It must return a negative
102 value if this element should go higher on the heap than I<$elem2>,
103 0 if they are equal, or a positive value if this element should
104 go lower on the heap than I<$elem2>. Just as with sort, the
105 Perl operators <=> and cmp cause the smaller value to be returned
106 first; similarly you can negate the meaning to reverse the order
107 - causing the heap to always return the largest element instead
108 of the smallest.
110 =back
112 =head1 INHERITING
114 This class can be inherited to provide an oject with the
115 ability to be heaped. If the object is implemented as
116 a hash, and if it can deal with a key of I<heap>, leaving
117 it unchanged for use by the heap routines, then the following
118 implemetation will work.
120 package myObject;
122 require Exporter;
124 @ISA = qw(Heap::Elem);
126 sub new {
127 my $self = shift;
128 my $class = ref($self) || $self;
130 my $self = SUPER::new($class);
132 # set $self->{key} = $value;
135 sub cmp {
136 my $self = shift;
137 my $other = shift;
139 $self->{key} cmp $other->{key};
142 # other methods for the rest of myObject's functionality
144 =head1 AUTHOR
146 John Macdonald, jmm@perlwolf.com
148 =head1 COPYRIGHT
150 Copyright 1998-2003, O'Reilly & Associates.
152 This code is distributed under the same copyright terms as perl itself.
154 =head1 SEE ALSO
156 Heap(3), Heap::Elem::Num(3), Heap::Elem::NumRev(3),
157 Heap::Elem::Str(3), Heap::Elem::StrRev(3).
159 =cut