/usr/share/perl5/Array/IntSpan/Fields.pm is in libarray-intspan-perl 2.003-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | #
# This file is part of Array-IntSpan
#
# This software is Copyright (c) 2014 by Dominique Dumont.
#
# This is free software, licensed under:
#
# The Artistic License 1.0
#
##########################################################################
#
# Array::IntSpan::Fields - IntSpan array using integer fields as indices
#
# Author: Dominique Dumont
##########################################################################
# Copyright 2003 Dominique Dumont. All rights reserved.
#
# This file is distributed under the Artistic License. See
# http://www.ActiveState.com/corporate/artistic_license.htm or
# the license that comes with your perl distribution.
#
# For comments, questions, bugs or general interest, feel free to
# contact Dominique Dumont ddumont@cpan.org
##########################################################################
# $Author$
# $Date$
# $Name$
# $Revision$
use strict;
use warnings;
package Array::IntSpan::Fields;
our $VERSION = '2.002';
use Array::IntSpan;
use Carp ;
use overload
# this emulate the usage of Intspan
'@{}' => sub { return shift->{range} ;} ,
# fallback on default behavior for all other operators
fallback => 1 ;
sub new
{
my $proto = shift ;
my $class = ref($proto) || $proto;
my $format = shift ;
if (ref $format)
{
# in fact the user want a regular IntSpan
return Array::IntSpan->new($format,@_);
}
my @temp = @_ ;
my $self = {};
bless $self, $class;
$self->set_format($format) ;
foreach my $i (@temp)
{
$i->[0] = $self->field_to_int($i->[0]);
$i->[1] = $self->field_to_int($i->[1]);
}
$self->{range}= Array::IntSpan->new(@temp) ;
return $self;
}
sub set_format
{
my ($self,$format) = @_ ;
croak "Unexpected format : $format" unless
$format =~ /^[\d\.]+$/ ;
$self->{format} = $format ;
my @array = split /\./, $self->{format} ;
# store nb of bit and corresponding bit mask
$self->{fields} = [map { [$_, (1<<$_) -1 ]} @array ] ;
}
sub int_to_field
{
my $self = shift ;
my @all_int = @_ ;
my @result ;
foreach my $int (@all_int)
{
my @res ;
foreach my $f (reverse @{$self->{fields}})
{
unshift @res, ($f->[0] < 32 ? ($int & $f->[1]) : $int ) ;
$int >>= $f->[0] ;
}
push @result, join('.',@res) ;
}
return wantarray ? @result : $result[0];
}
sub field_to_int
{
my $self = shift ;
my @all_field = @_;
my @result ;
foreach my $field (@all_field)
{
my $f = $self->{fields};
my @array = split /\./,$field ;
croak "Expected ",scalar @$f,
" fields for format $self->{format}, got ",
scalar @array," in '$field'\n" unless @array == @$f ;
my $res = 0 ;
my $i =0 ;
while ($i <= $#array)
{
my $shift = $f->[$i][0] ;
croak "Field value $array[$i] too great. ",
"Max is $f->[$i][1] (bit width is $shift)"
if $shift<32 and $array[$i] >> $shift ;
$res = ($res << $shift) + $array[$i++] ;
}
#print "field_to_int: changed $field to $res for format $self->{format}\n";
push @result, $res ;
}
return wantarray ? @result : $result[0];
}
sub get_range
{
my ($self,$s_field,$e_field) = splice @_,0,3 ;
my ($s, $e) = $self->field_to_int($s_field,$e_field) ;
my @newcb = $self->adapt_range_in_cb(@_) ;
my $got = $self->{range}->get_range($s,$e,@newcb) ;
my $ret = bless {range => $got }, ref($self) ;
$ret->set_format($self->{format}) ;
return $ret ;
}
sub lookup
{
my $self = shift;
my @keys = $self->field_to_int(@_);
$self->{range}->lookup(@keys) ;
}
sub clear
{
my $self = shift;
@{$self->{range}} = () ;
}
sub consolidate
{
my ($self,$s_field,$e_field) = splice @_,0,3 ;
my ($s, $e) = $self->field_to_int($s_field,$e_field)
if defined $s_field and defined $e_field;
my @newcb = $self->adapt_range_in_cb(@_) if @_;
return $self->{range}->consolidate($s,$e,@newcb) ;
}
foreach my $method (qw/set_range set_consolidate_range/)
{
no strict 'refs' ;
*$method = sub
{
my ($self,$s_field,$e_field,$value) = splice @_,0,4 ;
my ($s, $e) = $self->field_to_int($s_field,$e_field) ;
my @newcb = $self->adapt_range_in_cb(@_) ;
return $self->{range}->$method ($s, $e, $value, @newcb);
};
}
sub adapt_range_in_cb
{
my $self = shift;
# the callbacks will be called with ($start, $end,$payload) or
# ($start,$end)
my @callbacks = @_ ;
return map
{
my $old_cb = $_; # required for closure to work
defined $old_cb ?
sub
{
my ($s_int,$e_int,$value) = @_ ;
my ($s,$e) = $self->int_to_field($s_int,$e_int) ;
$old_cb->($s,$e,$value);
}
: undef ;
} @callbacks ;
}
sub get_element
{
my ($self,$idx) = @_;
my $elt = $self->{range}[$idx] || return () ;
my ($s_int,$e_int,$value) = @$elt ;
my ($s,$e) = $self->int_to_field($s_int,$e_int) ;
return ($s,$e, $value) ;
}
1;
__END__
=head1 NAME
Array::IntSpan::Fields - IntSpan array using integer fields as indices
=head1 SYNOPSIS
use Array::IntSpan::Fields;
my $foo = Array::IntSpan::Fields
->new( '1.2.4',
['0.0.1','0.1.0','ab'],
['1.0.0','1.0.3','cd']);
print "Address 0.0.15 has ".$foo->lookup("0.0.15").".\n";
$foo->set_range('1.0.4','1.1.0','ef') ;
=head1 DESCRIPTION
C<Array::IntSpan::Fields> brings the advantages of C<Array::IntSpan>
to indices made of integer fields like an IP address and an ANSI SS7 point code.
The number of integer and their maximum value is defined when calling
the constructor (or the C<set_format> method). The example in the
synopsis defines an indice with 3 fields where their maximum values
are 1,3,15 (or 0x1,0x3,0xf).
This module converts the fields into integer before storing them into
the L<Array::IntSpan> module.
=head1 CONSTRUCTOR
=head2 new (...)
The first parameter defines the size of the integer of the fields, in
number of bits. For an IP address, the field definition would be
C<8,8,8,8>.
=head1 METHODS
All methods of L<Array::IntSpan> are available.
=head2 set_format( field_description )
Set another field description. Beware: no conversion or checking is
done. When changing the format, old indices may become illegal.
=head2 int_to_field ( integer )
Returns the field representation of the integer.
=head2 field_to_int ( field )
Returns the integer value of the field. May craok if the fields values
are too great with respect to the filed description.
=head1 AUTHOR
Dominique Dumont, ddumont@cpan.org
Copyright (c) 2003 Dominique Dumont. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
|