File Coverage

blib/lib/Unidexer.pm
Criterion Covered Total %
statement 37 37 100.0
branch 14 14 100.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 0 3 0.0
total 60 64 93.7


line stmt bran cond sub pod time code
1             package Unidexer;
2              
3 3     3   182399 use 5.006; use strict; use warnings; our $VERSION = '0.01';
  3     3   28  
  3     3   13  
  3         3  
  3         77  
  3         14  
  3         4  
  3         1277  
4              
5             our $CHARS = 149186;
6              
7             sub new {
8 2     2 0 278 my ($class, @words) = @_;
9 2 100       9 if (ref $words[1]) {
10 1         4 $CHARS = shift @words;
11 1         2 @words = @{ $words[0] };
  1         5  
12             }
13 2         8 my $self = bless [_build_default()], __PACKAGE__;
14 2         5149 $self->index($_) for (@words);
15 2         17 return $self;
16             }
17              
18             sub _build_default {
19 53     53   52814 return map { [ ] } 0 .. $CHARS
  5072871         6204563  
20             }
21              
22             sub index {
23 22     22 0 74 my ($self, $word) = @_;
24 22         27 my $current = $self;
25 22 100       126 for (split "|", ref $word ? $word->{index} : $word) {
26 83         343 my $ord = ord($_);
27 83 100       199 $ord -= 97 if ($CHARS == 26);
28 83 100       133 unshift @{ $current }, _build_default() if ( scalar @{$current} < $CHARS );
  51         183  
  83         261  
29 83         236700 $current = $current->[$ord];
30             }
31 22         163 push @{$current}, $word;
  22         191  
32             }
33              
34             sub search {
35 10     10 0 2074 my ($self, $search) = @_;
36 10         13 my $word = $self;
37 10         33 for (split "|", $search) {
38 45         49 my $ord = ord($_);
39 45 100       88 $ord -= 97 if ($CHARS == 26);
40 45         73 $word = $word->[$ord];
41 45 100       73 last if !$word;
42             }
43 10 100 66     139 return ref $word && ref $word->[-1] ne 'ARRAY' ? $word->[-1] : die "Index cannot be found: ${search}";
44             }
45              
46             1;
47              
48             __END__