NAME

Algorithm::Heapify::XS - Perl extension for supplying simple heap primitives for arrays.

SYNOPSIS

use Algorithm::Heapify::XS qw(max_heapify max_heap_shift);
my @array= (1..10);
max_heapify(@array);
while (defined(my $top= max_heap_shift(@array))) {
  print $top;
}

DESCRIPTION

A heap is an array based data structure where the array is treated as a balanced tree of items where each item obeys a given inequality constraint with its parent and children, but not with its siblings. This allows it to be put into "nearly" sorted order more efficiently than an actual sort would.

This data structure has a number of nice properties:

a) the tree does not require "child" pointers, but instead infers parent/child relationships from their position in the array. The parent of a node i is defined to reside in position int((i-1)/2), and the left and right children of a node i reside in position (i*2+1) and (i*2+2) respectively.

b) "heapifying" an array is O(N) as compared to N * log2(N) for a typical sort.

c) Accessing the top item is O(1), and removing it from the array is O(log(N)).

d) Inserting a new item into the heap is O(log(N))

This means that for applications that need find only the top K of an array can do it faster than sorting the array, and there is no need for wrapper objects to represent the tree.

INTERFACE

All operations are in-place on the array passed as an argument, and all require that the appropriate "heapify" (either max_heapify or min_heapify) operation has been called on the array first. Typically they return the "top" of the heap after the operation has been performed, with the exception of the "shift" operation which returns the "top" of the heap before removing it.

There are four variants of all subs provided. The "max_" and "min_" variants, and the "maxstr_" and "minstr_" which provide descending and ascending and numeric and string ordering respectively. If you wish more precise control over the ordering of items in the heap, such as objects, then use overload to provide the required semantics by overloading the appropriate inequality operators, typically just one of <=> or cmp operators need be overloaded.

EXPORT

None by default. All exports must be requested, or you can use ":all" to import then all, you can also import groups by prefix, eg ":max", ":min", ":maxstr", ":minstr" and ":idx" to export

SUBS

$max= max_heapify(@array)
$min= min_heapify(@array)
$maxstr= maxstr_heapify(@array)
$minstr= minstr_heapify(@array)

These subs "heapify" the array and return its "top" (min/max) value. Prior use of the appropriate one of these subs is required to use all the other subs offered by this package.

$max= max_heap_shift(@array)
$min= min_heap_shift(@array)
$maxstr= maxstr_heap_shift(@array)
$minstr= minstr_heap_shift(@array)

Return and remove the "top" (min/max) value from a heapified array while maintain the arrays heap-order.

$max= max_heap_push(@array)
$min= min_heap_push(@array)
$maxstr= maxstr_heap_push(@array)
$minstr= minstr_heap_push(@array)

Insert an item into a heapified array while maintaining the arrays heap-order, and return the resulting "top" (min/max) value.

$max= max_heap_adjust_top(@array)
$min= min_heap_adjust_top(@array)
$maxstr= maxstr_heap_adjust_top(@array)
$minstr= minstr_heap_adjust_top(@array)

If the weight of the top item in a heapified array ($array[0]) has changed, this function will adjust its position in the tree, and return the resulting new "top" (min/max) value.

$max= max_heap_adjust_item(@array,$idx)
$min= min_heap_adjust_item(@array,$idx)
$maxstr= maxstr_heap_adjust_item(@array,$idx)
$minstr= minstr_heap_adjust_item(@array,$idx)

If the weight of a specific item in a heapified array has changed, this function will adjust its position in the tree, and return the resulting new "top" (min/max) value. If $idx is outside the array does nothing.

$idx= heap_parent_idx($idx)

Returns the defined location for the node residing at index $idx in a heap, or undef if the $idx is 0.

$idx= heap_left_child_idx($idx)
$idx= heap_right_child_idx($idx)

Returns the defined location for the children of a node residing at index $idx in a heap.

VERSION

This is version 0.04

INSTALLATION

To install this module type the following:

perl Makefile.PL
make
make test
make install

DEPENDENCIES

This module is implemented in XS, and requires a working C compiler and Perl build tools environment to build.

SEE ALSO

CPAN - There are other heap packages with different interfaces if you don't like this one.

AUTHOR

Yves Orton, <demerph@(that google thingee)>

COPYRIGHT AND LICENSE

Copyright (C) 2018 by Yves Orton

This software is released under the "MIT License".

See the file LICENSE.txt for more specifics.