Algorithm-ToNumberMunger

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

plain-data spec into a closure that maps one raw value to one number, so a
caller can build its mungers once from configuration and then apply them per row
with no re-parsing. All configuration errors are caught at build time; the
returned closure only croaks on genuinely un-mungeable *input*.

```perl
use Algorithm::ToNumberMunger;

# one munger from a spec hash
my $code = Algorithm::ToNumberMunger->build(
    { munger => 'enum', map => { GET => 0, POST => 1, PUT => 2 } },
);
my $n = $code->('POST');          # 1

# a whole table at once, then a full row in column order
my $plan = Algorithm::ToNumberMunger->compile(
    tags    => [ 'method', 'bytes', 'label' ],
    mungers => {
        method => { munger => 'enum', map => { GET => 0, POST => 1 } },
        bytes  => { munger => 'log',  offset => 1 },
        label  => { munger => 'hash', buckets => 1024 },

lib/Algorithm/ToNumberMunger.pm  view on Meta::CPAN

	$HAVE_XS = 1;
	1;
};

=head1 SYNOPSIS

    use Algorithm::ToNumberMunger;

    # one munger from a spec hash
    my $code = Algorithm::ToNumberMunger->build(
        { munger => 'enum', map => { GET => 0, POST => 1, PUT => 2 } },
    );
    my $n = $code->('POST');          # 1

    # a whole table of them at once, from a 'field => spec' hash
    my $by_tag = Algorithm::ToNumberMunger->build_all(
        \%mungers,
    );
    my $row_value = $by_tag->{method}->($raw{method});

=head1 DESCRIPTION

lib/Algorithm/ToNumberMunger.pm  view on Meta::CPAN

feature a threshold split can express. Because these ordinals are this
module's invention rather than a wire encoding, numeric inputs are B<not>
passed through -- a C<1.2> would land on the wrong scale -- and croak like
any other unmapped value (or take the C<default>).

=head2 http_method_enum

    { munger => 'http_method_enum', default => -1 }

Named-map enum (lookup rules as L</dns_rcode_enum>) for the registered HTTP
request methods: C<GET> 0, C<HEAD> 1, C<POST> 2, C<PUT> 3, C<DELETE> 4,
C<CONNECT> 5, C<OPTIONS> 6, C<TRACE> 7, C<PATCH> 8. HTTP has no numeric
method encoding, so these are unordered ordinals of this module's invention
(a canonical map beats every set inventing its own numbering) and numeric
inputs are not passed through. An unlisted -- possibly probing -- method
croaks unless a C<default> is given, and that unlisted-method signal is often
the interesting one.

=head2 sip_method_enum

    { munger => 'sip_method_enum', default => -1 }

lib/Algorithm/ToNumberMunger.xs  view on Meta::CPAN

     * the scalar's internal flag. This is well-defined for any input (a wide
     * character would make SvPVbyte croak) and matches the pure-Perl fallback,
     * which utf8-encodes before hashing. */
    p = (const unsigned char *) SvPVutf8(str, len);
    h = (U32) 2166136261UL ^ (U32) seed;
    for (i = 0; i < len; i++) {
        h ^= (U32) p[i];
        h *= (U32) 16777619UL;
    }
    RETVAL = (UV) h;
  OUTPUT:
    RETVAL

NV
_entropy_xs(str)
    SV *str
  PREINIT:
    STRLEN len;
    const unsigned char *p;
    STRLEN i;
    UV counts[256];

lib/Algorithm/ToNumberMunger.xs  view on Meta::CPAN

        ln2 = log((NV) 2.0);
        h   = 0.0;
        for (i = 0; i < 256; i++) {
            if (counts[i]) {
                pr = (NV) counts[i] / n;
                h -= pr * (log(pr) / ln2);
            }
        }
        RETVAL = h;
    }
  OUTPUT:
    RETVAL



( run in 2.011 seconds using v1.01-cache-2.11-cpan-7fcb06a456a )