GithubHelp home page GithubHelp logo

demerphq / algorithm-heapify-xs Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 72 KB

Perl module to provide basic heap/heapsort primitives, implemented in XS

License: Other

Perl 73.17% XS 26.83%
perl heap heapify xs

algorithm-heapify-xs's Introduction

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.

algorithm-heapify-xs's People

Contributors

demerphq avatar

Watchers

 avatar  avatar

algorithm-heapify-xs's Issues

Does not compile with threaded perls

See subject. Here a snippet of the compile log:

cc -c  -I.  -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_FORTIFY_SOURCE=2 -O3    -DVERSION=\"0.02\"  -DXS_VERSION=\"0.02\" -DPIC -fPIC "-I/usr/perl5.22.2t/lib/5.22.2/amd64-freebsd-thread-multi/CORE"   XS.c
XS.xs:27:5: error: use of undeclared identifier 'my_perl'
    SvGETMAGIC(a[child]);
    ^
/usr/perl5.22.2t/lib/5.22.2/amd64-freebsd-thread-multi/CORE/sv.h:2043:58: note: expanded from macro 'SvGETMAGIC'
#define SvGETMAGIC(x) ((void)(UNLIKELY(SvGMAGICAL(x)) && mg_get(x)))
                                                         ^

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.