Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

heap071::elem(3pm) [debian man page]

Heap071::Elem(3pm)					User Contributed Perl Documentation					Heap071::Elem(3pm)

NAME
Heap::Elem - Perl extension for elements to be put in Heaps SYNOPSIS
use Heap::Elem::SomeInheritor; use Heap::SomeHeapClass; $elem = Heap::Elem::SomeInheritor->new( $value ); $heap = Heap::SomeHeapClass->new; $heap->add($elem); DESCRIPTION
This is an inheritable class for Heap Elements. It provides the interface documentation and some inheritable methods. Only a child classes can be used - this class is not complete. METHODS
$elem = Heap::Elem::SomeInheritor->new( [args] ); Creates a new Elem. $elem->heap( $val ); $elem->heap; Provides a method for use by the Heap processing routines. If a value argument is provided, it will be saved. The new saved value is always returned. If no value argument is provided, the old saved value is returned. The Heap processing routines use this method to map an element into its internal structure. This is needed to support the Heap methods that affect elements that are not are the top of the heap - decrease_key and delete. The Heap processing routines will ensure that this value is undef when this elem is removed from a heap, and is not undef after it is inserted into a heap. This means that you can check whether an element is currently contained within a heap or not. (It cannot be used to determine which heap an element is contained in, if you have multiple heaps. Keeping that information accurate would make the operation of merging two heaps into a single one take longer - it would have to traverse all of the elements in the merged heap to update them; for Binomial and Fibonacci heaps that would turn an O(1) operation into an O(n) one.) $elem1->cmp($elem2) A routine to compare two elements. It must return a negative value if this element should go higher on the heap than $elem2, 0 if they are equal, or a positive value if this element should go lower on the heap than $elem2. Just as with sort, the Perl operators <=> and cmp cause the smaller value to be returned first; similarly you can negate the meaning to reverse the order - causing the heap to always return the largest element instead of the smallest. INHERITING
This class can be inherited to provide an oject with the ability to be heaped. If the object is implemented as a hash, and if it can deal with a key of heap, leaving it unchanged for use by the heap routines, then the following implemetation will work. package myObject; require Exporter; @ISA = qw(Heap::Elem); sub new { my $self = shift; my $class = ref($self) || $self; my $self = SUPER::new($class); # set $self->{key} = $value; } sub cmp { my $self = shift; my $other = shift; $self->{key} cmp $other->{key}; } # other methods for the rest of myObject's functionality AUTHOR
John Macdonald, jmm@perlwolf.com COPYRIGHT
Copyright 1998-2003, O'Reilly & Associates. This code is distributed under the same copyright terms as perl itself. SEE ALSO
Heap(3), Heap::Elem::Num(3), Heap::Elem::NumRev(3), Heap::Elem::Str(3), Heap::Elem::StrRev(3). perl v5.10.0 2007-08-11 Heap071::Elem(3pm)

Check Out this Related Man Page

Cache::File::Heap(3pm)					User Contributed Perl Documentation				    Cache::File::Heap(3pm)

NAME
Cache::File::Heap - A file based heap for use by Cache::File SYNOPSIS
use Cache::File::Heap; $heap = Cache::File::Heap->new('/path/to/some/heap/file'); $heap->add($key, $val); ($key, $val) = $heap->minimum; ($key, $val) = $heap->extract_minimum; $heap->delete($key, $val); DESCRIPTION
This module is a wrapper around a Berkeley DB using a btree structure to implement a heap. It is specifically for use by Cache::File for storing expiry times (although with a bit of work it could be made more general). See LIMITATIONS below. CONSTRUCTOR
my $heap = Cache::File::Heap->new( [$dbfile] ); The heap constructor takes an optional argument which is the name of the database file to open. If specified, it will attempt to open the database during construction. A new Cache::File::Heap blessed reference will be returned, or undef if the open failed. METHODS
$h->open($dbfile) Opens the specified database file. $h->close() Closes a previously opened heap database. Note that the database will be automatically closed when the heap reference is destroyed. $h->add($key, $val) Adds a key and value pair to the heap. Currently the key should be a number, whilst the value may be any scalar. Invokes 'die' on failure (use eval to catch it). $h->delete($key, $val) Removes a key and value pair from the heap. Returns 1 if the pair was found and removed, or 0 otherwise. ($key, $val) = $h->minimum() In list context, returns the smallest key and value pair from the heap. In scalar context only the key is returned. Note smallest is defined via a numerical comparison (hence keys should always be numbers). ($key, $vals) = $h->minimum_dup() In list context, returns the smallest key and an array reference containing all the values for that key from the heap. In scalar context only the key is returned. ($key, $val) = $h->extract_minimum() As for $h->minimum(), but the key and value pair is removed from the heap. ($key, $vals) = $h->extract_minimum_dup() As for $h->minimum_dup(), but all the values are removed from the heap. SEE ALSO
Cache::File AUTHOR
Chris Leishman <chris@leishman.org> Based on work by DeWitt Clinton <dewitt@unto.net> COPYRIGHT
Copyright (C) 2003-2006 Chris Leishman. All Rights Reserved. This module is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or implied. This program is free software; you can redistribute or modify it under the same terms as Perl itself. $Id: Heap.pm,v 1.6 2006/01/31 15:23:58 caleishm Exp $ perl v5.12.4 2011-08-05 Cache::File::Heap(3pm)
Man Page