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   389158 use 5.006; use strict; use warnings; our $VERSION = '0.02';
  3     3   11  
  3     3   15  
  3         10  
  3         83  
  3         27  
  3         6  
  3         1853  
4              
5             our $CHARS = 149186;
6              
7             sub new {
8 2     2 0 369056 my ($class, @words) = @_;
9 2 100       13 if (ref $words[1]) {
10 1         4 $CHARS = shift @words;
11 1         2 @words = @{ $words[0] };
  1         6  
12             }
13 2         7 my $self = bless [_build_default()], __PACKAGE__;
14 2         4873 $self->index($_) for (@words);
15 2         36 return $self;
16             }
17              
18             sub _build_default {
19 53     53   85398 return map { [ ] } 0 .. $CHARS
  5072871         9684171  
20             }
21              
22             sub index {
23 22     22 0 78 my ($self, $word) = @_;
24 22         35 my $current = $self;
25 22 100       180 for (split "|", ref $word ? $word->{index} : $word) {
26 83         256 my $ord = ord($_);
27 83 100       266 $ord -= 97 if ($CHARS == 26);
28 83 100       115 unshift @{ $current }, _build_default() if ( scalar @{$current} < $CHARS );
  51         195  
  83         240  
29 83         386701 $current = $current->[$ord];
30             }
31 22         122 push @{$current}, $word;
  22         214  
32             }
33              
34             sub search {
35 10     10 0 2967 my ($self, $search) = @_;
36 10         20 my $word = $self;
37 10         98 for (split "|", $search) {
38 45         67 my $ord = ord($_);
39 45 100       108 $ord -= 97 if ($CHARS == 26);
40 45         139 $word = $word->[$ord];
41 45 100       109 last if !$word;
42             }
43 10 100 66     145 return ref $word && ref $word->[-1] ne 'ARRAY' ? $word->[-1] : die "Index cannot be found: ${search}";
44             }
45              
46             1;
47              
48             __END__