File Coverage

blib/lib/Bijection.pm
Criterion Covered Total %
statement 34 34 100.0
branch 6 6 100.0
condition n/a
subroutine 11 11 100.0
pod 4 4 100.0
total 55 55 100.0


line stmt bran cond sub pod time code
1             package Bijection;
2 5     5   523392 use 5.006; use strict; use warnings; our $VERSION = '1.02';
  5     5   18  
  5     5   25  
  5         18  
  5         132  
  5         38  
  5         14  
  5         393  
3 5     5   2317 use Import::Export; use base qw/Import::Export/;
  5     5   106256  
  5         42  
  5         239  
  5         10  
  5         539  
4 5     5   32 use Carp qw/croak/;
  5         12  
  5         1774  
5             our %EX = (biject => [qw/all main/], inverse => [qw/all main/], bijection_set => [qw/all set/], offset_set => [qw/all set/]);
6              
7             our (@ALPHA, $OFFSET, $COUNT, %INDEX);
8             BEGIN {
9             sub bijection_set {
10 8     8 1 141871 @ALPHA = @_;
11 8 100       64 $ALPHA[0] =~ m/^[1-9](?!$)\d+$/ ? offset_set(shift @ALPHA) : offset_set(scalar @ALPHA);
12 8         35 $COUNT = @ALPHA;
13 8         29 my $index = -1;
14 8         2318 %INDEX = map +( $_ => ++$index ), @ALPHA;
15             }
16             sub offset_set {
17 8     8 1 30 $OFFSET = shift;
18             }
19 5     5   53 bijection_set(qw/b c d f g h j k l m n p q r s t v w x y z B C D F G H J K L M N P Q R S T V W X Y Z 0 1 2 3 4 5 6 7 8 9/);
20             }
21              
22             sub biject {
23 5035     5035 1 5297365 my ($id, $out) = @_;
24 5035 100       33596 croak "id to encode must be an integer and non-negative: $id" unless ($id =~ m/^\d+$/);
25 5032         11663 $id += $OFFSET;
26 5032         8154 do { $out .= $ALPHA[($id % $COUNT)]; $id = int($id/$COUNT); } while ($id > 0);
  12417         30707  
  12417         40810  
27 5032         19124 reverse $out;
28             }
29              
30             sub inverse {
31 5032     5032 1 43747 my ($out, $id) = (@_, 0);
32             $id = exists $INDEX{$_}
33             ? $id * $COUNT + $INDEX{$_}
34             : croak "invalid character $_ in $out"
35 5032 100       38063 for (split //, $out);
36 5031         26412 $id - $OFFSET;
37             }
38              
39             1;
40              
41             __END__