Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

OK, here is my version (which also handles IPv6). It does the famous in just under 3 seconds (your original code takes about 3.5 seconds and you new more-than-4-octets code takes over a minute -- no, I don't know why so slow for me), on one computer. It also appears to handle ranges that include which it doesn't appear either of your versions do properly (see

The major differences are that I "clean as I go" and I use single bitwise operations on strings when possible. This is so much faster and smaller than Net::CIDR, that I'm tempted to make a drop-in replacement for that module based on this (though I'll probably get side-tracked before that happens).

package CIDR_Lite; use strict; use vars qw($VERSION); $VERSION= '0.03'; my $cache; sub _setCache { my $nBytes= shift; my $nBits= 8*$nBytes; my $h= $cache->[$nBytes]; if( ! $h ) { $h= {}; $h->{zero}= my $zero= "\0" x $nBytes; @{ $h->{mask} }= ( map { pack( "B*", "0" x $_ . "1" x ($nBits-$_) ) } 0..$nBits, ); @{ $h->{width} }= ( $zero, map { pack( "B*", "0" x ($_-1) . "1" . "0" x ($nBits-$_-2) ) } 1..$nBits, ); $cache->[$nBytes]= $h; } return $h; } sub new { my $proto = shift; my $class = ref($proto) || $proto; my $nBytes= shift || 4; my $self= {}; @{$self}{qw( mask width zero )}= @{_setCache($nBytes)}{qw( mask width zero )}; $self->{nBytes}= $nBytes; $self->{nBits}= 8*$nBytes; bless $self, $class; } sub _plus { my $self= shift; my $base= shift; my $bits= shift; my $res= $base | $self->{width}[$bits]; while( $res le $base ) { $res &= ~$self->{width}[$bits--]; return "\xff"x(1+length($base)) if $bits < 0; $res |= $self->{width}[$bits]; } return $res; } sub add { my $self= shift; my $input = shift; my( $ip, $bits )= split "/", $input; my $start= pack( "C*", split /\./, $ip ) & ~$self->{mask}[$bits]; my $end= $self->_plus( $start, $bits ); ++($self->{ranges}{$start}) || delete $self->{ranges}{$start}; --($self->{ranges}{$end}) || delete $self->{ranges}{$end}; } sub clean { my $self = shift; my $ranges= $self->{ranges}; my $total= 0; for my $key ( sort keys %$ranges ) { if( 0 == $total ) { die "Impossible" if $ranges->{$key} <= 0; $total += $ranges->{$key}; $ranges->{$key}= 1; } elsif( 0 == ( $total += $ranges->{$key} ) ) { die "Impossible" if 0 <= $ranges->{$key}; $ranges->{$key}= -1; } else { delete $ranges->{$key}; } } } sub list { my $self= shift; my( $start, $total ); my @results; my $zero= $self->{zero}; for my $ip ( sort keys %{ $self->{ranges} } ) { $start= $ip unless $total; $total += $self->{ranges}{$ip}; if( 0 == $total ) { my $end= $ip; my $bits= $self->{nBits}; while( 1 ) { $bits-- while $zero eq ( $start & $self->{mask}[$bi +ts] ); my $next= $self->_plus( $start, ++$bits ); last if $end lt $next; push @results, join(".",unpack"C*",$start)."/".$bits; $start= $next; } my @temp; $bits= $self->{nBits}; while( 1 ) { $bits-- while $zero eq ( $end & $self->{mask}[$bits +] ); my $next= ~ $self->_plus( ~$end, ++$bits ); last if $next lt $start; push @temp, join(".",unpack"C*",$next)."/".$bits; $end= $next; } push @results, reverse @temp; } } wantarray ? @results : \@results; } 1;
Updated: Thanks to runrig for catching a couple of hard-coded 32s that got left in.

        - tye (but my friends call me "Tye")

In reply to (tye)Re2: Net::CIDR::Lite ?? (Merge CIDR addresses) by tye
in thread Net::CIDR::Lite ?? (Merge CIDR addresses) by runrig

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2021-02-25 20:00 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found